《電子技術(shù)應(yīng)用》
您所在的位置:首頁 > 嵌入式技術(shù) > 解決方案 > 讀寫24LCxx系列的EEPROM的實(shí)例程序

讀寫24LCxx系列的EEPROM的實(shí)例程序

2009-01-16
關(guān)鍵詞: EEPROM 源代碼

讀寫24LCxx系列的EEPROM的實(shí)例程序

;******************************************************** 
;*                     *
;*    ? PIC16F877A($4.6800) ?I2C Master 家&Alpha($5.2300);???﹃?&Alpha($5.2300); EEPROM ?絛ㄒ祘&Alpha($5.2300);  *
;*                                                   *
;* Written by:  Richard Yang                                *
;*              Sr. Corporate Application Engineer($26.0000)            *
;*              Microchip Technology Inc.                *
;* Date:        Oct. 2nd '2002                            *
;* Revision: 1.00                                     *
;********************************************************


;*************************************************
; This source code provides a demonstration of the MSSP peripheral
; on the PIC16F87x MCU. 
;
;
;***********      The subroutines for EEPROM      *****************
;
; EE_Random_Read  ; EEPROM Random address Read from EEPROM
; EE_SEQU_Read  ; EEPROM Sequential Read form EEPROM
; EE_Ack_Check  ; Polling current status of EEPROM
; EE_Page_Write  ; Page Write function for EEPROM
; EE_Byte_Write  ; Write a byte to EEPROM with address setting
;
;***********      The subroutines for I2C      *****************
;
; Init_I2C_Master  ; Initial I2C Module for Master Mode , 7-bit address
; START($0.5760)I2C  ; Send a START($0.5760) Condition !!
; StopI2C   ; Send s STOP Condition
; RSTART($0.5760)I2C  ; Send a Repeat START($0.5760) conditional to I2C
; Non_Ack   ; Send a Non-acknowledge signal to I2C
; An_Ack   ; Send a acknowledge signal to I2C
; Sebd_Byte  ; Send a byte to I2C bus
; RecI2C    ; Enable Read a byte form slave device
; I2C_Done  : Wait the I2C completed the currect process
;
;***************************************************

  list p=16f877a
  #i nclude <p16f877a.inc>

;
;***************************************
;       ﹚竡跑計(jì)?RAM???
;
   CBLOCK 0x20

  I2C_Ctrl 
  I2C_Addr
  I2C_Data
  I2C_Page_Length
  D_Count
  ENDC
  cblock 0x40
  I2C_Page_Buffer:8
  I2C_SEQU_Buffer:8
  endc
;
;***************************************
;       ﹚竡い耞?既?竟??

w_temp  EQU 0x72
status_temp EQU 0x73
pclath_temp EQU 0x74
;
;***************************************
;       ﹚竡 I2C ???の竲?
;
EEPROM_CMD equ 0xA0  ; Device adress of Slave Point
EE_Read  equ .1
EE_Write equ .0

;
#define  SCL PORTC,3  ; I2C SCL pin
#define  SDA PORTC,4  ; I2C SDA pin

;

;********************************************
; Locates START($0.5760)up code @ the reset vector
;********************************************
Reset_Addr
   org  0x00
   nop
   goto Main_Init
;
   org  0x04
   goto ISR
;
;*************************************************************
;**** The START($0.5760) Address of ISR is 0x004
;**** "PUSH" & "POP" ?ㄏノ絛ㄒ : 続ノ?鉤 PIC16F877($4.6800) &Tau;SHARE BANK ?PIC
;**************************************************************
ISR 
Push  movwf   w_temp             ; save off current W register contents
   movf STATUS,w           ; move status register into W register
   movwf status_temp        ; save off contents of STATUS register
   movf PCLATH,W
   movwf pclath_temp

;        Put your interrupt code here
;
Pop   movf pclath_temp,W
   movwf PCLATH
   movf    status_temp,w    ; retrieve copy of STATUS register
   movwf STATUS            ; restore pre-isr STATUS register contents
   swapf   w_temp,f
   swapf   w_temp,w          ; restore pre-isr W register contents
