#include //89s52頭文件#define io_74hc165_SH_LD P1_0 //硬件設(shè)置#define io_74hc165_CLK P1_1#define io_74hc165_SDA P1_2#define io_74hc164_SCK P1_3#define io_74hc164_SDA P1_4#ifndef RW_DEFINED#define RW_DEFINEDvoid _snop_(){}#endif//Crystal at 12.0MHz 1MIPS#define I2C_SDA P3_2 // 將p3.2口模擬數(shù)據(jù)口,必須要這樣。中斷接受數(shù)據(jù)#ifdef I2C_SCL#else#define I2C_SCL P1_6 // 將p1.1口模擬時鐘口,默認#endif#ifdef SlaveAddress#else#define SlaveAddress 0x02 //地址#endif#ifdef MasterAddress#else#define MasterAddress 0x01 //主機地址#endif#define delayNOP(); {_snop_();_snop_();_snop_();_snop_();};unsigned char s_control;unsigned char s_note;unsigned char s_svel;bit nm;bit SystemError; // 從機錯誤標志位//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: iic_start()// 函數(shù)功能: 啟動I2C總線子程序//--------------------------------------------------------------------------------------------------void iic_start(void){ EA=0; //時鐘保持高,數(shù)據(jù)線從高到低一次跳變,I2C通信開始I2C_SDA = 1;I2C_SCL = 1;delayNOP(); // 延時5usI2C_SDA = 0;delayNOP();I2C_SCL = 0;}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: iic_stop()// 函數(shù)功能: 停止I2C總線數(shù)據(jù)傳送子程序//--------------------------------------------------------------------------------------------------void iic_stop(void){I2C_SDA = 0; //時鐘保持高,數(shù)據(jù)線從低到高一次跳變,I2C通信停止I2C_SCL = 1;delayNOP();I2C_SDA = 1;delayNOP();I2C_SCL = 0;EA=1;}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: slave_ACK// 函數(shù)功能: 從機發(fā)送應(yīng)答位子程序//--------------------------------------------------------------------------------------------------void slave_ACK(void){I2C_SDA = 0;I2C_SCL = 1;delayNOP();I2C_SDA = 1;I2C_SCL = 0;}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: slave_NOACK// 函數(shù)功能: 從機發(fā)送非應(yīng)答位子程序,迫使數(shù)據(jù)傳輸過程結(jié)束//--------------------------------------------------------------------------------------------------void slave_NOACK(void){I2C_SDA = 1;I2C_SCL = 1;delayNOP();I2C_SDA = 0;I2C_SCL = 0;}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: check_ACK// 函數(shù)功能: 主機應(yīng)答位檢查子程序,迫使數(shù)據(jù)傳輸過程結(jié)束//--------------------------------------------------------------------------------------------------void check_ACK(void){ I2C_SDA = 1; // 將p1.0設(shè)置成輸入,必須先向端口寫1I2C_SCL = 1;F0 = 0;if(I2C_SDA == 1) // 若I2C_SDA=1表明非應(yīng)答,置位非應(yīng)答標志F0F0 = 1;I2C_SCL = 0;}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: IICSendByte// 入口參數(shù): ch// 函數(shù)功能: 發(fā)送一個字節(jié)//--------------------------------------------------------------------------------------------------void IICSendByte(unsigned char ch){unsigned char idata n=8; // 向I2C_SDA上發(fā)送一位數(shù)據(jù)字節(jié),共八位while(n--){if((ch&0x80) == 0x80) // 若要發(fā)送的數(shù)據(jù)最高位為1則發(fā)送位1{I2C_SDA = 1; // 傳送位1I2C_SCL = 1;delayNOP();I2C_SDA = 0;I2C_SCL = 0;}else{I2C_SDA = 0; // 否則傳送位0I2C_SCL = 1;delayNOP();I2C_SCL = 0;}ch = ch<<1; // 數(shù)據(jù)左移一位}}//--------------------------------------------------------------------------------------------------// 函數(shù)名稱: IICreceiveByte// 返回接收的數(shù)據(jù)// 函數(shù)功能: 接收一字節(jié)子程序//--------------------------------------------------------------------------------------------------unsigned char IICreceiveByte(void){unsigned char idata n=8; // 從I2C_SDA線上讀取一上數(shù)據(jù)字節(jié),共八位unsigned char tdata=0;while(n--){I2C_SDA = 1;I2C_SCL = 1;tdata = tdata<<1; // 左移一位,或_crol_(temp,1)if(I2C_SDA == 1)tdata = tdata|0x01; // 若接收到的位為1,則數(shù)據(jù)的最后一位置1elsetdata = tdata&0xfe; // 否則數(shù)據(jù)的最后一位置0I2C_SCL=0;}return(tdata);}bit IICwaitACK(){ //10us不屬于超時unsigned char i=0;bit j=0;I2C_SDA=1;//輸入狀態(tài)while (i!=10){if (I2C_SCL==1) //先SCL=1 SDA=0后SCL=0 SDA=1{j=1;break;}i++;}return j;}
評論