Atmega32-Timer0 CTC mode interrupt

Status
Not open for further replies.

Haricharan

New Member
dear friends
I have some problem with Atmega32 Timer0 application. I just wanted the timer0 to give a interrupt when a compare match occurs (CTC) mode. given below is the listing. i am using a Atmel studio 7 simulator. Everything works fine while simulating, but when match occurs between TCNT0 and OCR0, the flag OCF0 is also set in TIFR register bit 1, , and TCNT0 rolls back to 00 and starts again. but the program doesn't go to its vector address $0014. I have enabled SEI as well as CTC interrupt. ?. Kindly help thanks in advance
regards
BJ Hari charan


; soap.asm
; Author : BJ Hari; 5147d = 141Bh
;
;INCLUDE "M32DEF.INC"

.ORG $000

JMP START
.ORG $0002
;JMP SER_INTER

.ORG $0014
JMP MATCH

.ORG $75
START:
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
;********Serial port intializasion***********
LDI R16,(1<<RXEN)|(1<<TXEN)
OUT UCSRB,R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL)
OUT UCSRC,R16
LDI R16,39
OUT UBRRL,R16
LDI R16,$00
OUT UBRRH,R16
;***not used***interrupt intialisation**********
;SBI DDRC,3 ;
;SBI PORTD,2 ; pull up resistor
;LDI R20,(1<<INT0)
;OUT GICR,R20
;SEI
;WAIT:
;RJMP WAIT
;********************************************
;**Trigger Ultrasonic***PORT A AS OUTPUT INTIALISATION*****

SBI DDRA,3 ; OUT PUT
SBI DDRA,4
SBI PORTA,3;LED PULSE
CALL DELAY
CALL DELAY
CALL DELAY
CBI PORTA,3;LED PULSE OFF
SBI PORTA,4

MINI_DLAY:
LDI R17,70
NOP
COUNT:
DEC R17
BRNE COUNT
NOP
CBI PORTA,4

;********Timer0 intialisation****+ Start******
LDI R16,(1<<OCIE0) ; COMPARE MATCH INTERUPT ENABLE FOR TIMER0
OUT TIMSK,R16
SEI

LDI R20,00
OUT TCNT0,R20 ;
LDI R20,$0C ; FOR MATCHING
OUT OCR0,R20
LDI R20,$09; B; 0B00100011; CTC mode; CLOCK DIV BY 64
OUT TCCR0,R20; start timer0

FIND:
RJMP FIND

;**Hex to BCD*********DIV 1000*********03ff**
LDI R23,$03
LDI R22,$db
LDI R21,$03 ;small r23 r22 03FF
LDI R20,$0E8 ;r21 r20 03E8= 1000
REPEAT1:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20 ; remainder R23,R22
SBC R23,R21 ;
BRCC next1 ; branch on no carry
RJMP next2
next1:
INC R18
STS digit3,R18
RJMP REPEAT1
next2:
;*******Div by 100********
LDS R23,TEMPH ;get old R23,R22
LDS R22,TEMPL
LDI R21,$00 ;
LDI R20,$64 ;100
Repeat2:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20
SBC R23,R21
BRCC next3
RJMP next4
next3: INC R18
STS digit2,R18
RJMP Repeat2
next4:
;***********Div by 10 *****
LDI R18,0
LDS R23,TEMPH ; get old values
LDS R22,TEMPL
LDI R21,0
LDI R20,10
Repeat3:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20
SBC R23,R21 ;div by 10
BRCC next5
LDS R23,TEMPH
LDS R22,TEMPL
STS digit0,R22; REMINDER
Hang:
RJMP Hang
next5:
INC R18
STS digit1,R18
RJMP Repeat3

CHECK: ;WAIT FOR TIMER0 MATCH
RJMP CHECK; STOP Here after Compare match interrup

;**************************************************
SER_INTER:
;************************************************
LDI R16,$FF
OUT DDRB,R16 ;Port B output
LDI R22,$00
OUT PORTB,R22