;
   retfie                    ; return from interrupt
  
 
;----------------------------------------------------------------------

Main_Init  
   pagesel Init_I2C_Master  ; Set PAGE to PCLATH Register
      call    Init_I2C_Master  ; Init the MSSP for I2C Master
;
      banksel I2C_Ctrl
   movlw EEPROM_CMD   ; Load EEPROM command address @ 0xA0
   movwf I2C_Ctrl
;
;
Main
;
;----------------------------------------------------------------------
;
;* ???代剛祘&Alpha($5.2300);穦?盢?艙計(jì)?(0x40-0x47)糶??RMA0x40-0x47??竚     
;* ?ノPage Write?よ&Alpha($5.2300);盢8??じ艙?戈?糶? EEPROM ?                 
;* 秈︽?篈?盎代EEPROM琌??Ч&Theta;糶??笆??程?盢?糶                  
;* ??戈??Sequential Read?よ&Alpha($5.2300);眖EEPROM弄?ㄓ??I2C_SEQU_Bufferい    
;

Test_Page_RW
   movlw 0x08
   movwf I2C_Page_Length
   movlw I2C_Page_Buffer
_Fill_RAM movwf FSR
   movwf INDF
   incf FSR,W
   decfsz I2C_Page_Length,F
   goto _Fill_RAM
;   
Test_Page_Write
;   banksel I2C_Ctrl
   movlw EEPROM_CMD   ; Load EEPROM command address @ 0xA0
   movwf I2C_Ctrl
   movlw 0x10     ; Select EEPROM location at 0x00
   movwf I2C_Addr
   movlw .8
   movwf I2C_Page_Length
   call EE_Page_Write
;
   call EE_Ack_Check
;
   banksel I2C_Ctrl
   movlw EEPROM_CMD   ; Load EEPROM command address @ 0xA0
   movwf I2C_Ctrl
   movlw 0x10
   movwf I2C_Addr
   movlw .8
   movwf I2C_Page_Length
   call EE_SEQU_Read
;
   goto $
;
;---------------------------------------------------------
;
;* ???代剛祘&Alpha($5.2300);穦?糶????じ艙?戈?糶? EEPROM い               
;* ?秈︽?篈?盎代琌??Ч&Theta;糶??笆??程?盢?糶                           
;* ??戈??眖EEPROM弄?ㄓ??I2C_Data既?竟い                      
;       
Test_1byte_RW

   banksel I2C_Ctrl
   movlw EEPROM_CMD   ; Load EEPROM command address @ 0xA0
   movwf I2C_Ctrl
   movlw 0x00    ; Select EEPROM location at 0x00
   movwf I2C_Addr
   movlw 0xAA    ; Write data 0x5A to location 0x00 of EEPROM
   movwf I2C_Data
   call EE_Byte_Write
;
Test2($0.3696)  call EE_Ack_Check  ; Polling Acknowledge for next access($49.9920)
;
   banksel I2C_Data
   movlw 0x00    ; Clear I2C data buffer
   movwf I2C_Data
;
Test3
   banksel I2C_Ctrl
   movlw EEPROM_CMD   ; Load EEPROM command address @ 0xA0
   movwf I2C_Ctrl
   movlw 0x00
   movwf I2C_Addr
   call EE_Random_Read
   ;
   goto  $
;

;
;******************************************************
;*           Random Read a Byte from EEPROM         
;*
;* Input:
;*     - I2C_Ctrl : Control Byte of EEPROM   
;*      - I2C_Addr : Location of EEPROM  
;* Output:
;*      - I2C_Data : Read Data from EEPROM   
;******************************************************
;
; Send Command for RANDOM READ :
; " START($0.5760)+ 0xA0 + EE_Address + ReSTART($0.5760) + 0xA1 + Read_Data + NAck + Stop "
;

EE_Random_Read
    call START($0.5760)I2C  ; Set SSPCON2.SEN
