dabham 发表于 2015-1-3 22:48:42

求助,急

对定时器不是太明白
下面是摘自国外的一段程序,不明白为什么是 pin 49 输入,2560的板子
高手们给解释下,万分感谢

void Init_PWM4(void)
{
pinMode(49, INPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
      //Remember the registers not declared here remains zero by default...
TCCR4A =((1<<WGM40)|(1<<WGM41)|(1<<COM4C1)|(1<<COM4B1)|(1<<COM4A1));
TCCR4B = ((1<<WGM43)|(1<<WGM42)|(1<<CS41)|(1<<ICES4)); //Prescaler set to 8, that give us a resolution of 2us, read page 134 of data sheet
OCR4A = 40000; ///50hz freq...Datasheet says(system_freq/prescaler)/target frequency. So (16000000hz/8)/50hz=40000,
//must be 50hz because is the servo standard (every 20 ms, and 1hz = 1sec) 1000ms/20ms=50hz, elementary school stuff...   
OCR4B = 3000; //PH4, OUT5
OCR4C = 3000; //PH5, OUT4

TIMSK4 |= (1<<ICIE4); //Timer interrupt mask
sei();

}
/****************************************************
Interrupt Vector
****************************************************/
ISR(TIMER4_CAPT_vect)//interrupt.
{
if(((1<<ICES4)&TCCR4B) >= 0x01)
{
    if(Start_Pulse>Stop_Pulse) //Checking if the Stop Pulse overflow the register, if yes i normalize it.
    {
      Stop_Pulse+=40000; //Nomarlizing the stop pulse.
    }
    Pulse_Width=Stop_Pulse-Start_Pulse; //Calculating pulse
       if(Pulse_Width>5000) //Verify if this is the sync pulse
       {
      PPM_Counter=0; //If yes restart the counter
       }
       else
       {
      PPM_Counter=Pulse_Width; //Saving pulse.
      PPM_Counter++;
       }
    Start_Pulse=ICR4;
    TCCR4B &=(~(1<<ICES4)); //Changing edge detector.
}
else
{
    Stop_Pulse=ICR4; //Capturing time stop of the drop edge
    TCCR4B |=(1<<ICES4); //Changing edge detector.
   
}

}

dabham 发表于 2015-1-3 22:51:08

另外,timer4 和pin49有何关联

1090805647 发表于 2015-1-4 14:15:49

我更看不懂

darkorigin 发表于 2015-1-4 16:06:11

楼主的代码没看出来很急啊?
页: [1]
查看完整版本: 求助,急