PIC单片机的读写93XCXX程序

TITLE        "R/W EEPROM"

LIST  P = 16C54

;Serial Eprom interface to PIC16C5X.

;REV 2.0        8/26/92.

;

;Define Equates:

;

PIC54   EQU     1FFH

;

PAGE

;

ORG     0

START

goto    main            ;run test program

;

PAGE

;

TRUE    EQU     1

FALSE   EQU     0

S93C46  EQU     FALSE

S93LC46 EQU     FALSE

S93C56  EQU     FALSE

S93LC56 EQU     FALSE

S93C66  EQU     TRUE

S93LC66 EQU     FALSE

wire3   equ     TRUE

org8    EQU     FALSE

;

IF      S93LC46

IF      org8

LC468   EQU     TRUE

XC46    EQU     FALSE

H16     EQU     FALSE

H8      EQU     FALSE

ELSE

LC468   EQU     FALSE

XC46    EQU     TRUE

H16     EQU     FALSE

H8      EQU     FALSE

ENDIF

ELSE

ENDIF

IF      S93C46

LC468   EQU     FALSE

XC46    EQU     TRUE

H16     EQU     FALSE

H8      EQU     FALSE

ENDIF

IF      S93C56 + S93C66 + S93LC56 + S93LC66

IF      org8

H8      EQU     TRUE

H16     EQU     FALSE

LC468   EQU     FALSE

XC46    EQU     FALSE

ELSE

H16     EQU     TRUE

H8      EQU     FALSE

LC468   EQU     FALSE

XC46    EQU     FALSE

ENDIF

ELSE

ENDIF

;********************************************************************

;*                      Register Assignments                        *

;********************************************************************

indir   equ     0       ;Use this register as source/desTInaTIon

;for indirect addressing.

pc      equ     2       ;PIC Program Counter.

status  equ     3       ;PIC Status Register.

fsr     equ     4       ;File Select Register.

serial  equ     5       ;Port used for 93CX6 control.

;The following four registers must be

;located consecuTIvely in memory.

cmd     equ     1a      ;This register contains the 4 bit

;command op code for 93CX6 as follows:

;bit 7 msb of command op code

;bit 6 next bit of op code

;bit 5 next bit of op code

;bit 4 lsb of op code

;bit 3 A8 of address in case of

;56/66 in 8 bit mode.

addr    equ     1b      ;memory address of lower 7/8 bits

highb   equ     1c      ;Used in read/write routines to store the

;upper byte of a 16 bit data word,

;or the data in a 8 bit data word

lowb    equ     1d      ;Used in read/write routines to store the

;lower byte of a 16 bit data word,

;or not used in 8 bit data word.

cnthi   equ     1e      ;Used as the upper byte of a sixteen bit

;loop counter in RDYCHK routine.

cnt     equ     1f      ;Used as the lower byte of a sixteen bit

;loop counter in RDYCHK routine, and

;elswhere as an eight bit counter.

temp_cmd equ    1e      ;doubles as a temp register for cmd

temp_addr equ   1f      ;doubles as a temp register for addr

;********************************************************************

;*                      Bit Assignments                             *

;********************************************************************

carry   equ     0               ;Carry Flag of Status Register.

zflag   equ     2               ;Zero Flag of Status Register.

cs      equ     2               ;Port pin tied to CS on 93CX6.

din     equ     1               ;Port pin tied to DI on 93CX6.

dout    equ     1               ;Port pin tied to DO on 93CX6.

clock   equ     3               ;Port pin tied to CLK on 93CX6.

;********************************************************************

;*                      General Assignments                         *

;********************************************************************

no_err  equ     0

error   equ     1

tries   equ     20      ;After issuing a WRITE, ERASE, ERAL, or WRAL

;command, the approximate number of machine

;cycles X 256 to wait for the RDY status.

;This value must be adjusted for operating

;frequencies other than 4 MHz.

read    equ     b'10000000'     ;read command op code

write   equ     b'01000000'     ;write command op code

erase   equ     b'11000000'     ;erase command op code

ewen    equ     b'00110000'     ;erase enable command op code

ewds    equ     b'00000000'     ;erase disable command op code

