LPC2103开发板I2C测试试验
工程下載地址:http://blog.chinaunix.net/u3/106835/showart_2389150.html
#include "config.h" //相關頭文件
?
#define C02MasterAddr?
#define C02SlaveAddr??0???//讀寫數據的起始地址
#defineLED???????????(1 << 19)//LED IO口號
#defineLED2??????????(1 << 20)
?
#defineAA????????????(1 << 2)//I2CONSET的各位定義
#defineSI????????????(1 << 3)
#defineSTO???????????(1 << 4)
#defineSTA???????????(1 << 5)
#defineI2EN??????????(1 << 6)
#defineWRMode????????0????//寫模式
#defineRDMode????????1????//讀模式
#definelastbyte??????1????//是否為最后一個字節
?
uint8 I2C_buf1 = 5;
uint8 I2C_buf2 = 0;
//延時函數
void Delay(uint32 dly)
{
?? ?uint32i;
?? ?
?? ?for(; dly> 0; dly--)
?? ?{
??????for(i = 0; i < 50000; i++);
?? ?}
}
//I2C初始化函數
void I2C_Init(uint32 Fi2c)
{
?? ?if(Fi2c> 400000)
?? ?{
????? Fi2c =400000;
?? ?}
?? ?
?? ?PINSEL0 =(PINSEL0 & (~0xf0)) |0x50;//這里是讓P02,P03選第二功能,即I2C總線
?? ?
?? ?I2SCLH =(Fpclk/Fi2c + 1) / 2;//設置I2C速度為400K
?? ?I2SCLL =(Fpclk/Fi2c) / 2;
?? ?
?? ?I2CONCLR =0x2c; //主發送模式對STA,AA,SI清零
?? ?I2CONSET =0x40;//使用I2C總線
?? ?
//?? ?I2CONCLR =STA|AA|SI;
//?? ?I2CONSET =I2EN;
?? ?
}
//I2C bus start function
void I2C_Start(void)
{
?? ?I2CONSET =STA;//set start condition
???do{}while(0x08 != I2STAT);//A start condition hasbeen transmitted
?? ?I2CONCLR =STA;//clear start condition
}
//I2C bus stop function
void I2C_Stop(void)
{
?? ?I2CONSET =STO; //set STO condition
?? ?I2CONCLR =SI; //clear SI condition
}
//write byte function
void WriteByte(uint8 data)
{
?? ?I2DAT =data;// data transmitted
?? ?I2CONCLR =SI;//clear SI condition
}
//wirte I2C bus address mode is RDMode or WRMode
void WriteAddr(uint8 Mode)
{
???WriteByte(C02MasterAddr + Mode);//transmite theI2C address and R/W mode
?? ?
???if(Mode)
?? ?{
??????do{}while(0x40 != I2STAT);//SLA+R has beentransmitted;ACK has been recevied
?? ?}
?? ?else
?? ?{
??????do{}while(0x18 != I2STAT);//SLA+W has beentransmitted;ACK has been received
?? ?}
}
//write data to I2C
void WriteData(uint8 data)
{
???WriteByte(data);// data is will transmiteddata
???do{}while(0x28 != I2STAT);//Data byte in I2DAThas been transmitted;ACK has been received
}
//read i2c byte
uint8 ReadByte(uint8 last)
{
???if(last)
?? ?{
??????I2CONCLR = AA;
??????I2CONCLR = SI;
??????
??????do{} while(0x58 != I2STAT);//data byte has beenreceived;NOT ACK has been returned
?? ?}
?? ?else
?? ?{
??????I2CONSET = AA;//transmited ACK
??????I2CONCLR = SI;
??????do{}while(0x50 != I2STAT);//data byte has beenreceived;ACK has been returned.
???}???
?? ?
?? ?returnI2DAT;//return read data
}
//write data into 24c02
void WriteC02(void)
{
//?? ?uint8i;//define unsigned char i
???I2C_Start();
???WriteAddr(WRMode);
???WriteData(C02SlaveAddr);//the data storaddress
?? ?
???WriteData(I2C_buf1);
???I2C_Stop();
}
//read I2C data
void ReadC02(void)
{
//?? ?uint8i;
???I2C_Start();
???WriteAddr(WRMode);
???WriteData(C02SlaveAddr);
???I2C_Stop();
?? ?
???I2C_Start();
???WriteAddr(RDMode);
?? ?
?? ?I2C_buf2 =ReadByte(lastbyte);
?? ?
???I2C_Stop();
}
int main(void)
{
?? ?
?? ?PINSEL0 =0x00000000;
?? ?PINSEL1 =0x00000000;
?? ?
?? ?IODIR =LED|LED2;
?? ?IOSET =LED|LED2;
?? ?
???I2C_Init(400000);
?? ?
???WriteC02();
???Delay(1000);
???ReadC02();
?? ?
?? ?if(I2C_buf2== I2C_buf1)
?? ?{
???while(1){
??????IOCLR = LED;//correct LED1 light
??????Delay(50);
??????IOSET = LED;
??????Delay(50);
??????}
?? ?}
?? ?else
?? ?{
??????while(1)
??????{
?????????IOCLR = LED2;//error LED2 light
?????????Delay(50);
?????????IOSET = LED2;
?????????Delay(50);
??????}
?? ?}
?? ?
?? ?
return 0;???
}
轉載于:https://www.cnblogs.com/hnrainll/archive/2010/11/14/1877232.html
總結
以上是生活随笔為你收集整理的LPC2103开发板I2C测试试验的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue路由中设置linkActiveCl
- 下一篇: 枪械上的导气式原理到底是个啥