找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1669|回复: 2
打印 上一主题 下一主题
收起左侧

求解单片机红绿灯程序一直跳不出循环,一直在9-4循环

[复制链接]
跳转到指定楼层
楼主
#include<reg51.h>
  #define p1 P1
  #define uchar unsigned char
  #define uint unsigned int
  sbit gr0=P2^0;
  sbit ye0=P2^1;
  sbit re0=P2^2;
  sbit gr1=P2^3;
  sbit ye1=P2^4;
  sbit re1=P2^5;
  code unsigned shu[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
  code unsigned shux[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
  uchar type=1,flash=0;
  uchar temp,red=10;
  
  void delay(uint x)
  {
  uchar i;
  while(x--)
  for(i=0;i<120;i++);
  }
  void main()
  {
  switch(type)
  {
  case 1:
  re0=0;ye0=0;gr0=1;
  re1=1;ye1=0;gr1=0;
  temp=red;
  while(temp--)
  {
  P1=shu[temp];
  delay(1000);
  if(temp==4)break;
  }
  type=2;
  break;
  case 2:
  gr0=0;
  if(++flash!=6) return;
  while(temp--)
  {
  ye0=~ye0;
  P1=shu[temp];
  delay(1000);
  if(temp==4)
  break;
  }
  type=3;
  break;
  case 3:
  re0=1;ye0=0;gr0=0;
  re1=0;ye1=0;gr1=1;
  temp=red;
  while(temp--)
  {
  P3=shux[temp];
  delay(1000);
  if(temp==4)break;  
  }
  type=4;
  case 4:
  gr1=0;
  if(++flash!=6)return;
  while(temp--)
  {
  ye1=~ye1;
  P3=shux[temp];
  delay(1000);  
  }
  flash=0;
  type=1;
  }  
  }


51hei截图_20200616003931.png (64.56 KB, 下载次数: 53)

51hei截图_20200616003931.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:24758 发表于 2020-6-16 12:49 | 只看该作者
程序有点小问题,会导致*** error 65: access violation at C:0x020A : no 'execute/read' permission。
建议你将switch结构放到while里面,然后将case 2 和case 4中的 if(++flash!=6) return; 这个语句改写成其他形式。
回复

使用道具 举报

板凳
ID:739545 发表于 2020-6-16 13:23 | 只看该作者
if(temp==4)break;自己不是设置到从9~4自动跳出么? 代码写的可读性真差。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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