I am new to programming MCUs and am trying to use PWM on an ATtiny85. I have looked around online for various tutorials and have managed to get it working with the code below. My problem is that I expect the PWM frequency to be 8MHz/256 = 31.25kHz, but it is actually 3.9kHz, which would suggest that a pre-scaler value of 8 is being used. I have looked at the datasheet but still can't figure it out.
#define F_CPU 8000000
#include <avr/io.h>
int main(void)
{
//Table 11-3 Compare Output Mode, Fast PWM mode
//Table 11-5 Waveform Generation Mode Bit Description
//COM0A0 - non-inverting
TCCR0A |= (0<<WGM02)|(1<<WGM01)|(1<<WGM00)|(1<<COM0A1);
//Table 11-6 Clock Select Bit Description
TCCR0B |= (0<<CS02)|(0<<CS01)|(1<<CS00); //pre-scale factor = 1
OCR0A=128; // 128/256 = 0.5 duty cycle
//make PWM pin output
DDRB |= (1<<DDB0);
while(1){}
return 0;
}
I am programming the MCU using a Raspberry Pi with avrdude and avr-gcc as per this instructable: http://www.instructables.com/id/Programming-the-ATtiny85-from-Raspberry-Pi/
Any help or suggestions would be greatly appreciated. Thanks :)
TCCR0B |= (0<<CS02)|(0<<CS01)|(1<<CS00); //pre-scale factor = 1
doesn't do what you seem to think it does. It does set the CS00 bit, but has absolutely no effect on the CS01 and CS02 bits: you are ORing these values into the register (|=
), which can only change 0s to 1s, not vice versa. – jasonharper