Reputation: 51
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 :)
Upvotes: 2
Views: 975
Reputation: 51
I discovered that by default the fuses on the ATtiny85 are set to divide the 8MHz clock by 8 which accounts for the apparent pre-scale factor of 8 that I encountered. I changed the fuses according to this fuse calculator and it worked perfectly. It is strange that none of the tutorials I read mentioned this, but hopefully my struggles can help someone else who has the same problem.
Upvotes: 3