2014-10-30 41 views
0

我想在atmega8的PB1和PB2上使用硬件PWM。但是在代码中这些引脚的输出值在情况发生时不会更新。请指导。PWM在atmega8中没有得到更新

#include <avr/io.h> 
#include <util/delay.h> 
void init_pwmA(uint8_t a) 
{ 

    TCCR1A|=(1<<COM1A1)|(1<<WGM11); 
    TCCR1B|=(1<<WGM12) |(1<<WGM13)| (1<<CS10)|(1<<CS11); 
    ICR1=a; 
    OCR1A=0; 

} 
void init_pwmB(uint8_t a) 
{ 

    TCCR1A|= (1<<WGM11)|(1<<COM1B1); 
    TCCR1B|=(1<<WGM12) |(1<<WGM13)| (1<<CS10)|(1<<CS11); 
    ICR1=a; 
    OCR1B=0; 
} 


int main() 
{ 

    DDRD=0x00; 
    DDRB=0xFF; 
    PORTB=0x00; 
    while(1) 
    { 
     if(PIND&(0x01) == 0x01) 
     { 
      //PORTB=0b00000110; 
       init_pwmB(0); 
       init_pwmA(0); 

     } 
     if(PIND&(0x02) == 0x02) 
     { 

      init_pwmA(255); 


     } 
    } 
    return 0; 
} 

回答

0

在ICR1中是最高值(100%PWM的值),在OCR1A中是所需的PWM值。

所以更改代码以:在手动的Mega8

ICR1=0xff; //assuming, you want 255 to be your top value 
OCR1A=a; //your PWM value 

见表39.波形产生模式的位描述。