;
   bcf  I2C_Ctrl,0  ; set for write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   movf I2C_Addr,W  ; Send out the Rendom address of EEPROM 
   call Send_Byte
;
   call RSTART($0.5760)I2C  ; Send a Repeat START($0.5760) to I2C
;
   bsf  I2C_Ctrl,0  ; set for Read Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   call RecI2C  ; Enable I2C Receive
;
   BANKSEL SSPBUF
   movf SSPBUF,W ; Save to I2C_Data First !!
   movwf I2C_Data

   call Non_Ack  ; Initial NACK Response !!

   call StopI2C  ; Initial STOP Condition
   return
;
;***************************************************************
;*                Sequential Read from EEPROM         
;*
;* Input:
;*     - I2C_Ctrl : Control Byte of EEPROM   
;*      - I2C_Addr : START($0.5760) Location of EEPROM
;*     - I2C_Page_Length : How many byte need to read  
;* Output:
;*      - I2C_SEQU_Buffer : Sequential Read Data buffer
;*  
;***************************************************************
;
; Send Command for RANDOM READ :
; " START($0.5760)+ 0xA0 + EE_Address + ReSTART($0.5760) + 0xA1 + Read_Data + NAck + Stop "
;
EE_SEQU_Read
    call START($0.5760)I2C  ; Set SSPCON2.SEN
;
   bcf  I2C_Ctrl,0  ; set for write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   movf I2C_Addr,W  ; Send out the Rendom address of EEPROM 
   call Send_Byte
;
   call RSTART($0.5760)I2C  ; Send a Repeat START($0.5760) to I2C
;
   bsf  I2C_Ctrl,0  ; set for Read Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   movlw I2C_SEQU_Buffer
   movwf FSR
;
_Sequ_Loop call RecI2C   ; Enable I2C Receive
   BANKSEL SSPBUF
   movf SSPBUF,W  ; Save to I2C_Data First !!
   movwf INDF
   incf FSR,F
   decfsz I2C_Page_Length,F
   goto _Cont_Read
   goto _End_Read
_Cont_Read call An_Ack
   goto _Sequ_Loop
;
_End_Read call Non_Ack   ; Initial NACK Response !!
   call StopI2C   ; Initial STOP Condition
   return
;
;******************************************************
;*              EEPROM Acknowledge Polling
;*           
;*     --  The routine will polling the ACK  
;*           response from EEPROM       
;*    --  ACK=0 return        
;*   --  ACK=1 send ReSTART($0.5760) & loop check
;* 
;******************************************************
;
EE_Ack_Check
    call START($0.5760)I2C  ; Set SSPCON2.SEN
   
   bcf  I2C_Ctrl,0  ; Clear for Write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
_Ack_Polling
   BANKSEL SSPCON2
   btfss SSPCON2,ACKSTAT ; Check ACKSTAT bit , 0 = ACK , 1 = NACK
   goto ACK_Return  ; Ack = 0 ; EEPROM is Ready
_Ack_Hi        ; Ack = 1 ; EEPROM is Busy for Write
   call Delay_mS  ; Delay 500uS for next Ack polling
   call RSTART($0.5760)I2C  ; Send a Repeat START($0.5760) to I2C
   bcf  I2C_Ctrl,0  ; Clear for Write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
   goto _Ack_Polling
 
ACK_Return
   call StopI2C   ; Initial STOP Condition
   return
;
;**********************************************************
;*           Page Write 1 to 8 Bytes to EEPROM        
;*
;*  Input:
;*     - I2C_Ctrl : Control Byte of EEPROM  
;*     - I2C_Addr : Location of EEPROM  
;*   - I2C_Page_Buffer: RAM location of Data
;*     - I2C_Page_Length : Data length count
;*  
;**********************************************************
;
; Send Command for PAGE WRITE :
; " START($0.5760)+ 0xA0 + EE_Address + W_Data 0+ .. + W_Data N + Stop "
;
EE_Page_Write
   movf I2C_Page_Length,W
   btfsc STATUS,Z
   return
