|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2014-3-31 09:35:33
|
显示全部楼层
看到救星了呵呵呵。我是用INT4和INT5用做两路红外接收,我写的程序比较简单,方便的话给看一下,或者我哪里做的不对给指点一下
#include"main.h"
#include "g_para.h"
#define uchar unsigned char
#define uint unsigned int
uchar IR_end = 0; //红外
uchar IR_count = 0;
uchar IR_data[40] = {0};
uchar IR_data_sp = 0;
uchar IR_code[5] = {0};
uchar F_Front_Int5 = 0;
uchar F_Back_Int4 = 0;
uchar IR_Recive_int4 = 0; //红外
uchar IR_count_int4 = 0;
uchar IR_data_int4[40] = {0};
uchar IR_int4_sp = 0;
uchar IR_code_int4[5] = {0};
//static uchar F_Front_Int5 = 0;
//static uchar F_Back_Int4 = 0;
//static uchar first=0;
// 解码函数
//先发低位
uchar ir_to_char(uchar *ptr)
{
uchar i;
uchar data = 0;
for(i=0;i<8;i++)
{
if(ptr > 16)
data |= (1<<(i));
else
data &= (~(1<<(i)));
}
return data;
}
void timer0(void) //定时器0初始化10K Hz
{
SREG |= 0X80;
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 |= 0x38; //set count
OCR0 |= 0xC8;
TCCR0 |= 0x02; //start timer
TIMSK |= BIT(TOIE0); //溢出中断时能
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0x3a; //reload counter value
IR_count++;
if(IR_count >= 0xfe)
{
IR_count = 0xfc;
}
}
void int4_init(void) //外部中断初始化
{
DDRE &= ~BIT(PE4); //此处必须把int4引脚设置为输入带上拉
PORTE |= BIT(PE4);
DDRE &= ~BIT(PE5); //此处必须把int5引脚设置为输入带上拉
PORTE |= BIT(PE5);
CLI();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x0a; //extended ext ints
EIMSK = 0x30;
TIMSK = 0x00; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
}
void CLR_RX_int5(void)
{
uchar i;
for(i=0; i<40; i++)
{
IR_data = 0x00;
}
}
void CLR_RX_int4(void)
{
uchar i;
for(i=0; i<40; i++)
{
IR_data_int4 = 0x00;
}
}
//红外的解析 前胸接收
void ir_body_receive(void)
{
uchar i; //清空红外接收缓冲区
if(IR_end == 1)
{
IR_end = 0;
if((IR_count > 0xf0)&&(IR_count <= 0xff)) //重复吗
{
IR_count = 0;
IR_data_sp = 0; //数据开始
}
else if((IR_count > 0x25)&&(IR_count < 0x35))//判断引导码
{
IR_count = 0;
IR_data_sp = 0;
}
else
{
IR_data[IR_data_sp++] = IR_count;
IR_count = 0;
if(IR_data_sp >= 32)
{
IR_data_sp = 0;
IR_code[0] = ir_to_char(&IR_data[0]);
IR_code[1] = ir_to_char(&IR_data[8]);
IR_code[2] = ir_to_char(&IR_data[16]);
IR_code[3] = ir_to_char(&IR_data[24]);
if(( IR_code[0] == 137) && (IR_code[1] == 54) && (IR_code[2] == (~IR_code[3])) )
{
// EIMSK = 0x30;
setmotr_1;
system.FGmotor_cnts = 150; //电机振动时间
}
}
}
}
}
//红外的解析 后背接收
void ir_receive_int4(void)
{
uchar i; //清空红外接收缓冲区
if(IR_Recive_int4 == 1)
{
IR_Recive_int4 = 0;
if((IR_count > 0xf0)&&(IR_count <= 0xff)) //重复吗
{
IR_count = 0;
IR_int4_sp = 0; //数据开始
}
else if((IR_count > 0x25)&&(IR_count < 0x35))//判断引导码
{
IR_count = 0;
IR_int4_sp = 0;
}
else
{
IR_data_int4[IR_int4_sp++] = IR_count;
IR_count = 0;
if(IR_int4_sp >= 32)
{
IR_int4_sp = 0;
IR_code_int4[0] = ir_to_char(&IR_data_int4[0]);
IR_code_int4[1] = ir_to_char(&IR_data_int4[8]);
IR_code_int4[2] = ir_to_char(&IR_data_int4[16]);
IR_code_int4[3] = ir_to_char(&IR_data_int4[24]);
if(( IR_code_int4[0] == 137) && (IR_code_int4[1] == 54) && (IR_code_int4[2] == (~IR_code_int4[3])) )
{
// EIMSK = 0x30;
clpled;
setmotr_2;
system.FGmotor_cnts = 150; //电机振动时间
}
}
}
}
}
//中断4 后背
#pragma interrupt_handler int0_isr:6
void int0_isr(void)
{
IR_Recive_int4 = 1;
}
//中断5 前胸
#pragma interrupt_handler int5_isr:7
void int5_isr(void)
{
IR_end = 1;
}
|
|