【C51】Source code 1 -- LED pulse width modulation (PWM) dimming

Publisher:美丽花朵Latest update time:2016-09-28 Source: eefocusKeywords:C51  LED  PWM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  I have recently become fascinated with single-chip microcomputers. With the basic knowledge I learned in college, I started to teach myself C51 single-chip microcomputer programming. I followed the tutorial to read the programming related to light-emitting diodes (LEDs), and wrote the following LED PWM dimming program as a summary.

  Let’s talk about PWM dimming first. PWM stands for “Pulse Width Modulation”, which means changing the ratio of high and low levels within a fixed cycle to make the LED show different brightness levels.

  The blogger compiled a relatively simple one: within the cycle of CYCLE pulses, the number of low-level pulses PWM_LOW increases from 0 to CYCLE, and the number of high-level pulses will of course decrease, so that the LED will show a "gradually bright" effect, and the same applies to "gradually dimming".

 

Attached is the source code: (I am a rookie, so there are inevitably some poorly written parts. It is only for backup purposes. Welcome to give me some advice, and please go away if you hate me...)

#include 

#define CYCLE 300 // Define a cycle with 300 pulses

void Delay(unsigned int t);

sbit LED = P1^0;

void main(void)
{
    unsigned int PWM_LOW; //Number of low levels
    unsigned int PWM_HIGH; //Number of high levels

    while (1) {

        for (PWM_LOW=0; PWM_LOW             LED = 0;
            Delay(PWM_LOW);
            LED = 1;
            Delay(CYCLE-PWM_LOW);
        }

        for (PWM_HIGH=0; PWM_HIGH             LED = 1;
            Delay(PWM_HIGH);
            LED = 0;
            Delay(CYCLE-PWM_HIGH);
        }
    }
}

void Delay(unsigned int t)
{
    while (t--);
}

Keywords:C51  LED  PWM Reference address:【C51】Source code 1 -- LED pulse width modulation (PWM) dimming

Previous article:【C51】Source code 2 -- Basic display of 8-bit common cathode digital tube
Next article:Simple calculator with remainder designed by 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号