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

📄 eg1920.asm

📁 这是几年前我写的
💻 ASM
📖 第 1 页 / 共 5 页
字号:

; 内存分配

mBitSpace	EQU	40H			; 位空间
mDualSpace	EQU	08H			; 位字节空间
mByteSpace	EQU	58H			; 字节空间
mStrSpace	EQU	50H			; 串空间
mBufSpace	EQU	20H			; 缓存空间

#include "MacroSet.h"

$NOLIST
;***********************************************
;----------------------------------------------- 编译常数
	fTH0		EQU	0C6H		; 中断频率 9600 * 6 Hz
	fTH1		EQU	0D5H		; 片内波特率 4800

	fProtocolNum	EQU	03H		; 当前支持的协议数
;----------------------------------------------- 寄存器定义
DefDual	RegB					; 通用位寄存器
	b0		EQU	(RegB-20H)*8+0
	b1		EQU	(RegB-20H)*8+1
	b2		EQU	(RegB-20H)*8+2
	b3		EQU	(RegB-20H)*8+3
	b4		EQU	(RegB-20H)*8+4
	b5		EQU	(RegB-20H)*8+5
	b6		EQU	(RegB-20H)*8+6
	b7		EQU	(RegB-20H)*8+7

	RegC		EQU	R0		; 通用寄存器 R0
	iRegD		EQU	R1		; 中断专用寄存器 R1
	RegE		EQU	R2		; 通用寄存器 R2
	RegF		EQU	R3		; 通用寄存器 R3
	RegG		EQU	R4		; 通用寄存器 R4
	RegH		EQU	R5		; 通用寄存器 R5
	RegI		EQU	R6		; 通用寄存器 R6
	RegJ		EQU	R7		; 通用寄存器 R7
;----------------------------------------------- 寄存器内存地址定义
	R_C		EQU	00H		; 通用寄存器 R0
	iR_D		EQU	01H		; 中断专用寄存器 R1
	R_E		EQU	02H		; 通用寄存器 R2
	R_F		EQU	03H		; 通用寄存器 R3
	R_G		EQU	04H		; 通用寄存器 R4
	R_H		EQU	05H		; 通用寄存器 R5
	R_I		EQU	06H		; 通用寄存器 R6
	R_J		EQU	07H		; 通用寄存器 R7
;----------------------------------------------- 硬串行口
	p485OE		BIT	P3.7		; 发送使能
	p485Rx		BIT	P3.0		; 发送使能
	p485Tx		BIT	P3.1		; 发送使能
DefBit	bTxOK1					; 发送完成
DefBit	b485In					; 485有指令输入
DefByte	ResendPtr				; 转发指针
;----------------------------------------------- 软串行口
	c9600		EQU	06H
	c4800		EQU	0CH
	c2400		EQU	18H
	c1200		EQU	30H
	c600		EQU	60H

DefDual	BitSet
	bSelect3	EQU	(BitSet-20H)*8+0; 3通道被选中
	bSelect4	EQU	(BitSet-20H)*8+1; 4通道被选中
	bReSend		EQU	(BitSet-20H)*8+2; 转发有效

DefByte	BaudRate3				; 软串口3(正)波特率

DefByte RxBuf3					; 接收缓冲 (移位寄存器)	
DefBit	bStartBit3				; 起始位
DefByte	RxBitCount3				; 位计数

	BaudRate4	EQU	BaudRate3	; 软串口4(负)波特率

DefByte RxBuf4					; 接收缓冲 (移位寄存器)	
DefBit	bStartBit4				; 起始位
DefByte	RxBitCount4				; 位计数

DefByte	BaudRate5				; 软串口5(正变)波特率

DefBit	bStartBit5				; 起始位
DefByte	RxBitCount5				; 位计数

	BaudRate6	EQU	BaudRate5	; 软串口6(负变)波特率

DefBit	bStartBit6				; 起始位
DefByte	RxBitCount6				; 位计数
;----------------------------------------------- 软串口驱动
DefDual	Line					; 当前线路采样,以下定义的顺序不能改变
	bLine485	BIT	(Line-20H)*8+0	; 485码流
	bLine35		BIT	(Line-20H)*8+2	; 正码流	
	bLine46		BIT	(Line-20H)*8+3	; 负码流

