stm8/
#include "mapping.inc"
#include "STM8S105C_S.inc"
segment 'ram0'
couter ds.b 1
lsb1 ds.b 1
lsb2 ds.b 1
vol1 ds.b 1;用于存放电压值,显示电压缓冲区
vol2 ds.b 1
vol3 ds.b 1
r00 ds.b 1;用于运算,存放中间结果
r01 ds.b 1
r02 ds.b 1
r03 ds.b 1
r04 ds.b 1
r05 ds.b 1
temp_max ds.b 1
temp_min ds.b 1
temp ds.b 1;临时变量
segment 'rom'
main.l
; initialize SP
ldw X,#stack_end
ldw SP,X
#ifdef RAM0
; clear RAM0
ram0_start.b EQU $ram0_segment_start
ram0_end.b EQU $ram0_segment_end
ldw X,#ram0_start
clear_ram0.l
clr (X)
incw X
cpw X,#ram0_end
jrule clear_ram0
#endif
#ifdef RAM1
; clear RAM1
ram1_start.w EQU $ram1_segment_start
ram1_end.w EQU $ram1_segment_end
ldw X,#ram1_start
clear_ram1.l
clr (X)
incw X
cpw X,#ram1_end
jrule clear_ram1
#endif
; clear stack
stack_start.w EQU $stack_segment_start
stack_end.w EQU $stack_segment_end
ldw X,#stack_start
clear_stack.l
clr (X)
incw X
cpw X,#stack_end
jrule clear_stack
intel
mov CLK_ECKR,#01h ; 允许外部高速振荡器工 00000001
wait_hse_ready
ld a,CLK_ECKR
and a,#02h ;00000010 等待HSERDY置1
jreq wait_hse_ready ; 等待外部高速振荡器?
bset CLK_CSSR,#0 ;CSEEN<-1,时钟安全系统受
mov CLK_SWCR,#02h ; 使能时钟切换0000 0010
mov CLK_SWR,#0b4h ; 选择芯片外部的高速
wait_clk_switch
ld a,CLK_SWCR
and a,#08h ;0000 1000
jreq wait_clk_switch ; 等待切换成功
call init_IO; 初始化IO,驱动数码管
call init_adc
main_loop: ;主循环
call adc_convert
jp main_loop;重复rgdgsdg
init_IO:
;pc1-pc4control the led
;驱动
BSET PC_DDR,#1
BRES PC_CR1,#1
BRES PC_CR2,#1
BSET PC_DDR,#2
BRES PC_CR1,#2
BRES PC_CR2,#2
BSET PC_DDR,#3
BRES PC_CR1,#3
BRES PC_CR2,#3
BSET PC_DDR,#4
BRES PC_CR1,#4
BRES PC_CR2,#4
;温度显示范围
MOV PB_DDR,#0FFH
mov PB_CR1,#0
mov PB_CR2,#0
;驱动
BSET PE_DDR,#3
BRES PE_CR1,#3
BRES PE_CR2,#3
BSET PE_DDR,#2
BRES PE_CR1,#2
BRES PE_CR2,#2
BSET PE_DDR,#1
BRES PE_CR1,#1
BRES PE_CR2,#1
BSET PE_DDR,#0
BRES PE_CR1,#0
BRES PE_CR2,#0
MOV PD_DDR,#0FFH
mov PD_CR1,#0
mov PD_CR2,#0
;ANJIAN
BRES PA_DDR,#3
BRES PA_DDR,#4
BRES PA_DDR,#5
BRES PA_DDR,#6
BSET PA_CR1,#3
BSET PA_CR1,#4
BSET PA_CR1,#5
BSET PA_CR1,#6
BSET PA_CR2,#3
BSET PA_CR2,#4
BSET PA_CR2,#5
BSET PA_CR2,#6
;BAOJING
BSET PG_DDR,#1
BSET PG_DDR,#0
BSET PG_CR1,#1
BSET PG_CR1,#0
BRES PG_CR2,#1
BRES PG_CR2,#0
ret
init_adc:
mov ADC_CR2,#08h ; A/D结果数据左对齐,主读高8位.
mov ADC_CR1,#0 ; ADC时钟=主时钟/2=1MHZ, ADC转换模式=单次,禁止ADC转换
mov ADC_CSR,#8 ; 选择通道8
mov ADC_TDRH,#1 ;禁止施密特触发器
bset ADC_CR1,#0
ld a,#100
WAIT_ADC_ON
dec a
jrne WAIT_ADC_ON ; 延时一段时间,至少7uS,保证ADC模块的上电完成
ret
adc_convert:
bset ADC_CR1,#0
WAIT_ADC_EOC
ld a,ADC_CSR
and a,#80h
jreq WAIT_ADC_EOC ; 等待ADC结束
ld a,ADC_DRL ; 读出ADC结果的高8位
ld lsb1,a
ld a,ADC_DRH
ld lsb2,a
ret
temperature_sure:
ldw x,lsb1
cpw x ,#19eh
jrmi loop0
cpw x, #2c6h
jrmi loop16_29
cpw x, #35bh
loop0:
mov couter,#0h
cpw x,#110H
jreq exit
inc couter
cpw x,#119H
jreq exit
inc couter
cpw x,#122H
jreq exit
inc couter
cpw x,#12CH
jreq exit
inc couter
cpw x,#134H
jreq exit
inc couter
cpw x,#13DH
jreq exit
inc couter
cpw x,#147H
jreq exit
inc couter
cpw x,#150H
jreq exit
inc couter
cpw x,#15AH
jreq exit
inc couter
cpw x,#163H
jreq exit
inc couter
cpw x,#16DH
jreq exit
inc couter
cpw x,#177H
jreq exit
inc couter
cpw x,#180H
jreq exit
inc couter
cpw x,#18AH
jreq exit
inc couter
cpw x,#194H
jreq exit
loop16_29:
mov couter, #10h
cpw x,#1b2h
jreq exit
exit:
temperature_max.l
BRES ITC_SPR1,#6
BRES ITC_SPR1,#7
BTJT PA_IDR,#3,INC_MAX
BTJT PA_IDR,#4,DEC_MAX
BTJT PA_IDR,#5,INC_MIN
BTJT PA_IDR,#6,DEC_MIN
IRET
INC_MAX:
LD a,temp_max
INC a
ld temp_max, a
IRET
DEC_MAX:
LD a,temp_max
DEC a
LD temp_max, a
IRET
INC_MIN:
LD a,temp_min
INC a
LD temp_min, a
IRET
DEC_MIN:
LD a,temp_min
DEC a
LD temp_min, a
IRET
interrupt NonHandledInterrupt
NonHandledInterrupt.l
iret
delay:
ldw x,#00ffh
loop1:
decw x
jrne loop1
ret
motorola
segment 'vectit'
dc.l {$82000000+main} ; reset
dc.l {$82000000+NonHandledInterrupt} ; trap
dc.l {$82000000+NonHandledInterrupt} ; irq0
dc.l {$82000000+NonHandledInterrupt} ; irq1
dc.l {$82000000+NonHandledInterrupt} ; irq2
dc.l {$82000000+temperature_max} ; irq3
dc.l {$82000000+NonHandledInterrupt} ; irq4
dc.l {$82000000+NonHandledInterrupt} ; irq5
dc.l {$82000000+NonHandledInterrupt} ; irq6
dc.l {$82000000+NonHandledInterrupt} ; irq7
dc.l {$82000000+NonHandledInterrupt} ; irq8
dc.l {$82000000+NonHandledInterrupt} ; irq9
dc.l {$82000000+NonHandledInterrupt} ; irq10
dc.l {$82000000+NonHandledInterrupt} ; irq11
dc.l {$82000000+NonHandledInterrupt} ; irq12
dc.l {$82000000+NonHandledInterrupt} ; irq13
dc.l {$82000000+NonHandledInterrupt} ; irq14
dc.l {$82000000+NonHandledInterrupt} ; irq15
dc.l {$82000000+NonHandledInterrupt} ; irq16
dc.l {$82000000+NonHandledInterrupt} ; irq17
dc.l {$82000000+NonHandledInterrupt} ; irq18
dc.l {$82000000+NonHandledInterrupt} ; irq19
dc.l {$82000000+NonHandledInterrupt} ; irq20
dc.l {$82000000+NonHandledInterrupt} ; irq21
dc.l {$82000000+NonHandledInterrupt} ; irq22
dc.l {$82000000+NonHandledInterrupt} ; irq23
dc.l {$82000000+NonHandledInterrupt} ; irq24
dc.l {$82000000+NonHandledInterrupt} ; irq25
dc.l {$82000000+NonHandledInterrupt} ; irq26
dc.l {$82000000+NonHandledInterrupt} ; irq27
dc.l {$82000000+NonHandledInterrupt} ; irq28
dc.l {$82000000+NonHandledInterrupt} ; irq29
end
|