为什么我的显示总为零,大伙看看.以下是我的程序:
#include "reg52.h"
#include "absacc.h"
#include "math.h"
#define uchar unsigned char
#define uint unsigned int
#define dm XBYTE[0x8000]
#define wm XBYTE[0x8100]
#define W_Add 0xa0
#define R_Add 0xa1
sbit p15=P1^5;
sbit SCL=P3^4;
sbit SDA=P3^5;
void Start (void);
void Stop (void);
void NoAck (void);
void TestAck (void);
void Write_8Bit (uchar);
void Write_24c02 (uchar,uchar);
uchar Read_8Bit ();
uchar Read_24c02 (uchar address);
void Delay_50us (uint t);
const uchar code tab1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8E}; //共阳极
const uchar code tab2[]={0x01,0x08,0x10,0x20}; //tab1为数码管的段码,tab2为数码管的位码
void main (void)
{
uchar i;
while(1)
{
i =Read_24c02(0x01);
dm=tab1[i]; //显示"0"
wm=0x01;
Delay_50us(20);
if(p15==0) //键值
{
Delay_50us(300);
if(p15==0)
{
i++;
if(i >= 10)
i =0;
Write_24c02 (i,0x01);
Delay_50us(300);
while((p15&0xdf)==0);
}
}
}
}
void Delay_50us(uint t) /*延时程序*/
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}void Start (void)
{
SDA =1;
SCL =1;
Delay_50us(0);
SDA =0;
Delay_50us(0);
}
void Stop (void)
{
SDA=0;
SCL =1;
Delay_50us(0);
SDA =1;
Delay_50us(0);
}
void NoAck (void)
{
SDA =1;
Delay_50us(0);
SCL =1;
Delay_50us(0);
SCL =0;
}
void TestAck (void)
{
SDA =0;
Delay_50us(0);
SCL =1;
Delay_50us(0);
SCL =0;
}
void Write_8Bit (uchar input)
{
uchar temp;
for(temp=8;temp!=0;temp--)
{
SDA =(bit)(input&0x80);
Delay_50us(0);
SCL =1;
Delay_50us(0);
SCL =0;
input =input <<1;
}
}
void Write_24c02 (uchar ch,uchar address)
{
Start();
Write_8Bit (W_Add);
TestAck();
Write_8Bit (address);
TestAck();
Write_8Bit (ch);
TestAck();
Stop();
Delay_50us(120);
}
uchar Read_8Bit ()
{
uchar temp1,Rbyte=0;
for(temp1=8;temp1!=0;temp1--)
{
SCL =1;
Rbyte =Rbyte <<1;
Rbyte =Rbyte|((uchar)(SDA));
SCL=0;
}
return (Rbyte);
}
uchar Read_24c02 (uchar address)
{
uchar ch;
Start();
Write_8Bit (W_Add);
TestAck();
Write_8Bit (address);
TestAck();
Start();
Write_8Bit (R_Add);
TestAck();
ch =Read_8Bit();
NoAck();
Stop();
return(ch);
}
------解决方案--------------------------------------------------------
看看你的delay()函数吧。delay(0)??
------解决方案--------------------------------------------------------
LZ的I2C能正常工作吗?
Delay_50us(0)这样能产生CLOCK吗?
------解决方案--------------------------------------------------------
i2c是很麻烦的,必须保证时序满足要求。
很多时候,这种模拟时序是不能光看程序来解决问题的。必须用双通道示波器抓波形,看SCL/SDA的时序。
一直显示0,那显示函数是好的吗?
你别一下子就想读很多东西,而是一点一点来。