LDI R22,$FF
OUT PORTB,R22; toggle led
CALL DELAY
CALL DELAY
CALL DELAY
LDI R22,$00
OUT PORTB,R22;off

LDI ZH,HIGH(EXTRACT<<1)
LDI ZL,LOW(EXTRACT<<1)
AGAIN1:
LPM R20,Z
TST R20

BREQ OVER
CALL TRANSMT
INC ZL
RJMP AGAIN1
OVER:
RETI
;*******************
NOP
;*******************
TRANSMT:
SBIS UCSRA,UDRE
RJMP TRANSMT
OUT UDR,R20
CALL DELAY
RET
;********************
DELAY:
LDI R23,200
AGAIN:
LDI R24,255
HERE:
NOP
NOP
NOP
DEC R24
BRNE HERE
DEC R23
BRNE AGAIN
RET
;********************
.ORG $200
EXTRACT:

.DB $1F,$11," BJ HARICHARAN",$00

MATCH:
LDI R18,8
OUT TCCR0,R18 ; STOP TIMER0
RETI
 
Your Code spit out some errors in my STUDIO 7 IDE.
I've changed something:
Code:
; Replace with your application code
; soap.asm
; Author : BJ Hari; 5147d = 141Bh
;
.INCLUDE "M32DEF.INC"

.org 0x000                    ; kommt ganz an den Anfang des Speichers
jmp START           ; Interruptvektoren überspringen
                              ; und zum Hauptprogramm
reti; jmp EXT_INT0 ; IRQ0 Handler
reti; jmp EXT_INT1 ; IRQ1 Handler
reti; jmp EXT_INT2 ; IRQ2 Handler
reti; jmp TIM2_COMP ; Timer2 Compare Handler
reti; jmp TIM2_OVF ; Timer2 Overflow Handler
reti; jmp TIM1_CAPT ; Timer1 Capture Handler
reti; jmp TIM1_COMPA ; Timer1 CompareA Handler
reti; jmp TIM1_COMPB ; Timer1 CompareB Handler
reti;$012 jmp TIM1_OVF ; Timer1 Overflow Handler
jmp MATCH ; Timer0 Compare Handler
reti; jmp TIM0_OVF ; Timer0 Overflow Handler
reti; jmp SPI_STC ; SPI Transfer Complete Handler
reti; jmp USART_RXC ; USART RX Complete Handler
reti; jmp USART_UDRE ; UDR Empty Handler
reti; jmp USART_TXC ; USART TX Complete Handler
reti; jmp ADC ; ADC Conversion Complete Handler
reti; jmp EE_RDY ; EEPROM Ready Handler
reti; jmp ANA_COMP ; Analog Comparator Handler
reti; jmp TWI ; Two-wire Serial Interface Handler
reti; jmp SPM_RDY ; Store Program Memory Ready Handler
;
       
;.ORG $000

;JMP START
;.ORG $0002
;JMP SER_INTER

;.ORG $0014
;JMP MATCH

.ORG $75
START:
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
;********Serial port intializasion***********
LDI R16,(1<<RXEN)|(1<<TXEN)
OUT UCSRB,R16
LDI R16,(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL)
OUT UCSRC,R16
LDI R16,39
OUT UBRRL,R16
LDI R16,$00
OUT UBRRH,R16
;***not used***interrupt intialisation**********
;SBI DDRC,3 ;
;SBI PORTD,2 ; pull up resistor
;LDI R20,(1<<INT0)
;OUT GICR,R20
;SEI
;WAIT:
;RJMP WAIT
;********************************************
;**Trigger Ultrasonic***PORT A AS OUTPUT INTIALISATION*****

SBI DDRA,3 ; OUT PUT
SBI DDRA,4
SBI PORTA,3;LED PULSE
CALL DELAY
CALL DELAY
CALL DELAY
CBI PORTA,3;LED PULSE OFF
SBI PORTA,4