;  
    call START($0.5760)I2C  ; Set SSPCON2.SEN
;
   bcf  I2C_Ctrl,0  ; Clear for Write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   movf I2C_Addr,W  ; Send out the Command 
   call Send_Byte
;
   movlw I2C_Page_Buffer
   movwf FSR
_W1   movf INDF,W
   call Send_Byte
   incf FSR,F
   decfsz I2C_Page_Length,F
   goto _W1
;
    call StopI2C   ; Initial STOP Condition
   return
;
;
;*******************************************************
;*               Write a Byte to EEPROM               
;*
;* Input:
;*     - I2C_Ctrl : Control Byte of EEPROM   
;*      - I2C_Addr : Location of EEPROM  
;*      - I2C_Data : Data to EEPROM
;*  
;*******************************************************
;
; Send Command for BYTE WRITE :
; " START($0.5760)+ 0xA0 + EE_Address + Write_Data + Stop "
;
EE_Byte_Write
    call START($0.5760)I2C  ; Set SSPCON2.SEN
;
   bcf  I2C_Ctrl,0  ; Clear for Write Command
   movf I2C_Ctrl,W  ; Send Slave Address to I2C Bus
   call Send_Byte
;
   movf I2C_Addr,W  ; Send out the Command 
   call Send_Byte
;
   movf I2C_Data,W  ; Send out the Data
   call Send_Byte
;
    call StopI2C   ; Initial STOP Condition
   return
;
;**********************************************************************
;
;   As following subroutines perform commonly used I2C functions.
;      You can use these subroutines for your I2C access($49.9920)
;
;**********************************************************************
;
;*** Initial I2Cmodule fot 7-bit I2C Master Mode with 100Kcps @4MHz
;
Init_I2C_Master    
   BANKSEL TRISC   ; Initial PortC,bit 3 & 4 as Input
    bsf  SCL    ; RC3 = SCL  ,  RC4 = SDA
   bsf  SDA  
;
    BANKSEL PORTC
    bsf  SCL    ; SCL = Hi
    bsf  SDA    ; SDA = Hi
;
   movlw b'00101000'  ; I2C Master Mode, Clock Rate: FOSC/(4*SSPADD+1)
   movwf SSPCON   ;
;
         banksel SSPADD
         movlw   .9              ; This gives 100KHz I2C clock @ 4MHz
   movwf   SSPADD   ; (4MHz/4) / (9+1)= 100KHz
;
       movlw   b'10000000'     ; Disable slew rate control,
         movwf   SSPSTAT   ; and clear status bits
;
          movlw   b'00000000'     ; Set SCL,SDA into Ready status
         movwf   SSPCON2         
;    
   return
;
;******   Send START($0.5760) condition to bus   *******
;
START($0.5760)I2C    ; Initiate the I2C START($0.5760) condition.
         banksel SSPCON2
          bsf     SSPCON2,SEN
         goto I2C_Done
;
;******   Send STOP condition to bus   *******
;
StopI2C    
        banksel SSPCON2
         bsf     SSPCON2,PEN
         goto I2C_Done
;
;******   Send RESTART($0.5760) condition to bus   *******
;     
RSTART($0.5760)I2C   
         banksel SSPCON2
         bsf     SSPCON2,RSEN
         goto I2C_Done
;
;******   Send a Non-Acknowledge status to bus (ACK=1)    *******
;       
Non_Ack
         banksel SSPCON2
         bsf     SSPCON2,ACKDT    ; Set the ACK bit
         bsf     SSPCON2,ACKEN    ; Initiate the NACK sequence.
         goto I2C_Done
;
;******   Send an Acknowledge status to bus (ACK=0)     *******
;        
An_Ack
          banksel SSPCON2
         bcf     SSPCON2,ACKDT    ; Clear the ACK bit
         bsf     SSPCON2,ACKEN    ; Initiate the NACK sequence.
         goto I2C_Done
;
;******   Send data to I2C bus from Wreg.     *******
;
Send_Byte
   banksel SSPBUF
   movwf SSPBUF
   goto I2C_Done
