日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀] I^2C總線的SDA與CLK都是線與關(guān)系.為了避免混亂I^2C總線要通過(guò)總線仲裁決定總線由哪一臺(tái)主機(jī)控制I^2C總線的數(shù)據(jù)位的有效性:時(shí)鐘信號(hào)為高電平期間 數(shù)據(jù)線上的數(shù)據(jù)必須保持穩(wěn)定,只有在時(shí)鐘信號(hào)為低電平時(shí), 數(shù)據(jù)線上的

I^2C總線的SDA與CLK都是線與關(guān)系.
為了避免混亂I^2C總線要通過(guò)總線仲裁決定總線由哪一臺(tái)主機(jī)控制
I^2C總線的數(shù)據(jù)位的有效性:時(shí)鐘信號(hào)為高電平期間
數(shù)據(jù)線上的數(shù)據(jù)必須保持穩(wěn)定,只有在時(shí)鐘信號(hào)為低電平時(shí),
數(shù)據(jù)線上的數(shù)據(jù)才可以變化.
圖片
I^2C總線的起始信號(hào)與終止信號(hào),每一字節(jié)必須是8位長(zhǎng)度
發(fā)完還有一位應(yīng)答位,所以一共會(huì)有9位.
在編寫(xiě)程序時(shí),只要嚴(yán)格按照時(shí)序圖來(lái),萬(wàn)事OK,下面是我寫(xiě)的一示例程序,功能是先向2402芯片寫(xiě)入一字節(jié)數(shù)據(jù),然后再將其讀出,顯示到數(shù)碼管

#include#defineuintunsignedint#defineucharunsignedcharsbitscl=P1^6;sbitsda=P1^7;ucharcodetable[]={0x7e,0x0c,0xb6,0x9e,0xcc,0xda,0xfa,0x0e,0xfe,0xde};sbitsda0=P1^0;sbitclk=P1^1;sbitdig1=P1^2;sbitdig2=P1^3;sbitdig3=P1^4;sbitdig4=P1^5;voiddelay(uint);/**初始化IIC,將i^2C總線的電平拉高**/voidinit(){sda=1;scl=1;delay(1);}/**向IIC寫(xiě)入一個(gè)字節(jié)**/voidwrite_byte(ucharb){uchari,temp;temp=b;scl=0;for(i=0;i<8;i++){temp<<=1;sda=CY;delay(1);scl=1;delay(1);scl=0;}delay(1);scl=0;sda=1;}/**從IIC讀出一字節(jié)數(shù)據(jù)**/ucharread_byte(){uchari,temp;scl=0;sda=1;delay(1);for(i=0;i<8;i++){delay(1);temp<<=1;scl=1;delay(1);temp|=sda;delay(1);scl=0;}delay(1);scl=0;sda=1;returntemp;}/**IIC的開(kāi)始信號(hào)**/voidstart(){sda=1;delay(1);scl=1;delay(1);sda=0;delay(1);sda=1;scl=0;}/**IIC的停止信號(hào)**/voidstop(){sda=0;delay(1);scl=1;delay(1);sda=1;scl=0;delay(1);}/**IIC的應(yīng)答函數(shù)**/voidresponse(){uchartemp=0;sda=1;scl=0;delay(1);while(sda&&temp<250)temp++;scl=1;delay(1);scl=0;delay(1);sda=1;}//延遲函數(shù)voiddelay(uintu){uintx,y;for(x=u;x>0;x--)for(y=110;y>0;y--);}/**向數(shù)據(jù)碼管寫(xiě)入一個(gè)字節(jié)數(shù)據(jù)**/voidwrite(ucharu){uchari;for(i=0;i<8;i++){clk=1;sda0=u&0x80;clk=0;u=u<<1;}}/**顯示一個(gè)3位的整數(shù)**/voiddisplay(ucharnum){ucharbai,shi,ge;bai=num/100;shi=num%100/10;ge=num%10;/*顯示個(gè)位*/dig4=0;write(table[ge]);delay(10);dig4=1;/*顯示十位*/dig3=0;write(table[shi]);delay(10);dig3=1;/*顯示百位*/dig2=0;write(table[bai]);delay(10);dig2=1;}/**向2402芯片指定地址寫(xiě)入一字節(jié)數(shù)據(jù)**/voidwrite2402(ucharadd,ucharu){start();write_byte(0xa0);response();write_byte(add);response();write_byte(u);response();stop();}/**從2402芯片指定地址讀出一字節(jié)數(shù)據(jù)**/ucharread2402(ucharadd){ucharresult;start();write_byte(0xa0);response();write_byte(add);response();start();write_byte(0xa1);response();result=read_byte();stop();returnresult;}voidmain(){ucharresult;init();write2402(5,159);delay(20);result=read2402(5);while(1)display(result);}


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