PWM on atmega168P

Discussion in 'Embedded Systems and Microcontrollers' started by Abdullah Zia, Nov 21, 2012.

  1. Abdullah Zia

    Thread Starter New Member

    Mar 17, 2012
    3
    0
    Can any one please tell me how to generate 4 pwm signals of 50 Hz with variable duty cycle on the atmega 168 microcontroller?
    A friend was able to get 4 pwm signals but only two of them had a controllable duty cycle. (like...i could vary them)
    I wanna start with a 1 ms duty period and then later on vary it.
    he used the following code

    Code ( (Unknown Language)):
    1.  
    2. #include <avr/io.h>  
    3. #include <avr/delay.h>
    4.  
    5.  
    6.  
    7. void USART_Init( unsigned int ubrr)
    8. {
    9.     /*Set baud rate */
    10.     UBRR0H = (unsigned char)(ubrr>>8);
    11.     UBRR0L = (unsigned char)ubrr;
    12.    
    13.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    14.     /* Set frame format: 8data, 2stop bit */
    15.     UCSR0C = (1<<USBS0)|(3<<UCSZ00);
    16.    
    17. }
    18.  
    19. void InitPWM()
    20. {
    21.     TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);        //NON Inverted PWM
    22.     TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)
    23.  
    24.     ICR1=2499;  //fPWM=50Hz (Period = 20ms Standard).
    25.    
    26.     TCCR2A|=(1<<COM2A0)|(0<<COM2A1)|(1<<COM2B0)|(0<<COM2B1)|(1<<WGM21);        //Working on CTC mode
    27.     TCCR2B|=(1<<CS22)|(1<<CS21)|(1<<CS20) | (1<<FOC2B); //PRESCALER=64
    28.  
    29.     OCR2A=77;
    30.    
    31.     TCCR0A|=(1<<COM0A0)|(0<<COM0A1)|(0<<COM0B0)|(1<<COM0B1)|(1<<WGM01);        //Working on CTC mode
    32.     TCCR0B|=(1<<WGM00)|(1<<WGM02)|(1<<CS02)|(0<<CS01)|(1<<CS00); //PRESCALER=
    33.  
    34.     OCR0A=77;
    35.     OCR0B=77;
    36.    
    37.     DDRB|=(1<< PB1) | (1<<PB2) | (1<<PB3);   //PWM Pins as Out
    38.     DDRD|= (1<<PD3) | (1<<PD6)|(1<<PD5);
    39. }
    40.  
    41.  void SetPWMOutput(uint8_t duty)
    42.  {
    43.      OCR1A=duty;
    44.      OCR1B=duty;
    45.      OCR2B=66;
    46.  }
    47.  
    48.  
    49. void USART_Transmit( unsigned char data )
    50. {
    51.     /* Wait for empty transmit buffer */
    52.     while ( !( UCSR0A & (1<<UDRE0)) )
    53.     ;
    54.     /* Put data into buffer, sends the data */
    55.     UDR0 = data;
    56. }
    57.  
    58. unsigned char USART_Receive( void )
    59. {
    60.     /* Wait for data to be received */
    61.     while ( !(UCSR0A & (1<<RXC0)) )
    62.     ;
    63.     /* Get and return received data from buffer */
    64.     return UDR0;
    65. }
    66.  
    67. int main(void)
    68. {
    69.     //    char d=0;
    70.         InitPWM(); // calling the driver for the pwm signal
    71.     //    USART_Init(25);      
    72.     while(1)
    73.     {
    74.       // d = USART_Receive();
    75.     SetPWMOutput(127);
    76.     //USART_Transmit(d);
    77.     //_delay_loop_2(3200);
    78.     }
    79. }
    80.  
    ignore the uart functions ..they are for interfacing xbees .
    Help me out pleease? 0:)
    If someone has an alternative successful code i'd be very thankful! :)
    Thanks soo much.
     
    Last edited by a moderator: Nov 21, 2012
Loading...