首页 > 编程知识 正文

单片机有几个定时器(51单片机定时器怎么设置时间)

时间:2023-05-05 11:56:15 阅读:76912 作者:116

volatile uint8_t plus_nums=0; //记录下降沿数

volatile uint8_t nec_code[4]={0}; 保存//nec协议数据

volatile uint8_t received_flag=0; //接收完成标志

//#定义调试

# ifdef调试

sbit LED0=P1^0;

sbit LED1=P1^1;

sbit LED2=P1^2;

sbit LED3=P1^3;

sbit LED4=P1^4;

sbit LED5=P1^5;

sbit LED6=P1^6;

sbit LED7=P1^7;

sbit TEST=P2^0;

#endif

//*

*功能:初始化外部中断0、计时器中断0、计时器1

说明:计时器0不允许中断

*/

void Init_all_interrupt(void )

{

TMOD=0x01; //16位计数值

ET0=1;

EX0=1;

IT0=1;

TR0=0;

EA=1;

P1=0xff;

}

//*

*功能:解码NEC红外线代码,外部中断0。

*共计34个下降沿

! ! ! ! ! !

*测试的理论值与实际值相差很大,实际值较小。 石英振动频率应该小于11.0592MHZ

! ! ! ! ! !

*/

voidnec_falldown_int0(void ) interrupt 0

{

uint16_t temp_plus_time=0;

# ifdef调试

TEST=0;

#endif

plus_nums;

if(plus_nums==1) /第一个下降

{

TH0=0;

TL0=0;

TR0=1; //打开计时器

}

进行ELSEif(plus_nums==2)//"前导判定"的最初下降沿,清除开始定时脉冲

{

temp_plus_time=TH0;

temp_plus_time=temp_plus_time 8;

temp _ plus _ time=temp _ plus _ time|tl0; //保存脉冲数值

TH0=0;

TL0=0;

if(temp_plus_time=0x2bb0) temp_plus_time=0x3576 ) /脉冲时间为) 13.5ms )10%

{

# ifdef调试

LED0=0;

#endif

}

else //前导异常---重新检测整个nec协议序列

{

//全部复位--重新开始

plus_nums=0;

TR0=0; //关掉计时器0

}

}

else //3-34

{

temp_plus_time=TH0;

temp_plus_time=temp_plus_time 8;

temp _ plus _ time=temp _ plus _ time|tl0; //保存脉冲数值

TH0=0;

TL0=0;

//933~1140; 你不知道理论计数得到的下限值会相当小吗? ---! 只有一个可能性。 水晶振动频率有问题!

//650至1140-- -可测试

if () temp_plus_time=933 ) temp_plus_time=1140 ) ) /判断)逻辑0 ) .脉冲时间为) 1.125ms )10%

{

# ifdef调试

LED1=0;

#endif

}

//1866~2280; 你不知道理论计数得到的下限值会相当小吗? ---! 只有一个可能性。 水晶振动频率有问题!

//1566----可测试

elseif((temp_plus_time=1866 ) ) temp_plus_time=2280 ) /判断)逻辑1“.脉冲时间(2.25ms )10%”

{

temp_plus_time=plus_nums -3; //生成数组下标

//nec_arr_index/8可以使用位字段代替此运算来提高执行效率

nec _ code [ temp _ plus _ time/8 ]=nec _ code [ temp _ plus _ time/8 ]|(0x 01 (temp _ plus _ time %8) )

# ifdef调试

LED2=0;

#endif

}

else

{

//全部复位--重新开始

plus_nums=0;

TH0=0;

TL0=0;

TR0=0; //关闭计时器0,重新开始

for(temp_plus_time=0; temp_plus_time 4; temp_plus_time )

{

nec_code[temp_plus_time]=0;

}

# ifdef调试

LED7=~LED7;

#endif

}

接收到//结束标志脉冲

if(plus_nums==34 )

{

plus_nums=0; //结束了

接收_ flag=1; //可以进行解码操作了

TR0=0; //关掉计时器0

# ifdef调试

LED3=~LED3;

#endif

}

}

# ifdef调试

TEST=1;

#endif

}

//*

*功能: 71.111ms后未检测到第二个下降沿时,关闭计时器

*/

voidnec_plus_timeR0(void ) interrupt 1

{

uint8_t i;

TR0=0; //关闭计时器

TH0=0x00;

TL0=0x00;

//将所有全局变量归零

plus_nums=0;

for(I=0; i 4; I )

{

nec_code[i]=0;

}

# ifdef调试

LED4=0;

#endif

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。