當(dāng)前位置:首頁(yè) > 電源 > 數(shù)字電源
[導(dǎo)讀]PIC:讀寫(xiě)24LCxx系列的EEPROM的實(shí)例C語(yǔ)言程序//********************************************************* //* Using I2C Master Mode for access Slave (EEPRM) //* //* Writ

PIC:讀寫(xiě)24LCxx系列的EEPROM的實(shí)例C語(yǔ)言程序

//********************************************************* 
//*            Using I2C Master Mode for access Slave (EEPRM)  
//*                
//* Written by:  Richard Yang                                
//*              Sr. Corporate Application Engineer            
//*              Microchip Technology Inc.                
//* Date:        Oct. 3nd '2002                            
//* Revision: 1.00
//* Language tools : MPLAB-C18 v2.09.13
//*      MPLINK v3.10
//*      MPLAB-IDE v6.00.17 & ICD2                                  
//***********************************************************
/* Include Header files  */

#i nclude <p18f452.h>
#i nclude <i2c.h>   // Load I2C Header file from defult direct
#i nclude <timers.h>
#i nclude "P18LCD.h"   // Load P18LCD Header file form current working direct

/* Declare the Function Prototype */

void Initialize_I2C_Master(void);
void EE_Page_Write(unsigned char,unsigned char,unsigned char,unsigned char *);
void EE_SEQU_Read(unsigned char,unsigned char,unsigned char,unsigned char *);
void EEPROM_Write(unsigned char,unsigned char,unsigned char);
void EEPROM_ACK(unsigned char);
unsigned char EEPROM_Read(unsigned char,unsigned char);
void I2C_Done(void);
void Initialize_Timer2(void);
void isr_high_direct(void);
void isr_high(void);

#pragma romdata My_romdata=0x1000
const rom far unsigned char LCD_MSG1[]="SW2: Byte Write ";
const rom far unsigned char LCD_MSG2[]="SW6: Random Read";
const rom far unsigned char LCD_MSG3[]="Byte Write Mode ";
const rom far unsigned char LCD_MSG4[]="Random Read Mode";
const rom far unsigned char LCD_MSG5[]="Sended:         ";
const rom far unsigned char LCD_MSG6[]="Send:           ";
const rom unsigned char I2C_Write_Buffer[]="Microchip Technology";
#pragma romdata

/* Define following array in data memory */

unsigned char I2C_Read_Buffer [32];

/* define following variable in data memory at Access Bank */
#pragma udata access My_RAM
near unsigned char Debounce;
near unsigned char Send_Addr;
near unsigned char Send_Data;
near unsigned char Send_Length;
near unsigned char Read_Data;
near unsigned char P_SW2;
near unsigned char P_SW6;
#pragma udata

#define Page_Length 8
#define SW2  PORTAbits.RA4
#define SW6  PORTEbits.RE1
#define Bounce_Time 6
#define EE_CMD 0xA0

//***********************************************************
/*                 */
/*       Main  Program        */
/*                 */
//***********************************************************


void main(void)
{
 ADCON1=0b00000110;  // Disable A/D Function
 TRISAbits.TRISA4=1;  // Set SW2 for input
 TRISEbits.TRISE1=1;  // Set SW6 for Input
 Initialize_Timer2( );
 Initialize_I2C_Master( );
 OpenLCD( );
 
 if (SW2 & SW6)Debounce=0;
 else Debounce = Bounce_Time;
 
 while(1)
 {
  LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
  putrsLCD(LCD_MSG1);
  LCD_Set_Cursor(1,0);    // Put LCD Cursor on (1,0)
  putrsLCD(LCD_MSG2);
 
  P_SW2=P_SW6=0;
  Send_Addr=0;
 
  while(1)
  {
   if (P_SW2)
   {
    P_SW2=0;
    Debounce = Bounce_Time;
    LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG3);
    LCD_Set_Cursor(1,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG5);
   
    do
    {
     while (!P_SW2);
     P_SW2=0;
     LCD_Set_Cursor(1,8);
     Send_Data=I2C_Write_Buffer[Send_Addr];
     EEPROM_Write(EE_CMD,Send_Addr,Send_Data);
     puthexLCD(EE_CMD);
     putcLCD(' ');
     puthexLCD(Send_Addr);
     putcLCD(' ');
     puthexLCD(Send_Data);
     EEPROM_ACK(EE_CMD);
     Send_Addr++;
    } while (I2C_Write_Buffer[Send_Addr]!=0x00);
   break;
   }
  
   if (P_SW6)
   {
    P_SW6=0;
    Debounce = Bounce_Time;
    LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG4);
    LCD_Set_Cursor(1,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG6);
   
  
    while(1)
    {
     if (P_SW6)
     {
     P_SW6=0;
     LCD_Set_Cursor(1,5);
     Read_Data = EEPROM_Read(EE_CMD,Send_Addr);
     puthexLCD(EE_CMD);
     putcLCD(' ');
     puthexLCD(Send_Addr);
     putcLCD(' ');
     puthexLCD(EE_CMD);
     putcLCD(' ');
     puthexLCD(Read_Data);
     Send_Addr++;
     }
     if (P_SW2) break;
    }
    if (P_SW2) break;
   }
   if (P_SW2)
   {
    P_SW2=0;
     break;
   }
  }
 }
}
//************************************************
//*       #pragma Interrupt Declarations         *
//*             *
//* Function: isr_high_direct                    *
//*   - Direct execution to the actual           *
//*     high-priority interrupt code.            *
//************************************************
#pragma code isrhighcode = 0x0008

