找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1311|回复: 9
收起左侧

这个程序最后哪里有错啊?第一次弄不懂

[复制链接]
ID:617359 发表于 2019-9-27 20:30 | 显示全部楼层 |阅读模式
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int

uchar code tab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

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;

uchar Flash_Count=0;
uchar num=0;
Operation_Type=1;
void DelayMS(uint x);

void Traffic_light()
{
   switch(Operation_Type)
   {
     case 1:
           RED_A=1;YELLOW_A=1;GREEN_A=0;
           RED_B=0;YELLOW_B=1;GREEN_B=1;
           Operation_Type=2;
           for(num=9;num>2;--num)
           {
              P1=tab[num];
                  DelayMS(1000);
           }
           break;
         case 2:
           for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
           {   
             P1=tab[num];
                 DelayMS(200);
                 YELLOW_A=~YELLOW_A;
                 if(Flash_Count%5==0)num--;
           }
           Operation_Type=3;
           break;
         case 3:
           RED_A=0;YELLOW_A=1;GREEN_A=1;
           RED_B=1;YELLOW_B=1;GREEN_B=0;
           for(num=7;num>0;num--)
       {
             P1=tab[num];
                 DelayMS(1000);
           }
           Operation_Type=4;
           break;
         case 4:
           num=2;
           for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
           {
             P1=tab[num];
                 DelayMS(200);
                 YELLOW_B=~YELLOW_B;
                 if(Flash_Count%5==0)num--;
           }
           Operation_Type=1;
           break;
        }
}
void main()
{
   while(1)
{
     Traffic_lignt ();
   }
}

回复

使用道具 举报

ID:351097 发表于 2019-9-27 22:23 | 显示全部楼层
Operation_Type这个变量没有定义?
回复

使用道具 举报

ID:453974 发表于 2019-9-27 23:25 | 显示全部楼层
第一个问题是你 Traffic_lignt ();抄错了,第二个问题是void DelayMS(uint x);这个没写子函数
回复

使用道具 举报

ID:453974 发表于 2019-9-27 23:26 | 显示全部楼层
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int

uchar code tab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

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;

uchar Flash_Count=0;
uchar num=0;
Operation_Type=1;
void DelayMS(uint x);

void Traffic_light()
{
   switch(Operation_Type)
   {
     case 1:
           RED_A=1;YELLOW_A=1;GREEN_A=0;
           RED_B=0;YELLOW_B=1;GREEN_B=1;
           Operation_Type=2;
           for(num=9;num>2;--num)
           {
              P1=tab[num];
                  DelayMS(1000);
           }
           break;
         case 2:
           for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
           {   
             P1=tab[num];
                 DelayMS(200);
                 YELLOW_A=~YELLOW_A;
                 if(Flash_Count%5==0)num--;
           }
           Operation_Type=3;
           break;
         case 3:
           RED_A=0;YELLOW_A=1;GREEN_A=1;
           RED_B=1;YELLOW_B=1;GREEN_B=0;
           for(num=7;num>0;num--)
       {
             P1=tab[num];
                 DelayMS(1000);
           }
           Operation_Type=4;
           break;
         case 4:
           num=2;
           for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
           {
             P1=tab[num];
                 DelayMS(200);
                 YELLOW_B=~YELLOW_B;
                 if(Flash_Count%5==0)num--;
           }
           Operation_Type=1;
           break;
        }
}
void main()
{
   while(1)
{
     Traffic_light ();
   }
}
void DelayMS(uint x)
{
        for(x=0;x<1000;x++)
        {
       
        }
}
回复

使用道具 举报

ID:149144 发表于 2019-9-27 23:28 | 显示全部楼层

Operation_Type 没有定义
有效行 第 13 行  Operation_Type=1;
改为 unsigned char Operation_Type=1;
或    uchar Operation_Type=1;
回复

使用道具 举报

ID:235200 发表于 2019-9-28 02:13 | 显示全部楼层
要说明什么错误现象呀
回复

使用道具 举报

ID:213173 发表于 2019-9-28 05:19 | 显示全部楼层
错误1.主函数调用的子函数Traffic_lignt ();与实际子函数Traffic_light()不同名
错误2.延时函数只声明了函数名,没有函数体
void DelayMS(uint x)
{
        uint i,j;
        for(i=0;i<x;i++)
                for(j=0;j<125;j++);
}
回复

使用道具 举报

ID:155507 发表于 2019-9-28 07:04 | 显示全部楼层
给你改了,对比一下就知道哪里错了。

  1. #include <reg52.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. uchar code tab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

  5. sbit RED_A=P0^0;
  6. sbit YELLOW_A=P0^1;
  7. sbit GREEN_A=P0^2;
  8. sbit RED_B=P0^3;
  9. sbit YELLOW_B=P0^4;
  10. sbit GREEN_B=P0^5;

  11. uchar Flash_Count=0;
  12. uchar num=0;
  13. uchar Operation_Type=1;
  14. void DelayMS(uint z)
  15. {
  16.         uint x,y;
  17.         for(x=z;x>0;x--)
  18.            for(y=110;y>0;y--);
  19. }

  20. void Traffic_light()
  21. {
  22.         switch(Operation_Type)
  23.         {
  24.         case 1:
  25.                 RED_A=1;YELLOW_A=1;GREEN_A=0;
  26.                 RED_B=0;YELLOW_B=1;GREEN_B=1;
  27.                 Operation_Type=2;
  28.                 for(num=9;num>2;--num)
  29.                 {
  30.                         P1=tab[num];
  31.                         DelayMS(1000);
  32.                 }
  33.                 break;
  34.         case 2:
  35.                 for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
  36.                 {   
  37.                         P1=tab[num];
  38.                         DelayMS(200);
  39.                         YELLOW_A=~YELLOW_A;
  40.                         if(Flash_Count%5==0)num--;
  41.                 }
  42.                 Operation_Type=3;
  43.                 break;
  44.         case 3:
  45.                 RED_A=0;YELLOW_A=1;GREEN_A=1;
  46.                 RED_B=1;YELLOW_B=1;GREEN_B=0;
  47.                 for(num=7;num>0;num--)
  48.                 {
  49.                         P1=tab[num];
  50.                         DelayMS(1000);
  51.                 }
  52.                 Operation_Type=4;
  53.                 break;
  54.         case 4:
  55.                 num=2;
  56.                 for(Flash_Count=1;Flash_Count<=10;Flash_Count++)
  57.                 {
  58.                         P1=tab[num];
  59.                         DelayMS(200);
  60.                         YELLOW_B=~YELLOW_B;
  61.                         if(Flash_Count%5==0)num--;
  62.                 }
  63.                 Operation_Type=1;
  64.                 break;
  65.         }
  66. }

  67. void main()
  68. {
  69.         while(1)
  70.         {
  71.                 Traffic_light(); //Traffic_lignt ();
  72.         }
  73. }


复制代码
回复

使用道具 举报

ID:466578 发表于 2019-9-28 08:25 | 显示全部楼层
带倒计时的交通信号灯程序
不过没明白你说的错是什么
回复

使用道具 举报

ID:617497 发表于 2019-9-28 10:49 | 显示全部楼层
时序不清晰
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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