MINI_DLAY:
LDI R17,70
NOP
COUNT:
DEC R17
BRNE COUNT
NOP
CBI PORTA,4

;********Timer0 intialisation****+ Start******
LDI R16,(1<<OCIE0) ; COMPARE MATCH INTERUPT ENABLE FOR TIMER0
OUT TIMSK,R16
SEI

LDI R20,00
OUT TCNT0,R20 ;
LDI R20,$0C ; FOR MATCHING
OUT OCR0,R20
LDI R20,$09; B; 0B00100011; CTC mode; CLOCK DIV BY 64
OUT TCCR0,R20; start timer0

FIND:
RJMP FIND

;**Hex to BCD*********DIV 1000*********03ff**
LDI R23,$03
LDI R22,$db
LDI R21,$03 ;small r23 r22 03FF
LDI R20,$0E8 ;r21 r20 03E8= 1000
REPEAT1:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20 ; remainder R23,R22
SBC R23,R21 ;
BRCC next1 ; branch on no carry
RJMP next2
next1:
INC R18
STS digit3,R18
RJMP REPEAT1
next2:
;*******Div by 100********
LDS R23,TEMPH ;get old R23,R22
LDS R22,TEMPL
LDI R21,$00 ;
LDI R20,$64 ;100
Repeat2:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20
SBC R23,R21
BRCC next3
RJMP next4
next3: INC R18
STS digit2,R18
RJMP Repeat2
next4:
;***********Div by 10 *****
LDI R18,0
LDS R23,TEMPH ; get old values
LDS R22,TEMPL
LDI R21,0
LDI R20,10
Repeat3:
STS TEMPH,R23
STS TEMPL,R22
SUB R22,R20
SBC R23,R21 ;div by 10
BRCC next5
LDS R23,TEMPH
LDS R22,TEMPL
STS digit0,R22; REMINDER
Hang:
RJMP Hang
next5:
INC R18
STS digit1,R18
RJMP Repeat3

CHECK: ;WAIT FOR TIMER0 MATCH
RJMP CHECK; STOP Here after Compare match interrup

;**************************************************
SER_INTER:
;************************************************
LDI R16,$FF
OUT DDRB,R16 ;Port B output
LDI R22,$00
OUT PORTB,R22

LDI R22,$FF
OUT PORTB,R22; toggle led
CALL DELAY
CALL DELAY
CALL DELAY
LDI R22,$00
OUT PORTB,R22;off

LDI ZH,HIGH(EXTRACT<<1)
LDI ZL,LOW(EXTRACT<<1)
AGAIN1:
LPM R20,Z
TST R20

BREQ OVER
CALL TRANSMT
INC ZL
RJMP AGAIN1
OVER:
RETI
;*******************
NOP
;*******************
TRANSMT:
SBIS UCSRA,UDRE
RJMP TRANSMT
OUT UDR,R20
CALL DELAY
RET
;********************
DELAY:
LDI R23,200
AGAIN:
LDI R24,255
HERE:
NOP
NOP
NOP
DEC R24
BRNE HERE
DEC R23
BRNE AGAIN
RET
;********************
.cseg
;.ORG $200
EXTRACT:

.DB $1F,$11," BJ HARICHARAN"

MATCH:
LDI R18,8
OUT TCCR0,R18 ; STOP TIMER0
RETI

.dseg
TEMPH: .BYTE 1
TEMPL: .BYTE 1
digit0: .BYTE 1
digit1: .BYTE 1
digit2: .BYTE 1
digit3: .BYTE 1
In my STUDIO 7 the Timer 0 Interrupt Vector wouldn't be active.
Because all the Registers are proper filled, in real Chip it should work.
I guess there is a BUG in debugging Software. Try it out!

EDIT!
The Command:
LDI R20,$09; B; 0B00100011; CTC mode; CLOCK DIV BY 64
don't divide by 64 it divides by 1!
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…