今晚搞了一下AT24C04模拟时序,参考大神的例子。
代码:
#include "ac78xx_gpio.h"
#include "ac78xx_debugout.h"
#include "ac78xx.h"
#define uchar unsigned char
#define uint unsigned int
/* makesure n < 16 */
#define PAout(n) BIT_ADDR(GPIOA_ODR_ADDR, n)
#define PAin(n) BIT_ADDR(GPIOA_IDR_ADDR, n)
#define PBout(n) BIT_ADDR(GPIOB_ODR_ADDR, n)
#define PBin(n) BIT_ADDR(GPIOB_IDR_ADDR, n)
#define PCout(n) BIT_ADDR(GPIOC_ODR_ADDR, n)
#define PCin(n) BIT_ADDR(GPIOC_IDR_ADDR, n)
#define PDout(n) BIT_ADDR(GPIOD_ODR_ADDR, n)
#define PDin(n) BIT_ADDR(GPIOD_IDR_ADDR, n)
#define PEout(n) BIT_ADDR(GPIOE_ODR_ADDR, n)
#define PEin(n) BIT_ADDR(GPIOE_IDR_ADDR, n)
/* gpio bit-band control */
#define LED PAout(0)
#define FALSE 0
#define TRUE 1
#define WriteDeviceAddress 0xA0
#define ReadDviceAddress 0xA1
#define EEPROM_SCL GPIO_PA2
#define EEPROM_SDA GPIO_PA0
// E2PROM??
#define SET_SDA GPIO_SetPinBits(EEPROM_SDA) //SDA = 1
#define CLR_SDA GPIO_ResetPinBits(EEPROM_SDA) //SDA = 0
#define SDA_IN GPIO_GetPinValue(EEPROM_SDA) //Read SDA
#define SET_SCL GPIO_SetPinBits(EEPROM_SCL) //SCL = 1
#define CLR_SCL GPIO_ResetPinBits(EEPROM_SCL) //SCL = 0
void I2C_Delay(uint16_t number)
{
uint16_t j;
for(j=0;j<number;j++);
}
void DIR_In(void)
{
GPIO_SetDir(EEPROM_SDA, 0);//??SDA?????
GPIO_SetPullupEnable(EEPROM_SDA, ENABLE); //????
}
void DIR_Out(void)
{
GPIO_SetDir(EEPROM_SDA, 1);//??SDA?????
}
void Start_I2c(void)
{
DIR_Out();
SET_SDA;
I2C_Delay(400);
SET_SCL;
I2C_Delay(400);
CLR_SDA;
I2C_Delay(400);
CLR_SCL;
I2C_Delay(400);
}
void Stop_I2c(void)
{
DIR_Out();
CLR_SDA;
I2C_Delay(400);
SET_SCL;
I2C_Delay(400);
SET_SDA;
I2C_Delay(400);
CLR_SCL;
I2C_Delay(400);
}
void WriteAByte(uint16_t data)
{
uint16_t i;
DIR_Out();
for(i=0;i<8;i++)
{
CLR_SCL;
I2C_Delay(400);
if ((data>>7)&0x0001)
{
SET_SDA;
}
else
{
CLR_SDA;
}
I2C_Delay(400);
SET_SCL;
data <<= 1;
I2C_Delay(400);
}
CLR_SCL;
I2C_Delay(400);
}
uint16_t ReadAByte(void)
{
uint16_t i,bit=0,data=0;
DIR_In();
CLR_SCL;
I2C_Delay(400);
for (i=0; i<8; i++)
{
SET_SCL;
I2C_Delay(400);
if(SDA_IN)
{
bit=1;
}
else
{
bit=0;
}
data = (data << 1) | bit;
CLR_SCL;
I2C_Delay(400);
}
return (data);
}
uint16_t ReceiveI2CAck(void)
{
uint16_t i=5000;
SET_SDA;
DIR_In();
SET_SCL;
I2C_Delay(400);
while (SDA_IN)
{
if(!(--i))
{
CLR_SCL;
return(FALSE);
}
}
CLR_SCL;
I2C_Delay(400);
return(TRUE);
}
void SendI2CAck(void)
{
CLR_SCL;
I2C_Delay(400);
DIR_Out();
CLR_SDA;
SET_SCL;
I2C_Delay(400);
CLR_SCL;
I2C_Delay(400);
}
void SendI2CNAck(void)
{
CLR_SCL;
I2C_Delay(400);
DIR_Out();
SET_SDA;
SET_SCL;
I2C_Delay(400);
CLR_SCL;
I2C_Delay(400);
}
/**
* EEPROM_Config
*
* @param[in] void
*
* @return void
*
* @brief config eeprom pinmux and gpio
*
*/
void EEPROM_Config(void)
{
GPIO_SetDir(EEPROM_SCL, 1);//??SCL?????
GPIO_SetDir(EEPROM_SDA, 1);//??SDA?????
}
/**
* EEPROM_Read
*
* @param[in] address range 0-511(24c08)
*
* @return the value from address.
*
* @brief read eeprom
*
*/
uint8_t EEPROM_Read(uint8_t address)
{
uint8_t AX;
AX = address << 1;
Start_I2c();
WriteAByte(WriteDeviceAddress);
if (!ReceiveI2CAck())
{
return( FALSE );
}
WriteAByte( AX );
if (!ReceiveI2CAck())
{
return( FALSE );
}
Start_I2c();
WriteAByte(ReadDviceAddress);
if (!ReceiveI2CAck())
{
return( FALSE );
}
AX = ReadAByte();
SendI2CAck();
AX += (ReadAByte()) << 8;
SendI2CNAck();
Stop_I2c();
return( AX ); // ???,??????,?????
}
/**
* EEPROM_Write
*
* @param[in] address: 0-511(24c08), data: a 16bit value
*
* @return void
*
* @brief write eeprom
*
*/
void EEPROM_Write(uint8_t address, uint8_t data)
{
uint8_t AX;
AX = address <<1;
Start_I2c();
WriteAByte(WriteDeviceAddress);
ReceiveI2CAck();
WriteAByte( AX );
ReceiveI2CAck();
WriteAByte(data);
ReceiveI2CAck();
WriteAByte((data)>>8);
ReceiveI2CAck();
Stop_I2c();
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
I2C_Delay(5000);
}
/**
* main
*
* @param[in] none
* @return none
*
* @brief entry
*
*/
int main(void)
{
uint8_t buffer[3];
InitDebug(); //´òÓ¡³õʼ»¯
InitDelay(); //ÑÓʱº¯Êý³õʼ»¯
GPIO_SetDir(GPIO_PA2, 1);
GPIO_SetDir(GPIO_PA4, 1);
EEPROM_Write(0,0x5a);
EEPROM_Write(1,0xa5);
EEPROM_Write(2,0x69);
buffer[0]=EEPROM_Read(1);
buffer[1]=EEPROM_Read(2);
buffer[2]=EEPROM_Read(0);
while (1)
{
//printf("T1: %f\r\n",Temp1);
//printf("T2: %f\r\n",Temp2);
LED = !LED;
mdelay(500);
}
}
效果图:
|