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) + b % 10; //Display duty cycle level
}
//------------------------------------------
void X0_INT() interrupt 0
{
b++;
if(b > 19) b = 19; //The maximum duty cycle level is 19
}
//------------------------------------------
void X1_INT() interrupt 2
{
b--;
if(b < 1) b = 1; //The minimum duty cycle level is 1.
}
//===========================================
Keywords:MCS-51
Reference address:How to implement PWM with MCS-51 single chip microcomputer
PWM
PWM
New microcontrollers often contain hardware
without
}
//------------------------------------------
void
{
}
//------------------------------------------
void
{
}
//===========================================
Previous article:uCOS-Ⅱ C51 transplantation notes
Next article:51 MCU Comprehensive Learning System - Stepper Motor Control
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- Is this understanding of variable types correct?
- DE1SOC transfers the available memory address in demo2_axi_app in BMP
- Share a cost-effective headset
- Program file does not exist problem occurs in ON Semiconductor IDE
- New Year's work
- Application of indoor navigation and positioning system in the IoT ecosystem
- Share MSP430 control LED and water lamp programming examples
- Precision control techniques and methods for PCB board milling
- MSP430FR6989IPZR Mixed-Signal Microprocessor MCU
- Can anyone help me see how to write the transfer function of the following type of op amp circuit?