专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

单片机交通灯实验

作者:佚名   来源:本站原创   点击数:  更新时间:2013年07月18日   【字体:

   今天晚上12点准备睡觉,突然睡不着觉,脑子里想的都是单片机,呵呵,可能感觉来了吧,起来画图,编程序,搞到现在,嘿嘿,功夫不负有心人吧,弄了两三天的交通灯程序终于差不多完成了,就是调试时间还有点问题,调试了很久,不过现在跟之前的要求差不多了,嘿嘿。

参考资料:http://www.51hei.com/mcu/58.html
程序:#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit red1=P0^2;
sbit yellow1=P0^1;
sbit green1=P0^0;
sbit red2=P0^7;
sbit yellow2=P0^6;
sbit green2=P0^5;
void delay(int);//调用程序声明
void main()
{
  while(1)
   {
   red1=1;yellow1=0;green1=0;//南北红灯亮
   red2=0;yellow2=0;green2=1;//东西绿灯亮
   delay(500);//延时
   red1=1;yellow1=0;green1=0;//南北红灯亮
   red2=0;yellow2=1;green2=0;//东西黄灯亮
      delay(500);
   red1=0;yellow1=0;green1=1;//南北绿灯亮
   red2=1;yellow2=0;green2=0;/东西红灯亮
   delay(500);
   red1=0;yellow1=1;green1=0;//南北黄灯亮
   red2=1;yellow2=0;green2=0;//东西红灯亮。
   delay(500);
   }
}
void delay(int z)
{
 uint i,x;
  for(i=z;i>0;i--)
  for(x=600;x>0;x--);
}//延时子程序

PROTEUS的仿真图:
 


   我的RED1,YELLOW1,GREEN1灯代表的是南北的红,黄,绿三个灯,RED2,YELLOW2;GREEN2代表的是东西的红黄绿灯;RED1YELLOW1;GREEN1分别由P0^2;P0^1;P0^0;控制。RED2; YELLOW2  ;GREEN2分别由P0^7;   P0^6;  P0^5;控制。呵 呵。接下来的几天应该是找零件自己焊接并把时间的精确调试弄懂了。有点困了,睡觉觉去也。

关闭窗口

相关文章