Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

16Bit timer : ATmega8535

Status
Not open for further replies.

lord loh.

Member
I am trying to simulate the 16bit timer on Atmega8535. I have enabled the global interrupt enable and also the Interrupt on Timer1 overflow. My interrupts are also working fine but the TOV1 flag is never set. I put the break points at the right places to see the flag conditions on AVR Studio. If I manually set the flag, the interrupt occurs. But undernatural free running simulation, the OCF1A (Compare Register A match) flag is set. However the OCF1B is never set. I do not understand the reason for this behaviour.

Please help.

The code

main.asm
Code:
.EQU Xtal=8
.NOLIST
#include<m8535def.inc>
.include"macros.inc"
.LIST

.CSEG
;Interrupt Vector Table
.ORG 0x000
		rjmp main		;Reset Vector
.ORG 0x008
		rjmp timer1_ovf		;Timer1 Overflow
.ORG 0x00E
		rjmp adccomplete	;ADC conversion Complete.

;Main Subroutine
.ORG 0x015
main:
		ldi r16,HIGH(ramend)	;Init. Stack pointer
		out sph,r16
		ldi r16,LOW(ramend)
		out spl,r16
		bset SREG_I		;Enable Global Interrupt
		rcall loadtimer
		ldi r16,$04		;Enable Timer1 Overflow Interrupt.
		out timsk,r16
		ldi r16,$00
		out tccr1a,r16
		ldi r16,tccr1b_value	;Start Timer1
		out tccr1b,r16


terminate:	rjmp terminate		;END Programme here.

;Timer1 Overflow Interrupt service routine
timer1_ovf:
		nop
		nop
		reti

;ADC Conversion Complete service routine
adccomplete:
		reti

;Other Subroutines
loadtimer:
	;F424 counts @8MHz and prescale 64 accounts for 0.5s
	;0DBD to FFFF accounts for F424
		ldi r16,$0D
		out tcnt1h,r16
		ldi r16,$BD
		out tcnt1l,r16
		ret

macros.inc
Code:
//Half Second count Values @ some prescaler settings
.IF (xtal==8)
.EQU COUNT=0x0DBD
.EQU PRESCALE=0x03			;prescale 64
.ELIF (xtal==1)
.EQU COUNT=0x0DBD
.EQU PRESCALE=0x02			;prescale 2
.ENDIF

.SET tccr1b_value=PRESCALE
 
I cut and paste your code into the simulator and set a breakpoint at the first line of the timer overflow interrupt. The first interrupt happens at ~496msec, and takes roughly ~25 seconds to simulate on my relatively recent computer.

The TOV1 flag will only appear for a 1-2 cpu cycles, and is immediately cleared when the interrupt is called.

As for the OC1FA, OC1FB issue, I think that's a bug in the simulator. I guess having 3 seperate events happening at the same time is confusing it a bit.
 
hjames said:
The first interrupt happens at ~496msec, and takes roughly ~25 seconds to simulate on my relatively recent computer.
How on earth did you arrive at this figure? 496ms. @8Mhz, I computed my count to give me exactly .5 sec... I am new to AVR studios, but am familiar with a stop clock in MPLAB IDE.

I put my breakpoint at the interrupt vector and still did not find the TOV1 set. What version of AVR are you using? Mine is 4.12 service pack 2 Build 472

hjames said:
As for the OC1FA, OC1FB issue, I think that's a bug in the simulator. I guess having 3 seperate events happening at the same time is confusing it a bit.
Pleasing to know that there is a bug in the simulator and not my code... I discoverd a similar in MPLAB IDE. It was not simulating an interrupt for a particular timer at a particular prescaler.
 
I set a breakpoint at the beginning of the interrupt itself- putting breakpoints into jump tables strikes me as a little hazardous.

As for the timing number:
.5 * 8000000 / 64 = 62500
65536-62500 = 3036 = 0xBDC which is a character swap from the number you have.

personally I don't use the simulator or IDE much - I just downloaded this for the first time today. Most of the time I'm using avr-gcc on a Linux box, and just try to 1) incrementally code and test in hardware 2) get it right the first time :)
 
Oh!

I did 65,535-62500 :eek:

I'll correct that... Thank you. I am new to AVR and have taken to simulationg... But I am aware of simulation errors. I faced it in PIC where the programme just would not simulate as expected but worked clean on the device.

Thank you.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top