IIC Sample Code
#include <reg52.h>#include <math.h>#define DELAY_TIME 600#define TRUE 1#define FALSE 0#define uchar unsigned char#define uint unsigned int //--------------define IIC SCL,SDA port------------------------------ sbit SCL = P1 ^ 7; sbit SDA = P1 ^ 6; //-----------------define Max7219 port---------------------------sbit Max7219_pinCLK = P2 ^ 2; sbit Max7219_pinCS = P2 ^ 1;sbit Max7219_pinDIN = P2 ^ 0;//-------------delay time_us-------------------------------void DELAY(uint t) { while (t != 0) t--;}//-------------IIC START CONDITION-------------------------------void I2C_Start(void) { SDA = 1; //SDA output high DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); //SCL output high SDA = 0; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);} //-------------IIC STOP CONDITION-------------------------------void I2C_Stop(void) { SDA = 0; //SDA OUTPUT LOW DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SDA = 1; DELAY(DELAY_TIME); SCL = 0; //SCL OUTPUT LOW DELAY(DELAY_TIME);}//-------------IIC SEND DATA "0"-------------------------------void SEND_0(void) { SDA = 0; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);}//-------------IIC SEND DATA "1"-------------------------------void SEND_1(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); SCL = 0; DELAY(DELAY_TIME);}//-------------Check SLAVEs Acknowledge -------------------------------bit Check_Acknowledge(void) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME / 2); F0 = SDA; DELAY(DELAY_TIME / 2); SCL = 0; DELAY(DELAY_TIME); if (F0 == 1) return FALSE; return TRUE;}//-------------Write One Byte of Data -------------------------------void WriteI2CByte(uchar b) reentrant { char i; for (i = 0; i < 8; i++) if ((b << i) & 0x80) SEND_1(); else SEND_0();}//-------------Read One Byte of Data -------------------------------uchar ReadI2CByte(void) reentrant { char b = 0, i; for (i = 0; i < 8; i++) { SDA = 1; DELAY(DELAY_TIME); SCL = 1; DELAY(DELAY_TIME); //DELAY(10); F0 = SDA; DELAY(DELAY_TIME); //DELAY(10); SCL = 0; if (F0 == 1) { b = b << 1; b = b | 0x01; } else b = b << 1; } return b;}//-------------write One Byte of Data,Data from MASTER to the SLAVER -------------------------------//-------------SLAVER address bit:01101101-------------------------------void Write_One_Byte(uchar addr, uchar thedata) //Write "thedata" to the SLAVERs address of "addr"{ bit acktemp = 1; I2C_Start(); //IIC START WriteI2CByte(0xDA); //IIC WRITE operation,SLAVER address bit:01101010 acktemp = Check_Acknowledge(); //check the SLAVER WriteI2CByte(addr); /*address*/ acktemp = Check_Acknowledge(); WriteI2CByte(thedata); /*thedata*/ acktemp = Check_Acknowledge(); I2C_Stop(); //IIC STOP }//-------------Reaed One Byte of Data,Data from SLAVER to the MASTER -------------------------------uchar Read_One_Byte(uchar addr) { bit acktemp = 1; uchar mydata; I2C_Start(); WriteI2CByte(0xDA); acktemp = Check_Acknowledge(); WriteI2CByte(addr); acktemp = Check_Acknowledge(); I2C_Start(); WriteI2CByte(0xDB); //IIC READ operation acktemp = Check_Acknowledge(); mydata = ReadI2CByte(); acktemp = Check_Acknowledge(); I2C_Stop(); return mydata;}//-------------Delay_ms -------------------------------void Delay_xms(uint x) { uint i, j; for (i = 0; i < x; i++) for (j = 0; j < 112; j++) ;}//-------------Write One Byte to the Max7219-------------void Write_Max7219_byte(uchar DATA) { uchar i; Max7219_pinCS = 0; //CS low effect for (i = 8; i >= 1; i--) { Max7219_pinCLK = 0; Max7219_pinDIN = DATA & 0x80; DATA = DATA << 1; Max7219_pinCLK = 1; //when pinCLK is high send the Data }}//-------------decide which address shows the Data-------------void Write_Max7219(uchar address,uchar dat) { Max7219_pinCS = 0; Write_Max7219_byte(address); Write_Max7219_byte(dat); Max7219_pinCS = 1;}//-------------MAX_7219 Initialization-------------void Init_MAX7219(void) { Write_Max7219(0x09, 0xff); //Decoded mode:BCD code Write_Max7219(0x0a, 0x03); //Brightness Write_Max7219(0x0b, 0x07); //Scan limit:8 digital display Write_Max7219(0x0c, 0x01); //Power-Down modes :0,Normal code:1 Write_Max7219(0x0f, 0x01); //Display test:1;Test end,normal display:0}void main(void){ uchar yali1, yali2, yali3,wendu1,wendu2; uchar temp_a5; long int ad,temp; long float pas; uchar dis[8]; Init_MAX7219(); Delay_xms(1000); Write_Max7219(0x0f, 0x00); while (1) { temp_a5 = Read_One_Byte(0xA5);//Read ASIC Sys_config temp_a5 = temp_a5 & 0xFD; //(Raw_data_on: 0: output calibrated data,namly the value in 0x06-0x0a) Write_One_Byte(0xA5, temp_a5); //Set ADC output calibrated Data Write_One_Byte(0x30, 0x0A); //indicate a combined conversion (once temperature conversion immediately followed by once sensor signal conversion)(wire measure order in 0x30,000:single temp. measure;001:single pressure. measure;010:combination:single pressure and temp. measurement;011:sleep mode(combined mode measurement at certain time intervals)) while ((Read_One_Byte(0x30) & 0x08) > 0); //Judge whether Data collection is over // -------------READ ADC output Data of Pressure ------------- yali1 = Read_One_Byte(0x06); yali2 = Read_One_Byte(0x07); yali3 = Read_One_Byte(0x08); ad = yali1 * 65536 + yali2 * 256 + yali3; // -------------READ ADC output Data of Temperature ------------- wendu1= Read_One_Byte(0x09); wendu2= Read_One_Byte(0x0a); temp=wendu1*256+wendu2; /*Conversion, the following is the conversion formula of 100*/ if (ad > 8388608) // >8388606 as negative value, handle at display end { pas = (ad - 16777216)/64/1000; //unit is kpa } else { pas = ad/64/1000; //unit is kpa } if (pas < 0) pas = fabs(pas);/*Display program with Max7219*/ dis[0] = (long int)pas / 10000000; dis[1] = (long int)pas % 10000000 / 1000000; dis[2] = (long int)pas % 1000000 / 100000; dis[3] = (long int)pas % 100000 / 10000; dis[4] = (long int)pas % 10000 / 1000; dis[5] = (long int)pas % 1000 / 100; dis[6] = (long int)pas % 100 / 10; dis[7] = (long int)pas % 10; Write_Max7219(8, dis[0]); Write_Max7219(7, dis[1]); Write_Max7219(6, dis[2]); Write_Max7219(5, dis[3]); Write_Max7219(4, dis[4]); Write_Max7219(3, dis[5]); Write_Max7219(2, dis[6]); Write_Max7219(1, dis[7]); Delay_xms(100); //delay 100ms }}