CTC / OCR0a mystery question

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

Really appreciate any help that can be offered here - this is driving me crazy.

I'm using Timer0 on an ATMega328, trying to set it up for OCRa interrupt on match, and with auto-clear via CTC. So it should count to 4, reset to 0 via CTC, and generate a match interrupt.

Loading it on a processor, it gave indications of RESET, as I had some I/O strobes that have been taken out just to simplify the code.

So I'm running it on the simulator, and that also does a bizarre thing at the CTC match time (counter = 4).... just suddenly moves the PC and starts executing code again a few lines into the program! Never hits any of the vectors, just changes the PC and Stack-ptr.

If any of the experts on this board have time to look at it, I would really appreciate it.... short and sweet.... pasted below and enclosed.

tnx,

-mark

====================================================
.include	"m328Pdef.inc"			;standard processor definitions for ATMega328

.def	        tempr1	=r24			;Scratchpad register

.cseg  
.org 0

RESET:						;******* VECTOR TABLE ******	
		RJMP	INIT			;Reset
		RETI				;Int0 
		RETI				;Int1
		RETI				;PCint0
		RETI				;PCint1
		RETI				;PCint2
		RETI				;WDT
		RETI				;Timer2_CmpA
		RETI				;Timer2_CmpB
		RETI				;Timer2_Ovflow
		RETI				;Timer1_Capt
		RETI				;Timer1_CmpA
		RETI				;Timer1_CmpB
		RETI				;Timer1_Ovflow
		RJMP	Timer0_OCRa		;Timer0_CmpA
		RETI				;Timer0_CmpB
		RETI				;Timer0_Ovflow
		RETI				;SPI, STC
		RETI				;USART Rx
		RETI				;USART UDRE
		RETI				;USART Tx
		RETI				;ADC done
		RETI				;EEPROM done

INIT:		LDI	tempr1,low(RAMEND)	;Initialize the Stack Ptr
		OUT	SPL,tempr1
		LDI	tempr1,high(RAMEND)
		OUT	SPH,tempr1

; Timer0 setup
		LDI	tempr1,1<<CS01 		;Prescaler = /8 (8mhzXTAL / 8 = 1mhz)
		OUT	TCCR0B,tempr1

		LDI	tempr1,0		;Initialize Timer register
		OUT 	TCNT0,tempr1
		
		LDI	tempr1,4 		;Initialize OCR0a for the match
		OUT	OCR0A,tempr1

		LDI	tempr1,1<<WGM01		;Set WGM1 to a '1' to set Clear-Timer-on-Compare (CTC) mode
		OUT	TCCR0A, tempr1

		LDI	tempr1,1<<OCIE0A	;Interrupts enabled on OCR0a matches
		STS	TIMSK0,tempr1

		SEI				;Enable irq's

background:	RJMP	background		;Just loop here waiting on Timer0 OCRa

;  Timer0 - OCR0a irq

Timer0_OCRa:	RETI

$ Fixed code tags - JS $

Attachment(s): 

Mark
Elgin, IL (near Chicago)

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

How wide is each interrupt vector table entry on a Mega328?

[Hint: I do not think it is one word.]

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.

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

Makes sure the vector is set to correct address. Surest way to do that is just use .org with address from the datasheet. Check the mode that the timer is in using the mode table in the 'sheet for timer0.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Thanks for the repies.
Checked vector-size again - 1-word.
'.org 0' is in this trial code.

You're on the right trail though.... after initializing for the OCR0a irq with CTC resetting, I go into an endless loop waiting on the interrupt.

That loop is at $27.... guess what is on the stack when the simulator suddenly vectors to $1C ? .... a $27. Also, maybe a clue - it vectors to $1C, which is 4x the correct vector location ($07).

This seems like such a common operating mode.... but am starting to suspect a bug in the processor.... but the Sim does it too!

Really appreciate you looking at this.... am stumped now.

Mark
Elgin, IL (near Chicago)

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

fmr300 wrote:
Thanks for the repies.
Checked vector-size again - 1-word.

And I would suggest you check yet again this time be sure to look at the correct table, as it is a combined datasheet. (hint: page 65 of the Rev E datsheet - section 12.4, table 12-6) The .org comment was a suggestion to place a .org statement and the vector name in front of each entry, this way your vectors will always be at the correct address.

[FYI: ANY AVR with more than 8K of flash uses 2 words per vector entry instead of one - and "program address" in the datasheet is a word address, not a byte address]

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

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

2-words it is ! And that cures my issue. I was looking at the ATMega-88 vector table!

Wow.... I should have figured out that using RJMP in the vector table wouldn't work for large-flash devices !

Thanks for the help guys.... slightly embarrassed that I missed this.... it was soooooo acting like a vector problem..... hindsight is always 20-20.

This board is the best!

Mark
Elgin, IL (near Chicago)

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

Quote:
Wow.... I should have figured out that using RJMP in the vector table wouldn't work for large-flash devices !
Well, RJMP will work, it just won't cover all of flash, and you need to put something else (such as a NOP) to cover the other word or use labels as suggested above.

Regards,
Steve A.

The Board helps those that help themselves.