DefByte	BaudClock				; 波特率定时
	cBaudClock	EQU	11000000B	; 最大计数值

DefBuf	sBitbuf, 20H
	cBitBufMask	EQU	11011111B

DefByte	BitBufEptr				; 读出指针
DefBit	bTestBaud				; 申请检测波特率
DefBit	bCheckBaud				; 正在测试波特率

DefByte	BaudErr34				; 波特率错误计数
DefByte	BaudErr5				; 波特率错误计数
DefByte	BaudErr6				; 波特率错误计数
DefByte	PolarErr3				; 字节接收逻辑错误计数(极性)
DefByte	PolarErr4				; 字节接收逻辑错误计数(极性)
DefByte	PolarErr5				; 字节接收逻辑错误计数(极性)
DefByte	PolarErr6				; 字节接收逻辑错误计数(极性)

DefByte	ByteCount34				; 正常字节接收计数
	cByteCount34	EQU	18H
	
DefByte	ByteCount56				; 波特率测试字节接收计数
	cByteCount56	EQU	10H

DefByte	TenBitDelay1				; 10个Bit位 + 10ms 延时
DefByte	TenBitDelay2				; 10个Bit位 + 10ms 延时

DefByte	TenBitLen1				; 延时时间
DefByte	TenBitLen2				; 延时时间
	cTenBit9600	EQU	18H
	cTenBit4800	EQU	20H
	cTenBit2400	EQU	29H
	cTenBit1200	EQU	37H
	cTenBit600	EQU	4BH
;----------------------------------------------- 8字节输入优先型FiFo
;DefByte	FinOpt1					; 指针长度字节
;DefStr	Fin1, 		8			; 缓冲区
;DefBit	bFinEmpty1				; FiFo 空
;DefBit	bFinOver1				; FiFo 溢出

;DefByte	FinOpt2					; 指针长度字节
;DefStr	Fin2, 		8			; 缓冲区
;DefBit	bFinEmpty2				; FiFo 空
;DefBit	bFinOver2				; FiFo 溢出
;----------------------------------------------- 协议解释
DefByte	Check1					; 校验
DefByte	State1					; 状态
DefByte	ProCmd11				; 指令字节1
DefByte	ProCmd21				; 指令字节2

DefByte	Check2					; 校验
DefByte	State2					; 状态
DefByte	ProCmd12				; 指令字节1
DefByte	ProCmd22				; 指令字节2

DefByte	ProErr1					; 串接收计数
DefByte	ProErr2					; 串接收计数
	cProErr		EQU	20H
DefBit	bProErr1				; 本次协议解释错误
DefBit	bProErr2				; 本次协议解释错误

DefBit	bLineLock				; 锁定线路状态

;DefByte	CustNum					; EEPROM中 协议数量
DefByte	CustLenMax				; EEPROM中 协议最大长度
DefByte	CustLenMin				; EEPROM中 协议最小长度

DefByte	CmdRun					; b7=1, 指令有效
DefByte	Command1				; 
DefByte	Command2				; 
	cFree		EQU	00H		; 释放除辅助开关以外的其他开关
	cFocusNear	EQU	01H		; 聚焦-近
	cFocusFar	EQU	02H		; 聚焦-远
	cIrisOpen	EQU	03H		; 光圈-大
	cIrisClose	EQU	04H		; 光圈-小
	cZoomWide	EQU	05H		; 调焦-宽
	cZoomTele	EQU	06H		; 调焦-窄
	cUp		EQU	07H		; 上
	cDown		EQU	08H		; 下
	cLeft		EQU	09H		; 左
	cRight		EQU	0AH		; 右
	cAdd1On		EQU	0BH		; 辅助1开
	cAdd1Off	EQU	0CH		; 辅助1关
	cAdd2On		EQU	0DH		; 辅助2开
	cAdd2Off	EQU	0EH		; 辅助2关
	cUpLeft		EQU	0FH		; 左上
	cDownLeft	EQU	10H		; 左下
	cUpRight	EQU	11H		; 右上
	cDownRight	EQU	12H		; 右下
	cAuto		EQU	13H		; 自动