void isr_high_direct(void)
{         
     _asm      //begin in-line assembly
     goto isr_high   //go to isr_high function
     _endasm     //end in-line assembly
}[!--empirenews.page--]
#pragma code

//************************************************
//* Function: isr_high(void)                     *    
//*   High priority interrupt for Timer2         *        
//************************************************
#pragma interrupt isr_high

void isr_high(void)
{
 PIR1bits.TMR2IF=0;     // Clear Timer2 interrupt Flag
 
 if (Debounce==0)
 {
  if (!SW2)
  { P_SW2=1; Debounce =Bounce_Time; }
  if (!SW6)
  { P_SW6=1; Debounce =Bounce_Time; }
 } 
 else if (SW2 & SW6)Debounce--;
   else Debounce =Bounce_Time;   
}       
#pragma code

//***********************************************
//*         Write a Byte to EEPROM              
//*   - ctrl : Control Byte of EEPROM   
//*   - addr : Location of EEPROM   
//*   - data : Data Byte of EEPROM    
//***********************************************

void Initialize_Timer2(void)
{

  RCONbits.IPEN=1;   // Enable Interrupt Priority bit
  IPR1bits.TMR2IP=1;   // Set Timer2 for High Priority
  INTCONbits.GIEH=1;   // Enable High Priority Interrupt

  OpenTimer2 (TIMER_INT_ON  // Turn On the Timer2 with Interrupt
       & T2_PS_1_4   // (4Mhz/4) [4*10*(99+1)] = 4mS */
       & T2_POST_1_10);

  PR2 = 99;
}

//***********************************************
//*         Write a Byte to EEPROM              *
//*   - ctrl : Control Byte of EEPROM   *
//*   - addr : Location of EEPROM   *
//*   - data : Data Byte of EEPROM    *
//***********************************************
void EEPROM_Write(unsigned char ctrl,unsigned char addr,unsigned char data)
{
 IdleI2C();                // ensure module is idle
   StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed and clear SSPIF flag   

 WriteI2C(ctrl);     // Write Control+Write to EEPROM & Check BF flag
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();      // Clear SSPIF flag

 WriteI2C(addr);     // Write Address to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

 WriteI2C(data);     // Write Data to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

 StopI2C();      // Stop condition
 I2C_Done();      // Wait the Stop condition completed
}

//***********************************************
//*         Pae Write to EEPROM               
//*
//*   - ctrl : Control Byte of EEPROM 
//*   - addr : Location of EEPROM   
//*   - length : Write counter
//*   - *dptr : RAM point --> EEPROM
//*
//***********************************************
void EE_Page_Write(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
 IdleI2C();                // ensure module is idle
   StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed
 
 WriteI2C(ctrl);     // Write Control+Write to EEPROM & Check BF flag
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();      // Clear SSPIF flag

 WriteI2C(addr);     // Write Address to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

 while (length!=0)    // Check write completed ?
 {
  WriteI2C(*dptr);   // Write data to EEPROM
  while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
  I2C_Done();
  dptr++;      // Point to next byte
  length--;
 }
 
 StopI2C();      // Stop condition
 I2C_Done();      // Wait the Stop condition completed
}
 
