我想通过24c02,led1602实现按K1键写一个数(数的大小要求在0到9999,这个程序是自己基于之前能正常实现功能但只能读写0到255的数的一段代码改的),按K2键屏幕显示之前写的数的数值。但是实际操作后每次读出来的数不是255就是511,不知道哪里出了问题,希望大家耐心帮帮忙看看哪边出了问题,谢谢!!!
unsigned char wordCode0[6] = "write:";
unsigned char wordCode1[6] = " read:";
void At24c02Write(unsigned char ,unsigned short int dat );
unsigned short int At24c02Read(unsigned char );
void Delay10ms(unsigned int c);
void main()
{
unsigned short int num0,num1 = 0,n;
LcdInit();
LcdWriteCom(0x80);
for(n=0; n<6; n++)
{
LcdWriteData(wordCode0[n]);
}
LcdWriteCom(0x80 + 0x40);
for(n=0; n<6; n++)
{
LcdWriteData(wordCode1[n]);
}
while(1)
{
if(K1 == 0)
{
Delay10ms(1);
if(K1 == 0)
At24c02Write(2,num0);
while((n < 50)&&(K3==0))
{
n++;
Delay10ms(1);
}
n=0;
}
if(K2 == 0)
{
Delay10ms(1);
if(K2 == 0)
num1 = At24c02Read(2);
while((n < 50)&&(K2 == 0))
{
n++;
Delay10ms(1);
}
n=0;
}
if(K3 == 0)
{
Delay10ms(1);
if(K3 == 0)
num0++;
}
if(K4 == 0)
{
Delay10ms(1);
if(K4 == 0)
num0--;
}
LcdWriteCom(0x87);
LcdWriteData('0' + (num0/1000));
LcdWriteData('0' + (num0%1000/100));
LcdWriteData('0' + (num0%1000%100/10));
LcdWriteData('0' + (num0%1000%100%10));
LcdWriteCom(0x87 + 0x40);
LcdWriteData('0' + (num1/1000));
LcdWriteData('0' + (num1%1000/100));
LcdWriteData('0' + (num1%1000%100/10));
LcdWriteData('0' + (num1%1000%100%10));
}
}
void Delay10ms(unsigned int c)
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
void At24c02Write(unsigned char addr,unsigned short int dat)
{
unsigned short int a,b;
uchar c,d;
a=a|dat;
a=a>>8;
c=(uchar)a;
b=b|dat;
d=(uchar)b;
I2C_Start();
I2C_Sendbyte(0xa0);
ack();
I2C_Sendbyte(addr);
ack();
I2C_Sendbyte(c);
ack();
I2C_Sendbyte(d);
ack();
I2C_Stop();
}
unsigned short int At24c02Read(unsigned char addr)
{
unsigned short int num;
uchar e,f;
I2C_Start();
I2C_Sendbyte(0xa0);
ack();
I2C_Sendbyte(addr);
ack();
I2C_Start();
I2C_Sendbyte(0xa1);
ack();
e=I2C_Readbyte();
ack();
f=I2C_Readbyte();
I2C_Stop();
num=e;
num=num<<8;
num=num|f;
return num;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>