找回密码
 立即注册

QQ登录

只需一步,快速开始

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

红绿交通信号灯代码

[复制链接]
跳转到指定楼层
楼主
ID:353499 发表于 2018-6-17 23:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>//包含头文件,一般情况不需要改动,
//头文件包含特殊功能寄存器的定义
sbit LACTCH1=P2^2;   
sbit LACTCH2=P2^3;
/*------------------------------------------------
                 全局变量
------------------------------------------------*/
bit red,green,yellow;//定义红绿黄灯标志位
code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
                                        //共阴数码管 0-9
static unsigned char second=30,count;
unsigned char Dis_Shiwei;//定义十位
unsigned char Dis_Gewei; //定义个位
/*------------------------------------------------
              
------------------------------------------------*/
void delay(unsigned int cnt)
{
while(--cnt);
}
/*------------------------------------------------
              主程序
------------------------------------------------*/
main()
{
TMOD=0x01;//定时器设置50ms in 12M crystal
TH0=0x3c;
TL0=0xb0;
IE= 0x82;   //打开中断
TR0=1;
P1=0xfc;    //红灯亮
red=1;
while(1)
  {
  P0=Dis_Shiwei;//显示十位
  LACTCH1=1;    //锁存
  LACTCH1=0;
  P0=0xfe;
  LACTCH2=1;    // 锁存
  LACTCH2=0;
  delay(300);   //短暂延时
  P0=Dis_Gewei; //显示个位
  LACTCH1=1;    //锁存
  LACTCH1=0;
  P0=0xfd;
  LACTCH2=1;    // 锁存
  LACTCH2=0;
  delay(300);
  }
}
/*------------------------------------------------
             定时器0中断函数
------------------------------------------------*/
void tim(void) interrupt 1
{
TH0=0x3c;//重新赋值
TL0=0xb0;
count++;
if (count==20)
    {
    count=0;
    second--;//秒减1
        if(second==0)
           {
       if(red)
             {
                  red=0;green=1;
          second=27;
                  P1=0xF3;//绿灯亮27秒
                 }
           else if(yellow)
             {
                  yellow=0;red=1;
          second=30;
                  P1=0xFC;//红灯亮30秒
                 }
           else if(green)
             {
                 green=0;yellow=1;
         second=3;
                 P1=0xCF;//黄灯亮3秒
                                 }
           }
        Dis_Shiwei=tab[second/10];//十位显示值处理
    Dis_Gewei=tab[second%10]; //个位显示处理
   
    }
        }

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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