//***********************************************
//*         EEPROM Acknowledge Polling          *
//*   --  The routine will polling the ACK  *
//*       response from EEPROM       *
//*   --  ACK=0 return        *
//*   --  ACK=1 send Restart & loop check  *
//***********************************************
void EEPROM_ACK(unsigned char ctrl)
{
 unsigned char i;

 IdleI2C();                // ensure module is idle
   StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

 WriteI2C(ctrl);     // Write Control to EEPROM (WRITE)
 I2C_Done();      // Clear SSPIF flag

 while (SSPCON2bits.ACKSTAT)  // test for Acknowledge from EEPROM
    {
  for (i=0;i<100;i++);  // Delay for next Repet-Start

       RestartI2C();               // initiate Repet-Start condition
  I2C_Done();          // Wait Repet-Start condition completed

  WriteI2C(ctrl);    // Write Control to EEPROM (WRITE)
  I2C_Done();      // Clear SSPIF flag
    }  
 StopI2C();                      // send STOP condition
 I2C_Done();      // wait until stop condition is over
}

//***********************************************
//*    Random Read a Byte from EEPROM           *
//*   - ctrl : Control Byte of EEPROM (Write) *
//*     (Ctrl +1 ) : Read Command    *
//*   - addr : Address Byte of EEPROM   *
//*   - Return : Read Data from EEPROM   *
//***********************************************
unsigned char EEPROM_Read(unsigned char ctrl,unsigned char addr)
{
 unsigned char f;
 
 IdleI2C();                // ensure module is idle
   StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

 WriteI2C(ctrl);     // Write Control to EEPROM   
    while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

 WriteI2C(addr);     // Write Address to EEPROM        
    while(SSPCON2bits.ACKSTAT);   // test for ACK condition, if received 
 I2C_Done();      // Clear SSPIF flag

    RestartI2C();                // initiate Restart condition
 I2C_Done();

 WriteI2C(ctrl+1);    // Write Control to EEPROM             
 while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

 f=ReadI2C();     // Enable I2C Receiver & wait BF=1 until received data
 I2C_Done();      // Clear SSPIF flag

 NotAckI2C();     // Genarate Non_Acknowledge to EEPROM
 I2C_Done();   
                    
    StopI2C();                   // send STOP condition
 I2C_Done();      // wait until stop condition is over

 return(f);      // Return Data from EEPROM
}

//***********************************************
//*         Sequential Read from EEPROM               
//*
//*   - ctrl : Control Byte of EEPROM 
//*   - addr : Location of EEPROM   
//*   - length : Read counter
//*   - *dptr : Store EEPROM data to RAM [!--empirenews.page--]
//*
//***********************************************
void EE_SEQU_Read(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
 IdleI2C();                // ensure module is idle
   StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

 WriteI2C(ctrl);     // Write Control to EEPROM   
    while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

 WriteI2C(addr);     // Write Address to EEPROM        
    while(SSPCON2bits.ACKSTAT);   // test for ACK condition, if received 
 I2C_Done();      // Clear SSPIF flag

    RestartI2C();                // initiate Restart condition
 I2C_Done();

 WriteI2C(ctrl+1);    // Write Control to EEPROM             
 while(SSPCON2bits.ACKSTAT);    // Test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

 while (length!=0)
 {
  *dptr=ReadI2C();   // Enable I2C Receiver & Store EEPROM data to Point buffer
  I2C_Done();
  dptr++;
  length--;
   
  if (length==0) NotAckI2C();
  else AckI2C();    // Continue read next data, send a acknowledge to EEPROM
 
  I2C_Done();
 }

    StopI2C();                   // send STOP condition
 I2C_Done();      // wait until stop condition is over
}

//***********************************************
//*   Check I2C action that is completed        *
//***********************************************
void I2C_Done(void)
{
 while (!PIR1bits.SSPIF);  // Completed the action when the SSPIF is Hi.
 PIR1bits.SSPIF=0;      // Clear SSPIF
}

//************************************************
//* Initial I2C Master Mode with 7 bits Address  *
//* Clock Speed : 100KHz @4MHz      *
//************************************************
void Initialize_I2C_Master(void)
{
 OpenI2C(MASTER,SLEW_ON);
 SSPADD= 9;
}

 

 

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

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶(hù)希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱(chēng),數(shù)字世界的話(huà)語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱(chēng)"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