;
;******   Enable I2C Receive for Master Mode     *******
;
RecI2C
        banksel SSPCON2         
         bsf     SSPCON2,RCEN    ; Set the receive enable bit.
   goto I2C_Done
;
;******  Check the I2C stage is completed   ********
;
I2C_Done
         banksel PIR1
    btfss   PIR1,SSPIF ; Poll for SSPIF
         goto    $-1
         bcf     PIR1,SSPIF
         return
;
Delay_mS
   banksel D_Count
   movlw .124   ; Load .5mS Dealy Value
    movwf D_Count   
_D_mS  nop
   decfsz D_Count,F  
   goto _D_mS 
   return         
;
; *********** End Of Program !!!!!
;
   end

 

本站內(nèi)容除特別聲明的原創(chuàng)文章之外,轉(zhuǎn)載內(nèi)容只為傳遞更多信息,并不代表本網(wǎng)站贊同其觀點(diǎn)。轉(zhuǎn)載的所有的文章、圖片、音/視頻文件等資料的版權(quán)歸版權(quán)所有權(quán)人所有。本站采用的非本站原創(chuàng)文章及圖片等內(nèi)容無法一一聯(lián)系確認(rèn)版權(quán)者。如涉及作品內(nèi)容、版權(quán)和其它問題,請及時通過電子郵件或電話通知我們,以便迅速采取適當(dāng)措施,避免給雙方造成不必要的經(jīng)濟(jì)損失。聯(lián)系電話:010-82306118;郵箱:aet@chinaaet.com。
亚洲一区二区欧美_亚洲丝袜一区_99re亚洲国产精品_日韩亚洲一区二区
欧美成人精品一区二区| 欧美日韩一区在线观看| 99精品99| 亚洲福利在线观看| 欧美在线一级va免费观看| 正在播放欧美视频| av不卡在线| 一区二区欧美在线| 一区二区三区福利| 正在播放亚洲| 亚洲一区二区成人在线观看| 99国产精品视频免费观看| 亚洲精品乱码久久久久久蜜桃91 | 蜜臀av国产精品久久久久| 久久成人免费| 久久国产一区| 久久视频在线看| 久久综合久久久| 欧美成人精品| 欧美黄色免费网站| 欧美手机在线视频| 国产精品免费电影| 国产女人18毛片水18精品| 国产亚洲电影| 亚洲第一成人在线| 亚洲精品国久久99热| 一区二区高清视频| 亚洲欧美日韩国产成人精品影院 | 夜色激情一区二区| 中文国产成人精品| 香蕉久久一区二区不卡无毒影院| 欧美一区二区| 久久久青草婷婷精品综合日韩 | 亚洲在线电影| 久久国产精品久久国产精品| 亚洲人在线视频| 中日韩午夜理伦电影免费| 午夜精品美女自拍福到在线| 久久精品亚洲一区二区| 免费在线看一区| 欧美日韩天堂| 国产日韩精品久久久| 亚洲第一区在线观看| 99精品国产一区二区青青牛奶| 亚洲女人天堂av| 亚洲成色精品| 亚洲性xxxx| 久久久久久97三级| 欧美精品激情在线| 国产精品一级在线| 在线免费观看日韩欧美| 一区二区三区高清不卡| 欧美一区二区成人6969| 亚洲韩日在线| 亚洲在线中文字幕| 久久嫩草精品久久久精品一| 欧美日韩国产高清| 国产视频在线观看一区| 亚洲精品一区二区在线| 亚洲自拍高清| 亚洲精品永久免费| 欧美影视一区| 欧美国产亚洲视频| 国产裸体写真av一区二区| 在线成人av.com| 亚洲一区二区成人| 亚洲精品欧美激情| 久久国产一区| 欧美日韩色婷婷| 国内成+人亚洲| 在线视频免费在线观看一区二区| 久久精品人人做人人综合| 亚洲天堂男人| 麻豆精品视频在线观看| 国产精品久久一卡二卡| 亚洲国产成人久久综合| 亚洲欧美日韩国产综合精品二区 | 日韩一级黄色大片| 久久久精品网| 国产精品理论片在线观看| 亚洲成人在线免费| 午夜精品区一区二区三| 一片黄亚洲嫩模| 免费影视亚洲| 国产在线视频欧美一区二区三区| 一区二区三区蜜桃网| 亚洲欧洲美洲综合色网| 欧美专区日韩专区| 欧美午夜精品理论片a级大开眼界| 18成人免费观看视频| 久久99伊人| 欧美一区在线直播| 国产精品白丝av嫩草影院 | 一级日韩一区在线观看| 亚洲精选大片| 欧美.com| 黄色欧美成人| 欧美在线电影| 午夜亚洲影视| 国产精品成人国产乱一区| 亚洲精品影视在线观看| 亚洲国产精品久久久久婷婷老年| 久久经典综合| 国产精品一级| 亚洲一区二区三区在线看| 亚洲天堂视频在线观看| 欧美日本视频在线| 亚洲国产清纯| 亚洲肉体裸体xxxx137| 久久在线精品| 韩国自拍一区| 久久精品视频免费| 久久久久综合一区二区三区| 国产区精品视频| 亚洲欧美日韩精品| 亚洲欧美在线一区二区| 欧美性大战久久久久久久| 一本色道久久综合狠狠躁篇怎么玩 | 久久国产精品久久国产精品| 国产精品视频专区| 亚洲无人区一区| 午夜精品视频| 国产精品永久在线| 欧美一二区视频| 久久国产精彩视频| 国产一区二区三区av电影| 欧美在线观看一区二区三区| 久久久国产午夜精品| 精品av久久707| 亚洲欧洲精品成人久久奇米网| 你懂的视频欧美| 最新国产乱人伦偷精品免费网站 | 亚洲午夜精品| 欧美一级成年大片在线观看| 国产精品一区二区久久| 亚洲欧美国产高清va在线播| 欧美专区在线观看| 国语自产偷拍精品视频偷| 亚洲国产精品久久久| 欧美激情二区三区| 一本色道精品久久一区二区三区| 亚洲欧美日韩国产综合| 国产日韩欧美视频在线| 久久精品日韩欧美| 欧美激情亚洲精品| 日韩一区二区电影网| 西瓜成人精品人成网站| 国产一区二区电影在线观看| 久久精品日产第一区二区| 欧美激情2020午夜免费观看| 在线一区二区三区四区| 欧美在线不卡视频| 亚洲成人中文| 亚洲一区不卡| 国产日韩欧美不卡| 亚洲激情在线播放| 欧美日韩一二三四五区| 亚洲免费网站| 蜜桃久久av一区| 一本色道久久综合亚洲精品高清| 校园春色国产精品| 一区二区三区在线观看欧美| 亚洲视频图片小说| 黑丝一区二区| 中国成人黄色视屏| 国产在线播放一区二区三区| 亚洲美女视频在线免费观看| 国产精品久久久久久影院8一贰佰| 欧美一区二区三区日韩视频| 欧美激情国产精品| 午夜精品久久| 欧美久久电影| 欧美一区综合| 欧美日韩国产综合新一区| 欧美一区二区三区四区夜夜大片| 欧美国产日本在线| 香蕉成人久久| 欧美精品一区二区三区很污很色的| 亚洲欧美日本精品| 欧美精品精品一区| 欧美一级淫片播放口| 欧美日韩一级大片网址| 久久精品一区中文字幕| 国产精品成人在线| 亚洲国产成人精品久久| 国产精品成av人在线视午夜片| 亚洲国产欧美一区二区三区同亚洲| 国产精品高潮视频| 亚洲九九精品| 国产一区二区三区久久| 亚洲午夜免费视频| 激情文学一区| 香蕉久久夜色| 日韩系列在线| 噜噜噜久久亚洲精品国产品小说| 亚洲一区二区免费看| 欧美精品福利视频| 久久国产精品网站| 国产精品伊人日日| 中文亚洲欧美|