Cantafford
Member
Hello,
I'm trying to generate a PWM singlal with timer2 module of ATMega328P. Say I want to generate a 50% duty cycle signal. I have attached a DC motor at the output OOC2A(RB3) to see the value of the signal.
I wrote this code: Initialised TMR2 in fast pwm non inverting mode. My freq is 1Mhz and I used /8 prescaler option.
From my understanding OCR2A is the duty cycle when TMR2 is in PWM mode. Now since TMR2 is an 8-bit timer from my understanding writing 255 in OCR2A means 100% duty cycle. And 127 means ~50% duty cycle. So I wrote 127 in the OCR2A and expected a 50% duty cycle. Please correct me if I'm wrong here.
WHen I run the code the motor runs full speed no matter the value which I put in the OCR2A register . Am I missing something here?
I'm trying to generate a PWM singlal with timer2 module of ATMega328P. Say I want to generate a 50% duty cycle signal. I have attached a DC motor at the output OOC2A(RB3) to see the value of the signal.
I wrote this code: Initialised TMR2 in fast pwm non inverting mode. My freq is 1Mhz and I used /8 prescaler option.
From my understanding OCR2A is the duty cycle when TMR2 is in PWM mode. Now since TMR2 is an 8-bit timer from my understanding writing 255 in OCR2A means 100% duty cycle. And 127 means ~50% duty cycle. So I wrote 127 in the OCR2A and expected a 50% duty cycle. Please correct me if I'm wrong here.
WHen I run the code the motor runs full speed no matter the value which I put in the OCR2A register . Am I missing something here?
Code:
/*
* AVRGCC1.c
*
* Created: 2/19/2017 4:28:32 PM
* Author: Jarvis
*/
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000L
void pwm_init()
{
// initialize TCCR2 as: fast pwm mode, non inverting
TCCR2A |= (1<<COM2A1) | (1<<COM2A0) | (1<<WGM21) | (1<<WGM20);
TCCR2B |= (1<<CS21); // clkT2S/8 prescale
// OC2RA pin made as output
DDRB |= (1<<PB3);
}
int main(void)
{
pwm_init();
while(1)
{
OCR2A = 127;
}
}