Timer1 CompA ISR

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
Post Reply
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Timer1 CompA ISR

Post by sturgessb »

Hi all

Im trying to get a an ISR to fire every time Timer1 Reaches a certain value.

I have the following code but its just freezing up the zx (128ne)

Code: Select all

Register.TCCR1A = bx0000_0000
Register.TXXR1B = bx0000_0011
Register.TIMSK = bx0001_0000
Register.OCR1AL = 0
Register.OCR1AH = 0
and this is the ISR

Code: Select all

ISR TIMER1_COMPA()
    debug.print "compare match"
END ISR
What am i doing wrong?

Cheers

ben
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

I have also tried it on the zx24n with the following code....

Code: Select all

Sub InitTimer()
	Register.TCCR1B = bx0000_0100
	Register.TIMSK1 = bx0000_0010
	Register.OCR1AL = 0
	Register.OCR1AH = 0
	Register.TCNT1L = 0
	Register.TCNT1H = 0
End Sub


ISR Timer1_CompA()
	debug.print "MATCH"
END ISR
And still nothing.........?

Ben
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Post by pjc30943 »

[Unhelpful comment deleted in light of Mike's reply...]
Last edited by pjc30943 on 28 September 2009, 14:15 PM, edited 1 time in total.
Paul
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

I wouldn't use debug.print for production code but it is ok to get something going. I'm away from my office at present so I don't have a device to test with. I compared your registers to the datasheet. There are two things missing:

1. You need to use CTC mode which WGM12 (bit 4) in TCCR1B.

2. You need to use a non-zero value in OCR1. For the prescaler you have chosen of 64, I would suggest a hex value of 1C2 to get 512 interrupts per second. The math is 14745600/(512*64) = 450 = 0x1C2.

Similarly for the mega644p in the ZX-24n.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

mikep wrote:I wouldn't use debug.print for production code but it is ok to get something going.
I would vociferously recommend against using Debug.Print in an ISR under any conditions. The problem is that in an ISR interrupts are disabled. If the Com1 output queue is full, Debug.Print will wait for space to become available but, since interrupts are disabled, space will never become available.

A better strategy is to turn on an LED, or just toggle an I/O line and monitor it with a logic probe, logic analyzer, oscilloscope, etc. Any other indicator that you can think of that doesn't require interrupts to be serviced will also work.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

dkinzer wrote:The problem is that in an ISR interrupts are disabled. If the Com1 output queue is full, Debug.Print will wait for space to become available but, since interrupts are disabled, space will never become available.
Good point.

I never usually have to debug my ISRs in any case as they work first time because they are so short :)
Mike Perks
sturgessb
Posts: 287
Joined: 25 April 2008, 6:34 AM
Location: Norwich, UK

Post by sturgessb »

Ok ill change it to an LED flash.

and add the missing Bit.

Thanks
Post Reply