此程序有错,求高手完善。

2019-07-15 20:59发布

本帖最后由 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;
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
1036479708
1楼-- · 2019-07-16 20:19
ge=x%10;
这一步似乎也有错
1036479708
2楼-- · 2019-07-16 22:26
void timer0() interrupt 1
{
        uint t;
        TH0=(65536-10000)/256;
        TL0=(65536-10000)%256;
        if(t==1)
        t=0;
        t++;
        TR0=0;
}
这个中断只进入一次就不再进入了,希望你能把整个程序的执行过程简要的说明一下,否则光看程序不好查出错误
nickychung
3楼-- · 2019-07-17 02:24
1036479708 发表于 2014-1-23 23:13
temp=_crol_(temp1,2);
这个是哪来的,用来干嘛的

这是左移2位的意思!
钟情的屌丝男
4楼-- · 2019-07-17 05:47
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
bit  write=0;
sbit scl=P2^1;
sbit sda=P2^0;
sbit dula=P2^6;
sbit wela=P2^7;
uchar sec,tcnt;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
                                        0x66,0x6d,0x7d,0x07,
                                        0x7f,0x6f};
void delay()
{
;;
}                 
void delay1ms(uint z)
{
           uint x,y;
   for(x=z;x>0;x--)
     for (y=110;y>0;y--);
}
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 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 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 display(uchar shi_c,uchar ge_c)
{
   
   dula=0;
   P0=table[shi_c];
   dula=1;
   dula=0;
   wela=0;
   P0=0x7d;
   wela=1;
   wela=0;
   delay1ms(5);
   dula=0;
   P0=table[ge_c];
   dula=1;
   dula=0;
   wela=0;
   P0=0x7b;
   wela=1;
   wela=0;
   delay1ms(5);
}
void main()
{
   init();
   sec=read_add(4);//读出保存的数据赋予sec
   if(sec>100)
   sec=0;
   TMOD=0x01;
   ET0=1;
   EA=1;
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   TR0=1;
   while(1)
   {
             display(sec/10,sec%10);//分离显示函数
          if (write==1);
          {
                  write=0;
                write_add(4,sec);
          }
   }
}
void t0()interrupt 1
{
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%256;
  tcnt++;
  if(tcnt==20)
  {
           tcnt=0;
         sec++;
         write=1;
         if(sec==100)
         sec=0;
  }
}
这个程序是24c02的程序,有IIc总线,0-99循环,时间1S,希望对你有用
nickychung
5楼-- · 2019-07-17 10:02
 精彩回答 2  元偷偷看……
chenhengjin
6楼-- · 2019-07-17 15:26
问题不清楚啊,麻烦说清楚一点

一周热门 更多>