读写24LCxx系列的EEPROM的实例程序

2013-12-23 13:14:35来源: 互联网

读写24LCxx系列的EEPROM的实例程序

;******************************************************** 
;*                     *
;*    ? PIC16F877A ?I2C Master 家Α???﹃?Α EEPROM ?絛ㄒ祘Α  *
;*                                                   *
;* Written by:  Richard Yang                                *
;*              Sr. Corporate Application Engineer            *
;*              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
; StartI2C  ; Send a START Condition !!
; StopI2C   ; Send s STOP Condition
; RstartI2C  ; Send a Repeat Start 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

;
;***************************************
;       ﹚竡跑计?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 startup code @ the reset vector
;********************************************
Reset_Addr
   org  0x00
   nop
   goto Main_Init
;
   org  0x04
   goto ISR
;
;*************************************************************
;**** The Start Address of ISR is 0x004
;**** "PUSH" & "POP" ?ㄏノ絛ㄒ : 続ノ?钩 PIC16F877 Τ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
;
;----------------------------------------------------------------------
;
;* ???代刚祘Α穦?盢?舱计?(0x40-0x47)糶??RMA0x40-0x47??竚     
;* ?ノPage Write?よΑ盢8??じ舱?戈?糶? EEPROM ?                 
;* 秈︽?篈?盎代EEPROM琌??ЧΘ糶??笆??程?盢?糶                  
;* ??戈??Sequential Read?よΑ眖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 $
;
;---------------------------------------------------------
;
;* ???代刚祘Α穦?糶????じ舱?戈?糶? EEPROM い               
;* ?秈︽?篈?盎代琌??ЧΘ糶??笆??程?盢?糶                           
;* ??戈??眖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  call EE_Ack_Check  ; Polling Acknowledge for next access
;
   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+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop "
;

EE_Random_Read
    call StartI2C  ; 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 RstartI2C  ; Send a Repeat Start 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

[1] [2]

关键字:24LCxx  EEPROM

编辑:神话 引用地址:http://www.eeworld.com.cn/mndz/2013/1223/article_22237.html
本网站转载的所有的文章、图片、音频视频文件等资料的版权归版权所有人所有,本站采用的非本站原创文章及图片等内容无法一一联系确认版权者。如果本网所选内容的文章作者及编辑认为其作品不宜公开自由传播,或不应无偿使用,请及时通过电子邮件或电话通知我们,以迅速采取适当措施,避免给双方造成不必要的经济损失。
论坛活动 E手掌握
微信扫一扫加关注
论坛活动 E手掌握
芯片资讯 锐利解读
微信扫一扫加关注
芯片资讯 锐利解读
推荐阅读
全部
24LCxx
EEPROM

小广播

独家专题更多

富士通铁电随机存储器FRAM主题展馆
富士通铁电随机存储器FRAM主题展馆
馆内包含了 纵览FRAM、独立FRAM存储器专区、FRAM内置LSI专区三大部分内容。 
走,跟Molex一起去看《中国电子消费品趋势》!
走,跟Molex一起去看《中国电子消费品趋势》!
 
带你走进LED王国——Microchip LED应用专题
带你走进LED王国——Microchip LED应用专题
 
电子工程世界版权所有 京ICP证060456号 京ICP备10001474号 电信业务审批[2006]字第258号函 京公海网安备110108001534 Copyright © 2005-2016 EEWORLD.com.cn, Inc. All rights reserved