I2C
Revision 0:b88f2b54dc27, committed 2017-10-14
- Comitter:
- accelerator225
- Date:
- Sat Oct 14 13:57:22 2017 +0000
- Commit message:
- I2C
Changed in this revision
I2C.c | Show annotated file Show diff for this revision Revisions of this file |
I2C.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b88f2b54dc27 I2C.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C.c Sat Oct 14 13:57:22 2017 +0000 @@ -0,0 +1,210 @@ +#include "I2C.h" +#include"mbed.h" + +void Delay(u32 count){while (count--);} +void SDA_IN(){sda.input();} +void SDA_OUT(){sda.output();} +void IIC_SCL(int a = 0){scl.write(a);} +void IIC_SDA(int a = 0){sda.write(a);} +int READ_SDA(){return sda.read();} +void IIC_Init() +{ + SDA.mode(OpenDrain); + SCL.mode(OpenDrain); + SDA_OUT(); + IIC_SDA(1); + IIC_SCL(1); +} +void IIC_Start(void) +{ + SDA_OUT(); //sdaÏßÊä³ö + IIC_SDA(); + IIC_SCL(); + + Delay(5); + IIC_SDA(0);//START:when CLK is high,DATA change form high to low + + Delay(5); + IIC_SCL(0);//ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: void IIC_Stop(void) +*¹¦¡¡¡¡ÄÜ: //²úÉúIICÍ£Ö¹ÐźŠ+*******************************************************************************/ +void IIC_Stop(void) +{ + SDA_OUT();//sdaÏßÊä³ö + IIC_SCL(0); + IIC_SDA(0);//STOP:when CLK is high DATA change form low to high + + Delay(5); + IIC_SCL(1); + IIC_SDA(1);//·¢ËÍI2C×ÜÏß½áÊøÐźŠ+ + Delay(5); +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: unsigned char IIC_Wait_Ack(void) +*¹¦¡¡¡¡ÄÜ: µÈ´ýÓ¦´ðÐźŵ½À´ +//·µ»ØÖµ£º1£¬½ÓÊÕÓ¦´ðʧ°Ü +// 0£¬½ÓÊÕÓ¦´ð³É¹¦ +*******************************************************************************/ +unsigned char IIC_Wait_Ack(void) +{ + unsigned char ucErrTime=0; + SDA_IN(); //SDAÉèÖÃΪÊäÈë + IIC_SDA(1); + Delay(5); + while(READ_SDA) + { + ucErrTime++; + if(ucErrTime>50) + { + IIC_Stop(); + return 1; + } + Delay(5); + } + IIC_SCL(1); + Delay(5); + IIC_SCL(0);//ʱÖÓÊä³ö0 + return 0; +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: void IIC_Ack(void) +*¹¦¡¡¡¡ÄÜ: ²úÉúACKÓ¦´ð +*******************************************************************************/ +void IIC_Ack(void) +{ + IIC_SCL(0); + SDA_OUT(); + IIC_SDA(0); + Delay(5); + IIC_SCL(1); + Delay(5); + IIC_SCL(0); +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: void IIC_NAck(void) +*¹¦¡¡¡¡ÄÜ: ²úÉúNACKÓ¦´ð +*******************************************************************************/ +void IIC_NAck(void) +{ + IIC_SCL(0); + SDA_OUT(); + IIC_SDA(0); + + Delay(5); + IIC_SCL(1); + Delay(5); + IIC_SCL(1); +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: void IIC_Send_Byte(unsigned char txd) +*¹¦¡¡¡¡ÄÜ: IIC·¢ËÍÒ»¸ö×Ö½Ú +*******************************************************************************/ +void IIC_Send_Byte(unsigned char txd) +{ + unsigned char t; + SDA_OUT(); + IIC_SCL(0);//ÀµÍʱÖÓ¿ªÊ¼Êý¾Ý´«Êä + for(t=0;t<8;t++) + { + IIC_SDA=(txd&0x80)>>7; + txd<<=1; + + Delay(2); + IIC_SCL(1); + Delay(5); + IIC_SCL(0); + Delay(3); + } +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: unsigned char IIC_Read_Byte(unsigned char ack) +*¹¦¡¡¡¡ÄÜ: //¶Á1¸ö×Ö½Ú£¬ack=1ʱ£¬·¢ËÍACK£¬ack=0£¬·¢ËÍnACK +*******************************************************************************/ +unsigned char IIC_Read_Byte(unsigned char ack) +{ + unsigned char i,receive=0; + SDA_IN();//SDAÉèÖÃΪÊäÈë + for(i=0;i<8;i++ ) + { + IIC_SCL(0); + + Delay(5); + IIC_SCL(1); + receive<<=1; + if(READ_SDA)receive++; + + Delay(5); + } + if (ack) + IIC_Ack(); //·¢ËÍACK + else + IIC_NAck();//·¢ËÍnACK + return receive; +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: unsigned char IICreadBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char *data) +*¹¦¡¡¡¡ÄÜ: ¶ÁÈ¡Ö¸¶¨É豸 Ö¸¶¨¼Ä´æÆ÷µÄ length¸öÖµ +ÊäÈë dev Ä¿±êÉ豸µØÖ· + reg ¼Ä´æÆ÷µØÖ· + length Òª¶ÁµÄ×Ö½ÚÊý + *data ¶Á³öµÄÊý¾Ý½«Òª´æ·ÅµÄÖ¸Õë +·µ»Ø ¶Á³öÀ´µÄ×Ö½ÚÊýÁ¿ +*******************************************************************************/ +unsigned char IICreadBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char *data){ + unsigned char count = 0; + + IIC_Start(); + IIC_Send_Byte(dev<<1); //·¢ËÍдÃüÁî + IIC_Wait_Ack(); + IIC_Send_Byte(reg); //·¢Ë͵ØÖ· + IIC_Wait_Ack(); + IIC_Start(); + IIC_Send_Byte((dev<<1)+1); //½øÈë½ÓÊÕģʽ + IIC_Wait_Ack(); + + for(count=0;count<length;count++){ + + if(count!=length-1)data[count]=IIC_Read_Byte(1); //´øACKµÄ¶ÁÊý¾Ý + else data[count]=IIC_Read_Byte(0); //×îºóÒ»¸ö×Ö½ÚNACK + } + IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ + return count; +} + +/**************************ʵÏÖº¯Êý******************************************** +*º¯ÊýÔÐÍ: unsigned char IICwriteBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char* data) +*¹¦¡¡¡¡ÄÜ: ½«¶à¸ö×Ö½ÚдÈëÖ¸¶¨É豸 Ö¸¶¨¼Ä´æÆ÷ +ÊäÈë dev Ä¿±êÉ豸µØÖ· + reg ¼Ä´æÆ÷µØÖ· + length ҪдµÄ×Ö½ÚÊý + *data ½«ÒªÐ´µÄÊý¾ÝµÄÊ×µØÖ· +·µ»Ø ·µ»ØÊÇ·ñ³É¹¦ +*******************************************************************************/ +unsigned char IICwriteBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char* data){ + + unsigned char count = 0; + IIC_Start(); + IIC_Send_Byte(dev<<1); //·¢ËÍдÃüÁî + IIC_Wait_Ack(); + IIC_Send_Byte(reg); //·¢Ë͵ØÖ· + IIC_Wait_Ack(); + for(count=0;count<length;count++){ + IIC_Send_Byte(data[count]); + IIC_Wait_Ack(); + } + IIC_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ + + return 1; //status == 0; + +} \ No newline at end of file
diff -r 000000000000 -r b88f2b54dc27 I2C.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2C.h Sat Oct 14 13:57:22 2017 +0000 @@ -0,0 +1,32 @@ +#ifndef __IOI2C_H +#define __IOI2C_H + +void SDA_IN(); +void SDA_OUT(); +void IIC_SCL(int a);//scl output 1 +void IIC_SDA(int a);//sda output 1 +int READ_SDA(); +void IIC_Init(); +void IIC_Start(void); +void IIC_Stop(void); +void IIC_Send_Byte(unsigned char txd); +unsigned char IIC_Read_Byte(unsigned char ack);/ +unsigned char IIC_Wait_Ack(void); +void IIC_Ack(void); +void IIC_NAck(void); + +void IIC_Write_One_Byte(unsigned char daddr,unsigned char addr,unsigned char data); +unsigned char IIC_Read_One_Byte(unsigned char daddr,unsigned char addr); +unsigned char I2C_Readkey(unsigned char I2C_Addr); + +unsigned char I2C_ReadOneByte(unsigned char I2C_Addr,unsigned char addr); +unsigned char IICwriteByte(unsigned char dev, unsigned char reg, unsigned char data); +unsigned char IICwriteCmd(unsigned char dev, unsigned char cmd); +unsigned char IICwriteBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char* data); +unsigned char IICwriteBits(unsigned char dev,unsigned char reg,unsigned char bitStart,unsigned char length,unsigned char data); +unsigned char IICwriteBit(unsigned char dev,unsigned char reg,unsigned char bitNum,unsigned char data); +unsigned char IICreadBytes(unsigned char dev, unsigned char reg, unsigned char length, unsigned char *data); + +#endif + +//------------------End of File----------------------------