JZcrystalwlh888 发表于 2014-9-6 13:22:58

咨询大家关下,关于M48红外解码的问题。有谁能帮我解决

先贴上程序:IAR编译器
#define K1      PINB_Bit0
#define K2      PORTC_Bit6
#define K3      PORTC_Bit5
#define K4      PINC_Bit4

#define         IRIN PIND_Bit2

charIRPIN;


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;

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解码方式。

JZcrystalwlh888 发表于 2014-9-6 13:24:39

//10us 定时中断

#pragma vector=TIMER2_OVF_vect
__interrupt void TIMER2_OVF_Server(void)
{
TCNT2 = 0xF6; //reload counter value
count++;//定时器中断次数累加
}
忘记说了,定时器是10US中断,按道理来说应该可以解码呀

tam2907 发表于 2014-9-6 13:57:35

JZcrystalwlh888 发表于 2014-9-6 13:24
//10us 定时中断

#pragma vector=TIMER2_OVF_vect


捕获的方式应该好使一点

hzpyl 发表于 2014-9-6 14:06:25

10us太快了吧。
100~200us吧。

JZcrystalwlh888 发表于 2014-9-6 14:43:37

用捕获方式当然行,问题是我还得用它来做PWM,所以只能用这种方式了,请不要告诉我用DELAY函数

JZcrystalwlh888 发表于 2014-9-6 14:44:41

现在一个关键,就是找出引导码,

go_boy 发表于 2016-12-11 21:49:48

你好在小黑鱼的帖子里看见你仿制了他的温度控制系统,我也在做这个,能请教你需要注意什么吗?我把他关于1602的三个控制线改成我自己开发板的设定,晶振和单片机型号都对,可就是1602没有显示能请教一下嘛

rjx 发表于 2016-12-12 09:22:56

将这句 if(buf_count>50 && buf_count<470)改为 if(buf_count>50 && buf_count<650)试试看。
页: [1]
查看完整版本: 咨询大家关下,关于M48红外解码的问题。有谁能帮我解决