51 single chip microcomputer realizes breathing light

Publisher:创客1992Latest update time:2020-04-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I. Overview


Use 51 single chip microcomputer to control LED to realize the gradual change process from dark to bright and then from bright to dark.


II. Procedure


#include <STC15F2K60S2.H>

#include "MacroAndConst.h"

 

sbit LED = P1^5;

 

#define LED_ON() LED = 0; //LED is on

#define LED_OFF() LED = 1; //LED off

 

#define LED_PWM_LIMIT_MAX 99

#define LED_PWM_LIMIT_MIN 0

 

 

static u8 s_u8TimeCounter = 0; //interrupt count

static u8 s_u8LedDirection = 0; //LED direction control 0: gradually brighten 1: gradually fade out

static u8 s_u8LedPWMCounter = 0; //LED duty cycle

 

void Timer0Init(void)

{

AUXR |= 0x80; //Timer clock 1T mode

TMOD &= 0xF0; //Set timer mode

TL0 = 0x5C; //Set the initial timing value

TH0 = 0xF7; //Set the initial value of timing

TF0 = 0; // Clear TF0 flag

TR0 = 1; //Timer 0 starts timing

ET0 = 1; // Enable timer 0 interrupt

}

 

void Timer0Isr(void) interrupt 1

{

static int8 s_u8PWMCounter = 0;

TH0 = 0xF7; //Timer initial value (200us interrupt once)

TL0 = 0x5C;

if(++s_u8TimeCounter >= 100) //Adjust LED duty cycle every 20ms

{

s_u8TimeCounter = 0;

//If the direction changes gradually, the duty cycle increases

if((s_u8LedPWMCounter <= LED_PWM_LIMIT_MAX)&&(s_u8LedDirection == 0))

{

s_u8LedPWMCounter++;

if(s_u8LedPWMCounter > LED_PWM_LIMIT_MAX)

{

s_u8LedDirection = 1;

s_u8LedPWMCounter = LED_PWM_LIMIT_MAX;

}

}

//If the direction changes gradually, the duty cycle decreases

if((s_u8LedPWMCounter > LED_PWM_LIMIT_MIN)&&(s_u8LedDirection == 1))    

             //There is a pit here. If you write it as ">=", you will not achieve the expected effect.

{

s_u8LedPWMCounter--;

if(s_u8LedPWMCounter <= LED_PWM_LIMIT_MIN)

{

s_u8LedDirection = 0;

s_u8LedPWMCounter = LED_PWM_LIMIT_MIN;

}

}

s_u8PWMCounter = s_u8LedPWMCounter; //Get the duty cycle of LED

}

if(s_u8PWMCounter > 0) //If the duty cycle is greater than 0, the LED will be on, otherwise it will be off

{

LED_ON();

s_u8PWMCounter--;

}

else

{

LED_OFF();

}

}

Reference address:51 single chip microcomputer realizes breathing light

Previous article:Beginner's guide to 51 MCU - simple water lamp program
Next article:51 single chip microcomputer--LED flashing, simple experiment of running water light

Recommended ReadingLatest update time:2024-11-16 21:53

[MCU framework][BSP layer][cx32l003][bsp_led] LED configuration and use
LED is a semiconductor device. In fact, everyone is familiar with LED. The indicator lights of various electrical appliances, mobile phone keyboard lights, LED LCD screen backlights, high-brightness LED flashlights, etc., all emit light through LED. LED has two main uses, one is as an indicator light; the other is lig
[Microcontroller]
Introducing several solutions for automotive interior and exterior lighting LED drivers
LED (Light Emitting Diode) is a solid-state semiconductor device that can directly convert electricity into light. In recent years, its application areas have been continuously broadened, from traditional portable device backlight to medium and large-sized LCD monitor/LCD TV backlight, automotive and general lightin
[Power Management]
Introducing several solutions for automotive interior and exterior lighting LED drivers
Progress in MOCVD, a key technology for high-brightness LEDs
LED technology is advancing rapidly , driven by a range of new applications . Backlighting for notebook computers, desktop monitors and large-screen TVs is a key application for today's high-brightness LEDs , creating the need for large numbers of LEDs to be manufactured. In addition to
[Power Management]
Use the timer to control the LED light, turn on for 1 second and turn off for 1 second
//*f=6MHZ, timer 0 generates 100us timing in working mode 2 (auto-reload 8-bit timer mode) and query mode, //And output a continuous square wave r with a period of 2s at P0.0 to control the LED light, that is, on for 1s and off for 1s*// #include reg52.h sbit d1=P0^0; void timer(); unsigned int num,tt; void main()
[Microcontroller]
STM32-GPIO settings: quickly light up the first LED
Introduction The schematic diagrams of different development boards are different. Here, I use the Wildfire MINI-V3 (F103VET6) simple development board to briefly introduce the settings of the GPIO port and realize the key control of the LED light, so that readers can quickly become familiar with it and apply it flexi
[Microcontroller]
STM32-GPIO settings: quickly light up the first LED
LED bulb power supply analysis: non-isolation reverses isolation
   The article first analyzes the current mainstream 3WLED bulb solution with the largest market share, 3MW isolation. The advantages and disadvantages of this solution are proposed. Based on these advantages and disadvantages, the market development direction of the future solution is proposed. The selection of China
[Power Management]
LED bulb power supply analysis: non-isolation reverses isolation
Design of a 16-bit constant current driver chip for a color LED display
At present, large-scale color LED display screens have become the mainstream product of high-definition large-screen flat-panel display devices. This is a large-size flat-panel display device composed of display units composed of light-emitting diodes and their display driver integrated circuit chips. The integrate
[Power Management]
Design of a 16-bit constant current driver chip for a color LED display
RT8458 PWM Buck LED Driver Application Circuit
The RT8458 is a PWM controller with an integrated high side floating gate driver. It is used for step down converters by well controlling the external MOSFET and regulating a constant output current. The output duty cycle of the RT8458 can be up to 100% for wider input voltage application, such as E27 and PAR30 off-
[Power Management]
RT8458 PWM Buck LED Driver Application Circuit
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号