www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]讀寫24LCxx系列的EEPROM的實(shí)例程序;******************************************************** ;* * ;* ? PIC16F877A ?I2C Master 家Α???﹃?Α EEPROM ?絛ㄒ祘Α * ;

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

;******************************************************** 
;*                     *
;*    ? 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 <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 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
;
;----------------------------------------------------------------------
;
;* ???代剛祘Α穦?盢?艙計(jì)?(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

 

 

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

柏林2025年9月9日 /美通社/ -- 2025年9月5日,納斯達(dá)克上市公司優(yōu)克聯(lián)集團(tuán)(NASDAQ: UCL)旗下全球互聯(lián)品牌GlocalMe,正式亮相柏林國際消費(fèi)電子展(IFA 2025),重磅推出融合企...

關(guān)鍵字: LOCAL LM BSP 移動網(wǎng)絡(luò)

深圳2025年9月9日 /美通社/ -- PART 01活動背景 當(dāng)技術(shù)的鋒芒刺穿行業(yè)壁壘,萬物互聯(lián)的生態(tài)正重塑產(chǎn)業(yè)疆域。2025年,物聯(lián)網(wǎng)產(chǎn)業(yè)邁入?"破界創(chuàng)造"與"共生進(jìn)化"?的裂變時(shí)代——AI大模型消融感知邊界,...

關(guān)鍵字: BSP 模型 微信 AIOT

"出海無界 商機(jī)無限"助力企業(yè)構(gòu)建全球競爭力 深圳2025年9月9日 /美通社/ -- 2025年8月28日, 由領(lǐng)先商業(yè)管理媒體世界經(jīng)理人攜手環(huán)球資源聯(lián)合主辦、深圳?前海出海e站通協(xié)辦的...

關(guān)鍵字: 解碼 供應(yīng)鏈 AI BSP

柏林2025年9月9日 /美通社/ -- 柏林當(dāng)?shù)貢r(shí)間9月6日,在2025德國柏林國際電子消費(fèi)品展覽會(International Funkausstellung...

關(guān)鍵字: 掃地機(jī)器人 耳機(jī) PEN BSP

武漢2025年9月9日 /美通社/ -- 7月24日,2025慧聰跨業(yè)品牌巡展——湖北?武漢站在武漢中南花園酒店隆重舉辦!本次巡展由慧聰安防網(wǎng)、慧聰物聯(lián)網(wǎng)、慧聰音響燈光網(wǎng)、慧聰LED屏網(wǎng)、慧聰教育網(wǎng)聯(lián)合主辦,吸引了安防、...

關(guān)鍵字: AI 希捷 BSP 平板

上海2025年9月9日 /美通社/ -- 9月8日,移遠(yuǎn)通信宣布,其自研藍(lán)牙協(xié)議棧DynaBlue率先通過藍(lán)牙技術(shù)聯(lián)盟(SIG)BQB 6.1標(biāo)準(zhǔn)認(rèn)證。作為移遠(yuǎn)深耕短距離通信...

關(guān)鍵字: 藍(lán)牙協(xié)議棧 移遠(yuǎn)通信 COM BSP

上海2025年9月9日 /美通社/ -- 為全面落實(shí)黨中央、國務(wù)院和上海市委、市政府關(guān)于加快發(fā)展人力資源服務(wù)業(yè)的決策部署,更好發(fā)揮人力資源服務(wù)業(yè)賦能百業(yè)作用,8月29日,以"AI智領(lǐng) HR智鏈 靜候你來&quo...

關(guān)鍵字: 智能體 AI BSP 人工智能

北京2025年9月8日 /美通社/ -- 近日,易生支付與一汽出行達(dá)成合作,為其自主研發(fā)的"旗馭車管"車輛運(yùn)營管理平臺提供全流程支付通道及技術(shù)支持。此次合作不僅提升了平臺對百余家企業(yè)客戶的運(yùn)營管理效率...

關(guān)鍵字: 一汽 智能化 BSP SAAS

深圳2025年9月8日 /美通社/ -- 晶泰科技(2228.HK)今日宣布,由其助力智擎生技制藥(PharmaEngine, Inc.)發(fā)現(xiàn)的新一代PRMT5抑制劑PEP0...

關(guān)鍵字: 泰科 AI MT BSP

上海2025年9月5日 /美通社/ -- 由上海市經(jīng)濟(jì)和信息化委員會、上海市發(fā)展和改革委員會、上海市商務(wù)委員會、上海市教育委員會、上海市科學(xué)技術(shù)委員會指導(dǎo),東浩蘭生(集團(tuán))有限公司主辦,東浩蘭生會展集團(tuán)上海工業(yè)商務(wù)展覽有...

關(guān)鍵字: 電子 BSP 芯片 自動駕駛
關(guān)閉