哪位大神知道为什么吗?我的程序在理论没错误啊!怎么实现不了它的功能???
# include <reg52.h>
# include "intrins.h"
//# include "I2C_driver.h"
#define AT24C02 0xa0 //AT24C02 地址
sbit LS138A=P2^2; //译码器端
sbit LS138B=P2^3;
sbit LS138C=P2^4;
sbit K1 = P1^0; //保存
sbit K2 = P1^1; //读取
sbit K3 = P1^2; //+数据
sbit K4 = P1^3; //-数据
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned char Count1;
unsigned long D[17], LedOut[4];
unsigned int idata USEC;
typedef unsigned int uint;
typedef unsigned char uchar;
sbit SCL = P2^1;
sbit SDA = P2^0;
bit ack; //应答;1发送正常
/* 4us延时 */
void delay()
{
_nop_(); _nop_();
_nop_(); _nop_();
_nop_(); _nop_();
}
/* I2C启动信号 */
void I2C_Start()
{
SDA = 1;
_nop_();
SCL = 1; //在SCL高电平的期间,SDA高到低
delay();
_nop_();
SDA = 0;
delay();
SCL=0; /*钳住I2C总线,准备发送或接收数据 */
_nop_();
_nop_();
}
/* I2C终止信号 */
void I2C_Stop()
{
SDA = 0;
_nop_();
SCL = 1; //在SCL高电平的期间,SDA高到低
delay();
SDA = 1;
delay();
SCL = 0;
_nop_();
}
/* i2C 应答函数 */
void I2C_ack(bit a)
{
if(a == 0) SDA = 0;
else SDA = 1;
delay();
SCL = 1;
delay();
SCL = 0; /*清时钟线,钳住I2C总线以便继续接收*/
_nop_();
}
/* I2C写一字节数据 */
void I2C_writebyte(uchar datum)
{
uchar i;
for(i=0; i<8; i++)
{
if(i<<datum&0x80) SDA = 1;
else SDA = 0;
_nop_();
SCL = 1; /*置时钟线为高,通知被控器开始接收数据位*/
delay();
SCL = 0;
} //一帧的数据传输完毕,开始应答
SDA = 1; /*8位发送完后释放数据线,准备接收应答位*/
delay();
SCL = 1;
delay();
if(SDA ==1) ack = 0; //fei应答
else ack = 1;
SCL = 0;
delay();
}
/* I2C读一字节数据 */
uchar I2C_readbyte()
{
uchar datum, i;
datum = 0;
SDA = 1; /*置数据线为输入方式*/
for(i=0; i<8; i++)
{
SCL = 0; /*置时钟线为低,准备接收数据位*/
delay();
SCL = 1; /*置时钟线为高使数据线上数据有效*/
delay();
datum <<= 1;
if(SDA == 1)
datum |= 0x01;
_nop_();
_nop_();
}
SCL = 0;
delay();
return datum;
}
/* I2C写入多个字节的数据 */
bit I2Cwritenbyte(uchar SLA, uchar SUBA, uchar *datum, uchar n)
{
uchar i;
I2C_Start();
I2C_writebyte(SLA);
if(ack == 0) return 0;
I2C_writebyte(SUBA);
if(ack == 0) return 0;
for(i=0; i<n; i++)
{
I2C_writebyte(*datum);
if(ack == 0) return 0; //程序员自己设定是否继续
datum++;
}
I2C_Stop();
return 1;
}
/* I2C读入多个字节的数据 */
bit I2Creadnbyte(uchar SLA, uchar SUBA, uchar *datum, uchar n)
{
uchar i;
I2C_Start();
I2C_writebyte(SLA); /*发送器件地址*/
if(ack == 0) return 0;
I2C_writebyte(SUBA); /*发送器件子地址*/
if(ack == 0) return 0;
I2C_Start(); /*重新启动总线*/
I2C_writebyte(SLA+1); //发送器件写地址
if(ack == 0) return 0;
for(i=0; i<n-1; i++)
{
*datum = I2C_readbyte();
I2C_ack(0);
datum++;
}
*datum = I2C_readbyte();
I2C_ack(1); /*发送非应位*/
I2C_Stop(); /*结束总线*/
return(1);
}
void system_init()
{
TMOD|= 0x11;
TH1 = 0xfe; //11.0592
TL1 = 0x33;
TR1 = 1;
IE =0x8A;
}
void Delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
void display(uchar count)
{
uchar LedNumVal,i;
/********以下将2402中保存的数据送到LED数码管显示*************/
LedNumVal=count;
LedOut[0]=Disp_Tab[LedNumVal%10000/1000];
LedOut[1]=Disp_Tab[LedNumVal%1000/100];
LedOut[2]=Disp_Tab[LedNumVal%100/10]|0x80;
LedOut[3]=Disp_Tab[LedNumVal%10];
for(i=0; i<4; i++)
{
P0 = LedOut[i];
switch(i)
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
}
Delay(100);
}
}
void main()
{
unsigned char i;
unsigned char pDat[8];
system_init();
while(1)
{
//========================IIC 读取
if(K2 == 0) //第二个按钮读取数据
{
I2Creadnbyte(AT24C02, 0 , &pDat[0], 8);
for (i=0; i<4; i++)
{
D[14+i]=pDat[i*2+0]+pDat[i*2+1]*0x100;
Count1 = D[14];
}
}
//========================IIC 保存
if(K1 == 0) //第一个按钮保存数据
{
D[14]= Count1;
for (i=0; i<4; i++)
{
pDat[i*2+0]=D[14+i]; //D[14]是长字节的无符号型的数据
pDat[i*2+1]=D[14+i]>>8; //pDat[]是8位的数据,所以要进行转换防止数据丢失
}
I2Cwritenbyte(AT24C02, 0 , &pDat[0], 8);
D[14] = 0;
}
display(Count1);
}
}
void T1zd(void) interrupt 3 //3 为定时器1的中断号 1 定时器0的中断号 0 外部中断1 2 外部中断2 4 串口中断
{
TH1 = 0xfe; //12M
TL1 = 0x33;
if(USEC++==200)
{ USEC=0;
if (!K3) Count1++; //改变数据
if (!K4&Count1!=0) Count1--;
}
}
|