;*******************************************************************
;* This stationery serves as the framework for a user application. *
;* For a more comprehensive program that demonstrates the more     *
;* advanced functionality of this processor, please see the        *
;* demonstration applications, located in the examples             *
;* subdirectory of the "Freescale CodeWarrior for HC08" program    *
;* directory.                                                      *
;*******************************************************************

; Include derivative-specific definitions
            INCLUDE 'derivative.inc'
            
;
; export symbols
;
            XDEF _Startup
            ABSENTRY _Startup

;
; variable/data section
;
            ORG Z_RAMStart 
count1:		RMB    1    
count2:     RMB	   1       
            
            ORG    RAMStart         ; Insert your data definition here
ExampleVar: DS.B   1

;
; code section
;
            ORG    ROMStart
; all-gpios initialisation - type: output, state: log.1, pullups-off
gpio_init:
        lda     #$FF	
        sta     PTFDD	; Make Port F as output
        sta	    PTBDD   ; Make Port D as output
        
        lda     #$00
		sta 	PTFD	; Light Bar (Port F) = 00 (off) 
		
		lda     #$FF
		sta 	PTBD	; Seven Segment connected to Port B = FF (off for anode type)
		
	
        rts
        
;- GPIO_INIT --------------------------------------------------------------------------------      

;- TIMER_INIT -------------------------------------------------------------------------------
; timer_init: initializes timer to free run, no o.c., no i.c., leaves timer stopped
timer_init:
				
				
        ;mov     #$4F,TPM1SC ; overflow interrupt enable, prescaler=64 ($76 =%0100 1111)    PRESCALER = 128
                                    ; format: TOF(0) TOIE(1) CPWMS(0) CLKSB(0) CLKSA(1) PS2(1) PS1(1) PS0(1)
        mov     #$4E,TPM1SC ; overflow interrupt enable, prescaler=64 ($76 =%0100 1110)  PRESCALER = 64
                                    ; format: TOF(0) TOIE(1) CPWMS(0) CLKSB(0) CLKSA(1) PS2(1) PS1(1) PS0(1)
                                  
                                  ; PS2:PS1:PS0    TPM Clock Source Divided-By
								  ;	0:0:0 					1
								  ;	0:0:1 					2
								  ; 0:1:0 					4
								  ; 0:1:1   				8
								  ; 1:0:0 					16
                                  ; 1:0:1 					32
                                  ; 1:1:0 					64
                                  ; 1:1:1 					128
                                  
                                  ; CLKSB:CLKSA TPM Clock Source to Prescaler Input
								  ; 0:0 			No clock selected (TPMx disabled)
								  ; 0:1 			Bus rate clock (BUSCLK)
								  ; 1:0 			Fixed system clock (XCLK)
								  ; 1:1 			External source (TPMxCLK)1,2

        mov     #$4B,TPM1MODH      ; Modulo count = 19200 (4B00H)
        mov     #$00,TPM1MODL
				; Bus clock speed = 2457600 Hz
				; Prescaler = 64
				; Interrupt period = 1 / 2457600 * 64 * 19200 = 0.5 second 

        rts
;- TIMER_INIT -------------------------------------------------------------------------------        

_Startup:
            LDHX   #RAMEnd+1        ; initialize the stack pointer
            TXS
            CLI			; enable interrupts
                      
			;   MCU runs w/o LVI and COP support
			lda     #$00
		    sta     SPMSC1
		    
		    lda     #$00
		    sta     SOPT
		    
		    
            jsr gpio_init	; Initialize I/O Ports
            
            jsr	timer_init  ; Initialize timer
            
            clr     count1   ; count  = 0
            clr     count2   ; count2 = 0

mainLoop:
            ; Insert your code here

            jmp    mainLoop
        

timer_isr:

         bclr    7,TPM1SC           ; clear TOF (Timer Overflow Flag) in TPM1SC  - handler is finishing

         lda     count1
         
         cmp     #$00
         ble     SHOW0
         cmp     #$01
         ble     SHOW1
         cmp     #$02
         ble     SHOW2
         cmp     #$03
         ble     SHOW3
         cmp     #$04
         ble     SHOW4
         cmp     #$05
         ble     SHOW5
         cmp     #$06
         ble     SHOW6
         cmp     #$07
         ble     SHOW7
         cmp     #$08
         ble     SHOW8
         cmp     #$09
         ble     SHOW9

		 ; Show numbers on Seven Segment Display Connected to Port B
SHOW0:   MOV #$80,PTBD
         bra exitint
SHOW1:   MOV #$F3,PTBD
         bra exitint
SHOW2:   MOV #$48,PTBD
         bra exitint
SHOW3:   MOV #$61,PTBD
         bra exitint
SHOW4:   MOV #$33,PTBD
         bra exitint
SHOW5:   MOV #$25,PTBD
         bra exitint
SHOW6:   MOV #$04,PTBD
         bra exitint
SHOW7:   MOV #$F0,PTBD
         bra exitint
SHOW8:   MOV #$00,PTBD
         bra exitint
SHOW9:   MOV #$20,PTBD
         lda #$20
         mov #$ff,count1		; count = FF
         inc count2 		    ; When count1 = 09, then increase count2
         mov count2,PTFD	    ; Show count2 on Light bar (Port F) 
         bra exitint  
  
         ; RETURN FROM INTERRUPT
exitint:
	
         inc    count1
         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			; TPM1 Overflow
			DC.W	timer_isr
			
			ORG		Vadc1				; ADC Interrupt
			DC.W	spurious			; ADC Interrupt Server

            ORG	$FFFA
			DC.W  	spurious			; IRQ
			DC.W  	spurious			; SWI
			DC.W  	_Startup			; Reset
