- void infrared_5104(void);
- void infrared_task(void);
- #define infrared_io P42
- #define user_code 0x0d
- #define decode_ok infrared5104_struct.decode_ok
- #define state_old infrared5104_struct.state_old
- #define wait_rest infrared5104_struct.wait_rest
- #define bit_num infrared5104_struct.bit_num
- struct{
- unsigned char decode_ok:1;
- unsigned char state_old:1;
- unsigned char wait_rest:1;
- unsigned char bit_num:4;
- }infrared5104_struct;
- unsigned char infrared_count,infrared_5104_buf;
- void infrared_5104(void)
- {//置于125us定时器中断中执行
- if(wait_rest)
- {
- if(infrared_io == 1)
- {
- ++infrared_count;
- if(infrared_count >= 200)
- {
- infrared_count = 0;
- bit_num = 0;
- wait_rest = 0;
- }
- }
- else
- {
- infrared_count = 0;
- }
- return;
- }
- if(infrared_io == 0 && infrared_count < 16)
- {
- infrared_count++;
- }
- if(state_old != infrared_io)
- {
- state_old = infrared_io;
- if(state_old == 1)
- {//判断低电平计数
- if(infrared_count >= 2 && infrared_count <= 13)
- {
- infrared_5104_buf <<= 1;
- infrared_5104_buf &= 0xfe;
- if(infrared_count >= 8)
- {//bit1
- infrared_5104_buf |= 0x01;
- }
- bit_num++;
- if(bit_num == 4)
- {
- infrared_5104_buf &= 0x0f;
- if(infrared_5104_buf != user_code)
- {//用户码
- wait_rest = 1;
- }
- }
- else if(bit_num == 12)
- {
- decode_ok = 1;
- wait_rest = 1;
- }
- }
- else
- {
- wait_rest = 1;
- }
- }
- infrared_count = 0;
- }
- }
- void infrared_task(void)
- {
- if(decode_ok)
- {
- decode_ok = 0;
- }
- }
复制代码
|