编译没有问题 但是按键k2 读取不出数据 但是检查了很久读取数据的函数没有问题。。。
代码有点多 望大神指导一下 有不规范指出望指正
- //这是main.c
- #include<reg51.h>
- #include<i2c.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- char num=0;
- u8 pros[4];
- sbit LSA=P2^2; //38译码器IO口连接
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- sbit k1=P3^0; //独立按键管脚
- sbit k2=P3^1;
- sbit k3=P3^2;
- sbit k4=P3^3;
- void delay(u16 i) //延迟函数
- {
- while(i--);
- }
- void Keypros() //按键的函数
- {
- if(k1==0)
- {
- delay(100);
- if(k1==0)
- {
- AT24c02Write(1,num);
- }
- while(!k1);
- }
-
- if(k2==0)
- {
- delay(100);
- if(k2==0)
- {
- num=AT24c02Read(1);
- }
- while(!k2);
- }
-
- if(k3==0)
- {
- delay(100);
- if(k3==0)
- {
- num++;
- if(num>255)
- {
- num=0;
- }
- }
- while(!k3);
- }
-
- if(k4==0)
- {
- delay(100);
- if(k4==0)
- {
- num=0;
- }
- while(!k4);
- }
- }
- void KeyDown() //数码管动态显示
- {
- u8 i;
- for(i=0;i<4;i++)
- {
- switch(i)
- {
- case(0):LSA=0;LSB=0;LSC=0;break;
- case(1):LSA=1;LSB=0;LSC=0;break;
- case(2):LSA=0;LSB=1;LSC=0;break;
- case(3):LSA=1;LSB=1;LSC=0;break;
- }
- P0=pros[i];
- delay(100);
- P0=0X00;
- }
- }
- void Dispros()
- {
- pros[0]=smgduan[num/1000];
- pros[1]=smgduan[num%1000/100];
- pros[2]=smgduan[num%1000%100/10];
- pros[3]=smgduan[num%1000%100%10];
- }
- void main()
- {
- while(1)
- {
- Keypros();
- KeyDown();
- Dispros();
- }
- }
- //下面是i2c.c
- #include<i2c.h>
- void delay10us(void) //误差 0us
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=2;a>0;a--);
- }
- void I2cstart() //起始信号 下降沿
- {
- SCL=1;
- delay10us();
- SDA=1;
- delay10us();
- SCL=0;
- delay10us();
- SDA=0;
- delay10us();
- }
- void i2cstop() //终止信号 上升沿
- {
- SDA=0;
- delay10us();
- SCL=1;
- delay10us();
- SDA=1;
- delay10us();
- }
- unsigned char I2cSendByte(unsigned char dat) //用I2c发送一个数据
- {
- unsigned char a=0,b=0;
-
- for(a=0;a<8;a++)
- {
-
- SDA=dat>>7;
- dat=dat<<1;
- SCL=1;
- delay10us();
- SCL=0;
- delay10us();
- }
- SDA=1;
- delay10us();
- SCL=1;
- while(SDA)
- {
- b++;
- if(b>200)
- {
- SCL=0;
- delay10us();
- return 0;
- }
- }
- SCL=0;
- delay10us();
- return 1;
- }
- unsigned char I2cReadByte() //用i2c读取一个数据
- {
- unsigned char a=0,dat=0;
- SDA=1;
- delay10us();
- for(a=0;a<8;a++)
- {
-
- dat<<=1;
- dat|=SDA;
- delay10us();
- SCL=1;
- delay10us();
- SCL=0;
- delay10us();
- }
- return dat;
- }
- void AT24c02Write(unsigned char addr,unsigned char dat) //给at24c02写入一个数据
- {
- I2cstart();
- I2cSendByte(0xa0); //发送写入器件的地址
- I2cSendByte(addr); //发送器件内储存数据的地址
- I2cSendByte(dat); //发送数据
- i2cstop();
-
- }
- unsigned char AT24c02Read(unsigned char addr) // dat-addr 从at24c02读取一个数据
- {
- unsigned char num;
- I2cstart(); //起始型号
- I2cSendByte(0xa0); //发送写入器件的地址
- I2cSendByte(addr); //发送写入器件内数据的地址
- I2cstart(); //反向读取
- I2cSendByte(0xa1); //发送所要读取器件的地址
- num=I2cReadByte(); //(dat)-()
- i2cstop();
- return num;
- }
- //下面是I2C.H
- #ifndef _I2C_H_
- #define _I2C_H_
- #include<reg51.h>
- sbit SDA=P2^0;
- sbit SCL=P2^1;
- void I2cstart(); //起始信号
- void i2cstop(); //终止信号
- unsigned char I2cSendByte(unsigned char dat); //用i2c发送一个数据
- unsigned char I2cReadByte();
- void AT24c02Write(unsigned char addr,unsigned char dat);
- unsigned char AT24c02Read(unsigned char addr);
- #endif
-
|