登录  | 立即注册

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

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] C51---呼吸灯并且显示数码管

[复制链接]
发表于 2023-1-18 09:10:28 | 显示全部楼层 |阅读模式
学习笔记
学习科目: C51
学习安排: 学习PWM与数码管显示
开始时间: 2023-01-18
结束时间: 2023-01-18
#include<STC12C5A60S2.h>
sbit PWMOUT= P1^3;   
sbit  X     = P2^0   ;
sbit  Y     = P2^1   ;
sbit  Z     = P2^2     ;
sbit ENLED = P1^1 ;
unsigned char HighRH=0;     
unsigned char HighRL=0;      
unsigned char LowRH=0;            
unsigned char LowRL=0;            
unsigned char T1RH=0;            
unsigned char T1RL=0;      
unsigned long periodcut=0;  
   
void configPWM(unsigned int fr, unsignedint dc) ;   
void AdjustDutyCycle(unsigned char dc);                  
void configTimer1 (unsigned int ms);
intW[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,}   ;
intA[]={0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,}      ;
inttimes=0;
void main()
{
   ENLED = 0 ;
    X= 0 ;
    Y= 0 ;
    Z= 0 ;
       TMOD=0x11;
       P0=W[0];
       configPWM(1000,0)     ;
       configTimer1(1);  
       EA= 1 ;
       while(1);
}
void configTimer1 (unsigned int ms)                     
{
       unsignedlong tmp;
       tmp=32000000/12;
       tmp=tmp*ms/1000;
       tmp=65536-tmp;
       tmp=tmp+12;
       T1RH=(unsignedchar)(tmp>>8);
       T1RL=(unsignedchar)tmp;
       //TMOD&=0x0F;
       //TMOD|=0x10;
       TH1=T1RH;
       TL1=T1RL;
       ET1=1;
       TR1=1;
}
void configPWM(unsigned int fr, unsignedint dc)
       unsignedint high,low;
       periodcut=32000000/12/fr;
       high=(periodcut*dc)/100;
       low= periodcut - high ;
       high=65536 - high +12;
       low=65536-low+12;
       HighRH=(unsignedchar)(high>>8);
       HighRL=(unsignedchar)high;
       LowRH=(unsignedchar)(low>>8);
       LowRL=(unsignedchar)low;
       //TMOD&= 0xF0;
       //TMOD|= 0x01;
       TH0=HighRH       ;
       TL0=HighRL;
       ET0=1;
       TR0=1;
       PWMOUT=0;
}
void AdjustDutyCycle(unsigned char dc){         
       unsignedint high,low;
       high=(periodcut*dc)/100;
       low= periodcut - high ;
       high=65536 - high +12;
       low=65536-low+12;
       HighRH=(unsignedchar)(high>>8);
       HighRL=(unsignedchar)high;
       LowRH=(unsignedchar)(low>>8);
       LowRL=(unsignedchar)low;
}      
void InterruptTime0() interrupt 1
{
   
              
           if(PWMOUT==0)   }
              TH0= LowRH;
              TL0= LowRL;
              PWMOUT=1;
              }
              else              
              {
               TH0 = HighRH;
               TL0 = HighRL;
               PWMOUT=0;
              }
      
}      
      
void InterruptTime1() interrupt 3
{
        static unsigned char delay =0;
        static int dir=0;
        static unsigned char index = 0 ;
        static    unsignedint U=0 ;
      
        TH1 = T1RH;
        TL1 = T1RL;
               AdjustDutyCycle(index);
       delay++;
       if(delay==5)
       {
               if(dir==0)
              {                    
                      index++;
                      if(index==99){
                            dir=1;                                                      
                      }
               }
               else
               {
                     index--;
                     if(index==0)
                     {
                            dir=0;
                           
                            times++;
                            A[0]=W[times%10];
                            A[1]=W[times/10%10];
                            A[2]=W[times/100%10];
                            A[3]=W[times/1000%10];
                            A[4]=W[times/10000%10];
                            A[5]=W[times/100000%10];  
                     }
               }
               delay=0;
        }
        U++;
         switch(U){
               case 1 :      X=1; Y=0; Z=10=A[0]; break ;
               case 2 :        X=0; Y=0; Z=10=A[1]; break ;
               case 3 :        X=1; Y=1; Z=0; P0=A[2];break ;
               case 4 :        X=0; Y=1; Z=00=A[3]; break ;
                case 5 :         X=1; Y=0; Z=00=A[4]; break ;
                case 6 :         X=0; Y=0; Z=00=A[5]; break ;   
              }
              U=U%6;   
}
//很久之前写的代码了所以没有注释(现在也懒了)
//部里老板子 可能引脚有冲突可以自己改一下
//小萌新不太会写代码 如果有问题大佬们尽可留言或私信我 谢谢~~~

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

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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