/*利用IIC总线接口底层驱动代码,设计应用程序,实现下面功能:1.系统开机后,读取0x01、0x03和0x05 内存单元的数据,并从左至右显示在数码管上, .数字之间用“-”分隔,
2.将0x01单元的数据加1后,写回该内存单元,使用按键s1加1后结果如大于10,恢复0.0x03单元的数据加2后,写回该内存单元, .加2后结果如大于20,恢复0。将0x05单元的数据加3后,写回该内存单元,加3后结果如大于30,恢复0。*/
/*main.c文件*/
- #include "reg51.h"
- #include "i2c.h"
- typedef unsigned int u16; //对数据类型进行声明定义
- typedef unsigned char u8;
- void datapros();
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit k1=P3^1;
- sbit k2=P3^3;
- u8 display[8],addr,a=0,b=0,c=0;
- u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delayy(u16 i)
- {
- while(i--);
- }
- void DigDisplay()
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- switch(i) //位选,选择点亮的数码管,
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;//显示第0位
- case(1):
- LSA=1;LSB=0;LSC=0; break;//显示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; break;//显示第2位
- case(3):
- LSA=1;LSB=1;LSC=0; break;//显示第3位
- case(4):
- LSA=0;LSB=0;LSC=1; break;//显示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; break;//显示第5位
- case(6):
- LSA=0;LSB=1;LSC=1; break;//显示第6位
- case(7):
- LSA=1;LSB=1;LSC=1; break;//显示第7位
- }
- P0=display[i];
- delayy(100);
- P0=0x00;
- }
- }
- void datapros()//数码管数据的处理
- {
- display[0]=smgduan[a%10];
- display[1]=smgduan[a/10];
- display[2]=0x40;
- display[3]=smgduan[b%10];
- display[4]=smgduan[b/10];
- display[5]=0x40;
- display[6]=smgduan[c%10];
- display[7]=smgduan[c/10];
- }
- void ack() //取读保存在地址内的数据
- {
- a=At24c02Read(0x01);
- b=At24c02Read(0x03);
- c=At24c02Read(0x05);
- }
- void anjian()
- {
- if(k1==0)
- {
- delayy(1000);
- if(k1==0)
- {
-
- a=At24c02Read(0x01); //将0X01地址内的数据赋给A
- a++;
- if(a>10)
- {
- a=0;
- }
- At24c02Write(0x01,a); //将A数据写入0x01的地址中去
- delayy(1000);
-
- b=At24c02Read(0x03); //将0X03地址内的数据赋给B
- b=b+2;
- if(b>20)
- {
- b=0;
- }
- At24c02Write(0x03,b); //将B的数据写入0x03的地址中去
- delayy(1000);
- c=At24c02Read(0x05); //将0X05地址内的数据赋给C
- c=c+3;
- if(c>30)
- {
- c=0;
- }
- At24c02Write(0x05,c); //将C的数据写入0x05的地址中去
- delayy(1000);
- }
- while(!k1);
- }
- }
- void an() //将数据都至0,并保存起来
- {
- if(k2==0)
- {
- delayy(100);
- if(k2==0)
- {
- while(k2==0)
- {
- datapros();
- }
- a=0;
- At24c02Write(0x01,a);
- delayy(100);
- b=0;
- At24c02Write(0x03,b);
- delayy(100);
- c=0;
- At24c02Write(0x05,c);
- }
- while(!k2);
- }
- }
-
- void main()
- {
- ack();//开机取读最新保存的数据
- while(1)
- {
- an(); //数据至0
- anjian(); //数据运行
- datapros(); //数码管显示数据处理
- DigDisplay();//数码管处理
- }
- }
- /*i2c.c文件*/
- #include"i2c.h"
- unsigned char I2cSend(unsigned char dat); //声明函数
- unsigned char I2cRead();
- void Delay10us()//小延迟
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=2;a>0;a--);
- }
- void delay(int i) //大延迟
- {
- while(i--);
- }
- void I2cStart() //起始信号
- {
- SDA=1;
- Delay10us();
- SCL=1;
- Delay10us();//延迟时间,使SDA保持高位时间>4.7us后至低位
- SDA=0;
- Delay10us();//保持处于低位时间大于4us
- SCL=0;
- Delay10us();//起始之后SDA和SC要至0
- }
- void I2cStop() //终止信号
- {
- SDA=0;
- Delay10us(); //延迟SDA数据线处于低位时间,使SDA在低位时间>4us
- SCL=1;
- Delay10us();//延迟时间,使SCL在高位时间>4.7us
- SDA=1;
- Delay10us();//结束之后保持SDA和SCL都为1;表示总线空闲
- }
- unsigned char I2cSendByte(unsigned char dat) //发送字节
- {
- unsigned char a=0,b=0;
- for(a=0;a<8;a++)//要发送8位,从最高位开始
- {
- SDA=dat>>7; //dat的第一位右移7位,写入SDA
- dat=dat<<1; //dat左移一位把高一位移除,相当于dat从左到右依次写到SDA
- Delay10us();
- SCL=1;
- Delay10us();//延迟时间,使SCL在高位时间>4.7us
- SCL=0;
- Delay10us();
- }
- SDA=1;
- Delay10us();
- SCL=1;
- while(SDA)//等待应答,也就是等待从设备把SDA拉低
- {
- b++;
- if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
- {
- SCL=0;
- Delay10us(); //延时10us
- return 0; //发送失败返回0
- }
- }
- SCL=0;
- Delay10us(); //延时10us
- return 1; //发送成功返回1
- }
- unsigned char I2cReadByte() //取读一个字节
- {
- unsigned char a=0,dat=0;
- SDA=1; //起始和发送一个字节之后SCL都是0
- Delay10us();
- for(a=0;a<8;a++)//接收8个字节
- {
- SCL=1;
- Delay10us(); //dat左移并上SDA,相当于SDA从低位写入
- dat<<=1;
- dat|=SDA;
- Delay10us(); //延时10us
- SCL=0;
- Delay10us(); //延时10us
- }
- return dat; //接收完一个字节SCL=0,SDA=1
- }
- void At24c02Write(unsigned char addr,unsigned char dat)//将数据写入一个地址中
- {
- I2cStart(); //开始
- I2cSendByte(0xa0);//发送写器件地址
- I2cSendByte(addr);//发送要写入内存地址
- I2cSendByte(dat); //发送数据
- I2cStop(); //结束
- delay(1000); //等待数据稳定写入
- }
- unsigned char At24c02Read(unsigned char addr)//写入取读到的地址
- {
- unsigned char num;
- I2cStart(); //开始
- I2cSendByte(0xa0); //发送写器件地址 0xa0为写入标志
- I2cSendByte(addr); //发送要读取的地址 ,,先写入要读的地址
- I2cStart();
- I2cSendByte(0xa1); //发送读器件地址 0xa1为读标志
- num=I2cReadByte(); //读取数据
- I2cStop(); //结束
- return num;
- }
- /*i2c.h文件*/
- #include"i2c.h"
- unsigned char I2cSend(unsigned char dat); //声明函数
- unsigned char I2cRead();
- void Delay10us()//小延迟
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=2;a>0;a--);
- }
- void delay(int i) //大延迟
- {
- while(i--);
- }
- void I2cStart() //起始信号
- {
- SDA=1;
- Delay10us();
- SCL=1;
- Delay10us();//延迟时间,使SDA保持高位时间>4.7us后至低位
- SDA=0;
- Delay10us();//保持处于低位时间大于4us
- SCL=0;
- Delay10us();//起始之后SDA和SC要至0
- }
- void I2cStop() //终止信号
- {
- SDA=0;
- Delay10us(); //延迟SDA数据线处于低位时间,使SDA在低位时间>4us
- SCL=1;
- Delay10us();//延迟时间,使SCL在高位时间>4.7us
- SDA=1;
- Delay10us();//结束之后保持SDA和SCL都为1;表示总线空闲
- }
- unsigned char I2cSendByte(unsigned char dat) //发送字节
- {
- unsigned char a=0,b=0;
- for(a=0;a<8;a++)//要发送8位,从最高位开始
- {
- SDA=dat>>7; //dat的第一位右移7位,写入SDA
- dat=dat<<1; //dat左移一位把高一位移除,相当于dat从左到右依次写到SDA
- Delay10us();
- SCL=1;
- Delay10us();//延迟时间,使SCL在高位时间>4.7us
- SCL=0;
- Delay10us();
- }
- SDA=1;
- Delay10us();
- SCL=1;
- while(SDA)//等待应答,也就是等待从设备把SDA拉低
- {
- b++;
- if(b>200) //如果超过2000us没有应答发送失败,或者为非应答,表示接收结束
- {
- SCL=0;
- Delay10us(); //延时10us
- return 0; //发送失败返回0
- }
- }
- SCL=0;
- Delay10us(); //延时10us
- return 1; //发送成功返回1
- }
- unsigned char I2cReadByte() //取读一个字节
- {
- unsigned char a=0,dat=0;
- SDA=1; //起始和发送一个字节之后SCL都是0
- Delay10us();
- for(a=0;a<8;a++)//接收8个字节
- {
- SCL=1;
- Delay10us(); //dat左移并上SDA,相当于SDA从低位写入
- dat<<=1;
- dat|=SDA;
- Delay10us(); //延时10us
- SCL=0;
- Delay10us(); //延时10us
- }
- return dat; //接收完一个字节SCL=0,SDA=1
- }
- void At24c02Write(unsigned char addr,unsigned char dat)//将数据写入一个地址中
- {
- I2cStart(); //开始
- I2cSendByte(0xa0);//发送写器件地址
- I2cSendByte(addr);//发送要写入内存地址
- I2cSendByte(dat); //发送数据
- I2cStop(); //结束
- delay(1000); //等待数据稳定写入
- }
- unsigned char At24c02Read(unsigned char addr)//写入取读到的地址
- {
- unsigned char num;
- I2cStart(); //开始
- I2cSendByte(0xa0); //发送写器件地址 0xa0为写入标志
- I2cSendByte(addr); //发送要读取的地址 ,,先写入要读的地址
- I2cStart();
- I2cSendByte(0xa1); //发送读器件地址 0xa1为读标志
- num=I2cReadByte(); //读取数据
- I2cStop(); //结束
- return num;
- }
复制代码
|