; ==================== Example2: (in Assembly) ====================
; Test
timers in ATmega8515
; The pwm
is changing it's duty cycle between 0 anf 255 and output to pin1 (B0)
; The duty
cycle value is stored in register "pwm".
; The pwm
is counting increasing and when it reaches 255 it
.NOLIST
.INCLUDE
"m8515def.inc"
.LIST
.def='MSO-TAB-COUNT:1'> cnt =
r17 ; counter
.def='MSO-TAB-COUNT:1'> pwm =
r18
; Code
starts here
.CSEG
.ORG $0000
; Reset-
and Interrupt-vectors
rjmp='MSO-TAB-COUNT:1'> main ;
1 Reset-vector
rjmp='MSO-TAB-COUNT:1'> IInt0
; 2 External Interrupt Request 0
rjmp='MSO-TAB-COUNT:1'> IInt1
; 3 External Interrupt Request 1
rjmp='MSO-TAB-COUNT:1'> TCpt1
; 4 Timer/Counter1
Capture event
rjmp='MSO-TAB-COUNT:1'> TCmp1
; 5 Timer/Counter1
Compare matchA
rjmp='MSO-TAB-COUNT:1'> TCmp2 ;
6 Timer/Counter1 Compare matchB
rjmp='MSO-TAB-COUNT:1'> TOvf1
; 7 Timer/Counter1
Overflow
rjmp='MSO-TAB-COUNT:1'> TOvf0
; 8 Timer/Counter0
Overflow
rjmp='MSO-TAB-COUNT:1'> SrTrC ;
9 SPI/Serial transfer complete
rjmp='MSO-TAB-COUNT:1'> URxCp ;10
USART Rx complete
rjmp='MSO-TAB-COUNT:1'> UTxDe
;11 Uart Tx data
register empty
rjmp='MSO-TAB-COUNT:1'> UTxCp
;12 Uart Tx complete
rjmp='MSO-TAB-COUNT:1'> AnaCp
;13 Analog comparator
rjmp='MSO-TAB-COUNT:1'> XINT2 ;14
External Interrupt2
rjmp='MSO-TAB-COUNT:1'> TCMch ;15
Timer / Counter0 Compare match
rjmp='MSO-TAB-COUNT:1'> EERdy ;16
EEPROM ready
rjmp='MSO-TAB-COUNT:1'> SPRdy ;17
Store Program Memory Ready
;
************** Interrupt service routines ********
IInt0: ='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 2
IInt1:='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 3
TCpt1:='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 4
TCmp1:='MSO-TAB-COUNT:1'> reti ;
5
TCmp2:='MSO-TAB-COUNT:1'> reti ;
6
TOvf1:='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 7
TOvf0:='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 8
SrTrC:='MSO-TAB-COUNT:1'> reti='MSO-TAB-COUNT:1'> ; 9
URxCp:='MSO-TAB-COUNT:1'> reti ;10
UTxDe:='MSO-TAB-COUNT:1'> reti ;11
UTxCp:='MSO-TAB-COUNT:1'> reti ;12
AnaCp:='MSO-TAB-COUNT:1'> reti ;13
XINT2:='MSO-TAB-COUNT:1'> ; external interrupt2
; In
Extended MCU Interrupt Register EMCUIR
; bit0:
ISC2: Interrupt Sense Control 2
; If ISC2=0
then the falling edge of INT2 generates the interrupt
; Provided
that I-flag=1 in SREG and in bit5=INTF2=1 (interrupt-enable bit) in GICR.
='MSO-TAB-COUNT:2'> reti='MSO-TAB-COUNT:1'> ;14
TCMch:='MSO-TAB-COUNT:1'> tst pwm
='MSO-TAB-COUNT:2'> brne='MSO-TAB-COUNT:1'> tcm
='MSO-TAB-COUNT:2'> inc='MSO-TAB-COUNT:1'> cnt
tcm:='MSO-TAB-COUNT:1'> sbrs='MSO-TAB-COUNT:1'> cnt,0
='MSO-TAB-COUNT:2'> rjmp='MSO-TAB-COUNT:1'> tcm1
='MSO-TAB-COUNT:2'> inc='MSO-TAB-COUNT:1'> pwm
='MSO-TAB-COUNT:2'> rjmp='MSO-TAB-COUNT:1'> tcm2
tcm1:='MSO-TAB-COUNT:1'> dec='MSO-TAB-COUNT:1'> pwm
tcm2:='MSO-TAB-COUNT:1'> out='MSO-TAB-COUNT:1'> OCR0,pwm='MSO-SPACERUN:YES'>
='MSO-SPACERUN:YES'>
='MSO-TAB-COUNT:2'> ldi='MSO-TAB-COUNT:1'> r16,1 ;
set OCF0 flag for compare interrupt
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:1'> TIFR,r16
='MSO-TAB-COUNT:2'> reti='MSO-TAB-COUNT:1'> ;15
EERdy:='MSO-TAB-COUNT:1'> reti ;16
SPRdy:='MSO-TAB-COUNT:1'> reti ;17
;
**************** End of interrupt service routines ***********
;
----------------------------------------------------------------------------------
; Timers
notes
; T/C0
output OC0: pin1
; TCCR0
Timer/Counter Control register
; Bit7 = FOC0 = 0
in fast PWM mode
; Fast PWM:
(bit6=WGM00, bit3=WGM01) = (1,1) = FAST pwm MODE
; clock
select
; bits
2,1,0 = cs02, cs01, cs0='MSO-TAB-COUNT:2'> function
;='MSO-TAB-COUNT:4'> 0='MSO-TAB-COUNT:2'> 0 0 no
source (stoped)
;='MSO-TAB-COUNT:4'> 0='MSO-TAB-COUNT:2'> 0 1 clock
;='MSO-TAB-COUNT:4'> 0='MSO-TAB-COUNT:2'> 1 0 clock/8
;='MSO-TAB-COUNT:4'> 0='MSO-TAB-COUNT:2'> 1 1 clock/64
;='MSO-TAB-COUNT:4'> 1='MSO-TAB-COUNT:2'> 0 0 clock/256
;='MSO-TAB-COUNT:4'> 1='MSO-TAB-COUNT:2'> 0 1 clock/1024
; (bit5,bit4)=(com01,com00)=(1,0) non-inverting
mode oc0=0 on match and oc0=1 on
; bottom.
; TIMSK
Timer Interrupt Mask Register: set bit0 = Output Compare Match Interrupt
; Enable =
OCIE0
; TIFR
Timer Interrupt Flag Register: bit0 = ocf0 = Output Compare Flag 0 goes 1
; when
match occurs
;
; ********************
Main program ********************************
main:
='MSO-TAB-COUNT:1'> ldi r16,LOW(RAMEND)='MSO-TAB-COUNT:1'> ;Initiate Stackpointer.
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> SPL,r16
='MSO-TAB-COUNT:2'> ldi='MSO-TAB-COUNT:2'> r16,HIGH(RAMEND)
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> SPH,r16
='MSO-TAB-COUNT:2'> ldi='MSO-TAB-COUNT:2'> r16,$ff
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> ddrA,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> ddrB,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> ddrC,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> ddrD,r16
='MSO-TAB-COUNT:2'> out ddrE,r16
='MSO-TAB-COUNT:2'> com='MSO-TAB-COUNT:2'> r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> portA,r16
='MSO-TAB-COUNT:2'> out portB,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> portC,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> portD,r16
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> portE,r16
='MSO-TAB-COUNT:2'> clr='MSO-TAB-COUNT:2'> cnt
='MSO-TAB-COUNT:2'> clr='MSO-TAB-COUNT:2'> pwm
='MSO-TAB-COUNT:2'> ldi='MSO-TAB-COUNT:2'> r16,$6a='MSO-TAB-COUNT:1'> ; TCCR0 set. clock/256, fast PWM
mode
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> TCCR0,r16
='MSO-TAB-COUNT:2'> ldi='MSO-TAB-COUNT:2'> r16,1 ; set OCF0 flag for compare interrupt
='MSO-TAB-COUNT:2'> out='MSO-TAB-COUNT:2'> TIMSK,r16
='MSO-TAB-COUNT:2'> sei
loop:='MSO-TAB-COUNT:1'> rjmp='MSO-TAB-COUNT:1'> loop
|