| 
在线时间376 小时
UID3135871
注册时间2016-10-9
NXP金币97 

 
 TA的每日心情|  | 怒 2025-5-29 09:38
 | 
|---|
 签到天数: 632 天 连续签到: 1 天 [LV.9]以坛为家II 版主   
 
	积分4470 
 
最后登录2025-6-10 | 
 
| 一般读写EEPROM,我很少用,一般用查询模式,这里的LPC1343尝试用IIC的中断模式。我们选用IIC的引脚为P0_4,P0_5: 
   根据表可知,其实这个引脚仅有开漏模式,无论你配不配置成IIC引脚,这样这硬件设计时,必须加上拉电阻:
 
   IIC的状态寄存器:
 
   根据当前不同状态,对应动作~
 废话不多说了,IIC驱动代码:
 24C04.H
 
 复制代码#ifndef __24C04_H_
#define __24C04_H_
#include "LPC13xx.h"
#define AT24WC04   0xA0
extern u8  ucI2CAdd;                  // 
extern u8  ucI2CRamAdd;               // RAM read and write address
extern u8  ucI2CNum;                  // I2C read and write Num
extern u8  *ucI2CBuf;                 // I2C work pointer
extern u8  ucReadBuf[128];            // read data buf
extern u8  ucWriteBuf[128];           // write data buf
extern u8  ucI2CEnd;                  // end sign
extern u8  ucI2CAddEn;                // work enabled sign
u8 I2C_SendStr(u8 SlaveAddr, u8 WRAddr,u8 *Str,u8 number);
u8 I2C_RecvStr(u8 SlaveAddr, u8 RDAddr,u8 *Str,u8 number);
void AT24C04_I2C_Init(void);
#endif
 24C04.C
 
 
 在main函数里面,调用读写2个函数就可以了~复制代码#include "24c04.h"
u8  ucI2CAdd;                  // 
u8  ucI2CRamAdd;               // RAM read and write address
u8  ucI2CNum;                  // I2C read and write Num
u8  *ucI2CBuf;                 // I2C work pointer
u8  ucReadBuf[128];            // read data buf
u8  ucWriteBuf[128];           // write data buf
u8  ucI2CEnd;                  // end sign
u8  ucI2CAddEn;                // work enabled sign
u8 I2C_SendStr(u8 SlaveAddr, u8 WRAddr,u8 *Str,u8 number)
{
  if(0 == number)return(0);
  // setup basic parameter 
  ucI2CAdd = SlaveAddr;
  ucI2CNum = number;
  ucI2CRamAdd = WRAddr;
  ucI2CBuf = Str;
  ucI2CEnd = 0;
  ucI2CAddEn = 2;
  // set master Mode, startup bus
  LPC_I2C->CONCLR = 0x2C;
  LPC_I2C->CONSET = 0x60;
  // wait work finish
  while(0 == ucI2CEnd);
  if(1 == ucI2CEnd)return(1);
  else return(0);
}
u8 I2C_RecvStr(u8 SlaveAddr, u8 RDAddr,u8 *Str,u8 number)
{
  if(0 == number)return(0);
  // setup basic parameter 
  ucI2CAdd = SlaveAddr + 1;
  ucI2CNum = number;
  ucI2CRamAdd = RDAddr;
  ucI2CBuf = Str;
  ucI2CEnd = 0;
  ucI2CAddEn  = 1;
  // set master Mode, startup bus
  LPC_I2C->CONCLR = 0x2C;
  LPC_I2C->CONSET = 0x60;
  // wait work finish
  while(0 == ucI2CEnd);
  if(1 == ucI2CEnd) return(1);
  else return(0);
}
void AT24C04_I2C_Init(void)
{
  LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 16);       //--- 使能IOCON模块的时钟源 ---
  LPC_IOCON->PIO0_4 = (1 << 0);                 //--- PIO0_4为SCL功能引脚 ---
  LPC_IOCON->PIO0_5 = (1 << 0);                 //--- PIO0_5为SDA功能引脚 ---
  LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 5);        //--- 使能I2C模块的时钟源 ---
  LPC_SYSCON->PRESETCTRL |= (1 << 1);           //--- 禁止I2C复位 ---
  
  LPC_I2C->SCLH = 360;
  LPC_I2C->SCLL = 360;
  LPC_I2C->CONCLR = 0x2C;
  LPC_I2C->CONSET = 0x40;
  
  NVIC_EnableIRQ(I2C_IRQn);
}
void I2C_IRQHandler(void)
{
  unsigned char sta;
  sta = LPC_I2C->STAT;                    // read state
  switch(sta) 
    {
      case 0x08:
        if(1 == ucI2CAddEn)LPC_I2C->DAT = ucI2CAdd & 0xFE;   // send slave address + read sign
        else LPC_I2C->DAT = ucI2CAdd;         // send slave address
        LPC_I2C->CONCLR = 0x28;               // SI=0
        break;
      case 0x10:
        LPC_I2C->DAT = ucI2CAdd;                // reset, send slave address
        LPC_I2C->CONCLR = 0x28;               // SI=0
        break;
      case 0x18:
        if(ucI2CAddEn == 0)
          {
            if(ucI2CNum > 0)
              {
                LPC_I2C->DAT = *ucI2CBuf++;       // send data
                LPC_I2C->CONCLR = 0x28; 
                ucI2CNum--;
              }
              else
                {
                  LPC_I2C->CONSET = 0x10;      // send finish, 
                  LPC_I2C->CONCLR = 0x28;
                  ucI2CEnd = 1;      // set end sign
                }
            break;
          }
        if(ucI2CAddEn == 1)LPC_I2C->DAT = ucI2CRamAdd;       // send work address
        if(ucI2CAddEn == 2)
          {
            LPC_I2C->DAT = ucI2CRamAdd;           // send work address
            ucI2CAddEn = 0;             // clear sign
          }
        LPC_I2C->CONCLR = 0x28;               // SI=0 , clear interrupt sign
        break;
      case 0x28:
        if(ucI2CAddEn == 0)
          {
            if(ucI2CNum>0)
              {
                LPC_I2C->DAT = *ucI2CBuf++;         // send work address
                ucI2CNum--;
              }
            else
              {
                LPC_I2C->CONSET = 0x10;         // send finish
                ucI2CEnd = 1; 
              }
            LPC_I2C->CONCLR = 0x28;            // SI=0 , clear interrupt sign
          }
        if(ucI2CAddEn == 1)                             // read appoint address
          { 
            LPC_I2C->CONSET = 0x20;
            LPC_I2C->CONCLR = 0x08;
            ucI2CAddEn = 0;              // appoint address dispose finish
          }
        break;
      case 0x20:
      case 0x30:
      case 0x38:                  // bus error
        LPC_I2C->CONCLR = 0x28;
        ucI2CEnd = 0xFF;
        break;
      case 0x40:                  // already send sla+r and recive the ack
        if(1==ucI2CNum)LPC_I2C->CONCLR = 0x2C;// send no ack when last byte
        else
          {                    // recive and send ack
            LPC_I2C->CONSET = 0x04;
            LPC_I2C->CONCLR = 0x28;
          } 
        break;
      case 0x50:
        *ucI2CBuf++ = LPC_I2C->DAT;            // read data
        ucI2CNum--;
        if(1==ucI2CNum)LPC_I2C->CONCLR = 0x2C;
        else
          {
            LPC_I2C->CONSET = 0x04;
            LPC_I2C->CONCLR = 0x28;
          }
        break;
      case 0x58:
        *ucI2CBuf++ = LPC_I2C->DAT;            // read last byte data
        LPC_I2C->CONSET = 0x10;              // bus end
        LPC_I2C->CONCLR = 0x28;
        ucI2CEnd = 1; 
        break;
      case 0x48:
        LPC_I2C->CONCLR = 0x28;               // bus error
        ucI2CEnd = 0xFF; 
        break;
      default: 
        break;
    }
}
 编译,下载,查看串口:复制代码    AT24C04_I2C_Init();
    I2C_SendStr(AT24WC04,Address,(u8*)"Hello EEPROM!\r\n",sizeof("Hello EEPROM!\r\n"));
    delay_ms(500);
    memset(ucReadBuf, 0, sizeof ucReadBuf);
    I2C_RecvStr(AT24WC04,Address,ucReadBuf,20);
    delay_ms(500);
    Usart_SendString((char*)ucReadBuf);
   
 
 | 
 |