51 single chip microcomputer colorful LED light gradient dimming color adjustment program

Publisher:火星Latest update time:2015-05-25 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//----------------------------------------------------------------------------------------------------
// Main.c
//----------------------------------------------------------------------------------------------------
// Copyright (C) 2013 Shenyang JYXD.
// WEN BO RUI
// Tool chain: KEIL Full 'c'
//
//#pragma CD OE DB SB // Compilation directives
#include
#include
#define uchar unsigned char
#define uint unsigned int
uint Period; //Period refresh perioduint
PWM;
uint Pulse; //Number of pulsesuchar
Speed; //Speed ​​variableuchar
count;
uint excount;
bit Gra; //Gradual change/jump control
bitbit Gra7; //Colorful gradual light and extinguishing control bitbit
Gra3; //Red, green and blue gradual light and extinguishing control bitbit
BP; //Light to dark, dark to light conversion bitbit
LEDdelay; //LED timing to flag 
bitsbit key1 = P3^1; //Pattern combination key 1  
sbit key2 = P3^0; //pattern key combination 2 
sbit key3 = P3^5; //speed key combination 1
sbit key4 = P3^4; //speed key combination 2
uchar led1[12]={0x10,0x030,0x20,0x60,0x40,0x50,0x70,0x50,0x40,0x60,0x20,0x30};//red, yellow, green, cyan, blue, pink, white, pink, blue, cyan, green, yellow
uchar led2[24]={0x10,0x00,0x30,0x00,0x20,0x00,0x60,0x00,0x40,0x00,0x50,0x00,
    0x70,0x00,0x50,0x00,0x40,0x00,0x60,0x00,0x20,0x00,0x30,0x00};
uchar led3[3]={0x10,0x20,0x40}; //Red, green, blue
uchar ledcount,ledcount1;
//***********************************************************
//The following functions are defined
// 1 millisecond delay function delayms(); pattern and speed scanning function keyscan();
//***************************************************************
/*void delayms(unsigned int ms)
{
    unsigned char i ;
    while(ms--)
     {
        for(i = 0 ; i < 120 ; i++) ;
     }
}*/
void Pattern1(); //Pattern 1 function--------->No light off process when changing
void Pattern2(); //Pattern 2 function--------->The transition has a light-off processvoid
Pattern34();
void keyscan()
{
 if((key4==1)&&(key3==1)) {Speed ​​= 20;Period = 25;} //Preset 4 speeds 200MS
 if((key4==0)&&(key3==1)) {Speed ​​= 50;Period = 50;} //500MS
 if((key4==1)&&(key3==0)) {Speed ​​= 100;Period = 100;} //1S 
 if((key4==0)&&(key3==0)) {Speed ​​= 150;Period = 200;} //1.5S
 if((key2==1)&&(key1==1)) {ledcount1 = 0;Pattern1();} //Preset 4 patterns
    if((key2==0)&&(key1==1)) {ledcount1 = 0;Pattern2();}
 if((key2==1)&&(key1==0)) {ledcount = 0;Gra3 = 1;Pattern34();}
 if((key2==0)&&(key1==0)) {ledcount = 0;Gra7 = 1;Pattern34();}
}
void value()
{
 if(Speed ​​== 20) PWM = Pulse*8;
 if(Speed ​​== 50) PWM = Pulse*4; //Number of pulsesif
 (Speed ​​== 100) PWM = Pulse*2;
 if(Speed ​​== 150) PWM = Pulse;
}
void Reset(void)

 unsigned char code rst[ ]={0xe4,0xc0,0xe0,0xc0,0xe0,0x32};
 (*((void (*)(void))(rst)))();
}
void main()
{
 _nop_ ();
 _nop_ ();
 _nop_ ();
 P1 = 0;
 TMOD = 0x11; //11.0592M crystal oscillator 10ms timing initial value (one hundredth of a second) 
 TH0 = 0x0db;
 TL0 = 0x0ff;
 TH1 = 0x0ff; //0.05ms(1/20000 of a second)
 TL1 = 0x0D1;
 EA = 1; //Enable total interrupt
 ET0 = 1; //Enable T0 timer interrupt
 ET1 = 1; //Enable T1 timer interrupt
 TR0 = 1; //Enable T0 timer
 EX0=1; //Open external interrupt 0
  IT0=1; //Falling edge triggerwhile
 (1)
 {
  
  keyscan();
 }
}
//*****************************************************************************
//Pattern1() Pattern function 1-------------Jumping without turning off the light process
//*************************************************************************
void Pattern1()
{
 //keyscan();
 Gra = 0; //Jumping bit = 0
 if(LEDdelay)
  {
   LEDdelay = 0;
   P1 = led1[ledcount];
   ledcount++;
   if(ledcount>11)
    {
     ledcount = 0;
    }
  }
}
//***************************************************************************
//Pattern2() Pattern function 2-------------Jumping with light-off process
//***********************************************************************
void Pattern2()
{
 //keyscan();
 Gra = 0;
 if(LEDdelay)
  {
   LEDdelay = 0;
   P1 = led2[ledcount];
   ledcount++;
   if(ledcount>23)
    {
     ledcount = 0;
    }
  }   
}
//***************************************************************************
//Pattern3() Pattern function 3-------------3-color gradient
//***************************************************************************
/*void Pattern3()
{
 keyscan();
 if(Speed ​​== 20) Period = 25;
 if(Speed ​​== 50) Period = 50;
 if(Speed ​​== 100)Period = 100;   
 if(Speed ​​== 150)Period = 200;
 Gra = 1;
 Gra3 = 1;
}*/[page]
//***************************************************************************
//Pattern4() Pattern function 4-------------7-color gradient
//*******************************************************************
void Pattern34()
{
 Gra = 1; //Gradient bit = 1
}
/*************************************************************/
// Timer 0 interrupt service routine
/*********************************************************/
void time0(void) interrupt 1
{
 TR1 =0;
 TF0 =0;
 TH0=0xdb; //Rewrite the initial value
 TL0=0xff;  
    if(!Gra) //Execute the jump program
  {
   count ++;
   if(count > Speed)
    {
     LEDdelay = 1;
     count = 0;
    }
  }
 if(Gra)
  {
   TR1 = 1; //Turn on TR1;
   if(!BP) //       
    {
      if(Gra7)
      {
       P1 = led1[ledcount1]; //10ms arrives, the corresponding LED turns on
      }
     if(Gra3)
      {
       P1 = led3[ledcount1]; //10ms arrives, the corresponding LED turns on
      }
     Pulse++;
     value();
     if(Pulse >= Period)
      {
       Pulse = Period;
       BP = 1; //One cycle completes the conversion mode
      }
    }
   if(BP)
    {
     if(Gra7)
      {
       P1 = led1[ledcount1]; //10ms arrives, the corresponding LED turns on
      }
     if(Gra3)
      {
       P1 = led3[ledcount1]; //10ms arrives, the corresponding LED turns on
      }
     value();     
     Pulse--; //Number of pulsesif
     (Pulse == 0)
      {
       Pulse = 0;
       BP = 0; //One cycle completes the conversion mode
       ledcount1++;
       if(Gra7)
       {
        if(ledcount1 > 11)
         {
          ledcount1 = 0;
         }
         Gra7 = 0;
          }
       if(Gra3)
       {
        if(ledcount1 > 2)
         {
          ledcount1 = 0;
         }
         Gra3 = 0;
          }
      }
    }
  }
}
/****************************************************************/
// Timer 1 interrupt service routine
/********************************************************/
void timer1() interrupt 3
{
 PWM --;
 TH1 = 0xff; //0.05ms (one-twenty-thousandth of a second)
 TL1 = 0xD1;
 if(PWM == 0)
  {
   P1 = 0;
   TR1 = 0;
  }
}
/************************************************************/
// External interrupt 0 interrupt service routine falling edge trigger
/********************************************************/
void extern0(void) interrupt 0
{
 IE0 = 0; // Clear flag 
 excount++;
 if(excount>50) // 50 is about 3 seconds 
  {
   EA = 0;
   //P1_5 = 1;
   excount = 0;
   Reset();
  }
}
Reference address:51 single chip microcomputer colorful LED light gradient dimming color adjustment program

Previous article:Test digital tube display numbers (single chip microcomputer directly realizes bit selection common cathode)
Next article:Elevator control program based on single chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号