zoukankan      html  css  js  c++  java
  • 数码管流水灯升级程序理解

      1 /*
      2 该程序的功能:
      3 利用动态扫描和定时器1在数码管上显示出从765432开始,
      4 以1/10秒的速度往下递减直至765398并保持显示此数,,
      5 与此同时利用定时器0以500MS速度进行流水灯从上至下移动,
      6 当数码管上数减到停止时,实验板上流水灯也停止然后全部开始闪烁,
      7 3秒后(用T0定时)流水灯全部关闭、数码管上显示出“HELLO”。到此保持住。
      8 */
      9 #include <reg52.h>
     10 #include <intrins.h>
     11 #define uchar unsigned char
     12 #define uint unsigned int
     13 sbit dula=P2^6;
     14 sbit wela=P2^7;
     15 uchar code table[]={
     16 0x3f,0x06,0x5b,0x4f,
     17 0x66,0x6d,0x7d,0x07,
     18 0x7f,0x6f,0x77,0x7c,
     19 0x39,0x5e,0x79,0x71,
     20 0x76,0x79,0x38,0x3f,0}; //后面几个表示为hello这个字母的编码
     21 uchar temp,t0,t1,bai,shi,ge,flag,flag1;     
     22 uint shu;
     23 void init();
     24 void display(uchar,uchar,uchar,uchar,uchar,uchar);
     25 void delay(uint z)
     26 {
     27     uint t1,y;
     28     for(t1=z;t1>0;t1--)
     29         for(y=110;y>0;y--);
     30 }
     31 void main()
     32 {
     33     init();
     34     while(1)
     35     {
     36         if(flag1!=1) //如果标志位flag 不为1 时,也就是数码管换依旧保持着 765398 这个数字 
     37             display(7,6,5,bai,shi,ge);
     38         else       // flag1标志位为1时,流水灯也已经闪过三秒,此时显示 hello
     39             display(16,17,18,18,19,20); //直接用写在编码table中的数据 H表示编码中的第16个数,以此类推      
     40     }
     41 }
     42 
     43 void init()
     44 {
     45     shu=432;  //数的初值
     46     temp=0xfe;
     47     P1=temp;
     48     TMOD=0x11;
     49     TH0=(65536-50000)/256;
     50     TL0=(65536-50000)%256;
     51     TH1=(65536-50000)/256;
     52     TL1=(65536-50000)%256;
     53     EA=1;
     54     ET0=1;
     55     ET1=1;
     56     TR0=1;
     57     TR1=1;
     58 }
     59 
     60 void timer0() interrupt 1
     61 {
     62     TH0=(65536-50000)/256;
     63     TL0=(65536-50000)%256;
     64     t0++;
     65     if(flag!=1)       //这个flag 表示如果数码管上的数还没有从432减到398时,执行下面程序
     66         {
     67         if(t0==10)
     68             {
     69                 t0=1;
     70                 temp=_crol_(temp,1);
     71                 P1=temp;
     72             }
     73         }
     74     else   //如果减到了398的时候,开始执行下面的程序
     75         {
     76             if(t0%4==0)      // 表示 每200ms闪烁  led灯闪烁一次   也就说当遇到4的整数倍时,P1口的灯就会变化
     77                 P1=~P1;          
     78             if(t0==60)    //表示定时满足3秒时, 50ms * 60 = 3S 
     79             {
     80                 TR0=0;    //     当3秒后,定时器0停止计数
     81                 P1=0xff;//P1口全为高电平,关闭流水灯
     82                 flag1=1; //关闭流水灯的同时,使得标志位 flag1 为1
     83             }
     84         }    
     85 }
     86 
     87 void timer1() interrupt 3
     88 {
     89     TH1=(65536-50000)/256;
     90     TL1=(65536-50000)%256;
     91     t1++;
     92     if(t1==2)
     93         {
     94             t1=0;
     95             shu--;
     96             bai=shu/100;  //取出后三位数
     97             shi=shu%100/10;
     98             ge=shu%10;
     99             if(shu==398)
    100                 {
    101                     TR0=0;
    102                     TH0=(65536-50000)/256; //此处的TH0和TL0表示再次设定初值,定时为3秒;用于满足当数从432减到398这个要求后,流水灯不在流动,并且要闪烁三秒
    103                     TL0=(65536-50000)%256;
    104                     TR0=1;
    105                     flag=1;//用于表示设定初值开始3秒定时的启动
    106                     t0=0;    //由于t0中本来就有初值,故而将其复位为0
    107                     P1=0xff;//关闭所有的流水灯
    108                     TR1=0;     //定时器1停止工作
    109                 }    
    110         }    
    111 }
    112 
    113 void display(uchar aa,uchar bb,uchar cc,uchar ba,uchar sh,uchar g)
    114 {
    115         dula=1;
    116         P0=table[aa];
    117         dula=0;
    118         P0=0xff;
    119         wela=1;
    120         P0=0xfe;
    121         wela=0;
    122         delay(1);
    123 
    124            dula=1;
    125         P0=table[bb];
    126         dula=0;
    127         P0=0xff;
    128         wela=1;
    129         P0=0xfd;
    130         wela=0;
    131         delay(1);
    132 
    133        dula=1;
    134         P0=table[cc];
    135         dula=0;
    136         P0=0xff;
    137         wela=1;
    138         P0=0xfb;
    139         wela=0;
    140         delay(1);
    141         dula=1;
    142         P0=table[ba];
    143         dula=0;
    144         P0=0xff;
    145         wela=1;
    146         P0=0xf7;
    147         wela=0;
    148         delay(1);
    149 
    150         dula=1;
    151         P0=table[sh];
    152         dula=0;
    153         P0=0xff;
    154         wela=1;
    155         P0=0xef;
    156         wela=0;
    157         delay(1);
    158 
    159         dula=1;
    160         P0=table[g];
    161         dula=0;
    162         P0=0xff;
    163         wela=1;
    164         P0=0xdf;
    165         wela=0;
    166         delay(1);
    167 }
    View Code
  • 相关阅读:
    c++入门之初话结构体
    c++学习之字符串拼接
    数组赋值问题
    c++之sizeof的用法
    MySQL 创建一个简单的成绩管理系统
    KMP算法详解
    [Leetcode] Implement strstr
    [Leetcode] Multiply strings 字符串对应数字相乘
    [Leetcode] count and say 计数和说
    [Leetcode] Roman to integer 罗马数字转成整数
  • 原文地址:https://www.cnblogs.com/wises/p/9539581.html
Copyright © 2011-2022 走看看