登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 交通灯

[复制链接]
发表于 2022-1-17 19:34:31 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 单片机
学习安排: 单片机
开始时间: 2022-01-17
结束时间: 2022-01-17
直接看程序
#include<reg52.h>

sbit ENLED=P1^1;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;

unsigned char ledchar[]={
        0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
        0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,   //êy&#194;&#235;1ü&#213;&#230;&#214;μ
};
unsigned char ledbuff[7]={
        0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
bit flag1s=1;
unsigned char T0RH=0;
unsigned char T0RL=0;

void configTimer0(unsigned int ms);
void TrafficLight();

void main()
{
        EA=1;
        ENLED=0;
        configTimer0(1);
        while(1)
        {
          if(flag1s==1)
          {
                  flag1s=0;
                  TrafficLight();
                }
        }
}

void TrafficLight()
{
        unsigned char i=0;
        static unsigned char color=2;
        static unsigned char timer=0;
       
        if(timer==0)
        {
                switch(color)
                {
                        case 0: color=1;timer=2;ledbuff[6]=0xE7;break;
                        case 1: color=2;timer=29;ledbuff[6]=0xFC;break;
                        case 2: color=0;timer=39;ledbuff[6]=0x3F;break;
                        default:break;
                }
        }
        else
        {
                timer--;
        }
        ledbuff[0]=ledchar[timer%10];
        ledbuff[1]=ledchar[timer/10%10];
       
        ledbuff[5]=0xFF;
        ledbuff[4]=0xFF;
        ledbuff[3]=0xFF;
        ledbuff[2]=0xFF;
}

void configTimer0(unsigned int ms)
{
        unsigned long tmp;
        tmp=32768000/12;
        tmp=tmp*ms/1000;
        tmp=65536-tmp;
        T0RH=(unsigned char)(tmp>>8);
        T0RL=(unsigned char)tmp;
        TMOD&=0xF0;
        TMOD|=0x01;
        TH0=T0RH;
        TL0=T0RL;
        TR0=1;
        ET0=1;
}

void ledscan()
{
        static unsigned char i=0;
        P0=0xFF;
        if(i==0)
        {P20=121=022=1;i++0=ledbuff[0];}
        else if(i==1)
        {P20=021=022=1;i++0=ledbuff[1];}
        else if(i==2)
        {P20=121=122=0;i++0=ledbuff[2];}
        else if(i==3)
        {P20=021=1;P22=0;i++;P0=ledbuff[3];}
        else if(i==4)
        {P20=1;P21=0;P22=0;i++;P0=ledbuff[4];}
        else if(i==5)
        {P20=0;P21=0;P22=0;i++;P0=ledbuff[5];}
        else if(i==6)
        {P20=0;P21=1;P22=1;i=0;P0=ledbuff[6];}
}
void interruptTime0() interrupt 1
{
        static unsigned int tmr1s=0;
       
        TH0=T0RH;
        TL0=T0RL;
        ledscan();
        tmr1s++;
        if(tmr1s>=1000)
        {
                tmr1s=0;
                flag1s=1;
        }
       
}

好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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