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
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.
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
Rich (BB code):
#include <avr/io.h>
#include <avr/delay.h>
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void InitPWM()
{
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11); //NON Inverted PWM
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10); //PRESCALER=64 MODE 14(FAST PWM)
ICR1=2499; //fPWM=50Hz (Period = 20ms Standard).
TCCR2A|=(1<<COM2A0)|(0<<COM2A1)|(1<<COM2B0)|(0<<COM2B1)|(1<<WGM21); //Working on CTC mode
TCCR2B|=(1<<CS22)|(1<<CS21)|(1<<CS20) | (1<<FOC2B); //PRESCALER=64
OCR2A=77;
TCCR0A|=(1<<COM0A0)|(0<<COM0A1)|(0<<COM0B0)|(1<<COM0B1)|(1<<WGM01); //Working on CTC mode
TCCR0B|=(1<<WGM00)|(1<<WGM02)|(1<<CS02)|(0<<CS01)|(1<<CS00); //PRESCALER=
OCR0A=77;
OCR0B=77;
DDRB|=(1<< PB1) | (1<<PB2) | (1<<PB3); //PWM Pins as Out
DDRD|= (1<<PD3) | (1<<PD6)|(1<<PD5);
}
void SetPWMOutput(uint8_t duty)
{
OCR1A=duty;
OCR1B=duty;
OCR2B=66;
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) )
;
/* Get and return received data from buffer */
return UDR0;
}
int main(void)
{
// char d=0;
InitPWM(); // calling the driver for the pwm signal
// USART_Init(25);
while(1)
{
// d = USART_Receive();
SetPWMOutput(127);
//USART_Transmit(d);
//_delay_loop_2(3200);
}
}
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: