CTC mode frequency being weird

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I'm trying to drive an ultrasonic transducer at 40kHz using the CTC mode on an attiny44 running at 20MHz. I have set OCR0A at 249 because 40000 = 20000000/(2*1*(1+249)) according to the formula given in the datasheet.

My scope is only reading 10kHz, though; could somebody please look at my code and tell me what I am doing wrong?

Thanks!

My fuses are freshly flashed using

avrdude -p t44 -c bsd -U lfuse:w:0x7E:m

and here's my actual code:


#include 
#include 
#include 
#include 
#include 

#define outputSpeakerPinOne PA7


#define outputA(pin) (DDRA |= byte(pin)) // set PORTA pin for output
#define clearA(pin) (PORTA &= ~(byte(pin))) // clear pin in PORTA

int main(void)
{
	
	

	CLKPR = (1 << CLKPCE);    // set clock divider to /1
	CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
	
	OCR0A = 249;  //make ctc toggle the ocr pins produce 40,000 Hz wave, according to formula in datasheet

	outputA(outputSpeakerPinOne);  //Set up the output speaker on the Output Compare 0A pin
	
	TCCR0A = _BV(COM0A0) | _BV(WGM01); //Enable CTC Mode, toggle OC0A on Compare Match
	
	TCCR0B = _BV(CS00);  // Enable timer

	while (1)
	{

	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CKDIV8 ? (though that would be a /8 not a /4 factor?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
CKDIV8 ? (though that would be a /8 not a /4 factor?)
According to the lfuse setting given in the post it is set. But it shouldn't matter because the CKDIV8 Fuse only determines the initial value of CLKPS.

powerpants,
what optimization option do you use at compiling? Have you checked the assembler code whether the timing requirement for writing to CLKPS is met?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ha!

i discovered the problem! i mixed up my 3.3V FTDI breakout board that i was using to power the board with the 5V one that i meant to power the board with....and the tiny can't deal with my 20MHz resonator at that voltage.

it's the little things....

thanks for the replies