Timer1 doesn't start - Assembly ATmega2560

Hello again,

I have a slight problem with Timer1 in arduino 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, 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 for your help :slight_smile:
Best regards

I can not help you with the timer setup, sorry.

Your ISR toggles a bit in a very inefficient way.
Just write the bit(s) you want to toggle to the PINx register and they will toggle.

I know it does haha, it’s just for experiment purposes :smiley:

And I just menaged to fix it. I had a bug there with setting the bit WGM11 on TCCR1A which doesn’t run the CTC mode, but mode 2 (PWM, correct phase, 9-bit)
in lines

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

it should instead be

ldi r20, 0b00000000 ; CTC mode, int clk;
	sts TCCR1A, r20		
	ldi r20, 0b000001101 ; prescaler /1024
	sts TCCR1B, r20

Thank you though :slight_smile:
Best regards