今晚捣鼓了一下模拟I2C读取AT24C04。
代码:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "SI_EFM8BB1_Register_Enums.h" // SFR declarations
#include "InitDevice.h"
#define uchar unsigned char
#define uint unsigned int
//-----------------------------------------------------------------------------
// Pin Declarations
//-----------------------------------------------------------------------------
SI_SBIT(LED, SFR_P1, 4); // LED
SI_SBIT(S1, SFR_P0, 2); // BTN0
SI_SBIT(SCL, SFR_P1, 6); // SCL
SI_SBIT(SDA, SFR_P0, 7); // SDA
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 24500000/8 // Clock speed in Hz (default)
#define SW_PRESSED 0
#define SW_NOT_PRESSED 1
#define LED_ON 0
#define LED_OFF 1
//#define SCL_1 1
//#define SCL_0 0
//#define SDA_1 1
//#define SDA_0 0
unsigned char buffer[3];
void InitSCL(void)
{
P1MDOUT = P1MDOUT_B6__OPEN_DRAIN ;
}
void InitSDAInput(void)
{
//P1MDIN = P1MDIN_B6__BMASK | P1MDIN_B6__SHIFT | P1MDIN_B6__DIGITAL;
//P1MDIN = P1MDIN_B7__DIGITAL ;
//P1SKIP = P1SKIP_B7__SKIPPED ;
P0SKIP = P0SKIP_B7__SKIPPED;
}
void InitSDAOutput(void)
{
P0MDOUT = P0MDOUT_B7__OPEN_DRAIN ;
}
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
//-----------------------------------------------------------------------------------------
//????:Delayms()
//-----------------------------------------------------------------------------------------
void Delayms(unsigned int ims)
{
unsigned int i,j;
for(i=0;i<ims;i++)
for(j=0;j<65;j++) { Delay(1); }
}
void Start()
{
InitSDAOutput();
InitSCL();
SCL=1;
Delay(10);
SDA=1;
Delay(10);
SDA=0;
Delay(10);
SCL=0;
Delay(10);
}
void Stop()
{
InitSDAOutput();
InitSCL();
SDA=0;
Delay(10);
SCL=1;
Delay(10);
SDA=1;
Delay(10);
}
char ACK ()
{
char c;
InitSDAOutput();
InitSCL();
SDA=1;
Delay(10);
SCL=1;
Delay(10);
InitSDAInput();
Delay(10);
if(SDA)
c=1;
else
c=0;
Delay(10);
InitSDAOutput();
Delay(10);
SCL=0;
Delay(10);
return c;
}
void SendChar(unsigned char ch)
{
unsigned char i;
i=8;
InitSDAOutput();
InitSCL();
do
{
if(ch&0x80)
SDA=1;
else
SDA=0;
Delay(10);
SCL=1;
Delay(10);
SCL=0;
Delay(10);
ch<<=1;
}while(--i!=0);
}
unsigned char RecChar()
{
unsigned char i,j;
i=8;
j=0;
InitSCL();
InitSDAInput();
Delay(10);
do
{
SCL=1;
Delay(10);
j=(j<<1);
if(SDA)
j=j|SDA;
Delay(10);
SCL=0;
Delay(10);
}while(--i!=0);
InitSDAOutput();
return j;
}
void WriteChar(unsigned int addr,unsigned char ch)
{
unsigned char c;
c=((*((unsigned char *)&addr))<<1)&0x02;
Start();
SendChar(0xa0|c);
ACK();
SendChar(addr);
ACK();
SendChar(ch);
ACK();
Stop();
for(ch=0xff;ch!=0;ch--) ;
}
unsigned char ReadChar(unsigned int addr)
{
unsigned char ch;
ch=((*((unsigned char *)&addr))<<1)&0x02;
Start();
SendChar(0xa0|ch);
ACK();
SendChar(addr);
ACK();
Start();
SendChar(0xa1|ch);
ACK();
ch=RecChar();
Stop();
return ch;
}
//-----------------------------------------------------------------------------
// SiLabs_Startup() Routine
// ----------------------------------------------------------------------------
// This function is called immediately after reset, before the initialization
// code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a
// useful place to disable the watchdog timer, which is enable by default
// and may trigger before main() in some instances.
//-----------------------------------------------------------------------------
void SiLabs_Startup (void)
{
// Disable the watchdog here
}
//-----------------------------------------------------------------------------
// main() Routine
// ----------------------------------------------------------------------------
// Note: the software watchdog timer is not disabled by default in this
// example, so a long-running program will reset periodically unless
// the timer is disabled or your program periodically writes to it.
//
// Review the "Watchdog Timer" section under the part family's datasheet
// for details. To find the datasheet, select your part in the
// Simplicity Launcher and click on "Data Sheet".
//-----------------------------------------------------------------------------
int main (void)
{
unsigned int i;
char flag=0;
enter_DefaultMode_from_RESET();
InitSCL();
InitSDAOutput();
buffer[0]=ReadChar(0);
Delay(100); //??????
WriteChar(0,0x55);
Delayms(20);
WriteChar(1,0xaa);
Delayms(20);
WriteChar(2,0x25);
Delayms(20);
buffer[0]=ReadChar(1);
buffer[1]=ReadChar(2);
buffer[2]=ReadChar(0);
while (1)
{
} // Spin forever
}
效果图:
|