2

I have a slight problem with Timer1 in arduino (ATmega2560) while coding it in assembly. I try to run the Timer1 in CTC mode - I configure everything well in my opinion, but the TCNT1 just doesn't increment each clock cycle, as it should. I checked the registers in Atmel Studio and everything seems okay, but maybe I forgot about something?

Here is the code

.ORG 0
jmp start
.ORG 0x22
call isr_toggle_PA0
.ORG 0x400
start:
LDI R16, HIGH(RAMEND)
OUT SPH, R16
LDI R16, LOW(RAMEND)
OUT SPL, R16 ; initialize stack pointer
clr r16
ldi r16, 0xff
out DDRA, r16 ; port A output
ldi r16, 0x00
out DDRD, r16 ; port D input
clr r16
ldi r20, 1<<OCIE1A
sts TIMSK1, r20 ; enable Timer1 A comapre match interrupt
sei
ldi r20, HIGH(25000)
sts OCR1AH, r20
ldi r20, LOW(25000)
sts OCR1AL, r20
ldi r20, 0b00000010 ; CTC mode, int clk;
sts TCCR1A, r20 
ldi r20, 0b000000101 ; prescaler /1024
sts TCCR1B, r20
ldi r20, 0
sts TCCR1C, r20
; --- start main
main:
inc r16
rjmp main
; --- end main
isr_toggle_PA0: // toggle bit on PA0
 ldi r16,0x00000001
 in r17, PORTA
 eor r17,r16
 out PORTA, r17
 reti

Thank you and have a good day :)

asked Nov 26, 2018 at 14:26

1 Answer 1

3

Here:

ldi r20, 0b00000010 ; CTC mode, int clk;
sts TCCR1A, r20 
ldi r20, 0b000000101 ; prescaler /1024
sts TCCR1B, r20

you are setting the bit WGM11 on TCCR1A. The timer then runs in mode 2 (PWM, Phase Correct, 9-bit) rather than mode 4 (CTC).

answered Nov 26, 2018 at 15:50
1
  • Amazing!! Ineed that was the bug. For the ones wondering, the correct version is ldi r20, 0b00000000 ; CTC mode, int clk; sts TCCR1A, r20 ldi r20, 0b000001101 ; prescaler /1024 sts TCCR1B, r20 Commented Nov 26, 2018 at 16:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.