Using timer 2 on atmega328

Status
Not open for further replies.
That code is outputting PWM on pin 9 not pin 3... Pin 3 has a pot to change the PWM on pin 9..

500 is tooo high... 256 means 100%, 127 means 50% and 0 means 0%...
I selected pin 3 as output. And set pwm at 50% duty cycle. I dont need adc to set duty cycle. It was just for test purpose and it didnt work.
But i realize now that you was correct that 500 is too high. Timer 2 is 8 bit... And pin 3 should be under timer 2
 
Last edited:
Something really strange is going on. Yesterday i tried burn hex file without bootloader from arduino to atmega32 (with usbasp) and it works, so I though that my arduino is faulty. Today i bought arduino mega and it has same issue. I am thinking that arduino just screw up code.
 

I'm thinking it's more likely you have
 
OK so i discovered this. It actually works for me.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
DDRH |= (1 << PH6);
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = 80;
OCR2B = 39;
TIMSK2 = (1 << OCIE2A);
sei();
while (1);
}
 
My last comment actually do not work properly on other timers.
This piece of code always work. Writed in atmel studio

/*
* Timers.c
*
* Created: 01.11.2019 15:36:58
* Author : NNT
*/
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>

int main(void)
{
// timer 0
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
DDRD |= (1 << PD6) | (1 << PD5);
TCCR0A = 0b10100011;
TCCR0B = 0b00001001;
OCR0A = 0b10100000;
OCR0B = 0b01010000;

while (1)
{
}
}
 
I see you have moved away from Arduino... Entering territory I cannot help with... I only ever use Atmel chips in conjunction with the Arduino IDE..
 
Using all timers:
/*
* Timers.c
*
* Created: 01.11.2019 15:36:58
* Author : NNT
*/
#ifndef F_CPU
#define F_CPU 16000000UL /
#endif
#include <avr/io.h>
#include <avr/interrupt.h>

int main(void)
{
// timer 0
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
DDRD |= (1 << PD6) | (1 << PD5);
TCCR0A = 0b10100011;
TCCR0B = 0b00001001;
OCR0A = 0b10100000;
OCR0B = 0b01010000;

// timer 1
TCCR1A = 0;
TCCR1B = 0;
TCCR1C = 0;
TCNT1 = 0;
DDRB |= (1 << PB2) | (1 << PB1);
TCCR1A = 0b10100011;
TCCR1B = 0b00011001;
OCR1A = 0b10100000;
OCR1B = 0b01010000;
// timer 2
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
DDRD |= (1 << PD3);
DDRB |= (1 << PB3);
TCCR2A = 0b10100011;
TCCR2B = 0b00001001;
OCR2A = 0b10100000;
OCR2B = 0b01010000;
while (1)
{
}
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…