|
先贴上程序:IAR编译器
#define K1 PINB_Bit0
#define K2 PORTC_Bit6
#define K3 PORTC_Bit5
#define K4 PINC_Bit4
#define IRIN PIND_Bit2
char IRPIN;
uchar key_code=0;//遥控键值
uchar buf_key_code=0;//键值暂存
uint key_bit_count=0;//键编码脉冲计数
uint count=0;//定时中断次数计数
uint buf_count=0;//定时中断计数暂存
uchar ir_status=0;//脉冲接收器所处的状态,0:无信号,1:数据编码接收区
uchar buf_INT0_PIN;//INT0的管脚状态
uchar addr[4];
void port_init(void)
{
DDRB = 0xFF;
DDRC = 0xFB;
DDRD = 0xFB;
PORTB = 0xFF;
PORTC = 0xFF; //m103 output only
PORTD = 0xFF;
}
//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 250uSec
// actual value: 250.000uSec (0.0%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x06; //set count
TCCR0A = 0x00;
//TCCR0B = 0x02; //start timer
}
//TIMER2 initialize - prescale:8
// WGM: Normal
// desired value: 10uSec
// actual value: 10.000uSec (0.0%)
void timer2_init(void)
{
TCCR2B = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0xF6; //setup
OCR2A = 0x0A;
OCR2B = 0x00;
TCCR2A = 0x00;
//TCCR2B = 0x02; //start
}
///////////////////////////////////////////////
//100us 定时中断
#pragma vector=TIMER2_OVF_vect
__interrupt void TIMER2_OVF_Server(void)
{
TCNT2 = 0xF6; //reload counter value
count++;//定时器中断次数累加
}
///////////////////////////////////////////////////
///////////////////////////////////////////////////
#pragma vector=INT0_vect
__interrupt void INT0_Server(void) //这个函数死都不通
{
TCCR2B = 0x02; //开定时器中断
buf_count = count;
delay_us(108); //延时100us以消除下降沿跳变抖动
buf_INT0_PIN = PIND_Bit2;
if(buf_INT0_PIN == 0) //INT0引脚稳定为低电平,则表法确实是信号
{
count=10; //count重新计时,因上面延时了100us,故要补偿10次TO中断
if(buf_count>50 && buf_count<470) //若收到的信号合法,则再进行信号分析
{
if(ir_status == 0) //如果之前未收到引导码
{
if(buf_count>300) //判断是否引导码4ms
{
ir_status=1; //系统标记
buf_key_code = 0;
key_bit_count = 0; //从头再来
iTemp=13;
}
}
else
{
if(key_bit_count < 8) //收到数据少于8位,则将收到的数据写入buf_key_code
{
if(buf_count>150)
{
buf_key_code |= 0x01; //收到1
buf_key_code <<= 1;
key_bit_count++; //数据脉冲累加
}
else
if(buf_count>60) //收到0
{
buf_key_code <<= 1; //收到0
key_bit_count++;
// iTemp=12;
}
}
else //若收完8位数据则做以下处理
{
ir_status=0; //接收状态返回到空闲
key_code=buf_key_code;
key_bit_count=0;
buf_key_code=0;
buf_count=0;
TCCR2B = 0x00;
}
}
}
}
}
////////////////////////////////////////////////////
/////////////////////////////////////////////
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
//timer1_init();
//timer0_init();
timer2_init();
MCUCR = 0x02;
EICRA = 0x02; //extended ext ints
EIMSK = 0x01;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x01; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
void main( void )
{
//uint de;
init_devices();
while(1)
{
xian_shi(0);
}
}
我都快郁闷死了,采用定时器2加中断0解码方式。 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|