想写一个单片机程序,用AT24c02来实现四个按键,一个按键读取,一个保存数据,一个让数据从0自加,一个让数据清零,数码管显示,先显示一位。。
现在可以自加和清零,但是保存不行,读取也每次都会读到5,程序如下请指点。
- /*******main***************/
- #include<I2c.h>
- uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uchar num,disp[4];
- sbit k2=P3^0;//¶áè¡
- sbit k1=P3^1;//±£′æ
- sbit k3=P3^2;//×Ô¼ó
- sbit k4=P3^3; //Çåáã
- //uchar code wei[2]={0x80,0x40};
- void delay(uint i)
- {
- while(i--);
- }
- void Keypros()
- {P3=P3|0x0f;
- if(k1==0)
- {
- delay(1000); //Ïû¶¶′|àí
- if(k1==0)
- {
- At24c02_Write(1,num); //ÔúμØÖ·1ÄúD′èëêy¾Ynum
- }
- while(!k1);
- }
-
- if(k2==0)
- {
- delay(1000); //Ïû¶¶′|àí
- if(k2==0)
- {
- num=At24c02_Read(1); //¶áè¡EEPROMμØÖ·1ÄúμÄêy¾Y±£′æÔúnumÖD
- }
- while(!k2);
- }
- if(k3==0)
- {
- delay(100); //Ïû¶¶′|àí
- if(k3==0)
- {
- num++; //êy¾Y¼ó1
- if(num>255)num=0;
- }
- while(!k3);
- }
-
-
- if(k4==0)
- {
- delay(1000); //Ïû¶¶′|àí
- if(k4==0)
- {
- num=0; //êy¾YÇåáã
- }
- while(!k4);
- }
- }
- void datapros()
- {
- disp[0]=smgduan[num%10];
- disp[1]=smgduan[num/10];
- }
- void DigDisplay()
- {
- uchar i;
- for(i=0;i<1;i++)
- {
- P2=0xfe;
- P0=disp[i];
- delay(100);
- P0=0x00;P2=0x00;
- }
- }
- void main()
- {
- while(1)
- {
- datapros();
- DigDisplay();
- Keypros();
- }
- }
- /********************I2c*/*/*/*/*****************/
- #include<I2c.h>
- void Delay10us()
- {
- unsigned char a,b;
- for(b=1;b>0;b--)
- for(a=2;a>0;a--);
- }
- void I2c_Start()
- {
- SCL=0;
- Delay10us();
- SDA=1;
- Delay10us();
- SCL=1;
- Delay10us();
- SDA=0;
- Delay10us();
- }
- //void ack()
- //{
- //SCL=0;
- // _nop_();
- // SDA=0;
- // SCL=1;
- // _nop_();
- // SCL=0;
- // _nop_();
- // SDA=1;
- //
- //}
- void I2c_Stop()
- {
- SCL=0;
- Delay10us();
- SDA=0;
- Delay10us();
- SCL=1;
- Delay10us();
- SDA=1;
- Delay10us();
- }
- uchar I2c_SendByte(uchar dat)
- {
- uchar i,b=0;
- for(i=0;i<8;i++)
- {
- SDA=dat>>7;
- dat=dat<<1;
- Delay10us();
- SCL=1;
- Delay10us();
- SCL=0;
- Delay10us();
- }
- SDA=1;
- Delay10us();
- SCL=1;
- while(SDA)
- {
- if(++b>200)
- SCL=0;
- Delay10us();
- return 0;
- }
- SCL=0;
- Delay10us();
- return 1;
- // ack();
- }
- uchar I2c_ReadByte()
- {
- uchar i,dat=0;
- SDA=1;
- Delay10us();
- for(i=0;i<8;i++)
- {
- SCL=1;
- Delay10us();
- dat<<=1;
- dat|=SDA;
- Delay10us();
- SCL=0;
- Delay10us();
-
- }
- return dat;
- }
- void At24c02_Write(uchar address,uchar dat)
- {
- I2c_Start();
- I2c_SendByte(0xa0);
- I2c_SendByte(address);
- I2c_SendByte(dat);
- I2c_Stop();
- }
- uchar At24c02_Read(uchar address)
- {
- uchar dat;
- I2c_Start();
- I2c_SendByte(0xa0);
- I2c_SendByte(address);
- I2c_Start();
- I2c_SendByte(0xa1);
- dat=I2c_ReadByte();
- I2c_Stop();
- return dat;
- }
- /*************************I2c.h**************************/
- #ifndef _I2c_H_
- #define _I2c_H_
- #include<reg52.h>
- #include<intrins.h>
- typedef unsigned int uint;
- typedef unsigned char uchar;
- sbit SCL=P2^1;
- sbit SDA=P2^0;
- void I2c_Start();
- void I2c_Stop();
- unsigned char I2c_SendByte(unsigned char dat);
- unsigned char I2c_ReadByte();
- void At24c02_Write(unsigned char addr,unsigned char dat);
- unsigned char At24c02_Read(unsigned char addr);
- #endif
复制代码
|