📄 i2c.c
字号:
/**********************/
/*i2c.c */
/*Pierre Seguin */
/*zpierre007@yahoo.fr */
/**********************/
#include "LPC210x.h"
#include "i2c.h"
#define I2C_ERROR 1 // these are the values that are stored in i2c_status
#define I2C_OK 2 // after reading or writing a byte and during an I2C operation
#define I2C_BUSY 0
#define EEPROM_ADDR 0xA0
unsigned char mask[] = {0xA8, 0x1F, 0x7F, 0x7F, 0x3F, 0x3F, 0x07, 0x1F,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// mask values for registers
void InitI2C(void)
{
I2C_I2CONCLR = 0xFF;
// Set pinouts as scl and sda
PINSEL0 |= 0x50;
PINSEL0 &= (uint32_t)0xFFFFFF5F;
I2C_I2SCLL=60; //speed at 375Khz for a VPB Clock Divider = 1
I2C_I2SCLH=70;
I2C_I2CONSET = 0x40; //Active Master Mode on I2C bus
}
void SendSlaveAdress(uint8_t Addr_S)
{
if(!(Addr_S&0x01)) //test if it's a master adress
{
//while(I2C_I2STAT!=0xF8);
I2C_I2CONSET = STA;
while(I2C_I2STAT!=0x08); //Set and wait the start
I2C_I2DAT = Addr_S; // Charge slave Address
I2C_I2CONCLR = SIC|STAC; // Clear i2c interrupt bit to send the data
while(I2C_I2STAT!=0x18&&!(I2C_I2CONSET&SI)); //wait the ACK
}
else //it's a slave adress
{
I2C_I2CONSET = STA;
I2C_I2CONCLR = SIC;
while(I2C_I2STAT!=0x10&&!(I2C_I2CONSET&SI)); //Set and wait the start
I2C_I2DAT = Addr_S; // Charge slave Address
I2C_I2CONCLR = SIC|STAC; // Clear i2c interrupt bit to send the data
while(I2C_I2STAT!=0x40&&!(I2C_I2CONSET&SI)); //wait the ACK
}
}
uint8_t ReadOnI2C(void)
{
I2C_I2CONCLR = SIC; //ACK from the master
while(I2C_I2STAT!=0x50&&!(I2C_I2CONSET&SI)); //wait the data
// Read the data...
return I2C_I2DAT;
}
void WriteOnI2C(uint8_t Data)
{
I2C_I2DAT = Data; // Charge Data
I2C_I2CONCLR = SIC; // Clear i2c interrupt bit to send the data
while(I2C_I2STAT!=0x28&&!(I2C_I2CONSET&SI)); //wait the ACK
}
uint8_t WriteRTC(uint8_t Addr,uint8_t Data)
{
SendSlaveAdress(EEPROM_ADDR); //Send the Eeprom Adress
WriteOnI2C(Addr); //Send Addr
WriteOnI2C(chartobcd(Data)); //Send Data
STOPI2C
}
uint8_t ReadRTC(uint8_t Addr, uint8_t maskoff)
{
uint8_t tmp;
SendSlaveAdress(EEPROM_ADDR);//Send the Eeprom Adress
WriteOnI2C(Addr); //Send Addr
SendSlaveAdress(EEPROM_ADDR|1);
tmp=ReadOnI2C();
if (maskoff) tmp &= mask[Addr]; // mask result if required
STOPI2C
// return bcdtochar(tmp); // convert to decimal and return result
return tmp;
}
// function chartobcd
// converts a decimal value to the BCD equivalent
// decimal values must be stored in the Real Time Clock in BCD format
// passed is the decimal value to convert
// returned is the BCD equivalent
unsigned char chartobcd(unsigned char n)
{
return ((n / 10) << 4) | (n % 10);
}
// function bcdtochar
// converts a number in BCD format to the decimal equivalent
// decimal values must be stored in the Real Time Clock in BCD format
// passed is the BCD format value
// returned is the decimal equivalent
unsigned char bcdtochar(unsigned char bcdnum)
{
return (bcdnum & 0x0f) + (bcdnum >> 4) * 10;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -