找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 767|回复: 1
收起左侧

新手求助,AT89C51单片机Proteus仿真交通灯

[复制链接]
ID:1138816 发表于 2024-12-11 20:59 | 显示全部楼层 |阅读模式
为啥数码管不能正确显示啊(代码借鉴了论坛里的)
9F2F1D058C458BE42DA9236ED9BCCDEE.png

#include <REGX51.H>

sbit RED_A=P0^0;  
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3;   
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;

unsigned char seg[]={0xC0,0xF9,0xA4,0xB0,0x99,
                           0x92,0x82,0xF8,0x80,0x90};
unsigned char wei[]={0xFE,0xFD};
unsigned int count0=0,timeleft=5,Time_Count=0,
                   Flash_Count=0,Operation_Type=1;

void  inittimer()
{
        TMOD=0x01;
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        ET0=1;
        EA=1;
        TR0=1;
               
}        

void delay(unsigned int time)
{
        unsigned int i,j;
        
        for(i=0;i<time;i++)
        {
                for(j=0;j<120;j++);
        }
}        

void display(unsigned int time, unsigned char port)
{
    unsigned char tens=time/10;
    unsigned char units=time%10;

    P3 = wei[0];
    if (port==1)
                {
        P1=seg[tens];
    }
                else
                {
        P2=seg[tens];
    }
    delay(1);

    P3 = wei[1];
    if (port==1)
                {
        P1 = seg[units];
    }
                else
                {
        P2 = seg[units];
    }
    delay(1);
}

void main()
{
        inittimer();
        while(1){};
}

void T0_INT() interrupt 1
{
        TL0=(65536-50000)%256;
        TH0=(65536-50000)/256;
        
        count0++;
  if(count0>=20)
  {
     if (timeleft>0)
     {
        timeleft--;
     }  
     count0=0;
  }
        
        switch(Operation_Type)
        {
                case 1:        
                                RED_A=1;YELLOW_A=1;GREEN_A=0;
              RED_B=0;YELLOW_B=1;GREEN_B=1;
                                if(++Time_Count!=100)
                                return;
                                display(timeleft,2);
                                Time_Count=0;
                                Operation_Type=2;
                                break;

                case 2:        
        if(++Time_Count!=10)
                                return;   
        Time_Count=0;
                                YELLOW_A=~YELLOW_A;GREEN_A=1;
                                if(++Flash_Count!=10)
                                return;
                                display(timeleft,2);
                                Flash_Count=0;
                                Operation_Type=3;
                                break;

                case 3:
                                RED_A=0;YELLOW_A=1;GREEN_A=1;
                                RED_B=1;YELLOW_B=1;GREEN_B=0;
                                if(++Time_Count!=100)
                                return;
                                display(timeleft,1);
                                Time_Count=0;
                                Operation_Type=4;
                                break;

                case 4:
                                if(++Time_Count!=10)
                                return;     
        Time_Count=0;
        YELLOW_B=~YELLOW_B;GREEN_B=1;
        if(++Flash_Count!=10)
                                return;
                                display(timeleft,1);
        Flash_Count=0;
        Operation_Type=1;
                                break;
        }
}

回复

使用道具 举报

ID:161164 发表于 2024-12-12 15:34 | 显示全部楼层
不时显示了吗?
有问题请描述问题,预计表现,实际表现,提供代码,报错信息
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表