OC1 will not toggle

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

I am trying to setup my timer on the STK-500 using the AT90S8515 that came with
the kiit. I can get the timer running but I am having trouble getting the output pin to
toggle at the frequency I want. It seems to be toggling at Clk/2 or 1.854 MHz.

The code I have written (In AVRStudio 3.54) is as follows:

.def Temp = r16

.org 0x0000 ;Place following code at address 0x0000

rjmp Reset ;Relative jump to Reset Label

Reset:

ldi Temp,0xFF
out DDRD,Temp ;Set PortD to Output

ldi Temp,(1 << COM1A0) ;Toggle OC1X
out TCCR1A,Temp ;Set Timer Counter Control Register 1A
ldi Temp,(1<<ICES1)|(1<<CTC1)|(1<<CS10)
out TCCR1B,Temp ;Set update rising edge ICES1
;Clear on Match CTC1
;Use System Clock
ldi Temp,(1 << OCIE1B)
out TIMSK,Temp ;Set Timer Interrupt Mask to Output Compare channel B

ldi Temp,0x6e
out OCR1BH,Temp ;Set Compare value high byte
ldi Temp,0x9b
out OCR1BL,Temp ;Set Compare value low byte

sei ;Enable interrupts

Loop:

nop
rjmp Loop ;Endless loop for testing

This Code gives a frequency on Pin D5 of 1.854 MHz.
Changing the prescale changes this value so the timer is basically set.
I am sure there is something small I am missing if someone can point it out
I would be very appreciative.

Thank You

Bob Parry

Bob Parry

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

I see a couple of things.

1. You setup to toggle OC1A but load the output compare B register. According to the data sheet I have, output compare A registers are initialized to 0 which would give you Clk/2 toggle rate.

2. You have enabled interrupts but don't have an interrupt handler for output compare B. I think you wanted output compare A but then again if you have enabled clear on timer compare there really is no reason for output compare A interrupt. Flash address 0x000 through 0x00C are for interrupt handlers.

Hope this helps

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

Toggleing implies a divide by two. One clk cycle switches the OC1 bit on with the next clk cycle switching the OC1 bit off. Therefore in my experiece with the avr the OC1 bit freq will be Clk/2 when the CompA value is set at zero. Your code therefore is correct.
Regards
Ian

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

Thanks for the tips. I understand why the frequency is Clk/2, what I do not
kniw is why the frequency is not something less because of the value I loaded
into the output compare register High and Low. That maybe what the interrupt \
is for? I thought I had that implemented, but apparently not.

Bob Parry

Bob Parry

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

Bob
In re-reading the previous reply to your original post the author noted that you needed to load the compare value in OCR1AH and OCR1AL and not in OCR1BH and OCR1BL. I was recently playing with this same thing and found that the interrupt vector for COMPA must be followed with a RETI if other interupts with higher interrupt vector addresses are being used ie.

.org OC1Aaddr ; Output Compare1A Interrupt Vector Address
reti ; if this is not here then the next isr will be run

.org URXCaddr ; USART Receive Complete Interrupt Vector Address
rjmp UART_RX_interrupt ;interrupt handler[/code]

Hope you come right
Regards
Ian

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

Thanks Ian I got it. Interesting in the simulator (AVRStudio 3.56) PinD5 did not seem to
be toggling, however when I wrote to the chip it works fine. To get the proper frequency
I had to use Clk/2 for the calcs. As you said this makes sense.

Is there a way to see in the simulator the values in the TCTL1H and low, I do not see them
in the window. I would be using AVRStudio 4 but I tried to upgrade to 4.10 and have never
been able to get it to boot. (See my post on Getting 4.10 to boot)

Thanks Again

Bob Parry :D

Bob Parry

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

Quote:
I would be using AVRStudio 4 but I tried to upgrade to 4.10 and have never been able to get it to boot.

I had a similar problem when 4.07 first come out. It only worked when I increased the memory in my machine to about 200Mb because another application needed it and then I noticed that studio started to work.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Bob
Glad you have got your RB1 to toggle. Yes even in version 4.1 I can not see the output switching but I do see the corresponding PinB bit switching. As for TCTL1H and TCTL1L. I can see these registers when I click on TIMER_COUNTER_1 in the output Workscapce window (I/O tab).
For the record I am still new to the AVR and have been playing with my STK500 for about a month now. My first project is to build a PC based function generators and frequency counter.

Regards
Ian