微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > proteus交通灯仿真数码管不显示

proteus交通灯仿真数码管不显示

时间:10-02 整理:3721RD 点击:
/* Main.c file generated by New Project wizard
*
* Created:   周一 12月 5 2016
* Processor: 80C51
* Compiler:  Keil for 8051
*/
#include <reg51.h>
#include <stdio.h>
unsigned char num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char ctr[]={0x7f,0xf7,0xfe,0xfd};
unsigned char table[4]={0,0,0,0};
unsigned char v;  
unsigned int scdEW,scdSN;
unsigned int cntSN,cntEW;                  
unsigned char cntP3_4,cntP3_5,cntP3_6;
unsigned int cnt_num;
int greenEW = 6,yellowEW = 4,greenSN = 8,yellowSN = 4;
int EWSN;
unsigned char a;temp;
sbit IO=P2^0;   
sbit SCLK=P2^1;
sbit RST=P2^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit YY=P3^4;
sbit aa=P1^3;
sbit ab=P1^4;
sbit ac=P1^5;
sbit bb=P1^0;
sbit bc=P1^1;
sbit bd=P1^2;
void delay_ms(unsigned int n)
{
int i;
while(n--)
{
  i = 70;
  while(i--);
}
}
void display(void)
{
  delay_ms(2);
  P3 = 0xff;   
  P3 = ctr[v];   
  P0 = num[table[v]];   
  v++;
  if(v == 4)
     {
        v = 0;
     }
}
void write_byte(unsigned char dat)
{
ACC=dat;
RST=1;
for(a=8;a>0;a--)
{
  IO=ACC0;   
  SCLK=0;
  SCLK=1;
  ACC=ACC>>1;
}
}   
unsigned char read_byte()   
{
RST=1;
for(a=8;a>0;a--)
{
  ACC7=IO;
  SCLK=1;
  SCLK=0;
  ACC=ACC>>1;
}
return(ACC);
}  
void write_1302(unsigned char add,unsigned char dat)
{
RST=0;
SCLK=0;
RST=1;
write_byte(add);
write_byte(dat);
SCLK=1;
RST=0;
}
unsigned char read_1302(unsigned char add)   
{
unsigned char temp;
RST=0;
SCLK=0;
RST=1;
write_byte(add);
temp=read_byte();
SCLK=1;
return(temp);
}
unsigned char BCD_Decimal(unsigned char bcd)
{
unsigned char Decimal;
Decimal=bcd>>4;
return(Decimal=Decimal*10+(bcd&=0x0F));
}
void ds1302_int()   
{
RST=0;
SCLK=0;
write_1302(0x8e, 0x00);
write_1302(0x8e, 0x80);
}
void main(void)
{
unsigned char h;
unsigned char j=0;
TMOD=0x02;
TH0=0x06;  
TL0=0x06;  
EA=1;   
ET0=1;  
TR0=1;  
EX0=1;  
P1=0xf3;   
YY=1;  
scdEW=greenEW;     
scdSN=greenEW;  
table[0]=scdEW/10;
   table[1]=scdEW%10;
    table[2]=scdSN/10;
   table[3]=scdSN%10;
ds1302_int();

while(1)
{
   h=BCD_Decimal(read_1302(0x85));
   if((h>6&&h<8)||(h>11&&h<13)||(h>16&&h<19))
   {
    if(j!=1)
    {
     TR0=0;
     greenEW=15;
    greenSN=20;
     scdEW=greenEW;     
    scdSN=greenEW;  
    cntEW=7;
    cntSN=7;
    P1=0xf3;
    j=1;
    TR0=1;
    }
  }
  else j=0;
  if(P3^4==0)
  {
   TR0=0;
   cntP3_4++;
   while(!P3^4);
   if(cntP3_4==1)
   {
    scdEW=greenEW;   
    scdSN=greenEW;
    EWSN=greenEW;
    cntEW=7;
    P1=0xf7;
   
   }
   else if(cntP3_4==2)
   {
    scdEW=greenSN;   
    scdSN=greenSN;
    EWSN=greenSN;
    cntSN=3;
    P1=0xfe;
   }
   
   else
   {
    cntP3_4=0;
    TR0=1;
    cntEW = 7;
    cntSN = 7;
    cnt_num = 0;
    cntP3_4 = 0;
   }
   table[0] = scdEW/10;
     table[1] = scdEW%10;
      table[2] = scdSN/10;
     table[3] = scdSN%10;
   display();
  }
  if(P3^5==0)
  {
    EWSN++;
    while(!P3^5);         
    if(EWSN==100)
    {
     EWSN=0;
    }
    scdEW = EWSN;   
    scdSN = EWSN;  
    if(cntP3_4==1)
     greenEW=EWSN;
    else if(cntP3_4==2)
     greenSN=EWSN;
                 
   table[0]=scdEW/10;
     table[1]=scdEW%10;
      table[2] = scdSN/10;
     table[3] = scdSN%10;
  }
  display();
  if(P3^6==0)
  {
     EWSN--;
    while(!P3^6);
    if(EWSN==0)
    {
     EWSN=99;
    }
    scdEW=EWSN;
    scdSN=EWSN;
    if(cntP3_4==1)
     greenEW=EWSN;
    else if(cntP3_4==2)
     greenSN=EWSN;
    table[0]=scdEW/10;
      table[1]=scdEW%10;
       table[2] = scdSN/10;
      table[3]=scdSN%10;
  }
  display();
}
}
void t0(void) interrupt 1
{
cnt_num++;
if(cnt_num == 4000)
    {
       cnt_num = 0;
   if(scdEW-- == 0)
        {
   cntEW++;
  }
     if(scdSN-- == 0)
        {
   cntSN++;
  }
    }
switch(cntEW)
{
case 1:
  scdEW = yellowEW;
  aa=1;
  ab=0;
  ac=1;
  cntEW++;
  break;
case 3:  
  scdEW = greenSN;  
  aa=1;
  ab=1;
  ac=0;
  cntEW++;
  break;
case 5:
  scdEW = yellowEW;
  aa=1;
  ab=0;
  ac=1;
  cntEW++;
  break;
case 7:  
  scdEW = greenEW;     
  aa=0;
  ab=1;
  ac=1;
  cntEW=0;
  break;
}
switch(cntSN)
{
case 1:
  scdSN = yellowSN;
  bb=1;
  bc=0;
  bd=1;
  cntSN++;
  break;
case 3:  
  scdSN = greenSN;
  bb=0;
  bc=1;
  bd=1;

  cntSN++;
  break;
case 5:
  scdSN = yellowSN;
  bb=1;
  bc=0;
  bd=1;
  cntSN++;
  break;
case 7:  
  scdSN = greenEW;  
  bb=1;
  bc=1;
  bd=0;
  cntSN=0;
  break;
}
   table[0] = scdEW/10;
     table[1] = scdEW%10;
      table[2] = scdSN/10;
     table[3] = scdSN%10;
  
}


看看,什么好东西啊

感觉小编线路设计有缺陷

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top