Diferencia entre revisiones de «Ejemplo Timer RTC y TPM de forma simultánea»

De Wikitronica
Saltar a: navegación, buscar
Línea 2: Línea 2:
 
[[Categoría: EC3731 - Arquitectura del Computador 2]]
 
[[Categoría: EC3731 - Arquitectura del Computador 2]]
 
[[Categoría:MC9S08QE128]]
 
[[Categoría:MC9S08QE128]]
 
 
 
 
<syntaxhighlight lang="asm">
 
<syntaxhighlight lang="asm">
 
;*******************************************************************
 
;*******************************************************************

Revisión del 20:08 17 mar 2013

;*******************************************************************
;*Ejemplo de utilización de Timers por interrupciones              *
;*                                                                 *
;* Se utiliza el RTC para prender y apagar el led de PTC0 cada 2s. *
;* Se utiliza el TPM para prender y apgar el led de PTC1 cada 1s.  *
;*******************************************************************

; Include derivative-specific definitions
            INCLUDE 'derivative.inc'
;
; export symbols
;
            XDEF _Startup
            ABSENTRY _Startup
;
; variable/data section
;
            ORG    RAMStart         ; Insert your data definition here
ExampleVar: DS.B   1

;
; code section
;
            ORG    ROMStart

_Startup:
            LDHX   #RAMEnd+1        ; initialize the stack pointer
            TXS
            ;Configuracion de los pines del Demoqe128
            BSR Configurar_GPIO
			;------------------------------------------
			;Configuración del RTC
            BSR Configurar_RTC
			;------------------------------------------
			;Configuración del TPM
            BSR Configurar_TPM1                       
            CLI                     ; enable interrupts           
           
mainLoop:           
            NOP 
            feed_watchdog
            BRA    mainLoop
            
            
;**************************************************************
;* .             Subrutinas de Configuracion                  *
                                                              *
;**************************************************************
Configurar_GPIO:
			; Configuración de los leds del DEMOQE como salida
			; Los Leds tienen lógica negada
			;Parte inferior de Puerto C (0:5)
            LDA #$3F
            STA PTCDD
			STA PTCD
			;------------------------------------------
			;Parte superior de Puerto D (6:7)
            LDA #$C0
            STA PTEDD   
			STA PTED		 
            RTS
            
Configurar_RTC:           
			;------------------------------------------
            ;Habilitamos el Clock Gate para el RTC
            LDA SCGC2          
            ORA #%00000100
            STA SCGC2
            
			;------------------------------------------
            ;Definimos el valor del registro modulo del RTC
            LDA #$00
            STA RTCMOD
            
			;------------------------------------------
            ;Configuramos el RTCSC            
            ;Reloj configurado a 16ms. Fuente reloj interna 1khz.
            ;Interrupciones habilitadas                         
            LDA #%00010110
            ; RTIF=0, RTCLKS6=0 RTCLKS5=0, RTIE=1, RTCPS3=0, RTCPS2=1, RTCPS1=1, RTCPS0=0
            STA RTCSC
            RTS
            
Configurar_TPM1:
            ;Configuracion del timer TPM1 
            ;(Interrupcion en overflow del contador)
            LDA #%01001111
            ; TPM1SC: TOF=0,TOIE=1,CPWMS=0,CLKSB=0,CLKSA=1,PS2=1,PS1=1,PS0=1 
            ;Probar diferentes valores de PS2,PS1,PS0
            STA TPM1SC
            ;Valor maximo de conteo (Probar diferentes valores)
            LDHX    #$FFFF
            STHX    TPM1MOD                ; Compare 0 value setting 
            RTS
            
            
;**************************************************************
;* .             Rutinas de atencion a las interrupciones     *
                                                              *
;**************************************************************
            
Interrupcion_TPM1:
            ;Limpiamos las banderas de interrupcion
            ;Primero se lee el regristro TPMxSC
            LDA TPM1SC
            ;Luego se baja el bit 7 (TOF) escribiendo 0
            AND #%01111111
            STA TPM1SC           
            ;Negamos el pin0 de PTC (Led0)
            ;Revisamos si el PTC0 se encuentra encendido
            LDA PTCD
            BIT #%00000001
            BNE ApagarPTC0
            ;Si el PTC0 está apagado lo encendemos
PrenderPTC0:BSET 0,PTCD
            ;Regresamos de la interrupcion
            RTI

ApagarPTC0: BCLR 0,PTCD
            ;Regresamos de la interrupcion 
            RTI
            
            
Interrupcion_RTC:
            ;Limpiamos las banderas de interrupcion
            ;Se escribe un 1 en la bandera RTIF
            ;Diferente a TPM!!!
            LDA RTCSC
            ORA #%10000000
            STA RTCSC
          
            ;Negamos el pin1 de PTC (Led1)
            ;Revisamos si el PTC1 se encuentra encendido
            LDA PTCD
            BIT #%00000010
            BNE ApagarPTC1
            ;Si el PTC0 está apagado lo encendemos
PrenderPTC1:BSET 1,PTCD
            ;Regresamos de la interrupcion
            RTI

ApagarPTC1: BCLR 1,PTCD
            ;Regresamos de la interrupcion 
            RTI

;**************************************************************
;* spurious - Spurious Interrupt Service Routine.             *
;*             (unwanted interrupt)                           *
;**************************************************************
spurious:                           ; placed here so that security value
            NOP                     ; does not change all the time.
            RTI

;**************************************************************
;*                 Interrupt Vectors                          *
;**************************************************************
            ORG   Vtpm1ovf
            DC.W  Interrupcion_TPM1
            
            ORG   $FFCE
            DC.W  Interrupcion_RTC
            
            ORG   $FFFA
            DC.W  spurious          ;
            DC.W  spurious          ; SWI
            DC.W  _Startup          ; Reset