;************************************************
;*interal ram
stack_top equ 060h
;************************************************
;*code
org 0000h
ljmp main
org 0100h
main:
mov sp,#stack_top
clr ea
setb so
clr sck
clr si
setb cs
lcall wren_cmd
lcall byte_write
lcall wren_cmd
lcall byte_read
lcall wren_cmd
lcall rst_wdog
jmp main
;*****************************
;*name: wren_cmd
;*descritpion: set write enable latch
;*function:this routine sends the command to enable writes to the eeprom memory array or
;*status register
;*calls: outbyt
;*input: none
;*output:none
;*register usage: a
;****************************
wren_cmd:
clr sck
clr cs
mov a,#wren_inst
lcall outbyt
clr sck
setb cs
ret
;****************************
;*name: wrdi_cmd
;*description:reset write enable latch
;*function:this routine sends the commond to disable writes to the eeprom memory array or
;*status register
;*calls: outbyt
;*input: none
;*output: none
;*register usage: a
;****************************
wrdi_cmd:
clr sck
clr cs
mov a,#wrdi_inst
lcall outbyt
clr sck
setb cs
ret
;****************************
;*name: wrsr_cmd
;*description: write status register
;*function:this routine sends the command to write the wd0,wd1,bpo and bp0 eeprom
;*bits in the status register
;*call: outbyt,wip_poll
;*input: none
;*output: none
;*register usage: a
;***************************
wrsr_cmd:
clr sck
clr cs
mov a,#wrsr_inst
lcall outbyt
mov a,#status_reg
lcall outbyt
clr sck
setb cs
lcall wip_poll
ret
;***************************
;*name: rdsr_cmd
;*description: read status register
;*function: this routine sends the command to read the status register
;*call: outbyt, inbyt
;*input: none
;*output: a=status register
;*register usage: a
;***************************
rdsr_cmd:
clr sck
clr cs
mov a,#rdsr_inst
lcall outbyt
lcall inbyt
clr sck
setb cs
ret
;***************************
;*name: byte_write
;*description: signle byte write
;*funtion:this routine sends the command to write a signle byte to the eeprom memory array
;*calls: outbyt, wip_poll
;*input: none
;*output: none
;*register usage: a,b
;**************************
byte_write:
mov dptr,#byte_addr
clr sck
clr cs
mov a,#write_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
mov a,#byte_data
lcall outbyt
clr sck
setb cs
lcall wip_poll
ret
;******************************
;*name: byte_read
;*description: signle byte read
;*function: this routine sends the commond to read a signle byte from the eeprom momery array
;*calls: outbyt, inbyt
;*input: none
;*output: a=read byte
;*register usage: a,b
;*****************************
byte_read:
mov dptr,#byte_addr
clr sck
clr cs
mov a,#read_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
lcall inbyt
clr sck
setb cs
ret
;*****************************
;*name: page_write
;*description: page write
;*function: this routine sends the commond to write three consecutive bytes to the eeprom
;*memory array using page mode
;*calls: outbyt,wip_poll
;*input:none
;*output:none
;*register usage: a, b
;****************************
page_write:
mov dptr,#page_addr
clr sck
clr cs
mov a,#write_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
mov a,#page_data1
lcall outbyt
mov a,#page_data2
lcall outbyt
mov a,#page_data3
lcall outbyt
clr sck
setb cs
lcall wip_poll
ret
;*******************************
;*name: sequ_read
;*description: sequential read
;*function: this routine sends the command to read three consecutive bytes from the
;*memory array using sequential mode
;*calls: outbyt, inbyt
;*input: none
;*output:a=last byte read
;*register usage: a,b
;******************************
sequ_read:
mov dptr,#page_addr
clr sck
clr cs
mov a,#read_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
lcall inbyt
lcall inbyt
lcall inbyt
clr sck
clr cs
ret
;******************************
;*name: rst_wdog
;*description: reset watchdog timer
;*function : this routine resets the watchdog timer without sending a command
;*call: none
;*input: none
;*output: none
;*register usage: none
;*****************************
rst_wdog:
clr cs
setb cs
ret
;*****************************
;*name: wip_poll
;*description: write-in-progress polling
;*function: this routine polls for completion of a nonvolatile write cycle by examine
;*wip bit of the status register
;*calls: rdsr_cmd
;*input: none
;*output: none
;*register usage:r1,a
;*****************************
wip_poll:
mov r1,#max_poll
wip_poll1:
lcall rdsr_cmd
jnb acc.0,wip_poll2
djnz r1,wip_poll1
wip_poll2:
ret
;*****************************
;*name: outbyt
;*description: sends byte to eeprom
;*function: this routine shifts out a byte,starting with the msb, to the eeprom
;*calls: none
;*input: a=byte to be sent
;*output: none
;*register usage: r0,a
;*****************************
outbyt:
mov r0,#08
outbyt1:
clr sck
rlc a
mov si,c
setb sck
djnz r0,outbyt1
clr si
ret
;****************************
;*name: inbyt
;*description: receives byte from eeprom
;*function: this routine receives a byte ,msb first from the eeprom
;*calls: none
;*input: none
;*output: a=revceive byte
;*register usage: r0,a
;***************************
inbyt:
mov r0,#08
inbyt1:
setb sck
clr sck
mov c,so
rlc a
djnz r0,inbyt1
ret
END
stack_top equ 060h
;************************************************
;*code
org 0000h
ljmp main
org 0100h
main:
mov sp,#stack_top
clr ea
nop
setb so
nop
setb cs
nop
clr sck
nop
clr si
nop
lcall wren_cmd
lcall wrsr_cmd
lcall wren_cmd
lcall byte_write
lcall wren_cmd
lcall byte_read
lcall wren_cmd
lcall rst_wdog
jmp main
;*****************************
wren_cmd:
clr sck
nop
clr cs
nop
mov a,#wren_inst
lcall outbyt
clr sck
nop
setb cs
nop
ret
;****************************
wrdi_cmd:
clr sck
nop
clr cs
nop
mov a,#wrdi_inst
lcall outbyt
clr sck
nop
setb cs
nop
ret
;****************************
wrsr_cmd:
clr sck
nop
clr cs
nop
mov a,#wrsr_inst
lcall outbyt
mov a,#status_reg
lcall outbyt
clr sck
nop
setb cs
nop
lcall wip_poll
ret
;***************************
rdsr_cmd:
clr sck
nop
clr cs
nop
mov a,#rdsr_inst
lcall outbyt
lcall inbyt
clr sck
nop
setb cs
nop
ret
;***************************
byte_write:
mov dptr,#byte_addr
clr sck
nop
clr cs
nop
mov a,#write_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
mov a,#byte_data
lcall outbyt
clr sck
nop
setb cs
nop
lcall wip_poll
ret
;******************************
byte_read:
mov dptr,#byte_addr
clr sck
nop
clr cs
nop
mov a,#read_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
lcall inbyt
clr sck
nop
setb cs
nop
ret
;*****************************
page_write:
mov dptr,#page_addr
clr sck
nop
clr cs
nop
mov a,#write_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
mov a,#page_data1
lcall outbyt
mov a,#page_data2
lcall outbyt
mov a,#page_data3
lcall outbyt
clr sck
nop
setb cs
nop
lcall wip_poll
ret
;*******************************
sequ_read:
mov dptr,#page_addr
clr sck
nop
clr cs
nop
mov a,#read_inst
mov b,dph
mov c,b.0
mov acc.3,c
lcall outbyt
mov a,dpl
lcall outbyt
lcall inbyt
lcall inbyt
lcall inbyt
clr sck
nop
setb cs
nop
ret
;******************************
rst_wdog:
clr cs
nop
setb cs
ret
;*****************************
wip_poll:
mov r1,#max_poll
wip_poll1:
lcall rdsr_cmd
jnb acc.0,wip_poll2
djnz r1,wip_poll1
wip_poll2:
ret
;*****************************
outbyt:
mov r0,#08
outbyt1:
clr sck
rlc a
mov si,c
setb sck
djnz r0,outbyt1
clr si
ret
;****************************
inbyt:
mov r0,#08
inbyt1:
setb sck
nop
clr sck
mov c,so
rlc a
djnz r0,inbyt1
ret
END
ORG 00H
LJMP MAIN
MAIN:
NOP
LCALL WRSRWDT
LCALL WRDROM
LCALL RDDROM
LCALL CLRWDT
LJMP MAIN
;*****************************************************************
;X5045 WatchDog应用程序集
;===================================================================
;功能:向X5045串行输出8Bit数据
;入口参数:A=数据
;出口参数:(无)
;影响资源:A、R6
;占用堆栈:2
;===================================================================
SDO8B:
MOV R6, #08H
SDO8B0:
CLR SCK
RLC A
MOV SDI, C
SETB SCK
DJNZ R6, SDO8B0
RET
;===================================================================
;功能:从X5045串行读出8Bit数据
;入口参数:(无)
;出口参数:A=数据
;影响资源:A、R6
;占用堆栈:2
;===================================================================
SDI8B:
SETB SCK
MOV R6, #8
SDI8B0:
SETB SDO
CLR SCK
NOP
MOV C, SDO
RLC A
SETB SCK
DJNZ R6, SDI8B0
RET
;===================================================================
;功能:检查WIP标志,只到WIP标志无效时退出
;入口参数:(无)
;出口参数:(无)
;影响资源:A、R6、R7
;占用堆栈:4
;===================================================================
CHKWIP:
MOV R7, #200 ;设置最大循环次数
CHKWIP0:
CLR SCK
CLR CS50
MOV A, #RDSR
CALL SDO8B
CALL SDI8B
SETB CS50
SETB SCK
JNB ACC.0, CHKWIP1
DJNZ R7, CHKWIP0
CHKWIP1:
RET
;===================================================================
;功能:写X5045控制字节
;入口参数:ROMData=控制字节
;出口参数:(无)
;影响资源:A、R6、R7
;占用堆栈:8
;===================================================================
WRSRWDT:
CLR SCK
CLR CS50
MOV A, #WREN
CALL SDO8B
SETB CS50
NOP
CLR CS50
MOV A, #WRSR
CALL SDO8B
MOV A, #00
CALL SDO8B
SETB CS50
SETB SCK
CALL CHKWIP
RET
;===================================================================
;功能:向X5045写入一字节数据
;入口参数:ROMData=数据、DPTR=数据存储地址
;出口参数:(无)
;影响资源:A、PSW、R6、R7
;占用堆栈:8
;===================================================================
WRDROM:
CLR SCK
CLR CS50
MOV A,#WREN
CALL SDO8B ;允许写
SETB CS50
NOP
CLR CS50 ;激活写操作
MOV A, DPH
RRC A
MOV A, #WRITED
MOV ACC.3, C ;插入地址第8位
CALL SDO8B
MOV A, DPL
CALL SDO8B ;写入存储地址
MOV A,#00
CALL SDO8B ;写入数据
SETB CS50 ;结束写操作
SETB SCK
CALL CHKWIP
RET
;===================================================================
;功能:从X5045读出一字节数据
;入口参数:DPTR=数据存储地址
;出口参数:A=数据
;影响资源:A、PSW、R6
;占用堆栈:4
;===================================================================
RDDROM:
MOV A, DPH
RRC A
CLR SCK
CLR CS50
MOV A, #READD
MOV ACC.3, C
CALL SDO8B ;送读数据命令和第8位地址
MOV A, DPL
CALL SDO8B ;送数据低8位地址
CALL SDI8B ;读取数据
SETB CS50
SETB SCK
RET
;===================================================================
;功能:清除WatchDog定时器
;入口参数:(无)
;出口参数:(无)
;影响资源:A、DPTR、PSW、R6
;占用堆栈:4
;===================================================================
CLRWDT:
CLR CS50
NOP
SETB CS50
RET
END