DefByte	Protocol1				; 协议
DefByte	Protocol2				; 协议
	cPelcoD		EQU	00H
	cPelcoP		EQU	01H
	cSamSung	EQU	02H
	cCustom		EQU	07H

DefByte	CmdStrLen1				; 指令串长度
DefByte	CmdLenCount1				; 指令串长度计数 b3b2b1b0=长度

DefByte	CmdStrLen2				; 指令串长度
DefByte	CmdLenCount2				; 指令串长度计数 b3b2b1b0=长度

DefBit	bNoStopCmd
;----------------------------------------------- 解码器测试
	pButton		EQU	P3.4
DefBit	bLastButton

DefByte	Address					; 地址

DefByte	ButDelay
	cButDelay0	EQU	14H		; 2 秒   (0.1秒驱动)
	cButDelay1	EQU	64H		; 10 秒  (0.1秒驱动)
	
DefByte	TestDelay
	cTestManual	EQU	07H		; 1 分钟 (10秒驱动)
	cTestDefine	EQU	12H		; 3 分钟 (10秒驱动)
	cTestAuto	EQU	0DH		; 2 分钟 (10秒驱动)
	cTestInit	EQU	02H		; 20秒   (10秒驱动)

DefByte	SwState					; 开关状态
	cSwStart	EQU	00H
	cSwManual	EQU	01H
	cSwDefine	EQU	02H
	cSwAuto		EQU	03H
	cSwInit		EQU	04H
	cSwNormal	EQU	05H
	
DefBit	bButState

DefByte	DelayAuto
	cDelayAuto	EQU	14H		; 每个动作保持2秒

DefByte	TestPtr
;----------------------------------------------- 协议自定义
DefBit	bDefine					; 正在定义协议

DefStr	sTemp1, 16
DefStr	sTemp2, 16

DefBit	bTmpPtr					; sTemp1/sTemp2指针
DefByte	String1					; 串指针1
DefByte	String2					; 串指针2

DefByte	StrLen					; 串长度(临时变量)
DefByte	StrCount1				; 串1比较正确计数
DefByte	StrCount2				; 串2比较正确计数
	cStrCount	EQU	0AH		; 至少10次相同

DefByte	CustomLen				; 指令定义计数
;----------------------------------------------- 继电器驱动
DefByte	CmdState				; 状态
DefByte	DelaySwitch				; 方向开关延时
DefByte	VarDelaySw
	cDelaySw0	EQU	0AH		; 有重复且有停止指令延时 1s   (100ms Drv)
	cDelaySw1	EQU	02H		; 有重复但无停止指令延时 0.2s (100ms Drv)
	cDelaySw2	EQU	0FAH		; 无重复但有停止指令延时 25s  (100ms Drv)

DefByte	DelayCamera				; 镜头控制延时
DefByte	VarDelayCam
	cDelayCam0	EQU	05H		; 有重复且有停止指令延时 0.5s (100ms Drv)
	cDelayCam1	EQU	02H		; 有重复但无停止指令延时 0.2s (100ms Drv)
	cDelayCam2	EQU	32H		; 无重复但有停止指令延时 5s   (100ms Drv)

	pUp		BIT	P2.5		; 
	pDown		BIT	P2.7		; 
	pLeft		BIT	P2.6		; 
	pRight		BIT	P0.1		; 
	pAuto		BIT	P0.0		; 
	pAdd1		BIT	P0.6		; 0 开		1 关
	pAdd2		BIT	P0.7		; 0 开		1 关		
	pZoom		BIT	P0.4		; 0 开		1 关
	pFocus		BIT	P0.3		; 0 开		1 关
	pIris		BIT	P0.2		; 0 开		1 关
	pCamera		BIT	P0.5		; 0 +12V	1 -12V
