Prescaller for 128K internal on Tiny24

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

Hi

Im using a tiny24 and trying to produce a steady time base (ideally seconds)

Im using the 127Khz internal oscillator to drive an LED via a register counting and comparing how many times the OCF0B flag goes high. This is pulsing my LED at a rate I can see with my eye.

However when i change the CS00,02 bits to alter the pre-scaller, the rate doesnt seem to change.

Is the pre-scaler only for the internal 8MHz resonator or am I seeing flickering harmonics of the base frequency divided or something like that...?

Am I missing something simple?

Thanks

Justin

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

Code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
.Include "C:\Program Files\Atmel\AVR Tools\AvrAssembler2\Appnotes\tn24def.inc"

.equ start_of_sram  = 0x0060

.CSEG
.ORG 			0000

.DEF	COUNT	= R17
.DEF	TEMP 	= R16
.DEF	VALUE	= R18
.DEF	OUTPUT 	= R19

rjmp	reset

reset:

ldi		TEMP,	0b11111111		;porta as outputs
out		ddra,	TEMP

			
ldi		TEMP,	0b00000101		;CS02:00 TO 101 FOR PRESCALER /1024
out		tccr0b,	TEMP

cli								;DISABLE INTERRUPTS

clr		COUNT					;RESET COUNT
start:
LDI		TEMP,	0XEE			;LOAD OUTPUT COMPARE REGISTERS WITH ANY VALUE					
out		ocr0a,	TEMP			;
out		ocr0b,	TEMP			;
ldi		VALUE,	0xff			;LOAD VALUE WITH FF

in		TEMP,	tifr0			;CAPTURE TIFR0 REGISTER
sbrs	TEMP,	2				;CHECK OCF0B FLAG 
rjmp	start					;WHEN FLAG SET (TIMER = EE) SKIP TO NEXT SECTION

inc		COUNT					;INCREMENT COUNT

sub		VALUE,	COUNT			;CHECK COUNT = VALUE
brne	start					;WHEN = CONTINUE IF NOT CHECK FLAG AGAIN

clr		COUNT					;CLEAR COUNT

sbrs	OUTPUT ,2				;IF OUTPUT,2 CLEAR, JUMP TO ON. IF SET JUMP TO OFF
rjmp	on
rjmp	off

on:	
ori		OUTPUT ,0b00000100		;SET OUTPUT,2
out		porta,	OUTPUT			;OUTPUT ON PORT A TO ACTIVATE A2
rjmp	start					;START AGAIN

off:
andi	OUTPUT ,0b11111011		;CLR OUTPUT,2
out		porta,	OUTPUT			;OUTPUT ON PORT A TO DEACTIVATE A2
rjmp	start					;START AGAIN

Here is the code i have used.
I have set the fuses in AVR studio to 128Khz setting CKSEL = 0100 AND SUT = 00

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

Think i figured it.

I had to reset the flags straight away by using

LDI TEMP, 0B00000110
OUT tifr0, TEMP

as soon as the flag was set.

Before the count was being incremented the entire time the flag was set.

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

Im presuming now that it will be difficult to get a steady second count using this method as there are instructions inbetween incrementing/pulsing etc.

Would i be better to try and use the interrupt triggered by these flags to increment my count register?

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

Glad you figured it out. I'll comment on a few things in the code.

First, you don't need to specify the path to the .inc file.

.include 

will find the correct file.

.equ start_of_sram  = 0x0060 

There is already a define for this in the .inc file, it is called SRAM_START.

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

Why bother with this if there's no vectors in use?

OTOH this app looks like it's just crying out to use CTC and if you don't want to the timer assigned pin then you'd need an ISR.

(why bother doing all the counting yourself when the timer can do it for you?)

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

I see what you are saying about the CTC cliff.
I have got my LED flashing at about a rate of 1 second using the CTC and interrupt.
As im using 128kHz resonator and prescaling by 1024 i get 125Hz pulsing my timer. this means that every 125 pulses is 1 second so i set my value to 125 (7dH).
I know there is other coding involved but shouldnt my program increase the count every second and hence turn my LED on/off at a rate of 1 second? It is just a little slower than a second at present. If I used a smaller prescale value should this maybe improve the accuracy...?

My code now is:

.Include "C:\Program Files\Atmel\AVR Tools\AvrAssembler2\Appnotes\tn24def.inc"

.equ start_of_sram  = 0x0060

.CSEG
.ORG 			0000

.DEF	COUNT	= R17
.DEF	TEMP 	= R16
.DEF	VALUE	= R18
.DEF	OUTPUT 	= R19

ldi		temp, low(RAMEND)
out		SPL,temp


rjmp	reset

.ORG	0X0009
RJMP	LIGHT



reset:

cli

ldi		TEMP,	0b11111111		;porta as outputs
out		ddra,	TEMP

			
ldi		TEMP,	0b00000101		;CS02:00 TO 100 FOR PRESCALER /1024
out		tccr0b,	TEMP

ldi		TEMP,	0b00000010		;WGM02:00 TO 010 FOR CTC MODE
out		tccr0a,	TEMP

LDI		TEMP,	0B0000010		;ENABLE COMPARE MATCH A INTERRUPT
OUT		TIMSK0,	TEMP

LDI		TEMP,	0B00000000		;CLEAR TO ALLOW TIMER TO START COUNTING
OUT		GTCCR,	TEMP


LDI		TEMP,	0X7d			;LOAD OUTPUT COMPARE REGISTER A WITH 125					
out		ocr0a,	TEMP

clr		COUNT					;RESET COUNT


start:
sei								;ENABLE INTERRUPTS
			
LDI		VALUE,	0X01			;LOAD VALUE WITH SECOND COUNT
SUB		VALUE,	COUNT			;COMPARE COUNT TO VALUE
BRNE	START					;WHEN EQUAL CONTINUE TO NEXT PART


clr		count

sbrs	OUTPUT ,2				;IF OUTPUT,2 CLEAR, JUMP TO ON. IF SET JUMP TO OFF
rjmp	on
rjmp	off

on:	
ori		OUTPUT ,0b00000100		;SET OUTPUT,2
out		porta,	OUTPUT			;OUTPUT ON PORT A TO ACTIVATE A2
rjmp	start					;START AGAIN

off:
andi	OUTPUT ,0b11111011		;CLR OUTPUT,2
out		porta,	OUTPUT			;OUTPUT ON PORT A TO DEACTIVATE A2
rjmp	start					;START AGAIN


LIGHT:							;INTERRUPT ROUTINE
in		r16,	sreg			;CAPTURE SREG
inc 	count					;INCREMENT COUNT
out		sreg,	r16				;UPDATE SREG
RETI
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember that you always set the OCR to 1 less than you think you want. So if you want 125 counts you put 124 in OCR (because 0 is also counted)

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

still about 10 seconds slow per minute at 124

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

Quote:

Im using the 127Khz internal oscillator

Quote:

As im using 128kHz resonato

So, are you using the internal clock, or an external resonator?

What does the datasheet say about the accuracy of this clock? Is there an OSCCAL value that can be loaded? Does it vary with supply voltage and temperature?

That said, 16% is way off.

That model has clock output feature on CKOUT. Enable it and check the system clock speed.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.