久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2147|回復(fù): 0
收起左側(cè)

單片機(jī)IIC的簡(jiǎn)單應(yīng)用

[復(fù)制鏈接]
ID:665608 發(fā)表于 2020-6-17 20:17 | 顯示全部樓層 |閱讀模式
/*利用IIC總線接口底層驅(qū)動(dòng)代碼,設(shè)計(jì)應(yīng)用程序,實(shí)現(xiàn)下面功能:1.系統(tǒng)開機(jī)后,讀取0x01、0x03和0x05 內(nèi)存單元的數(shù)據(jù),并從左至右顯示在數(shù)碼管上, .數(shù)字之間用“-”分隔,

2.將0x01單元的數(shù)據(jù)加1后,寫回該內(nèi)存單元,使用按鍵s1加1后結(jié)果如大于10,恢復(fù)0.0x03單元的數(shù)據(jù)加2后,寫回該內(nèi)存單元, .加2后結(jié)果如大于20,恢復(fù)0。將0x05單元的數(shù)據(jù)加3后,寫回該內(nèi)存單元,加3后結(jié)果如大于30,恢復(fù)0。*/


/*main.c文件*/
  1. #include "reg51.h"                        
  2. #include "i2c.h"

  3. typedef unsigned int u16;          //對(duì)數(shù)據(jù)類型進(jìn)行聲明定義
  4. typedef unsigned char u8;

  5. void datapros();

  6. sbit LSA=P2^2;
  7. sbit LSB=P2^3;
  8. sbit LSC=P2^4;
  9. sbit k1=P3^1;
  10. sbit k2=P3^3;

  11. u8 display[8],addr,a=0,b=0,c=0;
  12. u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

  13. void delayy(u16 i)
  14. {
  15.         while(i--);

  16. }

  17. void DigDisplay()
  18. {
  19.            u8 i;
  20.         for(i=0;i<8;i++)
  21.         {
  22.                 switch(i)         //位選,選擇點(diǎn)亮的數(shù)碼管,
  23.                 {
  24.                         case(0):
  25.                                 LSA=0;LSB=0;LSC=0; break;//顯示第0位
  26.                         case(1):
  27.                                 LSA=1;LSB=0;LSC=0; break;//顯示第1位
  28.                         case(2):
  29.                                 LSA=0;LSB=1;LSC=0; break;//顯示第2位
  30.                         case(3):
  31.                                 LSA=1;LSB=1;LSC=0; break;//顯示第3位
  32.                         case(4):
  33.                                 LSA=0;LSB=0;LSC=1; break;//顯示第4位
  34.                         case(5):
  35.                                 LSA=1;LSB=0;LSC=1; break;//顯示第5位
  36.                         case(6):
  37.                                 LSA=0;LSB=1;LSC=1; break;//顯示第6位
  38.                         case(7):
  39.                                 LSA=1;LSB=1;LSC=1; break;//顯示第7位        
  40.                 }
  41.                 P0=display[i];
  42.                 delayy(100);        
  43.                 P0=0x00;
  44.         }               
  45. }

  46. void datapros()//數(shù)碼管數(shù)據(jù)的處理
  47. {
  48.         display[0]=smgduan[a%10];
  49.         display[1]=smgduan[a/10];
  50.         display[2]=0x40;
  51.         display[3]=smgduan[b%10];
  52.         display[4]=smgduan[b/10];
  53.         display[5]=0x40;
  54.         display[6]=smgduan[c%10];
  55.         display[7]=smgduan[c/10];                                
  56. }
  57. void ack() //取讀保存在地址內(nèi)的數(shù)據(jù)
  58. {
  59.         a=At24c02Read(0x01);
  60.         b=At24c02Read(0x03);
  61.         c=At24c02Read(0x05);                        
  62. }

  63. void anjian()
  64. {
  65.         if(k1==0)
  66.         {
  67.                 delayy(1000);
  68.                 if(k1==0)
  69.                 {
  70.                         
  71.                         a=At24c02Read(0x01);          //將0X01地址內(nèi)的數(shù)據(jù)賦給A
  72.                         a++;         
  73.                         if(a>10)
  74.                         {
  75.                                 a=0;
  76.                         }
  77.                         At24c02Write(0x01,a);        //將A數(shù)據(jù)寫入0x01的地址中去
  78.                         delayy(1000);
  79.                         
  80.                         b=At24c02Read(0x03);          //將0X03地址內(nèi)的數(shù)據(jù)賦給B
  81.                         b=b+2;         
  82.                         if(b>20)
  83.                         {
  84.                                 b=0;
  85.                         }        
  86.                         At24c02Write(0x03,b);        //將B的數(shù)據(jù)寫入0x03的地址中去
  87.                         delayy(1000);

  88.                         c=At24c02Read(0x05);          //將0X05地址內(nèi)的數(shù)據(jù)賦給C
  89.                         c=c+3;         
  90.                         if(c>30)
  91.                         {
  92.                                 c=0;
  93.                         }
  94.                         At24c02Write(0x05,c);        //將C的數(shù)據(jù)寫入0x05的地址中去
  95.                         delayy(1000);
  96.                 }
  97.                 while(!k1);
  98.         }                                   
  99. }

  100. void an() //將數(shù)據(jù)都至0,并保存起來
  101. {
  102.         if(k2==0)
  103.         {
  104.                 delayy(100);
  105.                 if(k2==0)
  106.                 {
  107.                         while(k2==0)
  108.                         {
  109.                                 datapros();
  110.                         }
  111.                         a=0;
  112.                         At24c02Write(0x01,a);
  113.                         delayy(100);
  114.                         b=0;
  115.                         At24c02Write(0x03,b);
  116.                         delayy(100);
  117.                         c=0;
  118.                         At24c02Write(0x05,c);
  119.                 }
  120.                 while(!k2);
  121.         }
  122. }
  123.         




  124. void main()
  125. {
  126.         ack();//開機(jī)取讀最新保存的數(shù)據(jù)
  127.         while(1)
  128.         {
  129.                 an(); //數(shù)據(jù)至0
  130.                 anjian();        //數(shù)據(jù)運(yùn)行
  131.                 datapros();        //數(shù)碼管顯示數(shù)據(jù)處理
  132.                 DigDisplay();//數(shù)碼管處理
  133.         }
  134. }


  135. /*i2c.c文件*/
  136. #include"i2c.h"

  137. unsigned char I2cSend(unsigned char dat);  //聲明函數(shù)
  138. unsigned char I2cRead();

  139. void Delay10us()//小延遲         
  140. {
  141.         unsigned char a,b;
  142.         for(b=1;b>0;b--)
  143.                 for(a=2;a>0;a--);
  144. }

  145. void delay(int i)           //大延遲
  146. {
  147. while(i--);
  148. }

  149. void I2cStart()        //起始信號(hào)                        
  150. {
  151.         SDA=1;
  152.         Delay10us();
  153.         SCL=1;
  154.         Delay10us();//延遲時(shí)間,使SDA保持高位時(shí)間>4.7us后至低位
  155.         SDA=0;
  156.         Delay10us();//保持處于低位時(shí)間大于4us
  157.         SCL=0;                        
  158.         Delay10us();//起始之后SDA和SC要至0               
  159. }

  160. void I2cStop() //終止信號(hào)
  161. {
  162.         SDA=0;
  163.         Delay10us(); //延遲SDA數(shù)據(jù)線處于低位時(shí)間,使SDA在低位時(shí)間>4us
  164.         SCL=1;                                       
  165.         Delay10us();//延遲時(shí)間,使SCL在高位時(shí)間>4.7us
  166.         SDA=1;
  167.         Delay10us();//結(jié)束之后保持SDA和SCL都為1;表示總線空閑               
  168. }

  169. unsigned char I2cSendByte(unsigned char dat) //發(fā)送字節(jié)
  170. {
  171.         unsigned char a=0,b=0;               
  172.         for(a=0;a<8;a++)//要發(fā)送8位,從最高位開始
  173.         {
  174.                 SDA=dat>>7;         //dat的第一位右移7位,寫入SDA
  175.                 dat=dat<<1;                //dat左移一位把高一位移除,相當(dāng)于dat從左到右依次寫到SDA
  176.                 Delay10us();
  177.                 SCL=1;
  178.                 Delay10us();//延遲時(shí)間,使SCL在高位時(shí)間>4.7us
  179.                 SCL=0;
  180.                 Delay10us();        
  181.         }
  182.         SDA=1;
  183.         Delay10us();        
  184.         SCL=1;
  185.         while(SDA)//等待應(yīng)答,也就是等待從設(shè)備把SDA拉低
  186.         {
  187.                 b++;
  188.                 if(b>200)         //如果超過2000us沒有應(yīng)答發(fā)送失敗,或者為非應(yīng)答,表示接收結(jié)束
  189.                 {
  190.                         SCL=0;
  191.                         Delay10us();        //延時(shí)10us
  192.                         return 0;           //發(fā)送失敗返回0
  193.                 }
  194.         }
  195.         SCL=0;
  196.         Delay10us();         //延時(shí)10us
  197.          return 1;                //發(fā)送成功返回1
  198. }

  199. unsigned char I2cReadByte()         //取讀一個(gè)字節(jié)
  200. {
  201.         unsigned char a=0,dat=0;
  202.         SDA=1;                        //起始和發(fā)送一個(gè)字節(jié)之后SCL都是0
  203.         Delay10us();
  204.         for(a=0;a<8;a++)//接收8個(gè)字節(jié)
  205.         {
  206.                 SCL=1;
  207.                 Delay10us();           //dat左移并上SDA,相當(dāng)于SDA從低位寫入
  208.                 dat<<=1;
  209.                 dat|=SDA;
  210.                 Delay10us();                //延時(shí)10us
  211.                 SCL=0;
  212.                 Delay10us();                //延時(shí)10us
  213.         }
  214.         return dat;                //接收完一個(gè)字節(jié)SCL=0,SDA=1
  215. }

  216. void At24c02Write(unsigned char addr,unsigned char dat)//將數(shù)據(jù)寫入一個(gè)地址中
  217. {
  218.         I2cStart();                        //開始
  219.         I2cSendByte(0xa0);//發(fā)送寫器件地址
  220.         I2cSendByte(addr);//發(fā)送要寫入內(nèi)存地址
  221.         I2cSendByte(dat);        //發(fā)送數(shù)據(jù)
  222.         I2cStop();                   //結(jié)束
  223.         delay(1000);           //等待數(shù)據(jù)穩(wěn)定寫入
  224. }

  225. unsigned char At24c02Read(unsigned char addr)//寫入取讀到的地址
  226. {
  227.         unsigned char num;
  228.         I2cStart();                   //開始
  229.         I2cSendByte(0xa0); //發(fā)送寫器件地址                0xa0為寫入標(biāo)志
  230.         I2cSendByte(addr); //發(fā)送要讀取的地址 ,,先寫入要讀的地址
  231.         I2cStart();
  232.         I2cSendByte(0xa1); //發(fā)送讀器件地址                0xa1為讀標(biāo)志
  233.         num=I2cReadByte(); //讀取數(shù)據(jù)
  234.         I2cStop();                   //結(jié)束
  235.         return num;        
  236. }

  237. /*i2c.h文件*/
  238. #include"i2c.h"

  239. unsigned char I2cSend(unsigned char dat);  //聲明函數(shù)
  240. unsigned char I2cRead();

  241. void Delay10us()//小延遲         
  242. {
  243.         unsigned char a,b;
  244.         for(b=1;b>0;b--)
  245.                 for(a=2;a>0;a--);
  246. }

  247. void delay(int i)           //大延遲
  248. {
  249. while(i--);
  250. }

  251. void I2cStart()        //起始信號(hào)                        
  252. {
  253.         SDA=1;
  254.         Delay10us();
  255.         SCL=1;
  256.         Delay10us();//延遲時(shí)間,使SDA保持高位時(shí)間>4.7us后至低位
  257.         SDA=0;
  258.         Delay10us();//保持處于低位時(shí)間大于4us
  259.         SCL=0;                        
  260.         Delay10us();//起始之后SDA和SC要至0               
  261. }

  262. void I2cStop() //終止信號(hào)
  263. {
  264.         SDA=0;
  265.         Delay10us(); //延遲SDA數(shù)據(jù)線處于低位時(shí)間,使SDA在低位時(shí)間>4us
  266.         SCL=1;                                       
  267.         Delay10us();//延遲時(shí)間,使SCL在高位時(shí)間>4.7us
  268.         SDA=1;
  269.         Delay10us();//結(jié)束之后保持SDA和SCL都為1;表示總線空閑               
  270. }

  271. unsigned char I2cSendByte(unsigned char dat) //發(fā)送字節(jié)
  272. {
  273.         unsigned char a=0,b=0;               
  274.         for(a=0;a<8;a++)//要發(fā)送8位,從最高位開始
  275.         {
  276.                 SDA=dat>>7;         //dat的第一位右移7位,寫入SDA
  277.                 dat=dat<<1;                //dat左移一位把高一位移除,相當(dāng)于dat從左到右依次寫到SDA
  278.                 Delay10us();
  279.                 SCL=1;
  280.                 Delay10us();//延遲時(shí)間,使SCL在高位時(shí)間>4.7us
  281.                 SCL=0;
  282.                 Delay10us();        
  283.         }
  284.         SDA=1;
  285.         Delay10us();        
  286.         SCL=1;
  287.         while(SDA)//等待應(yīng)答,也就是等待從設(shè)備把SDA拉低
  288.         {
  289.                 b++;
  290.                 if(b>200)         //如果超過2000us沒有應(yīng)答發(fā)送失敗,或者為非應(yīng)答,表示接收結(jié)束
  291.                 {
  292.                         SCL=0;
  293.                         Delay10us();        //延時(shí)10us
  294.                         return 0;           //發(fā)送失敗返回0
  295.                 }
  296.         }
  297.         SCL=0;
  298.         Delay10us();         //延時(shí)10us
  299.          return 1;                //發(fā)送成功返回1
  300. }

  301. unsigned char I2cReadByte()         //取讀一個(gè)字節(jié)
  302. {
  303.         unsigned char a=0,dat=0;
  304.         SDA=1;                        //起始和發(fā)送一個(gè)字節(jié)之后SCL都是0
  305.         Delay10us();
  306.         for(a=0;a<8;a++)//接收8個(gè)字節(jié)
  307.         {
  308.                 SCL=1;
  309.                 Delay10us();           //dat左移并上SDA,相當(dāng)于SDA從低位寫入
  310.                 dat<<=1;
  311.                 dat|=SDA;
  312.                 Delay10us();                //延時(shí)10us
  313.                 SCL=0;
  314.                 Delay10us();                //延時(shí)10us
  315.         }
  316.         return dat;                //接收完一個(gè)字節(jié)SCL=0,SDA=1
  317. }

  318. void At24c02Write(unsigned char addr,unsigned char dat)//將數(shù)據(jù)寫入一個(gè)地址中
  319. {
  320.         I2cStart();                        //開始
  321.         I2cSendByte(0xa0);//發(fā)送寫器件地址
  322.         I2cSendByte(addr);//發(fā)送要寫入內(nèi)存地址
  323.         I2cSendByte(dat);        //發(fā)送數(shù)據(jù)
  324.         I2cStop();                   //結(jié)束
  325.         delay(1000);           //等待數(shù)據(jù)穩(wěn)定寫入
  326. }

  327. unsigned char At24c02Read(unsigned char addr)//寫入取讀到的地址
  328. {
  329.         unsigned char num;
  330.         I2cStart();                   //開始
  331.         I2cSendByte(0xa0); //發(fā)送寫器件地址                0xa0為寫入標(biāo)志
  332.         I2cSendByte(addr); //發(fā)送要讀取的地址 ,,先寫入要讀的地址
  333.         I2cStart();
  334.         I2cSendByte(0xa1); //發(fā)送讀器件地址                0xa1為讀標(biāo)志
  335.         num=I2cReadByte(); //讀取數(shù)據(jù)
  336.         I2cStop();                   //結(jié)束
  337.         return num;        
  338. }

復(fù)制代碼

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 精品小视频| 鲁大师一区影视 | 在线综合视频 | 国产精品免费福利 | 欧美精品久久久久 | 国户精品久久久久久久久久久不卡 | 中文在线一区 | 久久久www成人免费无遮挡大片 | 99精品视频一区二区三区 | 国产成人黄色 | 久久99精品国产99久久6男男 | 欧美日韩精品一区二区三区视频 | 99福利视频 | 尤物在线视频 | 国产精品久久久久久久久久不蜜臀 | 国产三级精品三级在线观看四季网 | 久久免费大片 | 狠狠草视频 | 美国av毛片 | a在线观看| 久久新视频 | 一区二区精品 | 日韩aⅴ视频 | 久久不射电影网 | 免费在线播放黄色 | 欧美一级久久 | 韩日一区 | 国产福利91精品 | 天天操天天射天天 | 色综合天天天天做夜夜夜夜做 | 欧美日韩一二三区 | 狠狠躁夜夜躁人人爽天天高潮 | 美女久久 | 久久毛片| 久久精品国产99国产精品 | 免费午夜视频 | 欧洲免费视频 | 色网在线播放 | 久久久久一区二区三区 | 九九热九九 | 精品亚洲一区二区三区 |