;----------------------------------------------- EEPROM控制
	pXCK		BIT	P2.3		; x5045.SCK
	pXWP		BIT	P2.0		; x5045.WP
	pXCE		BIT	P2.2		; x5045.CE
	pXSI		BIT	P2.1		; x5045.SI
	pXSO		BIT	P2.4		; x5045.SO

DefDual	IrState         			; 串行 EEPROM 状态字节
	bSame		BIT	(IrState-20H)*8+0 ; 读出相同或写入相同
	bChipA0        	BIT     (IrState-20H)*8+1 ; 芯片地址A0
	bChipA1         BIT     (IrState-20H)*8+2 ; 芯片地址A1
	bChipA2         BIT     (IrState-20H)*8+3 ; 芯片地址A2
	bNoX5045        BIT     (IrState-20H)*8+4 ; 芯片无效       (未安装X5045)
	bNo24xx	        BIT     (IrState-20H)*8+5 ; 芯片无效       (未安装24xx)
	cArAdrMask      EQU     00001110B         ;#地址掩码

DefByte	XrDelay					; X5045写定时器
	cXrDelay	EQU	13H		  ; 7.6ms 延时(0.4ms Drv)

DefBit	bXHi8					; X5045 高位地址
DefByte	XrState					; X5045 状态寄存器 b5b4b3b2 有效
	cXrWenable	EQU	00000110B	  ; 写允许
	cXrWprotect	EQU	00000100B	  ; 写禁止
	cXrRstate	EQU	00000101B	  ; 读状态
	cXrWstate	EQU	00000001B	  ; 写状态
	cXrRead		EQU	00000011B	  ; 读操作
	cXrWrite	EQU	00000010B	  ; 写操作
;----------------------------------------------- X5045 空间分配
	eSign		EQU	00H		; 标志区 16字节 Eg19XX  03120100

	eBaudRate	EQU	20H
	eProtocol1	EQU	21H
	eProtocol2	EQU	22H
	eBitSet		EQU	23H
	eCustomLen	EQU	24H

DefStr	sTemp3,	16
;----------------------------------------------- LED 显示
	pLed2		BIT	P3.6		; LED
	pLed1		BIT	P3.5		; LED

DefByte	LedCmd1					; LED 工作方式
DefByte	LedCmd2					; LED 工作方式
	cLedOff 	EQU	00H		  ; 关闭
	cLedOn		EQU	01H		  ; 打开
	cLedFlash	EQU	02H		  ; 闪烁 亮灭等时
	cLedBlink	EQU	03H		  ; 闪烁 亮少灭多
	cLedBreak       EQU	04H		  ; 闪烁 亮多灭少
	cLedShot        EQU	05H		  ; 单次
	cLedPause	EQU	80H		  ; 保持原状,暂停

DefByte	DelayLed1				; 闪烁延时
DefByte	DelayLed2				; 闪烁延时
	cLedFlashOn	EQU	0AH
	cLedFlashOff	EQU	0AH
	cLedBlinkOn	EQU     01H
	cLedBlinkOff	EQU	10H
	cLedBreakOn	EQU	10H
	cLedBreakOff	EQU	02H
	cLedShotOn	EQU	05H
;----------------------------------------------- 系统
DefBit	bOK
DefBit	bSetChange				; 设置变化
DefBit	bSysFree				; 系统空闲

DefByte	RcdPtr

DefByte	Time400us
	cTime400us	EQU	17H

DefByte	Time10ms	
	cTime10ms	EQU	19H

DefByte	Time100ms
	cTime100ms	EQU	0AH

DefByte	Time10s		
	cTime10s	EQU	64H
;*********************************************** 宏定义
;----------------------------------------------- 存入线性缓冲(存一次取多次)
; 入口
; X = 串号,sTemp?
; 0字节的b7=1 表示串完整,b6用于ProDefine
; Dat = 数据
; 出口
; A = 0, 缓冲溢出,从头开始存入
; 占用
; RegC
PutStr	MACRO	X, Dat
	LOCAL	PutStr_5, PutStr_9
		MOV	A, #sTemp&X
		MOV	RegC, A
		MOV	A, @RegC
		CJNE	A, #0FH, $+3
		JC	PutStr_5

		MOV	@RegC, #01H
		INC	RegC
		MOV	@RegC, Dat
		CLR	A
		SJMP	PutStr_9
