⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spntestg.asm

📁 单片机快速入门原程序
💻 ASM
📖 第 1 页 / 共 2 页
字号:
;==================================
; 8051F005 test programe
;---------------------------------
;---------------------------------
name	f005_test

$include (C8051F000.inc)

;=================================
; Pseudo constant definition
;---------------------------------
$set(ExtOsc=1)		;1__for use external 11.0592Mhz OSC,0__for use internal 2Mhz OSC(1.709Mhz)
$set(HighBaudrate=1)	;1___high,0___low
;---------------------------------
$set(ProgProtect=0)
;---------------------------------


;=================================
; Parameter Constant definition
;---------------------------------
$if ExtOsc=1
OSC_FREQ	equ	55296	;OSC frequncy in 200Hz for 11.0592Mhz
$else
OSC_FREQ	equ	8545	;OSC frequncy in 200Hz for 1.709Mhz(Note:the maximum constant is 65536)
$endif

TIMER0_CNT	equ	(65536-(OSC_FREQ/6))	;OSC_FREQ/12/[10000h-X]=100Hz,Cnt=10000h-(OSC*200/1200)=10000h-(OSC/6)

;---------------------------------

$if HighBaudrate=1
BAUD_RATE	equ	576	;BaudRate in 100b/s
$else
BAUD_RATE	equ	96	;BaudRate in 100b/s
$endif
STACK_TOP	equ	70h
; BaudRate=(2**SMOD/32)*OSC/(12*(256-TCN1),SMOD=1,TCN1=256-(OSC/16/12/BaudRate)
; OSC_FREQ=OSC/200,BAUD_RATE=BaudRate/100
; TCNT1=256-(200*OSC_FREQ/16/12/(100*BAUD_RATE))=256-(OSC_FREQ/(6*16*BAUD_RATE))
; for accuracy, TCNT1=256-((OSC_FREQ/(6*16*BAUD_RATE))+1/2)=(256-((OSC_FREQ+3*16*BAUD_RATE)/(6*16*BAUD_RATE)))
BAUD_FACTOR	equ	(256-((OSC_FREQ+3*16*BAUD_RATE)/(6*16*BAUD_RATE)))
;注意:对于11.0592Mhz的晶振而言,最大的BAUD_RATE=576


$gen

;---------------------------------
;Delay factors for some delay[!!! These constants must be recalculated for every different OSC_FREQ]
;DDF__delay factor of 4T loop,F=Td/(1000000*4/OSC)=Td*(OSC/200)/4/5000=Td*(OSC_FREQ/4)/5000
;DF___delay factor of 7T loop,F=Td*(OSC_FREQ/7)/5000
;DFL__delay factor of 15T loop,F=Td*(OSC_FREQ/15)/5000
;TDF__delay factor of 12T timer counter,F=Td*(OSC_FREQ/12)/5000
;For the maximum value of middle result must not exceed 65535,the expression must be adjusted as following:
DDF30US		equ	((3*(OSC_FREQ/4)+250)/500)	;30*(OSC_FREQ/4)/5000+0.5,13__1.709M,83__11.0592M
;(Ued in Delay30us,a new delay function)
DF20US		equ	((OSC_FREQ+7*125)/(7*250))	;20*(OSC_FREQ/7)/5000+0.5,5__1.709M,32__11.0592M

;=================================
;   Constant definition for New SpO2Board
;---------------------------------
RedLedSw	equ	P0.4	;RedLedSwitch Bit(RedLedSw=1 and IredLedSw=0 for RedLed)
IredLedSw	equ	P0.5	;IredLedSwitch Bit(RedLedSw=0 and IredLedSw=1 for IredLed)

;---------
; Constant definition for ADC channel
PHONO_CH	equ	0
DAC0_CH		equ	1
LEDDRV_CH	equ	2
SENDET_CH	equ	3

;---------
; constant for data convertion
LEDDRV_ENRATIO	equ	16	;LedDrive current data enlarge ratio
;-----------

;=================================
; Bit argument area
;---------------------------------
bseg		at	20h
Erro:		dbit	1	;校验出错位
SendRdy:	dbit	1	;有发送数据位
WriteFlash:	dbit	1
DownLoadOk:	dbit	1
Start:		dbit	1	;起始标志位
First:		dbit	1	

REC_BUFF_LEN	equ	44
SEND_BUFF_LEN	equ	8
;=================================
; IRAM argument area
;---------------------------------
dseg	at	25h
RecBuff:	ds	REC_BUFF_LEN	;25h-50h
SendBuff:	ds	SEND_BUFF_LEN	;51h-58h
RecRdIdx:	ds	1		;59h
RecWrIdx:	ds	1		;5Ah
SendRdIdx:	ds	1		;5Bh
SendWrIdx:	ds	1		;5Ch
Len:		ds	1		;5Dh
ADDR_H0:	DS	1		;5EH
ADDR_L0:	DS	1		;5FH
ADDR_H:		DS	1		;60h 写FLASH地址低八位
ADDR_L:		DS	1		;61h 写FLASH地址高八位
CRC_H:		DS	1		;62h 生成多项式低八位
CRC_L:		DS	1		;63h 生成多项式高八位
BYT:		DS	1		;64h 一个字节的位数
BCC_H:		DS	1		;65h BCC的低八位
BCC_L:		DS	1		;66h BCC的高八位
SendAL:		DS	1		;67h 发送数据的长度
Len0:		ds	1		;68H

Delay20mf:	ds	1
Delay20f:	ds	1

;-------------------------
;=================================
; Start Code and Interrupt Entry
;---------------------------------
cseg	at	0
S0000:	LJMP	Init

;==================================
; PowerUp Entry
;--------------------------
	ORG	7600H
Init:	lcall	HardInit	;initializing the C8051F007 special Hardware
	MOV	R0,#0	;old is #7Fh for 128B,new is for 256B
	CLR	A
L57C3:	MOV	@R0,A
	DJNZ	R0,L57C3	;Clear 0-FF(256B) of IRAM 
	MOV	EMI0CN,#00H	;Select high address of RAM 
	MOV	R0,#0
	MOV	R7,#0
	CLR	A
L57CE:	
	MOVX	@R0,A
	INC	R0
	DJNZ	R7,L57CE	;Clear 0000-00FF(256B) of XRAM
;	MOV	EMI0CN,#00H	;Select high address of RAM 
	CLR	SendRdy	
	CLR	Erro
	CLR	Start
	mov	RecRdIdx,#0	;接收缓冲区读指针清零
	mov	RecWrIdx,#0	;接收缓冲区写指针清零				
	MOV	SendRdIdx,#0
	MOV	SendWrIdx,#0
	clr	WriteFlash	
	clr	DownLoadOk	
	MOV	SP,#STACK_TOP
	LJMP	DebugProc
;=============================
; special process of C8051F007 hardware
;-----------------------------
Hardinit:	
;-----------------------------
;	Reset Source select
;- - - - - - - - - - - - - - -
	clr	ea		;
	mov	RSTSRC, #000h	; Reset Source Register

;-----------------------------
;	Watch Dog Setup(write #FF will lock the WDT in enable state)
;- - - - - - - - - - - - - - -
	mov	WDTCN, #0deh            ; disable watchdog timer
	mov	WDTCN, #0adh		;(write successive DE then AD will disable WDT)
;-----------------------------
;	OSC setup
;- - - - - - - - - - - - - - -
$if ExtOsc=1
	mov   	OSCXCN, #67h	; enable external crystal oscillator with Fosc>6.74Mhz

	clr	A		; wait at least 1ms at internal OSC=2Mhz
	djnz	acc, $		; wait ~512us  (256*4T=256*(4/2Mhz)=512us)
	djnz	acc, $		; wait ~512us

osc_wait:                                ; poll for XTLVLD-->1
	mov	a, OSCXCN
	jnb	acc.7, osc_wait

	orl	OSCICN, #08h	; select external oscillator as system clock source

;	orl	OSCXCN, #80h	; enable missing clock detector
$else
	mov   	OSCXCN, #0	; disable external OSC and Ground the XTAL1 niternally(see an002.pdf)
$endif

;-----------------------------
;	CrossBar setup
;- - - - - - - - - - - - - - -
	mov	XBR0, #007h	; Enable UART's TX RX to P0.6 and P0.7
	mov	XBR1, #000h	; XBAR1: Initial Reset Value
	mov	XBR2, #0C0h	; Enable CrossBarCoder and enable Weak-PullUp function

	mov	PRT0CF, #0F0h	; Output configuration for P0(P0.5=LEDB,P0.4=LEDA)
	mov	PRT1CF, #0FFh  
	mov	PRT2CF, #0FFh  
	mov	PRT3CF, #0FFh  

;-----------------------------
;	Comparator setup
;- - - - - - - - - - - - - - -
	mov	CPT0CN, #000h	  ; Disable Comparator0

;-----------------------------
;	Reference Control Register Configuration
;- - - - - - - - - - - - - - -
	mov	REF0CN, #003h	; InternalSensorOff,EnableInternalBiase,EnableInternalRef

;-----------------------------
;	SPI Configuration
;- - - - - - - - - - - - - - -
	mov	SPI0CN, #000h	; Disable SPI
	mov	SPI0CFG, #000h	; SPI Configuration Register
	mov	SPI0CKR, #000h	; SPI Clock Rate Register

;-----------------------------
;	DAC Configuration
;- - - - - - - - - - - - - - -
	mov	DAC0CN, #080h	; DAC0 Control Register(Enable ADC0 and Right justified)
	mov	DAC0L, #00h	; DAC0 Low Byte Register
	mov	DAC0H, #00h	; DAC0 High Byte Register

	mov	DAC1CN, #080h	; DAC1 Control Register(Enable ADC0 and Right justified)
	mov	DAC1L, #00h	; DAC1 Low Byte Register
	mov	DAC1H, #00h	; DAC1 High Byte Register

;-----------------------------
; SMBus Configuration
;- - - - - - - - - - - - - - -
	mov	SMB0CN, #000h	; SMBus Control Register(Disable SMBus)
	mov	SMB0ADR, #000h	; SMBus Address Register
	mov	SMB0CR, #000h	; SMBus Clock Rate Register

;-----------------------------
;	PCA Configuration(Disable all PCA's)
;- - - - - - - - - - - - - - -
	mov	PCA0MD, #000h   ; PCA Mode Register
	mov	PCA0CN, #000h   ; PCA Control Register
	mov	PCA0L, #000h    ; PCA Counter/Timer Low Byte
	mov	PCA0H, #000h    ; PCA Counter/Timer High Byte 

	;Module 0
	mov	PCA0CPM0, #000h	; PCA Capture/Compare Register 0
	mov	PCA0CPL0, #000h	; PCA Counter/Timer Low Byte
	mov	PCA0CPH0, #000h	; PCA Counter/Timer High Byte

	;Module 1
	mov	PCA0CPM1, #000h	; PCA Capture/Compare Register 1
	mov	PCA0CPL1, #000h	; PCA Counter/Timer Low Byte
	mov	PCA0CPH1, #000h	; PCA Counter/Timer High Byte

	;Module 2
	mov	PCA0CPM2, #000h	; PCA Capture/Compare Register 2
	mov	PCA0CPL2, #000h	; PCA Counter/Timer Low Byte
	mov	PCA0CPH2, #000h	; PCA Counter/Timer High Byte

	;Module 3
	mov	PCA0CPM3, #000h	; PCA Capture/Compare Register 3
	mov	PCA0CPL3, #000h	; PCA Counter/Timer Low Byte
	mov	PCA0CPH3, #000h	; PCA Counter/Timer High Byte

	;Module 4
	mov	PCA0CPM4, #000h	; PCA Capture/Compare Register 4
	mov	PCA0CPL4, #000h	; PCA Counter/Timer Low Byte
	mov	PCA0CPH4, #000h	; PCA Counter/Timer High Byte

;-----------------------------
; ADC Configuration
;- - - - - - - - - - - - - - -
	mov	AMX0CF, #00h	; AMUX Configuration Register(all input is single ended)
	mov	AMX0SL, #000h	; AMUX Channel Select Register(linear address)
$if ExtOsc=0
	mov	ADC0CF, #020h	; ADC Configuraion Register(SAR=SYSCLK/2,Gain=1)
$else
	mov	ADC0CF, #060h	; ADC Configuraion Register(SAR=SYSCLK/8,Gain=1)
$endif
;	mov	ADC0CN, #0C0h	; ADC Control Register(Enable ADC0,tracking for 3SAR after start)
	mov	ADC0CN, #080h	; ADC Control Register(Enable ADC0,always tracking)
	
	mov	ADC0LTH, #000h	; ADC Less-Than High Byte Register
	mov	ADC0LTL, #000h	; ADC Less-Than Low Byte Register
	mov	ADC0GTH, #0FFh	; ADC Greater-Than High Byte Register
	mov	ADC0GTL, #0FFh	; ADC Greater-Than Low Byte Register

	CLR	RedLedSw
	CLR	IredLedSw	;turn off LedDrv and IredDrv

	lcall	Time0Init

	lcall	UartInit

	ret
;==========================
;  TIMER0 initialization
;Set the TIMER0 mode 0 and enable T0 INT, Timer constant is D000
;(with 14.746Mhz OSC,the timer freq is 14746000/12/(10000h-D000h)=100Hz)
;--------------------------
Time0Init:
	mov	CKCON, #000h	; Clock Control Register(use 12T for Timer OSC source)
	MOV	A,TMOD		;TMOD
	ANL	A,#0F0H
	ORL	A,#1
	MOV	TMOD,A		;TMOD
	CLR	TF0

	MOV	TH0,#high(TIMER0_CNT)
	MOV	TL0,#low(TIMER0_CNT)

	SETB	TR0
	SETB	ET0
	RET	
;==========================
;	UART initialization
;- - - - - - - - - - - - - - -
UartInit:
	MOV	A,TMOD	;TMOD(计数器/定时器方式寄存器)
	ANL	A,#0FH  ;"定时器0的方式,时钟源,是否允许定时器0工作"不变
	ORL	A,#20H  ;定时器1的方式2(自动重装载的8位计数器/定时器)
	MOV	TMOD,A	;TMOD,Timer2 8Bits reload mode
	ORL	PCON,#80H	;SMOD=1,则串口波特率为SCON中的串口方式定义值的两倍;PCON:电源控制寄存器
	mov	a,#low(BAUD_FACTOR);BAUD_FACTOR=(256-((OSC_FREQ+3*16*BAUD_RATE)/(6*16*BAUD_RATE)))
	jnz	UInit0
	dec	a	;if the BAUD_FACTOR=256,set it to 255
UInit0:	mov	tl1,a
	mov	th1,a
;---ASC setup
;$if BCIProto=1
;	MOV	a,#0D0H	;Mode3 8bit OddParity,Enable RX
;$else
	MOV	a,#50H	;Mode1 8Bit NoParity,Enable RX
;$endif
	MOV	SCON,A
	setb	p0.6	;(原为P0.0)0B1H?,TXD
	setb	p0.7
	setb	ti    ;发送中断标志位置位
	clr	ri      ;接收中断标志位清零
	;setb	es	;串行口(UART)中断允许位,Enable UART int,

;---Start timer
	setb	tr1	;start Timer1
	clr	ET1	;Disable Timer1Int
	ret
;---------------------------
;--------------------------
;*************************************************
; New codes for new system based on C8051F007
;*************************************************
;==================================
Delay30us:
	mov	a,#DDF30US
dly0:	dec	a	;1T
	jnz	Dly0	;2/3T,83*4T=332*0.0904=30us
	ret
;==================================
Delay20us:
	mov	Delay20f,#DF20US
dly1:	
	mov	a,Delay20f ;2T
	dec	Delay20f   ;2T     
	jnz	dly1	;2/3T,32*7T=224*0.0904=20us
	ret
;----------------------------
DFL20MS		equ	(4*(OSC_FREQ/15))	;20000*(OSC_FREQ/15)/5000+0.5,73__1.709M,474__11.0592M
DFL2MS		equ	(2*(OSC_FREQ/5)/15)	;2000*(OSC_FREQ/15)/5000+0.5,73__1.709M,474__11.0592M
;(Used in GetDeadLevel for 50ms delay,maximum delay is:575ms___1.709M,88ms__11.0592M)
;----------------------------
Delay20Ms:
	mov	r0,#high(DFL20MS)
	mov	a,#low(DFL20MS)
	mov	Delay20f,a
	jz	Gdl20Ms0
	inc	r0	;if DF300US=X00h,do not adjust R0,else R0=R0+1
Gdl20Ms0:
	inc	Delay20mf	;2T
	inc	Delay20mf	;2T
	DEC	Delay20f	;2T
	dec	Delay20mf	;2T
	dec	Delay20mf	;2T
	MOV	A,Delay20f	;2T
	JNZ	Gdl20Ms0	;2/3T,inner delat 15T

	JB	RS0,Gdl20Ms1
	JB	RS1,Gdl20Ms1
	mov	WDTCN, #0A5h	;clear WDT
Gdl20Ms1:	
	djnz	r0,Gdl20Ms0

	ret
;----------------------------
Delay2Ms:
	mov	r0,#high(DFL2MS)
	mov	a,#low(DFL2MS)
	mov	Delay20f,a
	jz	Gdl2Ms0
	inc	r0	;if DF300US=X00h,do not adjust R0,else R0=R0+1
Gdl2Ms0:
	inc	Delay20mf	;2T
	inc	Delay20mf	;2T
	DEC	Delay20f	;2T
	dec	Delay20mf	;2T
	dec	Delay20mf	;2T
	MOV	A,Delay20f	;2T
	JNZ	Gdl2Ms0	;2/3T,inner delat 15T

	JB	RS0,Gdl2Ms1
	JB	RS1,Gdl2Ms1
	mov	WDTCN, #0A5h	;clear WDT
Gdl2Ms1:	
	djnz	r0,Gdl2Ms0

	ret
;==================================
;----------------------更新程序----------------------
DebugProc:
Main:	lcall	Wait1S
	
mm00:	jnb	ri,mm02			;4T
	LCALL	RECIEV			;4T
	LCALL	PStart			;4T 判断是否是起始位,长度

	JNB	Start,mm02		;4T
	CLR	Start
	JB	DownLoadOk,mm10
	MOV	A,Len
	PUSH	ACC
	DEC	Len
;	DEC	Len
mm01:;	LCALL	Delay140us
;	LCALL	Delay20us
;	jnb	ri,mm02
	JNB	RI,mm01
	LCALL	RECIEV		;存入接收缓冲区
	DJNZ	Len,mm01
	DEC	Len
	POP	ACC
	MOV	Len,A
	JNB	First,mm02
	CLR	First
	lcall	Rubber

mm02:	mov	a,RecRdIdx	;2T RECBUFF有数据?
	clr	c		;1T
	subb	a,RecWrIdx	;2T
	jz	mm05		;3T
	DEC	RecRdIdx
	lcall	RecDataProc	;有则进行接收数据判断处理

mm05:	mov	a,SendRdIdx	;2T
	clr	c		;1T
	subb	a,SendWrIdx	;2T
	jz	mm08		;3T

	lcall	SEND

	
mm08:	jnb	WriteFlash,mm10	;4T
	CLR	WriteFlash
;	JNB	First,mm09
;	CLR	First
;	lcall	Rubber	
mm09:	lcall	WriteCode	

mm10:	jnb	DownLoadOk,mm00	;4T
	CLR	DownLoadOk
	ljmp	AppProg
;=================================================
		;擦除子程序
;=================================================
Rubber:	MOV	FLSCL,#88H
	MOV	PSCTL,#03H
	MOV	DPTR,#0000H
	MOVX	@DPTR,A
	MOV	PSCTL,#01H
	MOV	A,#02H
	MOVX	@DPTR,A
	INC	DPTR
	MOV	A,#76H
	MOVX	@DPTR,A
	INC	DPTR
	MOV	A,#00H
	MOVX	@DPTR,A
	MOV	PSCTL,#03H	;将FLASH0200H-7400H擦除
	MOV	DPTR,#7400H
	MOVX	@DPTR,A
	MOV	PSCTL,#00H
;	MOV	R6,#58		
;Rubber3:MOV	R7,#2
;Rubber5:INC	DPH
;	DJNZ	R7,Rubber5
;	MOVX	@DPTR,A
;	DJNZ	R6,Rubber3
;	MOV	PSCTL,#00H
;	MOV	FLSCL,#88H
;	MOV	PSCTL,#03H
;	MOV	DPTR,#7A00H
;	MOVX	@DPTR,A
	RET
;=================================================
		;延时一秒子程序
;=================================================
Wait1S:
	MOV	R5,#100
Wait1S1:MOV	R4,#200
Wait1S2:JB	RI,Wait1S3
	LCALL	Delay30us       ;延时50us
	LCALL	DeLay20us
	DJNZ	R4,Wait1S2
	DJNZ	R5,Wait1S1
	LJMP	AppProg     ;一秒后PC仍无响应,则执行应用程序
Wait1S3:CLR	RI
	MOV	A,SBUF
	CLR	C
	SUBB	A,#55H
	JNZ	Wait1S2
	LCALL	ExacP
	lcall	SEND
	SETB	First
	RET
;=================================================
;APPLICATION:
;	cpl	p1.6
;	MOV	R4,#50
;APPLIC2:LCALL	Delay20Ms       
;	DJNZ	R4,APPLIC2
;	JMP	APPLICATION  
;=================================================
;======================================================
		;接收子程序
;======================================================
RECIEV:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -