Hi all,
I'm working on a project to set up high speed pwm on the attiny85 using the PCK. My desired PWM frequency will eventually be 400kHz. However, after following the data sheet, timer/counter1 does not work. The interrupt flags are not setting and the counter is not resetting at the programmed value. If I remove the set up of PCK and use the system clock, the interrupt flags and PWM work correctly. I would really appreciate any help or thoughts
#include <avr/io.h>
#include <avr/interrupt.h>
int main(void)
{
PORTB = 0; //Reset values on port B
//After setting up the timer counter, set the direction of the ports to output
DDRB |= (1<<PB1) | (1<<PB0); //Set the direction of PB1 to an output
//PLLCSR - PLL control and status register: PLL is a clock multiplier - multiplies system 8Mhz by 8 to 64Mhz
// PLL is enabled when:PLLE bit is enabled, CKSEL fuse is programmed to 0001. This clock is
// switched off in sleep modes!
PLLCSR |= (1<<PLLE); //PLL enable
//Wait until the PLOCK bit is enabled before allowing the PCK to be enabled
while ((PLLCSR & (1<<PLOCK)) == 0x00)
{
//Do nothing until plock bit is set
}
PLLCSR |= (1<<PCKE); //Enable asynchronous mode, sets PWM clock source
// enable pwm set source to pck clear the pin when match with ocr1x
TCCR1 = (1<<CTC1) |
(1<<PWM1A) |
(1<<(CS10)) |
(1<<COM1A1);
GTCCR = (1<<PWM1B) | (1<<COM1B1);
//Set PWM TOP value - counter will count and reset after reaching this value
// OCR1C
//400Khz 159
//450khz 141
//500khz 127
OCR1C = 159;
//enable Timer1 OVF interrupt
TIMSK = (1<<OCIE1A) | (1<<TOIE1);
sei();
//This should set the duty cycle to about 50%
OCR1A = 120;
unsigned int i = 0;
while(1)
{
//OCR1A = 0xFF/2;
if ((TIFR | (1<<OCF1A)) == 1){
i = i+1;
}
}
}