PutStr_5:
		INC	@RegC
		ADD	A, #01H + sTemp&X
		MOV	RegC, A
		MOV	@RegC, Dat
PutStr_9:
	ENDM
;----------------------------------------------- 取线性缓冲(存一次取多次)
; 入口
; X = 串号,sTemp?
; Ptr = 位置指针
; 0字节b7=1 表示串完整
; 出口
; A = 0, 失败
; Dat = 数据
; 占用
; RegC
GetStr	MACRO	X, Ptr, Dat
	LOCAL	GetStr_1, GetStr_3, GetStr_6, GetStr_9
		MOV	A, #sTemp&X
		MOV	RegC, A
		MOV	A, @RegC
		ANL	A, #0FH
		CJNE	A, Ptr, GetStr_3
GetStr_1:
		CLR	A
		SJMP	GetStr_9
GetStr_3:
		JC	GetStr_1
GetStr_6:
		MOV	A, Ptr
		ADD	A, #01H + sTemp&X
		MOV	RegC, A
		MOV	Dat, @RegC
GetStr_9:
	ENDM
;----------------------------------------------- 串行位接收(4/8)
; 入口:
; Rx = 接收位
; X = 串口通道号
; 占用 
; A, RegC
BitIn	MACRO	Rx, X
	LOCAL	BitIn_1, BitIn_2, BitIn_3, BitIn_4, BitIn_5, BitIn_9

		JB	bStartBit&X, BitIn_1	; if 起始位有效

		JB	Rx, BitIn_9
						  ; else 起始位
		SETB	bStartBit&X
		MOV	RxBitCount&X, #09H	  ; 暂时默认 8 位数据
		SJMP	BitIn_9
BitIn_1:		
		DJNZ	RxBitCount&X, BitIn_5
						  ; if 停止位
		JNB	Rx, BitIn_3
		
		CLR	bStartBit&X
		INC	ByteCount34		    ; 接收字节计数
		JNB	bSelect&X, BitIn_9

		INC	CmdLenCount1		    ; 本串字节计数
		JNB	bDefine, BitIn_2
						    ; if 协议定义
		PutStr	3, RxBuf&X, RegC	      ; 存入 sTemp3
		SJMP	BitIn_4
BitIn_2:					    ; else
		PutStr	1, RxBuf&X, RegC	      ; 存入 sTemp1
BitIn_4:
;BitIn_0:					    ; else 存入 Fin1
;		PutFin	1, RxBuf&X, RegC
;;		JNB	bReSend, BitIn_2
;;						      ; if 处于转发状态
;;		CLR	ES
;;		JNB	bTxOK1, BitIn_4

;;		CLR	bTxOK1
;;		SETB	p485OE
;;		MOV	SBUF, RegC
;;		SJMP	BitIn_2
;;BitIn_4:
;;		PutFin	2, RxBuf&X, RegC	        ; 再存入 Fin2
;;BitIn_2:
		CLR	b485In
		MOV	LedCmd1, #cLedShot
		MOV	A, TenBitLen1
		MOV	TenBitDelay1, A

		SJMP	BitIn_9
BitIn_3:
						    ; if 停止位错误
		INC	PolarErr&X
		INC	RxBitCount&X
		SJMP	BitIn_9
BitIn_5:					  ; else 正常位接收
		MOV	A, RxBuf&X
		MOV	C, Rx
		RRC	A
		MOV	RxBuf&X, A
BitIn_9:
	ENDM
;----------------------------------------------- 串行位测试(4/8)
; 入口:
; Rx = 接收位
; X = 串口通道号
BitTest	MACRO	Rx, X
	LOCAL	BitTest_1, BitTest_2, BitTest_7, BitTest_9

		JB	bStartBit&X, BitTest_1

		JB	Rx, BitTest_9

		SETB	bStartBit&X
		MOV	RxBitCount&X, #09H	  ; 暂时默认 8 位数据
		SJMP	BitTest_9
