各位大神 求助一个I2C总线的问题 看了好久都不知道哪错了
时间:10-02
整理:3721RD
点击:
下面程序是想用定时器方式3实现数码管显数和流水灯 但是流水灯不知道为什么就是不对 应该只亮一个灯的 但是实际却亮了好几个 谢谢各位大神了
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit dula=P2^6;
sbit wela=P2^7;
sbit sda=P2^0;
sbit scl=P2^1;
bit write=0;
uint numh,numl;
uchar sec,p;
void delay()
{;;}
void delayms(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0X03;
TH0=6;
TL0=6;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
scl=1;
delay();
sda=1;
delay();
}
void display(uchar sec)
{
uchar shi,ge;
shi=sec/10;
ge=sec%10;
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar a=0;
scl=1;
delay();
while((sda==1)&&(a<250))
a++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar b,temp;
temp=date;
for(b=0;b<8;b++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar c,d;
scl=0;
delay();
sda=1;
delay();
for(c=0;c<8;c++)
{
scl=1;
delay();
d=(d<<1)|sda;
scl=0;
delay();
}
return d;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
sec=read_add(1); //数码管数据在第1存储器里
if(sec>100)
sec=0;
write_add(2,0xfe); //为二极管数据所在存储器赋初值
p=read_add(2);
while(1)
{
display(sec);
P1=p; //二极管数据在第2存储器里
if(write==1)
{
write=0;
write_add(1,sec);
}
if(write==2)
{
write=0;
write_add(2,p);
}
}
}
void Time0_SHUMA() interrupt 1
{
TH0=6;
numh++;
if(numh>=3686)
{
numh=0;
sec++;
write=1;
if(sec==100)
{
sec=0;
}
}
}
void Time0_P1() interrupt 3
{
uchar e,f;
TL0=6;
numl++;
if(numl>=3686)
{
numl=0;
e=p<<7;
f=p>>1;
p=e|f;
write_add(2,p);
write=2;
}
}
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
sbit dula=P2^6;
sbit wela=P2^7;
sbit sda=P2^0;
sbit scl=P2^1;
bit write=0;
uint numh,numl;
uchar sec,p;
void delay()
{;;}
void delayms(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
TMOD=0X03;
TH0=6;
TL0=6;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
scl=1;
delay();
sda=1;
delay();
}
void display(uchar sec)
{
uchar shi,ge;
shi=sec/10;
ge=sec%10;
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar a=0;
scl=1;
delay();
while((sda==1)&&(a<250))
a++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar b,temp;
temp=date;
for(b=0;b<8;b++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar c,d;
scl=0;
delay();
sda=1;
delay();
for(c=0;c<8;c++)
{
scl=1;
delay();
d=(d<<1)|sda;
scl=0;
delay();
}
return d;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void main()
{
init();
sec=read_add(1); //数码管数据在第1存储器里
if(sec>100)
sec=0;
write_add(2,0xfe); //为二极管数据所在存储器赋初值
p=read_add(2);
while(1)
{
display(sec);
P1=p; //二极管数据在第2存储器里
if(write==1)
{
write=0;
write_add(1,sec);
}
if(write==2)
{
write=0;
write_add(2,p);
}
}
}
void Time0_SHUMA() interrupt 1
{
TH0=6;
numh++;
if(numh>=3686)
{
numh=0;
sec++;
write=1;
if(sec==100)
{
sec=0;
}
}
}
void Time0_P1() interrupt 3
{
uchar e,f;
TL0=6;
numl++;
if(numl>=3686)
{
numl=0;
e=p<<7;
f=p>>1;
p=e|f;
write_add(2,p);
write=2;
}
}
循环程序里面对不对
问题太多,LZ根本不了解单片机,就是LZ这个程序估计LZ也看不太懂。多花点时间理解吧。
interrupt 3是定时器1的中断函数