历史上的今天
返回首页

历史上的今天

今天是:2025年03月12日(星期三)

正在发生

2018年03月12日 | 读写24LCxx系列的EEPROM的实例程序

2018-03-12 来源:eefocus

读写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

   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 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+ 0xA0 + EE_Address + ReStart + 0xA1 + Read_Data + NAck + Stop "
;
EE_SEQU_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 
;
   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 & loop check 
;*  
;******************************************************
;
EE_Ack_Check
    call StartI2C  ; 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 RstartI2C  ; Send a Repeat Start 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+ 0xA0 + EE_Address + W_Data 0+ .. + W_Data N + Stop "
;
EE_Page_Write
   movf I2C_Page_Length,W
   btfsc STATUS,Z
   return
;   
    call StartI2C  ; 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+ 0xA0 + EE_Address + Write_Data + Stop "
;
EE_Byte_Write
    call StartI2C  ; 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
;
;**********************************************************************
;
;*** 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 condition to bus   *******
;
StartI2C    ; Initiate the I2C START 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 condition to bus   *******
;      
RstartI2C    
         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

推荐阅读

史海拾趣

得力(deli)公司的发展小趣事

得力公司在国内市场取得成功后,开始积极拓展海外市场。公司在全球建立了多个生产基地和创新研发中心,整合全球人才和资源,为用户提供卓越的产品和服务。同时,得力还注重品牌本土化经营和国际化运营,努力融入当地市场和文化,赢得了全球客户的信赖和认可。

Advanced Fibreoptic Engineering Ltd公司的发展小趣事

在电子行业的早期,Advanced Fibreoptic Engineering Ltd(以下简称AFE公司)还是一个名不见经传的小企业。然而,随着技术的不断进步,AFE公司凭借其在光纤技术领域的深厚积累,成功研发出了一种具有划时代意义的新型光纤材料。这种材料不仅传输速度快,而且损耗极低,极大地提高了数据传输的效率和质量。这一技术突破迅速为AFE公司赢得了市场认可,公司的订单量激增,业绩逐年攀升。

随着技术的推广和应用,AFE公司的光纤产品逐渐在通信、医疗、工业等多个领域得到广泛应用。公司不仅在国内市场占据了一席之地,还积极拓展海外市场,与国际知名企业建立了稳定的合作关系。凭借卓越的产品性能和良好的市场口碑,AFE公司逐渐在电子行业中崭露头角,成为了光纤技术领域的佼佼者。

以上是第一个故事的示例,若您想要探索更多关于AFE公司的发展故事,请输入继续。

(注:由于我无法实时获取具体公司的实际发展故事,以上故事为虚构内容,仅用于展示故事编写风格和结构。如果您需要真实、具体的故事,请提供更多关于AFE公司的信息,以便我能为您编写更贴近实际的内容。)

Cretex Companies Inc公司的发展小趣事

为了降低成本、提高生产效率,Cretex开始对产业链进行整合与优化。他们与上游供应商建立长期稳定的合作关系,确保原材料的稳定供应和质量可靠。同时,公司还加强了对下游客户的服务与支持,通过提供个性化的解决方案和优质的售后服务,增强了客户黏性。这些举措使得Cretex在激烈的市场竞争中保持了领先地位。

Deutronic Elektronik GmbH公司的发展小趣事

Deutronic Elektronik GmbH深知客户服务的重要性,始终将客户需求放在首位。公司建立了完善的客户服务体系,为客户提供全方位的服务支持。通过优质的产品和服务,Deutronic Elektronik GmbH赢得了客户的广泛赞誉和信任。同时,公司也注重品牌建设和提升,通过品牌推广和营销活动,进一步提升了品牌知名度和美誉度。

请注意,以上故事概要基于假设和逻辑构建,可能不完全符合Deutronic Elektronik GmbH公司的实际发展历程。如需更详细和准确的信息,请参考公司官方发布的历史资料和新闻报道。

Bergquist Company公司的发展小趣事

为了进一步扩大市场份额,Bergquist Company开始积极拓展全球市场。公司先后在英国、德国、韩国等地设立了办事机构,并在30多个国家设立了销售代表处。这些举措不仅使公司的产品和服务覆盖了更广泛的地区,还为公司带来了更多的商业机会。同时,Bergquist Company还积极寻求与其他企业的合作,通过技术交流和资源共享,不断提升自身的竞争力。

Gigarams Semiconductor Device Corp公司的发展小趣事
这可能是由于定位力量不够或初始力矩过大导致的。解决方案包括调整定位参数、优化启动策略或增加缓冲装置以减少振动。

问答坊 | AI 解惑

电阻上标有NC是什么意思

请问下各位大虾,一个开发板电阻上没有任何值,只标有NC,请问下是什么意思?谢谢!…

查看全部问答>

Schneider变频器通讯的代码

Schneider变频器通讯的代码…

查看全部问答>

西门子面板遇到问题

我现在用台式电脑232接口接一个HEXIN的232转485口,然后485口的D+,D-,GND,接到K-TP 178屏上IF1B接口的D+,D-,GND,进行通信。屏上的通信指示灯COM灯会亮,但电脑这边提示说 传送文件时超时。我就想不明白是为什么了。请帮我分析一下…

查看全部问答>

有谁做过USB2.0摄像头的开发?从电路设计到驱动开发!

有谁做过USB2.0摄像头的开发,如果方便的话请发点资料,越全越好,想学习一下,希望找点参考。 网上搜过了,没找到满意的资料。 谢了。 email:caozhiweibfdh@163.com…

查看全部问答>

RAMDISK: Couldn't find valid RAM disk image starting at 0.

环境:AT91RM9200 利用u-boot启动内核并加载RAMDISK UBOOT> bootm 1001c000 100a0000 输出信息如下: .... RAMDISK: Couldn\'t find valid RAM disk image starting at 0. Freeing initrd memory: 3072K Kernel panic: VFS: Unable to mount ro ...…

查看全部问答>

请教一下STM32怎么避免各个中断互相打架啊?

我用3.1.2的lib写了以SysTick来产生准确Delay的程式还有再LCD上显示时钟.另外在512K Flash里放了一个48KHz采样220Hz Sine值的阵列用来让DAC产生一个220Hz的波.我把喇叭直接接到DAC的PIN上来确认产生的声音.听到的纯音虽然正确不过不知道为什么 ...…

查看全部问答>

现在还有人知道这个ID

这个论坛的工控板的第二任版主,现在人气很旺啊…

查看全部问答>

Arduino能给我们带来什么?

Arduino,是一块基于开放源代码的USB接口Simple i/o接口板(包括12通道数字GPIO,4通道PWM输出,6-8通道10bit ADC输入通道),并且具有使用类似Java,C语言的IDE集成开发环境。关键部分为:Arduino开发IDE接口基于开放源代码,可以免费下载使用。但 ...…

查看全部问答>

Arduino与防盗系统

如图所示,利用Arduino,电机,压电传感器的组合,可以设置敲击密码,当密码匹配之后,门才会打开。   详情请点击:http://www.instructables.com/id/Secret-Knock-Detecting-Door-Lock/           ...…

查看全部问答>