找回密码
 立即注册
查看: 597|回复: 3

红外发射求助

[复制链接]
  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-06-30 19:51:14

1

主题

3

回帖

13

积分

新手上路

积分
13
发表于 2024-6-30 20:04:08 | 显示全部楼层 |阅读模式
  1. #ifndef STC8_H
  2. #define STC8_H
  3. #include <STC8G.H>
  4. #endif
  5. #include<intrins.h>
  6. #include "Timer0.h"
  7. //以12MHZ烧录
  8. void Delay3000ms(void)        //@11.0592MHz
  9. {
  10.         unsigned char data i, j, k;
  11.         _nop_();
  12.         i = 169;
  13.         j = 80;
  14.         k = 87;
  15.         do
  16.         {
  17.                 do
  18.                 {
  19.                         while (--k);
  20.                 } while (--j);
  21.         } while (--i);
  22. }
  23. void main()
  24. {
  25.             P5M0 |= 0x10; P5M1 &= ~0x10;
  26.                         Timer0_Init();
  27.                         P54 = 0;
  28.                         while(1)
  29.                         {
  30.                        
  31.                                 SendCode(0xfc,0xAf);//1111 1100 0110 1001
  32.                                 Delay3000ms();
  33.                         }
  34. }
复制代码
  1. #ifndef STC8_H
  2. #define STC8_H
  3. #include <STC8G.H>
  4. #endif
  5. bit OP=0;                                   //红外发射管的亮灭控制位
  6. unsigned int count =0;                //载波发射时间
  7. unsigned int Lcount =0;        //发射载波后,等待的时间
  8. void Timer0_Init(void)                //26微秒@12.000MHz
  9. {
  10.         AUXR &= 0x7F;                        //定时器时钟12T模式
  11.         TMOD &= 0xF0;                        //设置定时器模式
  12.         TL0 = 0xE6;                                //设置定时初始值
  13.         TH0 = 0xFF;                                //设置定时初始值
  14.         TF0 = 0;                                //清除TF0标志
  15.         TR0 = 1;                                //定时器0开始计时
  16.         ET0 = 1;
  17.         EA = 1;
  18. }
  19. void SendSingleCode(unsigned char _code)
  20. {
  21.         while (Lcount>0);//直到当前不在发射数据
  22.         switch (_code)
  23.         {
  24.         case 0://560/26=21.6,560/26=21.6
  25.                 count = 22;
  26.                 Lcount = 22;
  27.                 break;
  28.        
  29.         case 1://560/26=21.6,1690/26=65
  30.                 count = 22;
  31.                 Lcount = 65;
  32.                 break;
  33.         case 2://引导码  9000/26=346,4500/26=173
  34.                 count = 346;
  35.                 Lcount = 173;
  36.                 break;
  37.         case 3://重复码 9000/26=346,2250/26=86.5
  38.                 count = 346;
  39.                 Lcount = 87;       
  40.                 break;
  41.         case 4://最后一位时,需要给个短暂的高电平,让接收端识别出最后一位的时间
  42.                 count = 10;
  43.                 break;
  44.         case 5://最后保持一定时间的低电平,用于重复码过短的问题
  45.                 Lcount = 3800;
  46.                 break;
  47.         }
  48. }
  49. void SendStart()
  50. {
  51.         SendSingleCode(2);
  52. }
  53. void SendRepeat()
  54. {
  55.         SendSingleCode(3);
  56.         SendSingleCode(4);//短暂的高电平
  57.         SendSingleCode(5);//长时间的低电平
  58. }
  59. <font color="#ff8c00">void test()
  60. {
  61.         SendStart();
  62.         SendSingleCode(0);
  63.         SendSingleCode(0);
  64.         SendSingleCode(0);
  65.         SendSingleCode(0);
  66.         SendSingleCode(1);
  67.         SendSingleCode(1);
  68.         SendSingleCode(1);
  69.         SendSingleCode(1);
  70.        
  71.         SendSingleCode(1);
  72.         SendSingleCode(1);
  73.         SendSingleCode(1);
  74.         SendSingleCode(1);
  75.         SendSingleCode(0);
  76.         SendSingleCode(0);
  77.         SendSingleCode(0);
  78.         SendSingleCode(0);
  79.        
  80.         SendSingleCode(0);
  81.         SendSingleCode(0);
  82.         SendSingleCode(0);
  83.         SendSingleCode(0);
  84.         SendSingleCode(1);
  85.         SendSingleCode(1);
  86.         SendSingleCode(1);
  87.         SendSingleCode(1);
  88.        
  89.         SendSingleCode(1);
  90.         SendSingleCode(1);
  91.         SendSingleCode(1);
  92.         SendSingleCode(1);
  93.         SendSingleCode(0);
  94.         SendSingleCode(0);
  95.         SendSingleCode(0);
  96.         SendSingleCode(0);
  97.        
  98.         SendSingleCode(4);//短暂的高电平
  99.         SendSingleCode(5);//长时间的低电平
  100. }</font>
  101. <font color="#ff0000">void SendCode(unsigned char _address,unsigned char _command)//地址码,命令码
  102. {
  103.         unsigned char temp ,i;
  104.         SendStart();
  105.        
  106.         for (i = 0; i < 8; i++)
  107.         {
  108.                 temp = _address>>i;
  109.                 temp &= 0x01;
  110.                 SendSingleCode(temp);
  111.         }
  112.                 for (i = 0; i < 8; i++)
  113.         {
  114.                 temp = _address>>i;
  115.                 temp &= 0x01;
  116.                 temp = ~temp;
  117.                 SendSingleCode(temp);
  118.         }
  119.                 for (i = 0; i < 8; i++)
  120.         {
  121.                 temp = _command>>i;
  122.                 temp &= 0x01;
  123.                 SendSingleCode(temp);
  124.         }
  125.                 for (i = 0; i < 8; i++)
  126.         {
  127.                 temp = _command>>i;
  128.                 temp &= 0x01;
  129.                 temp = ~temp;
  130.                 SendSingleCode(temp);
  131.         }
  132.        
  133.         SendSingleCode(4);//短暂的高电平
  134.         SendSingleCode(5);//长时间的低电平
  135.        
  136. }</font>
  137. void timeint(void) interrupt 1
  138. {
  139.        
  140.        
  141.         if (count>0)         //如果是待发送的有效数据flag=1,就在此产生载波(亮灭交变)
  142.         {
  143.                 OP=~OP;
  144.                 count--;                                                
  145.         }
  146.         else
  147.         {
  148.                 OP = 0;                 //发送低电平
  149.                 if (Lcount>0)
  150.                 {
  151.                         Lcount--;
  152.                 }
  153.                
  154.         }
  155.        
  156.          P54 = OP;           //往发射模块的 IO口发送出去
  157. }
