https://m.toutiao.com/is/dtQX1Pt/?=I2c通訊介紹及代碼演示
I2c通訊介紹及代碼演示 1.啟動和停止波形圖 2.程序 就SDA有區(qū)別 啟動 | 停止 | Sda=1 | Sda=0 | Scl=1 | Scl=1 | Sda=1 | Sda=1 | Scl=0 | Scl=0 |
/* ------------------------------------------------ 啟動IIC總線
------------------------------------------------*/ void Start(void) { Sda=1; _nop_(); Scl=1; _nop_(); Sda=0; _nop_(); Scl=0; } /* ------------------------------------------------ 停止IIC總線
------------------------------------------------*/ void Stop(void) { Sda=0; _nop_(); Scl=1; _nop_(); Sda=1; _nop_(); Scl=0; } 3.應(yīng)答與非應(yīng)答 應(yīng)答 | 非應(yīng)答 | Sda=0 | Sda=1 | Scl=1 | Scl=1 | Scl=1 | Scl=0 |
/* ------------------------------------------------ 應(yīng)答IIC總線
------------------------------------------------*/ void Ack(void) { Sda=0; _nop_(); Scl=1; _nop_(); Scl=0; _nop_(); } /* ------------------------------------------------ 非應(yīng)答IIC總線
------------------------------------------------*/ void NoAck(void) { Sda=1; _nop_(); Scl=1; _nop_(); Scl=0; _nop_(); } 4.發(fā)送與接收 發(fā)送數(shù)據(jù) | 接收數(shù)據(jù) | Scl=0 | Sda=1(執(zhí)行1次) | 數(shù)據(jù) | Scl=0 | Scl=1(執(zhí)行1次) | Scl=1 | | 數(shù)據(jù) |
/* ------------------------------------------------ 發(fā)送一個字節(jié)
------------------------------------------------*/ void Send(unsigned char Data) { unsigned char BitCounter=8; unsigned char temp; do { temp=Data; Scl=0; _nop_(); if((temp&0x80)==0x80) Sda=1; else Sda=0; Scl=1; temp=Data<<1; Data=temp; BitCounter--; } while(BitCounter); Scl=0; } /* ------------------------------------------------ 讀入一個字節(jié)并返回
------------------------------------------------*/ unsigned char Read(void) { unsigned char temp=0; unsigned char temp1=0; unsigned char BitCounter=8; Sda=1; do { Scl=0; _nop_(); Scl=1; _nop_(); if(Sda) temp=temp|0x01; else temp=temp&0xfe; if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; } while(BitCounter); return(temp); } 5.i2C總體程序 sbit Sda=P2^0; //定義總線連接端口 sbit Scl=P2^1; /* ------------------------------------------------ 啟動IIC總線
------------------------------------------------*/ void Start(void) { Sda=1; _nop_(); Scl=1; _nop_(); Sda=0; _nop_(); Scl=0; } /* ------------------------------------------------ 停止IIC總線
------------------------------------------------*/ void Stop(void) { Sda=0; _nop_(); Scl=1; _nop_(); Sda=1; _nop_(); Scl=0; } /* ------------------------------------------------ 應(yīng)答IIC總線
------------------------------------------------*/ void Ack(void) { Sda=0; _nop_(); Scl=1; _nop_(); Scl=0; _nop_(); } /* ------------------------------------------------ 非應(yīng)答IIC總線
------------------------------------------------*/ void NoAck(void) { Sda=1; _nop_(); Scl=1; _nop_(); Scl=0; _nop_(); } /* ------------------------------------------------ 發(fā)送一個字節(jié)
------------------------------------------------*/ void Send(unsigned char Data) { unsigned char BitCounter=8; unsigned char temp; do { temp=Data; Scl=0; _nop_(); if((temp&0x80)==0x80) Sda=1; else Sda=0; Scl=1; temp=Data<<1; Data=temp; BitCounter--; } while(BitCounter); Scl=0; } /* ------------------------------------------------ 讀入一個字節(jié)并返回
------------------------------------------------*/ unsigned char Read(void) { unsigned char temp=0; unsigned char temp1=0; unsigned char BitCounter=8; Sda=1; do { Scl=0; _nop_(); Scl=1; _nop_(); if(Sda) temp=temp|0x01; else temp=temp&0xfe; if(BitCounter-1) { temp1=temp<<1; temp=temp1; } BitCounter--; } while(BitCounter); return(temp); } /* ------------------------------------------------ 寫入DA數(shù)模轉(zhuǎn)換值
------------------------------------------------*/ void DAC(unsigned char Data) { Start(); Send(AddWr); //寫入芯片地址 Ack(); Send(0x40); //寫入控制位,使能DAC輸出 Ack(); Send(Data); //寫數(shù)據(jù) Ack(); Stop(); } /* ------------------------------------------------ 讀取AD模數(shù)轉(zhuǎn)換的值,,有返回值
------------------------------------------------*/ unsigned char ReadADC(unsigned char Chl) { unsigned char Data; Start(); //寫入芯片地址 Send(AddWr); Ack(); Send(0x40|Chl);//寫入選擇的通道,,本程序只用單端輸入,差分部分需要自行添加 //Chl的值分別為0,、1,、2、3,,分別代表1-4通道 Ack(); Start(); Send(AddRd); //讀入地址 Ack(); Data=Read(); //讀數(shù)據(jù) Scl=0; NoAck(); Stop(); return Data; //返回值 } 拿去套用就行 i2C總流程 /* ------------------------------------------------ 寫入DA數(shù)模轉(zhuǎn)換值
------------------------------------------------*/ void DAC(unsigned char Data) { Start(); Send(AddWr); //寫入芯片地址 Ack(); Send(0x40); //寫入控制位,使能DAC輸出 Ack(); Send(Data); //寫數(shù)據(jù) Ack(); Stop(); } /* ------------------------------------------------ 讀取AD模數(shù)轉(zhuǎn)換的值,,有返回值
------------------------------------------------*/ unsigned char ReadADC(unsigned char Chl) { unsigned char Data; Start(); //寫入芯片地址 Send(AddWr); Ack(); Send(0x40|Chl);//寫入選擇的通道,,本程序只用單端輸入,差分部分需要自行添加 //Chl的值分別為0,、1,、2、3,,分別代表1-4通道 Ack(); Start(); Send(AddRd); //讀入地址 Ack(); Data=Read(); //讀數(shù)據(jù) Scl=0; NoAck(); Stop(); return Data; //返回值 } 這個是我ad四路掃描程序部分
|