stc12 MCU LED_PWM dimming

Publisher:blq0681Latest update time:2015-02-10 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere



This program has 3 files listed below:

stc12c5a.h Header file download: http://www.51hei.com/mcu/3282.html

main.c
 #include "stc12c5a.h"
#include "type.h"

 


#define FRE 22118400
#define TIME_FRE 10000 //Time 1/10000=0.0001s=0.1ms (This time is used to speed up the LED flashing frequency)
#define T0RELOAD (65536 - (FRE / TIME_FRE))


//***************************************Initialize variables
volatile U32 SYS_TIME = 0;
volatile U32 LED_PWM = 0;


//*****************************************延时函数
void delay(U32 dly)
{
    U32 i;
    while(--dly > 0)
    {
        for(i=0;i++;i<1000000);
    }
}[page]


//********************************************Timer0 initialization function
void Timer0_Init()
{
 TMOD &= 0XF0;
 TMOD |= 0X01; //16-bit counting mode of timer 0
 
 TH0 = T0RELOAD / 256;
 TL0 = T0RELOAD % 256;
 AUXR |= 0X80; //Counter 0 clock is Fosc, that is, 1T
 PT0 = 1; //Timer 0 priority control bit, 1 is high priority 0 is low priority
 EA = 1; //Open total interrupt
 ET0 = 1; //Open timer 0 interrupt
 TR0 = 1; //Start timer 0
}


//********************************************Main function
void main()
{
 Timer0_Init();
 //Modify the program according to actual situation
 while(1)
 {
  P33 = 1;
  if(P33 == 0) // KEY1
  {
   delay(5000);
   if(P33 == 0)
   {
    LED_PWM = 0; // Turn off LED
   }
  }
  
  P34 = 1;
  if(P34 == 0) // KEY2 button is kept pressed, KED gradually becomes brighter
  {
   delay(5000);
   if(P34 == 0)
   {
    if(LED_PWM < 100)
    LED_PWM ++;
   }
  }
  
  P35 = 1;
  if(P35 == 0) // KEY3 button is kept pressed, KED gradually becomes darker
  {
   delay(5000);
   if(P35 == 0)
   {
    if(LED_PWM >0)
    LED_PWM --;
   }
  }
 }
 


}
//*********************************************Timer0 interrupt
void Timer0_Routine() interrupt 1
{
 TH0 = T0RELOAD / 256;
 TL0 = T0RELOAD % 256;
 //The following program should be modified according to the actual situation
 SYS_TIME ++;
 if(SYS_TIME < LED_PWM)
 {
  P0 = 0; //Turn on the LED
 }
 else
 {
  P0 = 0XFF; //Turn off the LED
 }
  
 if(SYS_TIME > 100)
 {SYS_TIME = 0;}


}

 

 


 

 

type.h 
 #ifndef     _TYPE_H_
#define     _TYPE_H_

 
typedef unsigned char   U8;
typedef signed   char   S8;
typedef unsigned int    U16;
typedef signed   int    S16;
typedef unsigned long   U32;
typedef signed   long   S32;

 
#endif

Reference address:stc12 MCU LED_PWM dimming

Previous article:Detailed explanation of original code, inverse code and complement code
Next article:About the use of extern in modular programming

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号