eral    equ     b'00100000'     ;erase all command op code

wral    equ     b'00010000'     ;write all command op code

;********************************************************************

;*                      Macro Definitions                           *

;********************************************************************

sel     MACRO                   ;Selects the 93CX6 device.

bsf     serial,cs       ;Chip Select (CS) = '1' to select

ENDM                    ;the device

dsel    MACRO                   ;De-select the 93CX6 device.

bcf     serial,cs       ;Chip Select (CS) = '0' to de-select

;the device.

ENDM

strtbt  MACRO                   ;Issue the Start Bit to the 93CX6.

bsf     serial,din      ;Start Bit = '1'.

clkit                   ;Clock it out.

ENDM

clkit   MACRO                   ;Clocks a serial data bit into or out

;of the 93CX6 device.

bsf     serial,clock    ;Clock (CLK) = '1'.

nop             ;Adjust the number of nop instructions

;between the assertion and de-assertion of

;CLK in proportion to the PIC operating

;frequency.  Refer to the 93CX6 data for the

;minimum CLK period.

bcf     serial,clock    ;Clock (CLK) = '0'.

ENDM

;

PAGE

;********************************************************************

;*                      DOUTx                                       *

;********************************************************************

;doutxx, outputs up to 11 bits of op code/data, depending on whether

;a 46/56/66 serial eeprom is being used. The number of bits over 8 are

;saved in the cmd register and the rest in the addr register. Before

;calling this routine the cmd and the addr registers should be loaded

;as follows:

;cmd reg.bits 7|6|5|4|3|2|1|0

;--------------|-|-|-|-|-|-|-|

;             X|X|X|X|X|X|X|Y| --> not used

;             X|X|X|X|X|X|Y|X| --> mot used

;             X|X|X|X|X|Y|X|X| --> not used

;             X|X|X|X|Y|X|X|X| --> 9th bit of address if necessary

;             X|X|X|Y|X|X|X|X| --> lsb of command op code

;             X|X|Y|X|X|X|X|X| --> 3rd bit of command op code

;             X|Y|X|X|X|X|X|X| --> 2nd bit of command op code

;             Y|X|X|X|X|X|X|X| --> msb of command op code

;

;addr reg. 6/7/8 bits of address if necessary.

IF      H8

dout11

bcf     serial,din

rlf     indir           ;rotate thru carry

btfsc   status,carry    ;set?

bsf     serial,din      ;yes, set output to 1

clkit                   ;clk data

ENDIF

IF      H16+H8

dout10

rlf     indir           ;rotate thru carry

bcf     serial,din      ;set output to 0

btfsc   status,carry    ;set?

bsf     serial,din      ;else set output to 1

clkit                   ;clk data

ENDIF

IF      H8+H16+LC468

dout9

rlf     indir           ;rotate thru carry

bcf     serial,din      ;set output to 0

btfsc   status,carry    ;set?

bsf     serial,din      ;else set output to 1

clkit                   ;clk data

incf    fsr             ;inc pointer

ENDIF

dout8

movlw   8               ;Initialize loop counter.

movwf   cnt

;

d_o_8   bcf     serial,din      ;Assume that the bit to be transfered is a

;'0'.  Hence, de-assert DI.

rlf     indir           ;Rotate the actual bit to be transferred into

;the carry bit.

btfsc   status,carry    ;Test the carry, if our assumption was

;correct, skip the next instruction.

bsf     serial,din      ;No, actual bit was a '1'.  Assert DI.

clkit                   ;Clock the 93CX6.

decfsz  cnt             ;Repeat until cnt = 0.

goto    d_o_8           ;Cnt still > 0.

rlf     indir           ;Restore register to its original condition.

bcf     serial,din      ;make sure din is low

retlw   no_err          ;Exit with good status.

;********************************************************************

;*                      DIN8                                        *

;********************************************************************

;Din8 will input 8 bits of data from the

;93CX6.  Before calling this routine, the FSR

;must point to the register being used to

;hold the incomming data.

din8

IF      wire3

;set up the RA1 as a input before proceeding

movlw   b'00000010'     ;set up porta

tris    serial          ;

ENDIF

movlw   8               ;Initialize loop counter.

movwf   cnt             ;

d_i_8

rlf     indir           ;Make room for the incomming bit in the

;destination register.

bcf     indir,0         ;Assume that the incomming bit is a '0' and

;clear the LSB of the destination register.

clkit                   ;Clock a bit in the 93CX6.

btfsc   serial,dout     ;Test the incomming bit, if our assumption

;was correct, skip the next instruction.

bsf     indir,0         ;No, actual bit is a '1'.  Set the LSB of the

;destination register.

decfsz  cnt             ;Repeat until cnt = 0.

goto    d_i_8           ;Cnt still > 0.

IF      wire3

;setup RA1 back to output

movlw   0               ;set RA1 as output

tris    serial          ;      /

ENDIF

retlw   no_err          ;Exit with good status.

;********************************************************************

;*                      RDYCHK                                      *

;********************************************************************

;Rdychk will read the 93CX6 READY/BUSY status

;and wait for RDY status within the alloted

;number of processor cycles.  If RDY status

;is not present after this set period, the

;routine will return with an error status.

rdychk

IF      wire3

;set up RA1 as a input before proceeding

movlw   b'00000010'     ;set up porta

tris    serial          ;

ENDIF

movlw   tries           ;Initialize time-out counter.

movwf   cnthi           ;

clrf    cnt             ;

dsel                    ;De-select the 93CX6.

;       nop                     ;NOTE:  Check the 93CX6 data sheet for

;minimum CS low time.  Depending upon

;processor frequency, a nop(s) may be

;between the assertion and de-assertion of

;Chip Select.

sel                     ;Re-select the 93CX6.

notrdy  btfsc   serial,dout     ;If DO is a '0', 93CX6 has yet to completed

;the last operation (still busy).

goto    rdynoerr        ;skip to no error

decfsz  cnt             ;No, not yet ready.  Decrement the LSB of our

;16 bit timer and check for expiration.

goto    notrdy          ;Still some time left.  Try again.

decfsz  cnthi           ;Least significant byte expired - decrement

;and check for expiration of the MSB.

goto    notrdy          ;Still some time left.  Try again.

IF      wire3

;setup RA1 back to output

movlw   0               ;set RA1 as output

tris    serial          ;      /

ENDIF

retlw   error           ;RDY status was not present in the alloted

;time, return with error status.

rdynoerr

IF      wire3

;setup RA1 back to output

movlw   0               ;set porta as output

tris    serial          ;      /

ENDIF

retlw   no_err

;********************************************************************

;*                      SEE                                         *

;********************************************************************

;See will control the entire operation of a

;93CX6 device.  Prior to calling the routine,

;load a valid command/memory address into

;location cmd, and for WRITE or WRAL

;commands, load registers highb and lowb with

;16 bits of write data.  Upon exit, the W

;register will contain the completion status.

;Only 93CX6 instructions which require a

;status check can return with an error as the

;completion status.  The values that denote

;the completion status are defined as

;variables 'error' and 'no_err' in the

;general assignments section.

see

movf    cmd,w           ;save cmd

movwf   temp_cmd

movf    addr,w          ;save addr

movwf   temp_addr       ;

movlw   cmd             ;Load W with the location of the cmd

;register.

movwf   fsr             ;Transfer that information into the File

;Select Register.  The fsr now points to

;location cmd.

sel                     ;Select the 93CX6.

strtbt                  ;Send a start bit.

;

btfsc   cmd,7           ;bit 7 = 0?

IF      XC46

goto    sca8            ;xfer 8 bit cmd/adr

ENDIF

IF      LC468

goto    sca9            ;xfer 9 bit cmd/adr

ENDIF

IF      H16

goto    sca10           ;xfer 10 bit cmd/adr

ENDIF

IF      H8

goto    sca11           ;xfer 11 bit cmd/adr

ENDIF

;        goto    set_cmd_addr    ;no then set cmd/addr

btfss   cmd,6           ;bit 6 = 0 ?

IF      XC46

goto    sc8             ;xfer 8 bit cmd/adr

ENDIF

IF      LC468

goto    sc9             ;xfer 9 bit cmd/adr

ENDIF

IF      H16

goto    sc10            ;xfer 10 bit cmd/adr

ENDIF

IF      H8

goto    sc11            ;xfer 11 bit cmd/adr

ENDIF

;        goto    set_cmd         ;yes then set cmd

IF      XC46

goto    sca8            ;xfer 8 bit cmd/adr

ENDIF

IF      LC468

goto    sca9            ;xfer 9 bit cmd/adr

ENDIF

IF      H16

goto    sca10           ;xfer 10 bit cmd/adr

ENDIF

IF      H8

goto    sca11           ;xfer 11 bit cmd/adr

ENDIF

;        goto    set_cmd_addr    ;else set cmd/addr

see1

movf    temp_cmd,w      ;retore cmd

movwf   cmd             ;       /

movf    temp_addr,w     ;restore addr

movwf   addr            ;       /

btfsc   cmd,6           ;Check for a WRITE or ERASE command.

goto    see2            ;Yes, parse the command further.

btfsc   cmd,7           ;Check for a READ command.

goto    read_           ;Yes, process READ command.

btfsc   cmd,5           ;Check for a EWEN or ERAL command.

goto    see3            ;Yes, parse the command further.

btfsc   cmd,4           ;Check for a WRAL command.

goto    write_          ;Yes, process WRITE/WRAL command.

exit_   dsel                    ;No further processing required; 93CX6

;command completed.

;

retlw   no_err          ;Return with good completion status.

see2    btfss   cmd,7           ;Check for a ERASE command.

goto    write_          ;No, process WRITE command.

exit2_  call    rdychk          ;ERASE command requires a status check.

dsel                    ;De-select the 93CX6.

addwf   pc              ;Compute completion status from results of

;status check.

retlw   no_err          ;Return with good completion status.

retlw   error           ;Return with bad completion status.

see3    btfsc   cmd,4           ;Check for a EWEN command.

goto    exit_           ;Yes, no further processing required, exit

;now.

goto    exit2_          ;No, ERAL command which requires a status

;check.

read_   incf    fsr             ;Increment the File Select Register to point

;to the register receiving the upper byte of

;the incomming 93CX6 data word.

call    din8            ;Input the upper byte.

IF      org8

ELSE

incf    fsr             ;Increment the File Select Register to point

;to the register receiving the lower byte.

call    din8            ;Input 8 more bits.

ENDIF

goto    exit_           ;No further processing required, exit now.

write_  incf    fsr             ;Increment the File Select Register to point

;to the upper byte of the 16 bit 93CX6 data

;word to be transmitted.

call    dout8           ;Output that byte.

IF      org8

ELSE

incf    fsr            ;Increment the File Select Register to point

;to the lower byte.

call    dout8          ;Output the lower byte of the 16 bit 93CX6

;data word.

ENDIF

goto    exit2_          ;Exit with a status check.

;

;

IF      XC46

sca8

movlw   b'11000000'     ;clr all but hi 2

andwf   cmd,w           ;save in w

iorwf   addr            ;mask in addr.

incf    fsr             ;inc FSR

call    dout8           ;output

goto    see1            ;return

ENDIF

IF      LC468

;

sca9

bcf     addr,7          ;clr hi of addr

btfsc   cmd,6           ;xfer cmd's bit 6

bsf     addr,7          ;to addr's bit 7

call    dout9           ;output

goto    see1            ;return

ENDIF

IF      H16

;

sca10

call    dout10          ;output cmd reg

goto    see1            ;return

ENDIF

;

IF      H8

sca11

bcf     cmd,5           ;xfer cmd's bit 3 to

btfsc   cmd,3           ;cmd's bit 5

bsf     cmd,5           ;       /

call    dout11          ;output

goto    see1            ;return

;

ENDIF

;

;

;

IF      XC46

sc8

movf    cmd,w           ;get command

movwf   addr            ;save in addr

incf    fsr             ;inc pointer

call    dout8           ;output

goto    see1            ;return

;

ENDIF

IF      LC468

sc9

rlf     cmd,w           ;rotate cmd left

movwf   addr            ;save in addr

call    dout9           ;xfer 9 bits

goto    see1            ;return

ENDIF

IF      H16

;

sc10

rlf     cmd             ;rotate cmd

rlf     cmd,w           ;left twice

movwf   addr            ;save in addr

clrf    cmd             ;clear command

call    dout10          ;xfer 10 bits

goto    see1            ;return

ENDIF

;

IF      H8

sc11

bcf     addr,7          ;xfer cmd's bit 4

btfsc   cmd,4           ;to addr's bit 7

bsf     addr,7          ;       /

call    dout11          ;xfer 11 bits

goto    see1            ;return

ENDIF

;********************************************************************

;*                      Test Program                                *

;********************************************************************

main                    ;We've include a sample program to exercise

;the PIC to 93C66 interface using a simple

;erase, write and verify routine.

;8 bit organization has been used

;with a 3 wire interface.

clrf    serial          ;Clear the port tied to the 93C66 device.

movlw   b'11110000'     ;Intialize the data direction register for

tris    serial          ;that port.

movlw   ewen            ;Load W with the Erase/Write Enable command.

movwf   cmd             ;Transfer W into cmd register.

call    see             ;Enable the 93C66 device.

movlw   eral            ;Load W with the Erase All command.

movwf   cmd             ;Transfer W into cmd register.

call    see             ;Erase the 93C66.

xorlw   error           ;Check completion status.

btfsc   status, zflag   ;Test for error condition.

goto    errloop         ;Yes, bad completion status, error-out.

;Write loop:

tstptrn equ     0xAA            ;Define the test pattern to be written.

movlw   write           ;Load W with the Write command.

movwf   cmd             ;Transfer W into cmd register.

movlw   tstptrn         ;Intialize the 93C66 data registers with

;write data.

movwf   highb           ;load in high byte only

;since 8 bit low byte is ignored

clrf    addr            ;start at addr 0

test1   call    see             ;Write data word into 93C66 device.

xorlw   error           ;Check completion status.

btfsc   status,zflag    ;Test for error condition.

goto    errloop         ;Yes, bad completion status, error-out.

incfsz  addr            ;No, increment the 8 bit memory address

;field.

goto    test1           ;write another location

btfss   cmd,3           ;see if all done

goto    wrt_nxt_pg      ;no then write next page

goto    read_tst        ;read written data

wrt_nxt_pg

bsf     cmd,3           ;set page bit

goto    test1           ;No, write another location.

;Read loop:

read_tst

movlw   read            ;Load W with the Read command.

movwf   cmd             ;Transfer W into cmd register.

test2   call    see             ;Read addressed data word from 93C66 device.

movlw   tstptrn         ;Load W with the pattern written.

subwf   highb,w         ;Verify the data read against what was

;written.

btfss   status,zflag    ;Same?

goto    errloop         ;No, error-out.

incfsz  addr            ;Yes, both byte correct, increment the 8 bit

;memory address field.

goto    test2           ;do next byte

btfss   cmd,3           ;check page bit

goto    rd_nxt_pg       ;no then chk next page

goto    allok           ;all done!!!

rd_nxt_pg

bsf     cmd,3           ;set page bit

goto    test2           ;No, read another location.

allok   goto    allok           ;Home safe!

;

;

errloop

goto    errloop

;

;

;

;KEY DEFINITIONS

;

ORG     PIC54

SYS_RESET

GOTO    START

;

END

pic单片机c语言nop,PIC单片机的读写93XCXX程序相关推荐

  1. 单片机c语言编程下载,单片机C语言编程.doc

    单片机C语言编程.doc 单片机C语言编程指令格式 功能简述 字节数 周期 一.数据传送类指令 MOV A, Rn 寄存器送累加器 1 1 MOV Rn,A 累加器送寄存器 1 1 MOV A ,Ri ...

  2. 单片机C语言 8,PIC单片机C语言程序设计_8_

    PICC编译器的安装<PIC单片机C语言程序(7)>一文介绍MPLABIDE7.40的安装时,我们已将光盘中的PICC编译器的压缩软件PICC8.05.rar(见该文图12)粘贴到我的电脑 ...

  3. 51单片机c语言 if 语句,单片机if语句的用法

    单片机if语句格式 sbit P2^0=A1; sbit P1^0=A2; sbit 语句等号后边应该是地址. sbit P2^0=A1:能通过编译码?本人试一下:test.C(6): error C ...

  4. stc单片机c语言 pdf,STC单片机C语言程序设计 第13章 STC单片机C语言指针.pdf

    STC单片机C语言指针 主讲:何宾 Email :hebin@mail.buct.edu.cn 2016.03 C51编译器中指针的分类 Cx51编译器支持使用字符"*"声明变量指 ...

  5. arduino和单片机c语言,Arduino和单片机的区别-与非网

    Arduino是一款便捷灵活.方便上手的开源电子原型平台.包含硬件(各种型号的Arduino板)和软件(ArduinoIDE).由一个欧洲开发团队于2005年冬季开发.其成员包括Massimo Ban ...

  6. 51单片机c语言编程函数,单片机C语言教程:C51函数

    其实一直出现在例子中的 main()也算是一个函数,只不过它比较特殊,编译时以它做为程序的开始段.有了函数C 语言就有了模块化的优点,一般功能较多的程序,会在编写程序时把每项单独的功能分成数个子程序模 ...

  7. 单片机c语言条件语句,单片机c语言教程:C51复合语句和条件语句

    曾经在BBS上有朋友问过我{}是什么意思?什么作用?在  C  中是有不少的括号,如{},[],()等,确实会让一些初入门的朋友不解.在 VB 等一些语言中同一个()号会有不一样的 作用,它能用于组合 ...

  8. 单片机C语言仿真图,单片机C语言程序设计代码和仿真图.doc

    word完美格式 精心整理 学习帮手 <单片机C语言程序设计实训100例-基于8051+Proteus仿真>案例 第 01 篇 基础程序设计 01闪烁的LED /* 名称:闪烁的LED 说 ...

  9. 51单片机c语言程序控制,51单片机C语言程序设计源代码

    <51单片机C语言程序设计源代码>由会员分享,可在线阅读,更多相关<51单片机C语言程序设计源代码(281页珍藏版)>请在人人文库网上搜索. 1.51 SCM c语言教程示例2 ...

最新文章

  1. SAP的安装后基本设定
  2. Linux 文件系统的目录结构
  3. php 选择路径上传方法,PHP上传文件时自动分配路径的方法,_PHP教程
  4. oracle 远程exp导出,EXP远程导出完整Oracle数据库
  5. 没有找到MFC80D.DLL,因此这个应用程序未能启动。重新安装应用程序可能会修复问题。...
  6. Ant在MyEclipse中的配置总结
  7. 【PAT乙级】1054 求平均值 (20 分)
  8. wxWidgets:wxFileDirPickerEvent类用法
  9. Kettle使用_28 转换之数值范围与字符串操作
  10. 项目管理实践之版本控制工具SVN在Windows平台下的平台搭建
  11. Javascript 四种输出方式
  12. Cron定时任务应用到Thinkphp – 贤生博客
  13. 计算机软件编程与嵌入式软件编程区别,简单看懂什么是单片机编程与嵌入式系统...
  14. 操作系统、体系结构和传奇船长的泰坦级超级旗舰
  15. 戴尔PC,“传奇”要怎样延续?
  16. js获取当前时间的年月日时分秒以及时间的格式化
  17. C# Environment.StackTrace、StackFrame、Tirm的使用技巧
  18. Python练习——二维数组中每行最大值和每行和
  19. Ubuntu20.04中安装MySQL在阿里云服务器上
  20. oracle建立备用文件夹,dataguard重建 ora-01665 控制文件不是备用控制文件

热门文章

  1. linux系统获取用户信息失败怎么办,异常详细信息: System.Data.SqlClient.SqlException: 用户 'NT AUTHORITY\IUSR' 登录失败解决办法...
  2. mysql中有哪些数值型函数_mysql数值型函数汇总
  3. uiuc工程学院计算机,UIUC计算机工程专业排名2020年
  4. Crypto windows 安装
  5. python 填充多边形 学习笔记
  6. python字典随机取数
  7. cannot import name 'ImageRecordInt8Iter'
  8. opencv dnn 人脸 年龄
  9. windows exec: gcc: executable file not found in %PATH%
  10. OpenGL与OpenCV实现增强现实