复制代码
问题挺奇怪的,看我代码标红的部分,我用了4个for循环分别发送地址码,地址反码,命令码,命令反码

发送的地址码和命令码分别是0xfc,0xAf
但是接收端只能收到16位数据,正常应该是32位啊,而且正好是反码没有
我又写了个test(),一位一位的发,不用for循环,又正常了
所以我想知道是不是什么导致for循环出问题了
截图202406302000268050.jpg
回复

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-06-30 19:51:14

1

主题

3

回帖

13

积分

新手上路

积分
13
发表于 2024-6-30 20:46:11 | 显示全部楼层
试了下把SendCode里的四个for循环换成了:
i=0;
temp = _address>>i;
temp &= 0x01;
SendSingleCode(temp);
i++;
...
这样一行一行写,就又是正常了
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-06-30 19:51:14

1

主题

3

回帖

13

积分

新手上路

积分
13
发表于 2024-6-30 21:04:53 | 显示全部楼层
最终把代码改成这个了,虽然正常收发了,但是还是不知道原因是什么,难道是不能在for循环里取反?
  1. void SendCode(unsigned char _address,unsigned char _command)//地址码,命令码
  2. {
  3.         unsigned char temp ,i;
  4.         SendStart();
  5.        
  6.         for (i = 0; i < 8; i++)
  7.         {
  8.                 temp = _address>>i;
  9.                 temp &= 0x01;
  10.                 if(temp)
  11.                 {
  12.                         SendSingleCode(1);
  13.                 }
  14.                 else
  15.                 {
  16.                         SendSingleCode(0);
  17.                 }
  18.         }
  19.                 for (i = 0; i < 8; i++)
  20.         {
  21.                 temp = _address>>i;
  22.                 temp &= 0x01;
  23.                 if(!temp)
  24.                 {
  25.                         SendSingleCode(1);
  26.                 }
  27.                 else
  28.                 {
  29.                         SendSingleCode(0);
  30.                 }
  31.         }
  32.                 for (i = 0; i < 8; i++)
  33.         {
  34.                 temp = _command>>i;
  35.                 temp &= 0x01;
  36.                 if(temp)
  37.                 {
  38.                         SendSingleCode(1);
  39.                 }
  40.                 else
  41.                 {
  42.                         SendSingleCode(0);
  43.                 }
  44.         }
  45.                 for (i = 0; i < 8; i++)
  46.         {
  47.                 temp = _command>>i;
  48.                 temp &= 0x01;
  49.                 if(!temp)
  50.                 {
  51.                         SendSingleCode(1);
  52.                 }
  53.                 else
  54.                 {
  55.                         SendSingleCode(0);
  56.                 }
  57.         }
  58.        
  59.         SendSingleCode(4);//短暂的高电平
  60.         SendSingleCode(5);//长时间的低电平
  61.        
  62. }
复制代码
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:初来乍到
  • 打卡总天数:1
  • 最近打卡:2024-06-30 19:51:14

1

主题

3

回帖

13

积分

新手上路

积分
13
发表于 2024-6-30 21:23:09 | 显示全部楼层
wnag*** 发表于 2024-6-30 21:13
你用11.0592M,程序执行时间与定时器中断时间刚好契合,也就是程序中要发送下一位了,定时器刚好把上一位的 ...

你这应该涉及底层机器语言执行了吧,我对这不怎么熟
你的意思是定时器把for给打断了之后,当前的for被放弃了,然后执行下一个for?
回复 支持 反对

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-5-3 19:50 , Processed in 0.120834 second(s), 66 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表