|
我用的是Mega48,使用内部RC 8M,256分频,接收头是使用3个脚的一体化接收头,输出脚接的是外部中断0,红外发射的是用的NEC标准的,现在出现的问题是
本程序只在上电的的第一次按键有效,然后再按其他键就不可以了,甚至死机!!!!郁闷死了,这个程序用51的单片机可以正确解码,换成AVR的就不可以
了,也不知道 是那里的问题,调了2天了一没有发现问题,高手帮我看下吧,好急呀,上面一直在催.........................
//ICC-AVR application builder : 2008-5-6 下午 09:31:05
// Target : M48
// Crystal: 8.0000Mhz
#include <iom48v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long int
#define LED_guan PORTB |= (1 << PB1) //位置高
#define LED_kai PORTB &= ~(1 << PB1) //位置低
/****************红外遥控变量定义**********************************/
ulong IRcode=0x00000000; //32位的键代码
uint customcode=0x0000; //16位用户码
uint time_us=0x0000; //两个下降沿之间的时间
uchar timeH,timeL; //保存TH1 TL的值
uchar Lcustomcode=0x00; //低8用户码
uchar Hcustomcode=0x00; //高8
uchar datacode=0x00; //8位键数据码
uchar Rdatacode=0x00; //8位键数据反码
uchar NewIRcode=0; //指示当处理完了32位码后,就有了新的遥控码
uchar DataRight=0; //为1时读取数据正确
uchar cn=0;
void delay2(uint m)
{
uint j,i;
for(i=0;i<m;i++)
for(j=0;j<300;j++);
}
void port_init(void)
{
PORTB = 0xff;
DDRB = 0xff;
PORTC = 0xff; //m103 output only
DDRC = 0xff;
PORTD = 0xff;
DDRD = 0xFB;//////////PD2是输入的
}
void delay(uint m)
{
uint i,j;
for(i=0;i<m;i++)
for(j=0;j<300;j++);
}
void INIT0(void)
{
CLI(); //disable all interrupts
/***************T1*********************/
////TIMSK1=0x01; //使能T1.
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
////TCCR1A = 0x00;
/////TCCR1B = 0x04; //start Timer,256分频
/*************************************/
/*****************INT0******************/
EICRA = 0x02; //extended ext ints
EIMSK = 0x01;////下降沿触发
/***************************************/
SEI(); //re-enable interrupts
}
/****************************T1中断***************************/
#pragma interrupt_handler timer1_ovf_isr:14
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
///////TCNT1H = 0x00; //reload counter high value
///////////TCNT1L = 0x00; //reload counter low value
}
/****************************外部中断0***************************/
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
///static uchar cn=0;
TCCR1A = 0x00;
TCCR1B = 0x00;
////while(1){ LED_kai;}
timeH=TCNT1H ;
timeL=TCNT1L ;
TCNT1H =0;
TCNT1L =0;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer,256分频
time_us=(unsigned int)timeH;
time_us=time_us<<8;
time_us=time_us|timeL;
if(time_us>414&&time_us<428) {cn=1;IRcode=0;} //遇到引导码,就把cn清0,IRcode清0
if(cn<34)
{
if(time_us>32&&time_us<41) //////'0'
{IRcode=IRcode|0x00000000; if(cn<33) IRcode=IRcode>>1;}
else if(time_us>62&&time_us<77) //'1 '
{IRcode=IRcode|0x80000000;if(cn<33) IRcode=IRcode>>1;}
}
//////else if(time_us>344&&time_us<359) {IRcode=IRcode;}//遇到重复码
cn++;
if(cn==34) {NewIRcode=0x01;TCCR1A = 0x00;TCCR1B = 0x00; }
}
void IR_jiema(void)
{
if(NewIRcode==0x01) //如果有新的遥控码就读
{
customcode=IRcode; //取红外码中的用户码
Lcustomcode=IRcode; //取低8位用户码
IRcode=IRcode>>8;
Hcustomcode=IRcode; //取高8位用户码
IRcode=IRcode>>8;
datacode=IRcode; //取数据码
IRcode=IRcode>>8;
Rdatacode=IRcode; //取数据码的反码
////if((customcode!=0x00ff)||(~Rdatacode!=datacode)) {DataRight=0;IRcode=0;} //校验用户码,反码
///// else DataRight=1;
NewIRcode=0; //读完之后清零,表示新码已读
}
}
void main()
{
port_init();
INIT0();
while(1)
{
IR_jiema();
if(datacode==0x0e)////////0xe0 是1号按键,检测到有键,就让LED点亮。
{
{LED_kai;}
}
else
if(datacode==0x0D)/////0x0d 是2号按键,检测到有键,就让LED闪烁。
{
{
LED_kai;
delay2(1000);
LED_guan;
delay2(1000);
}
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|