各位大侠 这是一个红外接收和发送程序 接收使用外部中断0,1。产生中断的时候用定时器1记录电平长宽。用数据存起来在发射程序中使用 但是 编译没有错误 下载到芯片里却没有反应 请大家给我找找错误 谢谢了。
//ICC-AVR application builder : 2010-5-6 20:45:56
// Target : M128
// Crystal: 8.0000Mhz
#include <iom128v.h>
#include <macros.h>
#define uchar unsigned char;
unsigned char a[140];//数据存储数组
unsigned char b[140];
unsigned char i=0;
unsigned char j=0;//记录信号位数
unsigned char k;
//TIMER0 initialize - prescale:Stop
// WGM: Normal
// desired value: 8MHz
// actual value: Out of range
//TIMER1 initialize - prescale:Stop
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 8MHz
// actual value: Out of range
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
}
#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x00 /*INVALID SETTING*/; //reload counter high value
TCNT1L = 0x00 /*INVALID SETTING*/; //reload counter low value
TCCR1B=0X00;//关断定时器1
TIMSK=0X00;//关定时器中断使能
}
//TIMER3 initialize - prescale:Stop
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 8MHz
// actual value: Out of range
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
TCCR1B=0x00;//关断计数器
a=TCNT1H;
b=TCNT1L;
i++;
j++;
timer1_init();
TCCR1B = 0x02; //start Timer 八分子一分频 开启计数器 工作模式为普通模式
}
#pragma interrupt_handler int1_isr:3//external interupt on INT1
void int1_isr(void)//外部中断1子程序
{
TCCR1B=0x00;//关断计数器
a=TCNT1H;b=TCNT1L;
i++;j++;
timer1_init();
TCCR1B = 0x02; //start Timer 八分子一分频 开启计数器 工作模式为普通模式
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
timer1_init();
EICRA = 0x0E; //extended ext ints 开外部中断并且0为下降沿有效,1为上升沿有效
EICRB = 0x00; //extended ext ints
EIMSK = 0x03;//中断0 1 使能。
TIMSK = 0x40; //timer interrupt sources 定时器溢出中断使能。
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay_1s(unsigned int k)/////////////////// 发射子程序
{
unsigned int m,n;
for(m=0;m<k;m++)
{for(n=0;n<1000;n++);}
}
int main(void)
{
init_devices();//设备初始化
while(TIFR==0X00)
{
}
EIMSK = 0x00;//关中断01中断使能
delay_1s(1000);
delay_1s(1000);delay_1s(1000);delay_1s(1000);delay_1s(1000);
DDRA=0X01;//灯亮,学习完成
PORTA=0X01;
CLI();
delay_1s(1000);
DDRA=0X00;
PORTA=0X00;
TCCR3B = 0x00;
TCCR0 = 0x00;//关定时器0,1
k=j;//红外信号位数
j=1;
DDRA=0X02;//红外发射口置0
PORTA=0X00;
TCNT3H=~a[j];
TCNT3L=~b[j];
TCNT0=242;
ETIMSK=0X04;//定时器3溢出中断使能
TIMSK=0X01;//定时器0溢出中断使能
SEI();
TCCR3B=0x12;
TCCR0=0x02;//开始定时器0,3开始计数
while(j<k)
{}
while(1)//指示灯亮发射完成
{
DDRA=0X04;
PORTA=0X04;
}
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 242; /*INVALID SETTING*/; //reload counter value13微秒计数
PORTA=~PORTA;
}
#pragma interrupt_handler timer3_ovf_isr:30
void timer3_ovf_isr(void) //TIMER3 has overflow 定时器3溢出中断子程序
{
j++;
TCNT3H =~a[j]; /*INVALID SETTING*/; //reload counter high value
TCNT3L =~b[j]; /*INVALID SETTING*/; //reload counter low value
if(j%2==1)// 奇数数组时候
{ TCCR0=0X02;//开定时器0
}
if(j%2==0)//偶数数组时候
{
TCCR0=0X00;//关断计数器0
PORTA=0X00;//38k端口保持低电平
}
} |