本帖最后由 nickychung 于 2014-1-23 10:08 编辑
/*这个程序的要求是要用的I IC总线,然后动态显示98-255*/ 此程序有错,数码管显示从0知道25然后再回到0,同时只显示前两个数码管。请高手完善
#include<reg52.h>
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void led(uchar address,uint z);
void init1();
void delay()
{;;}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void response()
{
uchar i;
scl=1;
delay();
while((sda==1)&&i<250)i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void delay1(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date=read_byte();
stop();
return date;
}
void main()
{
init();
write_add(23,0xfe);
delay1(50);
led(read_add(23),98);
while(1);
}
void led(uchar address,uint z)
{
uchar temp,bai,shi,ge,temp1;
uint x;
init1();
x=z;
temp1=address;
while(1)
{
bai=x/100;
shi=x%100/10;
ge=x%10;
temp=temp1;
P0=temp;
P1=table[bai];
delay1(1);
temp=_crol_(temp1,1);
P0=temp;
P1=table[shi];
delay1(1);
temp=_crol_(temp1,2);
P0=temp;
P1=table[ge];
delay1(1);
x++;
if(x==255)
x=0;
}
}
void init1()
{
TMOD=0x01;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
EA=1;
TR0=1;
ET0=1;
}
void
timer0() interrupt 1
{
uint t;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
if(t==1)
t=0;
t++;
TR0=0;
}
一周热门 更多>