标题: 刚做的交通灯的程序出现了问题不知道怎么改,求指点 [打印本页]

作者: 我在尽力    时间: 2019-7-13 08:43
标题: 刚做的交通灯的程序出现了问题不知道怎么改,求指点
#include<reg52.h>
sbit Key1 = P3^3;
unsigned char code LedChar[] = {  //数码管显示字符转换表
    0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
    0x7F, 0x6F,
};
unsigned char code LedBuff[] = {
0xFF,0xFF
};
unsigned char cnt = 0;
unsigned char sec = 0;
unsigned char cnt200 = 0;  
unsigned int cnt1000 = 0;
void LinghtenLed();
void ShowLed();
void delay();
unsigned char i;
unsigned char buf[2];   //中间转换缓冲区
void time()
{
TMOD = 0X01;
TH0 = 0xFC;
TL0 = 0x67;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
   unsigned char buf[2];   //中间转换缓冲区
  static unsigned int j;
  while (1)
    {
   if(Key1 == 0)
   {
    EA = 0;
    for(j=0;j<300;j++);
   }
   else
   {
        if (cnt200 >= 200)  //0.2s刷新
        {
            cnt200 = 0;
            LinghtenLed();
        }
        if (cnt1000 >= 1000)  //1s刷新
        {
            cnt1000 = 0;
      sec++;
      P3 = ~0x03;
            ShowLed();
        }
     for (i=1; i>=0; i--)  //从最高为开始,遇到0不显示,遇到非0退出循环
    {
        if (buf[i] == 0)
            LedBuff[i] = 0x00;
        else
            break;
    }
    for ( ; i>=0; i--)  
      {
        LedBuff[i] = LedChar[buf[i]];
    }
    }
}
}
void LinghtenLed()
{
static unsigned char flag = 0;
if(flag==0)
{
  P0=0x24;
  ShowLed();
  if(sec == 25)
  {

   sec = 0;
   flag = 1;
  }
}
  else if(flag == 1)
  {
   P0=0x90;
   ShowLed();
   if(sec == 20)
   {
    flag = 2;
    sec = 0;
   }
  }
  else if(flag == 2)
  {
   P0 = 0x48;
   ShowLed();
   if(sec == 5)
   {
    flag = 3;
    sec = 0;
   }
   
  }
  else
  {
   P0 = 0xFF;
   ShowLed();
   delay();
   P0 = ~P0;
   flag = 0;
  }
}
void ShowLed()
{
    char i;  
  
    static unsigned long min = 0;  //记录经过的秒数
   
   min++;
   buf[0]=min%10;
  buf[1]=min/10%10;
   
}
void delay()
{
static unsigned int j;
for(j=0;j<2000;j++);
j=0;
}
/* 定时器0中断服务函数 */
void InterruptTimer0() interrupt 1
{
    static unsigned char i = 0;   //动态扫描的索引
    TH0 = 0xFC;  //重新加载初值
    TL0 = 0x67;
    cnt200++;
    cnt1000++;
    P2 = 0x00;   
    switch (i)
    {
        case 0:  i++; P2=LedBuff[0]; break;
        case 1:  i++; P2=LedBuff[1]; break;
        default: break;
    }
}




作者: xianfajushi    时间: 2019-7-13 09:41
https://blog.csdn.net/xianfajushi/article/details/80553964
作者: xianfajushi    时间: 2019-7-13 09:41
https://blog.csdn.net/xianfajushi/article/details/89946765
作者: 鹏博士PBs    时间: 2019-7-13 09:42
采用PLC梯形图编程也能实现交通灯的运行
作者: admin520    时间: 2019-7-13 18:05
源码在这

51黑论坛_c源文件.rar

2.33 KB, 下载次数: 8


作者: 我在尽力    时间: 2019-7-14 16:12
xianfajushi 发表于 2019-7-13 09:41
https://blog.csdn.net/xianfajushi/article/details/80553964

谢谢,代码简洁,对我这个初学者帮助很大

作者: 我在尽力    时间: 2019-7-14 16:22
鹏博士PBs 发表于 2019-7-13 09:42
采用PLC梯形图编程也能实现交通灯的运行

学习单片机还没多久,不知道PLC,刚补了一下PLC的介绍,感觉受说的梯形图中的逻辑关系和元件状态还不是我所能理解的,也谢谢你的建议
作者: 1105488942    时间: 2019-7-14 18:55
unsigned char code LedBuff[]
报错是因为你对写到代码区的数据进行修改,把code去掉就没错了




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1