找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1608|回复: 0
打印 上一主题 下一主题
收起左侧

单片机IIC的简单应用

[复制链接]
跳转到指定楼层
楼主
ID:665608 发表于 2020-6-17 20:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*利用IIC总线接口底层驱动代码,设计应用程序,实现下面功能:1.系统开机后,读取0x01、0x03和0x05 内存单元的数据,并从左至右显示在数码管上, .数字之间用“-”分隔,

2.将0x01单元的数据加1后,写回该内存单元,使用按键s1加1后结果如大于10,恢复0.0x03单元的数据加2后,写回该内存单元, .加2后结果如大于20,恢复0。将0x05单元的数据加3后,写回该内存单元,加3后结果如大于30,恢复0。*/


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

  3. typedef unsigned int u16;          //对数据类型进行声明定义
  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)         //位选,选择点亮的数码管,
  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()//数码管数据的处理
  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() //取读保存在地址内的数据
  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地址内的数据赋给A
  72.                         a++;         
  73.                         if(a>10)
  74.                         {
  75.                                 a=0;
  76.                         }
  77.                         At24c02Write(0x01,a);        //将A数据写入0x01的地址中去
  78.                         delayy(1000);
  79.                         
  80.                         b=At24c02Read(0x03);          //将0X03地址内的数据赋给B
  81.                         b=b+2;         
  82.                         if(b>20)
  83.                         {
  84.                                 b=0;
  85.                         }        
  86.                         At24c02Write(0x03,b);        //将B的数据写入0x03的地址中去
  87.                         delayy(1000);

  88.                         c=At24c02Read(0x05);          //将0X05地址内的数据赋给C
  89.                         c=c+3;         
  90.                         if(c>30)
  91.                         {
  92.                                 c=0;
  93.                         }
  94.                         At24c02Write(0x05,c);        //将C的数据写入0x05的地址中去
  95.                         delayy(1000);
  96.                 }
  97.                 while(!k1);
  98.         }                                   
  99. }

  100. void an() //将数据都至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();//开机取读最新保存的数据
  127.         while(1)
  128.         {
  129.                 an(); //数据至0
  130.                 anjian();        //数据运行
  131.                 datapros();        //数码管显示数据处理
  132.                 DigDisplay();//数码管处理
  133.         }
  134. }


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

  137. unsigned char I2cSend(unsigned char dat);  //声明函数
  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()        //起始信号                        
  150. {
  151.         SDA=1;
  152.         Delay10us();
  153.         SCL=1;
  154.         Delay10us();//延迟时间,使SDA保持高位时间>4.7us后至低位
  155.         SDA=0;
  156.         Delay10us();//保持处于低位时间大于4us
  157.         SCL=0;                        
  158.         Delay10us();//起始之后SDA和SC要至0               
  159. }

  160. void I2cStop() //终止信号
  161. {
  162.         SDA=0;
  163.         Delay10us(); //延迟SDA数据线处于低位时间,使SDA在低位时间>4us
  164.         SCL=1;                                       
  165.         Delay10us();//延迟时间,使SCL在高位时间>4.7us
  166.         SDA=1;
  167.         Delay10us();//结束之后保持SDA和SCL都为1;表示总线空闲               
  168. }

  169. unsigned char I2cSendByte(unsigned char dat) //发送字节
  170. {
  171.         unsigned char a=0,b=0;               
  172.         for(a=0;a<8;a++)//要发送8位,从最高位开始
  173.         {
  174.                 SDA=dat>>7;         //dat的第一位右移7位,写入SDA
  175.                 dat=dat<<1;                //dat左移一位把高一位移除,相当于dat从左到右依次写到SDA
  176.                 Delay10us();
  177.                 SCL=1;
  178.                 Delay10us();//延迟时间,使SCL在高位时间>4.7us
  179.                 SCL=0;
  180.                 Delay10us();        
  181.         }
  182.         SDA=1;
  183.         Delay10us();        
  184.         SCL=1;
  185.         while(SDA)//等待应答,也就是等待从设备把SDA拉低
  186.         {
  187.                 b++;
  188.                 if(b>200)         //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
  189.                 {
  190.                         SCL=0;
  191.                         Delay10us();        //延时10us
  192.                         return 0;           //发送失败返回0
  193.                 }
  194.         }
  195.         SCL=0;
  196.         Delay10us();         //延时10us
  197.          return 1;                //发送成功返回1
  198. }

  199. unsigned char I2cReadByte()         //取读一个字节
  200. {
  201.         unsigned char a=0,dat=0;
  202.         SDA=1;                        //起始和发送一个字节之后SCL都是0
  203.         Delay10us();
  204.         for(a=0;a<8;a++)//接收8个字节
  205.         {
  206.                 SCL=1;
  207.                 Delay10us();           //dat左移并上SDA,相当于SDA从低位写入
  208.                 dat<<=1;
  209.                 dat|=SDA;
  210.                 Delay10us();                //延时10us
  211.                 SCL=0;
  212.                 Delay10us();                //延时10us
  213.         }
  214.         return dat;                //接收完一个字节SCL=0,SDA=1
  215. }

  216. void At24c02Write(unsigned char addr,unsigned char dat)//将数据写入一个地址中
  217. {
  218.         I2cStart();                        //开始
  219.         I2cSendByte(0xa0);//发送写器件地址
  220.         I2cSendByte(addr);//发送要写入内存地址
  221.         I2cSendByte(dat);        //发送数据
  222.         I2cStop();                   //结束
  223.         delay(1000);           //等待数据稳定写入
  224. }

  225. unsigned char At24c02Read(unsigned char addr)//写入取读到的地址
  226. {
  227.         unsigned char num;
  228.         I2cStart();                   //开始
  229.         I2cSendByte(0xa0); //发送写器件地址                0xa0为写入标志
  230.         I2cSendByte(addr); //发送要读取的地址 ,,先写入要读的地址
  231.         I2cStart();
  232.         I2cSendByte(0xa1); //发送读器件地址                0xa1为读标志
  233.         num=I2cReadByte(); //读取数据
  234.         I2cStop();                   //结束
  235.         return num;        
  236. }

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

  239. unsigned char I2cSend(unsigned char dat);  //声明函数
  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()        //起始信号                        
  252. {
  253.         SDA=1;
  254.         Delay10us();
  255.         SCL=1;
  256.         Delay10us();//延迟时间,使SDA保持高位时间>4.7us后至低位
  257.         SDA=0;
  258.         Delay10us();//保持处于低位时间大于4us
  259.         SCL=0;                        
  260.         Delay10us();//起始之后SDA和SC要至0               
  261. }

  262. void I2cStop() //终止信号
  263. {
  264.         SDA=0;
  265.         Delay10us(); //延迟SDA数据线处于低位时间,使SDA在低位时间>4us
  266.         SCL=1;                                       
  267.         Delay10us();//延迟时间,使SCL在高位时间>4.7us
  268.         SDA=1;
  269.         Delay10us();//结束之后保持SDA和SCL都为1;表示总线空闲               
  270. }

  271. unsigned char I2cSendByte(unsigned char dat) //发送字节
  272. {
  273.         unsigned char a=0,b=0;               
  274.         for(a=0;a<8;a++)//要发送8位,从最高位开始
  275.         {
  276.                 SDA=dat>>7;         //dat的第一位右移7位,写入SDA
  277.                 dat=dat<<1;                //dat左移一位把高一位移除,相当于dat从左到右依次写到SDA
  278.                 Delay10us();
  279.                 SCL=1;
  280.                 Delay10us();//延迟时间,使SCL在高位时间>4.7us
  281.                 SCL=0;
  282.                 Delay10us();        
  283.         }
  284.         SDA=1;
  285.         Delay10us();        
  286.         SCL=1;
  287.         while(SDA)//等待应答,也就是等待从设备把SDA拉低
  288.         {
  289.                 b++;
  290.                 if(b>200)         //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
  291.                 {
  292.                         SCL=0;
  293.                         Delay10us();        //延时10us
  294.                         return 0;           //发送失败返回0
  295.                 }
  296.         }
  297.         SCL=0;
  298.         Delay10us();         //延时10us
  299.          return 1;                //发送成功返回1
  300. }

  301. unsigned char I2cReadByte()         //取读一个字节
  302. {
  303.         unsigned char a=0,dat=0;
  304.         SDA=1;                        //起始和发送一个字节之后SCL都是0
  305.         Delay10us();
  306.         for(a=0;a<8;a++)//接收8个字节
  307.         {
  308.                 SCL=1;
  309.                 Delay10us();           //dat左移并上SDA,相当于SDA从低位写入
  310.                 dat<<=1;
  311.                 dat|=SDA;
  312.                 Delay10us();                //延时10us
  313.                 SCL=0;
  314.                 Delay10us();                //延时10us
  315.         }
  316.         return dat;                //接收完一个字节SCL=0,SDA=1
  317. }

  318. void At24c02Write(unsigned char addr,unsigned char dat)//将数据写入一个地址中
  319. {
  320.         I2cStart();                        //开始
  321.         I2cSendByte(0xa0);//发送写器件地址
  322.         I2cSendByte(addr);//发送要写入内存地址
  323.         I2cSendByte(dat);        //发送数据
  324.         I2cStop();                   //结束
  325.         delay(1000);           //等待数据稳定写入
  326. }

  327. unsigned char At24c02Read(unsigned char addr)//写入取读到的地址
  328. {
  329.         unsigned char num;
  330.         I2cStart();                   //开始
  331.         I2cSendByte(0xa0); //发送写器件地址                0xa0为写入标志
  332.         I2cSendByte(addr); //发送要读取的地址 ,,先写入要读的地址
  333.         I2cStart();
  334.         I2cSendByte(0xa1); //发送读器件地址                0xa1为读标志
  335.         num=I2cReadByte(); //读取数据
  336.         I2cStop();                   //结束
  337.         return num;        
  338. }

复制代码

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表