📄 main.asm
字号:
;*******************************************************************************************
; MAIN.ASM
;*******************************************************************************************
; Copyright (c) 2006 Freescale Semiconductor
; http://www.freescale.com/
;*******************************************************************************************
;
; ADC Demo for DEMO9RS08KA2
; ------------------------------------------------------------------------------------------
; ADC Implementation
; extern RC circuit needed
; Compare the Vin level with Capacitor charge waveform.
; Result is Stored in ConvertedValue Variable
;
;*******************************************************************************************
INCLUDE 'derivative.inc' ; Include derivative-specific definitions
; export symbols
XDEF _Startup
ABSENTRY _Startup
Table_Data EQU $3E00
; Variable declarations
ACMP_ENABLE SET $92
ACMP_DISABLED SET $20
MTIM_INIT SET $50
MTIM_ENABLE SET $40
MTIM_STOP_RESET SET $30
MTIM_128_DIV SET $07
FREE_RUN SET $00
DEBUG_MODE SET $00
RUN_MODE SET $01
MODE: EQU DEBUG_MODE
; variable/data section
ORG RAMStart
ConvertedValue: DS.B 1 ; This varible store converted value
SensorReading DS.B 1 ; store ACMP read value
pcBuffer: DS.B 2 ; temporal backup SPC
Temp_Page DS.B 1 ; Temporal backup Page
counter DS.B 1
roll_bit DS.B 1
Letter_Counter DS.B 1
; code section
ORG ROMStart
;**************************************************************
;* MACRO DEFINITIONS *
;**************************************************************
TRIM_ICS: MACRO ; Macro used to configure the ICS with TRIM
mov #$FF,PAGESEL ; change to last page
lda #$FA ; load the content which TRIM value is store
tax ; move A content to X
lda ,x ; read D[X]
sta ICSTRM ; Store TRIM value
ENDM
ENTRY_SUB: MACRO ;Macro for "stacking" SPC
sha
sta pcBuffer + 2*(\1)
sha
sla
sta pcBuffer + 2*(\1) +1
sla
ENDM
NOP ;needs to separate MACROS
EXIT_SUB: MACRO ;Macro for restore SPC
sha
lda pcBuffer + 2*(\1)
sha
sla
lda pcBuffer + 2*(\1) +1
sla
ENDM
;**************************************************************
;* CONFIGURES SYSTEM CONTROL *
;**************************************************************
Init_mc:
IFNE MODE
mov #HIGH_6_13(SOPT), PAGESEL
mov #$01, MAP_ADDR_6(SOPT) ; Disables COP and enables RESET (PTA2) pin
ELSE
mov #HIGH_6_13(SOPT), PAGESEL
mov #$03, MAP_ADDR_6(SOPT) ; Disables COP, enables BKGD (PTA3) and RESET (PTA2) pins
ENDIF
clr ICSC1 ; FLL is selected as Bus Clock
TRIM_ICS
clr ICSC2
rts
;**************************************************************
;* Analog Comparator Initial Configuration *
;**************************************************************
ACMP_Conf:
MOV #ACMP_ENABLE,ACMPSC ; ACMP Enabled, ACMP+ pin active, Interrupt enabled, Rising edges detections
rts
;**************************************************************
;* Modulus Timer Configuration for SCI *
;**************************************************************
SCI_Init:
mov #$70, MTIMSC ; Enables interrupt, stops and resets timer counter
mov #$1A, MTIMMOD ; MTIM modulo with 26 counts before interrupt.
mov #$05, MTIMCLK ; Selects internal clock as reference bus and 32 preescaler
rts
;**************************************************************
;* Modulus Timer Configuration for ADC *
;**************************************************************
MTIM_ADC_Init:
mov #MTIM_128_DIV,MTIMCLK ; Select bus clock as reference, Set prescaler with 64
mov #FREE_RUN,MTIMMOD ; Configure Timer as free running
mov #MTIM_STOP_RESET,MTIMSC
rts
;**************************************************************
;* Discharge Capacitor *
;**************************************************************
Discharge_Cap:
bset 5,PTADD ; PTA5 as output
bset 5,PTAD ; Set PTA5
bset 1,PTADD ; Configure PTA1 as Output
bclr 1,PTAD ; Start Capacitor discharging
lda #$FE ; Set delay time
waste_time:
dbnza waste_time ; wait until Delay = 0
rts
;**************************************************************
;* MAIN *
;**************************************************************
_Startup:
bsr Init_mc
mainLoop:
bsr MTIM_ADC_Init ; Configure MITM
bsr Discharge_Cap ; Discharge Capacitor
bsr ACMP_Conf ; Configure ACMP+ and ACMP-
mov #MTIM_ENABLE,MTIMSC ; Timer Counter Enabled
wait ; Wait for ACMP interrupt
bset 1,MTIMSC
lda MTIMCNT ; read counter timer value
sta SensorReading ; store counter value
mov #HIGH_6_13(SIP1), PAGESEL
brset 3, MAP_ADDR_6(SIP1),ReadVal ; branch if ACMP interrupt arrives
jsr SCI_Init
jsr SCI_Send
bra mainLoop
;**************************************************************
;* Read Value from ACMP *
;**************************************************************
ReadVal:
mov #MTIM_STOP_RESET,MTIMSC ; Stop and reset counter
mov #ACMP_DISABLED, ACMPSC ; ACMP Disabled, Clear Interrupt flag
ENTRY_SUB 0
jsr LookupTable ; Search on table
EXIT_SUB 0
rts
;**************************************************************
;* Table Search *
;**************************************************************
LookupTable:
lda SensorReading
rola ; Getting 2 MSB
rola
rola
and #$03
add #(Table_Data>>6) ; Page Calculating
mov #PAGESEL,Temp_Page ; Backup actual page
sta PAGESEL ; Page Change
lda SensorReading
and #$3F ; Extract 6 LSB
add #$C0 ; Index to paging window
tax
lda ,x ; Load table result
sta ConvertedValue ; Store result
mov #Temp_Page, PAGESEL ; Back Page
rts
;**************************************************************
;* SCI SEND DATA *
;**************************************************************
SCI_Send:
mov #08,counter ; variable for control
mov #01,roll_bit ; Mask Variable
bclr 4,MTIMSC ; run Timer
bclr 5,PTAD ; Start Bit
mov #HIGH_6_13(SIP1), PAGESEL
wait2:
brset 2, MAP_ADDR_6(SIP1),data
bra wait2
data: ; Start to Send a data Bit
lda MTIMSC ; Clear overflow interrupt flag
mov #$60,MTIMSC ; Reset MTIM Counter, Clear overflow flag
lda ConvertedValue
and roll_bit ; Mask with data
beq value_0 ; If bit=0 call Value_0
bra value_1 ; If bit=1 call Value_1
temp:
lda roll_bit
asla ; Shift left Mask Bit
sta roll_bit
dbnz counter,wait2 ; loop until 8 data bits
wait3:
brset 2, MAP_ADDR_6(SIP1),data2
bra wait3
data2:
lda MTIMSC
mov #$60,MTIMSC ; Reset MTIM Counter, Clear overflow flag
bset 5,PTAD ; Stop Bit
wait4:
brset 2, MAP_ADDR_6(SIP1),data3
bra wait4
data3:
lda MTIMSC
mov #$60,MTIMSC ; Reset MTIM Counter, Clear overflow flag
wait5:
brset 2, MAP_ADDR_6(SIP1),data4
bra wait5
data4:
lda MTIMSC
mov #$60,MTIMSC ; Reset MTIM Counter, Clear overflow flag
rts
value_0: ; send 0 to data port
bclr 5,PTAD
bra temp
value_1:
bset 5,PTAD ; send 1 to data port
bra temp
;**************************************************************
;* Startup Vector *
;**************************************************************
ORG $3FFD
JMP _Startup ; Reset
;**************************************************************
;* Data Table *
;**************************************************************
ORG Table_Data
dc.b 0,4,8,12,16,20,23,27,30,34,38,41,44,48,51,54
dc.b 57,60,64,67,70,72,75,78,81,84,86,89,92,94,97,99
dc.b 102,104,107,109,111,113,116,118,120,122,124,126,128,130,132,134
dc.b 136,138,140,142,143,145,147,149,150,152,153,155,157,158,160,161
dc.b 163,164,166,167,168,170,171,172,174,175,176,177,179,180,181,182
dc.b 183,184,185,187,188,189,190,191,192,193,194,195,196,197,197,198
dc.b 199,200,201,202,203,203,204,205,206,207,207,208,209,209,210,211
dc.b 212,212,213,214,214,215,215,216,217,217,218,218,219,219,220,221
dc.b 221,222,222,223,223,224,224,225,225,225,226,226,227,227,228,228
dc.b 228,229,229,230,230,230,231,231,232,232,232,233,233,233,234,234
dc.b 234,235,235,235,235,236,236,236,237,237,237,237,238,238,238,238
dc.b 239,239,239,239,240,240,240,240,240,241,241,241,241,242,242,242
dc.b 242,242,242,243,243,243,243,243,243,244,244,244,244,244,244,245
dc.b 245,245,245,245,245,245,246,246,246,246,246,246,246,246,247,247
dc.b 247,247,247,247,247,247,247,248,248,248,248,248,248,248,248,248
dc.b 248,248,249,249,249,249,249,249,249,249,249,249,249,249,249,250
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -