历史上的今天

今天是:2024年10月18日(星期五)

正在发生

2019年10月18日 | PIC16C74单片机SPI方式读写串行EEPROM程序

发布者:Harmonious88 来源: eefocus关键字:PIC16C74  单片机  SPI方式  读写串行EEPROM 手机看文章 扫描二维码
随时随地手机看文章

; list  p=16C74, st=off
; PORTC PIN DESCRIPTION
; SCK bit 3, SDI bit 4, SDO bit 5, CS bit 7
; Fosc = 10.0 MHz, thus each instr. cycle = 400ns

;***************Ram Register Definitions*******************************

 
   rxdata equ 25h
   addr   equ 26h
   loops  equ 27h

;***************Bit Definitions****************************************
;#define   CS    PORTC,7           ; SPI chip select bit definition

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


;***************25Cxxx command definitions
;#define      WREN  6                 ;write enable latch
;#define      WRDI  4                 ;reset the write enable latch
;#define      RDSR  5                 ;read status register
;#define      WRSR  1                 ;write status register
;#define      READ  3                 ;read data from memory
;#define      WRITE 2                 ;write data to memory

; Bit defines within status register
;#define      WIP   0                 ;write in progress
;#define      WEL   1                 ;write enable latch
;#define      BP0   2                 ;block protection bit
;#define      BP1   3                 ;block protection bit
;**********************************************************************

 ;        include "p16c74.inc"     ; 16C74 include file
 ;        __CONFIG   _WDT_OFF & _CP_OFF & _HS_OSC & _PWRTE_ON

;**********************************************************************
    page

         org   0000              ; Reset Vector
         clrf  PCLATH             ; ensure PCLATH bit 3 is cleared
         clrf  INTCON             ; ensure all interrupts are disabled
         goto  start              ; jump to the beginning of the program

         org   004              ; interrupt vector, do nothing
isr:      goto  isr                ; do nothing, location just
                                  ; identified in code


;***************BEGIN MAIN PROGRAM*************************************
;
start:    bcf   STATUS,5         ; need to set bank 0
         clrf  PORTC              ; initialize port c
         bsf   CS                 ; make sure cs is set 
         bsf   STATUS,5         ; need to set bank 1
         movlw 0x10               ; all bits are outputs except SDI
         movwf TRISC              ; for SPI setup
         bcf   STATUS,RP0         ; need to set bank 0
         movlw 0x31               ; SPI master, clk/16, ckp=1
         movwf SSPCON             ; SSPEN enabled
         movlw 0x10               ; *** put beginning address in addr**
         movwf addr               ; for later use

loop
;The first thing we will do is the WREN
         call  wren               ; call the write enable routine
;Next write status reg. to clear the block protect bits
         call  wrsr               ; call the write status routine
;Next do a busy test
         call  busy_test          ; test WIP bit in status register
;Then do the WREN before writing to the array
         call  wren               ; call the write enable command

;Next write 0xA5 (or any other value) to 0x10
         bcf   CS                 ; set chip select line low
         movlw WRITE              ; WRITE control byte
         call  output             ; call the output subroutine
         movlw b'00000000'        ; high addr byte is all 0's
         call  output             ; call the output subroutine
         movf  addr,w             ; low addr byte
         call  output             ; call the output subroutine
         movlw b'10100101'        ; load 0xA5 as data to be sent out
         call  output             ; call the output subroutine
         bcf   SSPCON,CKP         ; set clock idle low, mode 0,1
         bsf   CS                 ; set chip select, begin write cycle
         bsf   SSPCON,CKP         ; set clock idle high, mode 1,1

         call  busy_test
         call  rdsr               ; call the read status subroutine

;Now, read location 0x10h and store in rxdata. With Picmaster a
;user can break, read that memory location to see if the read worked
         bcf   CS                 ; set chip select line low
         movlw READ               ; READ control byte
         call  output             ; call the output subroutine
         movlw b'00000000'        ; high addr byte is all 0's
         call  output             ; call the output subroutine
         movf  addr,w             ; get ready to send next byte
         call  output             ; call the output subroutine
         movlw b'01011010'        ; move don't care byte of 0x5A into
         call  output             ; call the output subroutine
         bsf   CS                 ; bring chip select high end
                                  ; terminate read command

;While program is continuously looping, the user may halt (if using an
;emulator), and look at the data in rxdata. If it is 0xA5, the
;read/write worked.
         call  wait               ; little delay between SPI sequence
         goto  loop               ; do it all over again
                                  ; loop can be used to evaluate SPI
                                  ; signals on oscilloscope


;********************* BEGIN SUBROUTINES*******************************
;*** DELAY ROUTINE - 400uS ***
;
wait     movlw .200               ; timing adjustment variable
         movwf loops              ; move variable into loops
top      nop                      ; sit and wait
         nop                      ; no operation
         decfsz loops,f           ; loop complete?
         goto   top               ; no, go again
         return                   ; yes, return from sub


;****** This is the OUTPUT transmit/receive subroutine. ***************
output   movwf SSPBUF             ; place data in buffer to send
loop1    bsf   STATUS,RP0         ; specify bank 1
         btfss SSPSTAT,BF         ; has data been received (xmit done)?
         goto  loop1              ; not done yet, keep trying
         bcf   STATUS,RP0         ; specify bank 0
         movf  SSPBUF,W           ; empty receive buffer
         movwf rxdata             ; put received byte into rxdata
         return                   ; return from subroutine

;*******Write Enable Subroutine****************************************
wren     bcf   CS                 ; set chip select line low
         movlw WREN               ; WREN control byte
         call  output             ; Call the output subroutine
         bcf   SSPCON,CKP         ; set clock idle low, mode 0,1
         bsf   CS                 ; set chip select, begin write
         bsf   SSPCON,CKP         ; set clock idle high, mode 1,1
         return                   ; return from subroutine

;*******Read Status Register Subroutine********************************
rdsr     movlw RDSR               ; RDSR control byte
         call  output             ; Call the output subroutine
         movlw b'00000101'        ; this byte is a don't care byte
         call  output             ; status reg data will be in rxdata
         bsf   CS                 ; set chip select
         return                   ; return from subroutine

;*******Write Status Register Subroutine*******************************
wrsr     bcf    CS                ; set chip select line low
         movlw  WRSR              ; WRSR control byte
         call   output            ; Call the output subroutine
         movlw  b'00001000'       ; set BP1 bit in status register
         call   output            ; this will clear block protect bits
         bcf    SSPCON,CKP        ; set clock idle low, mode 0,1
         bsf    CS                ; set chip select
         bsf    SSPCON,CKP        ; set clock idle high, mode 1,1
         return                   ; return from subroutine

;*******Busy Test - WIP bit in Status Register*************************
busy_test
         bcf    CS                ; set chip select line low
         movlw  RDSR              ; RDSR control byte
         call   output            ; Call the output subroutine
         movlw  b'00000000'       ; send dummy byte
         call   output            ; to initiate clock sequence for read
         bsf    CS                ; else, set chip select high
         btfsc  rxdata,WIP        ; test WIP bit read from status register
         goto   busy_test         ; repeat busy test
         return                   ; return from subroutine

         end

[1] [1]
关键字:PIC16C74  单片机  SPI方式  读写串行EEPROM 引用地址:PIC16C74单片机SPI方式读写串行EEPROM程序

上一篇:PIC单片机定时器中断源程序
下一篇:PIC16C63单片机串口通信程序

推荐阅读

中国储能网讯:电解水制氢技术可以用来调节电网峰谷变化削峰填谷,维持电网系统稳定。适合小规模分布式发电制氢场景,可以与太阳能、风能等潮汐式发电结合并广泛应用。未来若能降低成本,电解水制氢技术将成为能源互联网的杀手级应用。 (本文来源:玖牛投资 微信号) 近日,加拿大氢能企业Hydrogenics宣布将为德国一加氢站提供大规模电解水制氢设备...
谷歌的Pixel 4系列已于几天前推出,但现在外媒已经发现了新设备面部解锁功能的一些问题。BBC发现,用户即使闭着眼睛也可以使用面部解锁来解锁Pixel 4。 这显然是一个漏洞,会使攻击者无需得到手机主人的许可即可轻松地解锁该设备,例如用户在睡觉或是被束缚,Pixel 4都能被解锁。 对此BBC向谷歌询问为何如此时,该公司证实了Pixel 4...
苹果周三正式发布了 iPhone 12 系列 5G 新品,包括 iPhone 12 mini、iPhone 12、iPhone 12 Pro 与 iPhone 12 Pro Max。  其中,iPhone 12 与 iPhone 12 Pro 将于今晚 8 点开始预购,10 月 23 日发售。价格分别为:iPhone 12:64GB 版售价 6299 元,128GB 版售价 6799 元,256GB 版售价 7599 元。iPhone 12 ...
广告摘要声明广告据不完整统计,生产手机的流程,包括表面贴装单板、单板功能测试、组装、预加工、整机测试、包装。一台手机从组装到最后成品会经历上千道工序,手机的诞生面对如此繁琐生产工艺,但机器人仍在其中牢牢占有一席之地,机器人的好坏决定整条生产线的生产良率及生产效率。机器人和高度集成的自动化设备一体装置,主要功能在于快速拾取物料,并...

史海拾趣

小广播
最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

厂商技术中心

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

 
机器人开发圈

电子工程世界版权所有 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2026 EEWORLD.com.cn, Inc. All rights reserved