How to implement PWM with MCS-51 single chip microcomputer

Publisher:SerendipityDawnLatest update time:2016-01-13 Source: eefocusKeywords:MCS-51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
About the method of implementing PWM in MCS-51 microcontroller
PWM  stands for "pulse width modulation".
PWM  actually outputs a series of rectangular waves on a pin of the microcontroller, and its period is generally fixed; while the time occupied by its high level and low level can be controlled and adjusted. The ratio of the time occupied by the high level and the low level is called the duty cycle, and its value is between 0 and 1. PWM is  widely used. Simply put, it can control the brightness of lights, the speed of DC motors, and even output voice signals.
New microcontrollers often contain hardware  PWM  modules, so it is very simple to write programs; but the old MCS-51 series does not have this condition. In fact, it is not difficult to implement PWM on a microcontroller 
without  PWM  hardware . All you need is a timer to make it work in interrupt mode. The following is an example of a question on the Internet to explain the method of implementing  PWM  . Question link: http://zhidao.baidu.com/question/170790790.html?push=core Question: AT89C52 microcontroller outputs adjustable  PWM  wave. Requirements: 1. The crystal oscillator of the microcontroller is 12MHz, and the output cycle is 1s. 2. Use two switches to control the increase and decrease of the duty cycle, divided into 20 levels. In response to this question, Zuoerlundao designed a simple experimental circuit: the 8  lines  of  the P2  port  output  PWM  waveforms; two buttons are connected  to  /INT1 and /INT0  to adjust the duty cycle; two BCD  digital displays are connected   to  the P0  port to display the current duty cycle level.  During the experiment, an oscilloscope can be used to observe  the waveform output by the P2  port.  If  the frequency of the PWM  waveform is increased,  the LED  can also be used to observe the effect of gradually brightening and dimming. At present, only the flashing time changes. //The program is as follows: //======================================== #include unsigned  int  a,  b; //------------------------------------------ void  main() { TMOD  =  0x01;  //T0 timing mode 1 TH0  =  (65536-50000)  /  256;  //50ms@12MHz TL0  =  (65536-50000)  %  256; TR0  =  1;  ET0  =  1; EX0  =  1;  EX1  =  1; IT0  =  1;  IT1  =  1; EA  =  1;  a  =  0; b  =  10; while(1); } //------------------------------------------ void  time0()  interrupt  1 { TL0  =  (65536-50000)  %  256; TH0  =  (65536-50000)  /  256;  //50ms@12MHz a++; if(a  ==  20)  a  =  0;  //Adjust the period here. if(a  <  b)  P2  =  0xff;  //Adjust the duty cycle here. else  P2  =  0x00; P0  = 


















                         
          
         
                 
         
         
                 
         
         




         
          
         
                   
                
           
         ((b  10)  <<  4)  10;  //Display duty cycle level
}
//------------------------------------------
void  X0_INT()  interrupt  0
{
         b++;
         if(b  19)  19;            //The maximum duty cycle level is 19
}
//------------------------------------------
void  X1_INT()  interrupt  2
{
         b--;
         if(b  1)  1;              //The minimum duty cycle level is 1.
}
//===========================================
Keywords:MCS-51 Reference address:How to implement PWM with MCS-51 single chip microcomputer

Previous article:uCOS-Ⅱ C51 transplantation notes
Next article:51 MCU Comprehensive Learning System - Stepper Motor Control

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号