找回密码
 立即注册

QQ登录

只需一步,快速开始

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

交通灯设计

[复制链接]
跳转到指定楼层
楼主
ID:246258 发表于 2017-11-5 16:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**************************************************************************************
*                              ½»í¨μÆêμÑé                                                                                                  *
êμÏÖÏÖÏó£oÏÂÔØ3ìDòo󣬰′ÕÕêóÆμ2ù×÷Õyè·½óÏߣ¬3êÏÖ½»í¨μÆÏÖÏó
×¢òaêÂÏî£o                                                                                                                                                                  
***************************************************************************************/

#include "stc15.h"                         //′ËÎļtÖD¶¨òåáËμ¥Ƭ»úμÄò»D©ìØêa1|Äü¼Ä′æÆ÷

typedef unsigned int u16;          //¶Ôêy¾YààDí½øDDéùÃ÷¶¨òå
typedef unsigned char u8;


u8 code DuanMa[10]={
//                                 0    1    2    3    4    5    6    7    8    9   
                               0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

u8 Second;

/*******************************************************************************
* oˉ êy Ãû         : Delay5ms()
* oˉêy1|Äü                   : Ñóê±oˉêy£¬′óÔ¼Ñóê±5Ms
*******************************************************************************/
void Delay5ms()                //@11.0592MHz
{
        unsigned char i, j;

        i = 54;
        j = 199;
        do
        {
                while (--j);
        } while (--i);
}

/*******************************************************************************
* oˉ êy Ãû         : Timer0Init
* oˉêy1|Äü                   : ¶¨ê±Æ÷03õê¼»ˉ
* êä    èë         : ÎT
* êä    3ö         : ÎT
*******************************************************************************/
void Timer0Init()
{
        TMOD|=0X01;//Ñ¡ÔñÎa¶¨ê±Æ÷0ģ꽣¬1¤×÷·½ê½1£¬½öóÃTR0′ò¿aÆô¶ˉ¡£

        TH0=0XFC;        //¸ø¶¨ê±Æ÷¸33õÖ죬¶¨ê±1ms
        TL0=0X18;       
        ET0=1;//′ò¿a¶¨ê±Æ÷0ÖD¶ÏÔêDí
        EA=1;//′ò¿a×üÖD¶Ï
        TR0=1;//′ò¿a¶¨ê±Æ÷                       
}
void GPIO_Init()        //3õê¼»ˉI/O¿ú
{
        P0M1=0;
        P0M0=0;
        P1M1=0;
        P1M0=0;
        P2M1=0;
        P2M0=0;
        P3M1=0;
        P3M0=0;
        P4M1=0;
        P4M0=0;
        P5M1=0;
        P5M0=0;
}
/*******************************************************************************
* oˉ êy Ãû       : main
* oˉêy1|Äü                 : Ö÷oˉêy
* êä    èë       : ÎT
* êä    3ö             : ÎT
*******************************************************************************/
void main()
{         
        GPIO_Init();
//        Second = 1;
       
        Timer0Init();
//                if(67<Second&&Second)
//                    {  Second=1;
//                                }
       

        while(1)
        {
                if(1<Second&&Second<=30)
        {
       
          P20=0;P21=1;P22=1;P23=1;
                 P0=DuanMa[(30-Second)/10];
          Delay5ms();
       
                P20=1;P21=0;P22=1;P23=1;
          P0=DuanMa[(30-Second)%10];
                Delay5ms();
               
                P20=1;P21=1;P22=0;P23=1;
                 P0=DuanMa[(34-Second)/10];
          Delay5ms();
       
                P20=1;P21=1;P22=1;P23=0;
          P0=DuanMa[(34-Second)%10];
                Delay5ms();
                P25=0;P41=0;P43=1;P24=1;
        }
        if(30<Second&&Second<=34)
        {
               
          P20=0;P21=1;P22=1;P23=1;
                 P0=DuanMa[(34-Second)/10];
          Delay5ms();
       
                P20=1;P21=0;P22=1;P23=1;
          P0=DuanMa[(34-Second)%10];
                Delay5ms();
               
                P20=1;P21=1;P22=0;P23=1;
                 P0=DuanMa[(34-Second)/10];
          Delay5ms();
       
                P20=1;P21=1;P22=1;P23=0;
          P0=DuanMa[(34-Second)%10];
                Delay5ms();
                P25=1;P26=0;P41=0;P24=1;
        }
       
        if(34<Second&&Second<=63)
        {
               
                P20=1;P21=1;P22=0;P23=1;
                P0=DuanMa[(63-Second)/10];
                Delay5ms();
               
                P20=1;P21=1;P22=1;P23=0;
                P0=DuanMa[(63-Second)%10];
                Delay5ms();
               
                P20=0;P21=1;P22=1;P23=1;
                 P0=DuanMa[(67-Second)/10];
          Delay5ms();
       
                P20=1;P21=0;P22=1;P23=1;
          P0=DuanMa[(67-Second)%10];
                Delay5ms();
                P26=1;P41=1;P42=0;P24=0;
        }
        if(63<Second&&Second<=67)
        {
       
          P20=0;P21=1;P22=1;P23=1;
                 P0=DuanMa[(67-Second)/10];
          Delay5ms();
       
                P20=1;P21=0;P22=1;P23=1;
          P0=DuanMa[(67-Second)%10];
                Delay5ms();
               
               
               
                P20=1;P21=1;P22=0;P23=1;
                 P0=DuanMa[(67-Second)/10];
          Delay5ms();
       
                P20=1;P21=1;P22=1;P23=0;
          P0=DuanMa[(67-Second)%10];
                Delay5ms();
                P42=1;P43=0;P24=0;
                }
                if(67<Second)
                    {  Second=1;
                                }
        }                                       
}



/*******************************************************************************
* oˉ êy Ãû         : void Timer0() interrupt 1
* oˉêy1|Äü                   : ¶¨ê±Æ÷0ÖD¶Ïoˉêy
* êä    èë         : ÎT
* êä    3ö         : ÎT
*******************************************************************************/
void Timer0() interrupt 1
{
        static u16 i;
        TH0=0XFC;        //¸ø¶¨ê±Æ÷¸33õÖ죬¶¨ê±1ms
        TL0=0X18;
        i++;
        if(i==1000)
        {
                i=0;
                Second ++;       
        }       
}

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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