BitTest_1:		
		DJNZ	RxBitCount&X, BitTest_9
						  ; if 停止位
		JNB	Rx, BitTest_7

		CLR	bStartBit&X
		INC	ByteCount56		    ; 接收字节计数
		SJMP	BitTest_9
BitTest_7:					    ; if 停止位错误
		INC	PolarErr&X
		INC	RxBitCount&X
BitTest_9:
	ENDM
;----------------------------------------------- 串行 N81 位发送
; 启动发送:MOV TxByte #data, CLR bTxOK
; 5-14个指令周期
ByteOut MACRO	Tx, X
	LOCAL	ByteOut_0,ByteOut_1,ByteOut_3,ByteOut_6,ByteOut_9

		JB	bTxOK&X, ByteOut_3
		MOV	A, TxBaudCount&X
		CLR	C
		SUBB	A, #03H
		JZ	ByteOut_0
		JC	ByteOut_0
		MOV	TxBaudCount&X, A
		SJMP	ByteOut_9
ByteOut_0:		
		MOV	A, TxBaudRate&X
		MOV	TxBaudCount&X, A
		MOV	A, TxBitCount&X
		JNB	ACC.7, ByteOut_1
		
		ANL	TxBitCount&X, #7FH
		CLR	Tx			; 起始位
		SJMP	ByteOut_9
ByteOut_1:
		JNZ	ByteOut_6
		
		MOV	TxBitCount&X, #88H	; 暂定 8 位数据位
		SETB	bTxOK&X
ByteOut_3:
		SETB	Tx			; 停止位
		SJMP	ByteOut_9
ByteOut_6:					; if 处于工作状态
		MOV	A, TxByte&X
		RRC	A
		MOV	TxByte&X, A
		MOV	Tx, C
		DEC	TxBitCount&X
ByteOut_9:
	ENDM
;----------------------------------------------- Bit位驱动
BitDrv	MACRO

BitDrv_0:
		MOV	RegC, BitBufEptr
		MOV	A, RegC
		INC	A
		INC	A
		XRL	A, iRegD
		ANL	A, #00011111B
		JNZ	BitDrv_A

		LJMP	BitDrv_9
BitDrv_A:
		INC	RegC
		MOV	A, @RegC
		MOV	RegB, A			; 线路采样值

		XRL	A, #0CH
		ANL	A, #0CH
		JNZ	BitDrv_F

		JB	bStartBit3, BitDrv_F
		JB	bStartBit4, BitDrv_F
						; if 维持 1,且无通道正在工作,抛弃
		SETB	bSysFree
		LJMP	BitDrv_8		
BitDrv_F:		
		CLR	bSysFree
		INC	RegC
		ANL	R_C, #cBitBufMask
		MOV	A, #cBaudClock+1	; 1 = 起始波形补偿, 针对9600

		MOV	C, bSelect3
		ANL	C, bStartBit3
		MOV	bOK, C
		MOV	C, bSelect4
		ANL	C, bStartBit4
		ORL	C, bOK			; C = 补偿条件

		SUBB	A, @RegC
		MOV	RegE, A			; E = 脉宽
		CLR	bOK
BitDrv_1:
		MOV	A, RegE
		CJNE	A, BaudRate3, $+3
		JNC	BitDrv_B

		LJMP	BitDrv_2
BitDrv_B:
		CLR	C
		SUBB	A, BaudRate3
		MOV	RegE, A
		SETB	bOK			; 该脉冲大于1个位宽

		BitIn	b2, 3
		BitIn	b3, 4

		LJMP	BitDrv_1
BitDrv_2:					; if 不够
		MOV	A, BaudRate3
		RR	A
		ANL	A, BaudRate3		  ; A = 1/3 BaudRate
		CJNE	A, R_E, $+3
		JC	BitDrv_C
						  ; if <= 1/3, 准备抛弃
		JB	bOK, BitDrv_H
						    ; if 是单个窄脉冲 < 1/3 波特率

⌨️ 快捷键说明

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