PWM (Pulse Width Modulation) is simply a frequency conversion technology that controls the output voltage by changing the pulse width and controls the output frequency by changing the cycle. If it is still not clear, let's take a look at some examples in real life. Why does the speed of our electric fan change when we turn the button? Why does the volume of the radio change when we adjust the volume button? And the buzzer we will talk about later will also emit different frequencies according to different input values, etc.! ! These are all applications of PWM, and they are all controlled by the frequency signal output by PWM.
2. PWM in ARM Linux
According to the manual of S3C2440, S3C2440A has five 16-bit timers inside. Timers 0, 1, 2, and 3 all have pulse width modulation (PWM) function. Timer 4 is an internal timer without output pins. Timer 0 has a dead zone generator for high current devices. See the figure below for explanation!!
Based on the technical manual of S3C2440 and the structure diagram above, let's summarize the characteristics of the internal timer module of 2440:
1) There are 5 16-bit timers in total, and timers 0, 1, 2, and 3 all have pulse width modulation function (PWM);
2) Each timer has a comparison buffer register (TCMPB) and a count buffer register (TCNTB);
3) Timers 0 and 1 share an 8-bit prescaler (prescaler), and timers 2, 3, and 4 share another 8-bit prescaler (prescaler), and its value range is 0~255;
4) Timers 0 and 1 share a clock divider, and timers 2, 3, and 4 share another clock divider. Both clock dividers can generate 5 different divided signal values (i.e.: 1/2, 1/4, 1/8, 1/16 and TCLK);
5) The two 8-bit prescalers are programmable and divide PCLK according to the loaded value. The values of the prescaler and clock divider are stored in the timer configuration registers TCFG0 and TCFG1 respectively;
6) There is a TCON control register that controls the properties and status of all timers. Bits 0 to 7 of TCON control timer 0, bits 8 to 11 control timer 1, bits 12 to 15 control timer 2, bits 16 to 19 control timer 3, and bits 20 to 22 control timer 4.
Still according to the description of the S3C2440 manual and the structure of the figure above, the steps to start a PWM timer function are as follows (assuming that the first timer is used):
1) Set the prescaler value and clock division value of timer 0 respectively for the comparison buffer register and count buffer register of timer 0;
2) Set the initial value of the comparison buffer register TCMPB0 and the count buffer register TCNTB0 (that is, the output clock frequency of timer 0);
3) Turn off the dead zone generator of timer 0 (set the 4th bit of TCON);
4) Turn on the automatic reload of timer 0 (set the 3rd bit of TCON);
5) Turn off the inverter of timer 0 (set the 2nd bit of TCON);
6) Turn on the manual update TCNTB0&TCMPB0 function of timer 0 (set the 1st bit of TCON);
7) Start timer 0 (set the 0th bit of TCON);
8) Clear the manual update TCNTB0&TCMPB0 function of timer 0 (set the 1st bit of TCON).
From this, we can see that the output frequency of PWM is related to the values of the comparison buffer register and the count buffer register, and the values of the comparison buffer register and the count buffer register are related to the values of the prescaler and the clock divider; to use the PWM function is actually to operate the relevant registers of the timer. There is also a formula in the manual: Timer output frequency = PCLK / {prescaler value + 1} / clock divider value. Let's use a buzzer example to illustrate the use of PWM function.
1. Types and working principles of buzzers
Buzzers are mainly divided into two types: piezoelectric buzzers and electromagnetic buzzers.
Piezoelectric buzzers are mainly composed of multivibrators, piezoelectric buzzers, impedance matchers, resonance boxes, shells, etc. Some piezoelectric buzzers are also equipped with light-emitting diodes on their shells. Multivibrators are composed of transistors or integrated circuits. When the power is turned on (1.5~15V DC working voltage), the multivibrator starts to oscillate and outputs an audio signal of 1.5~2.5kHZ, and the impedance matcher drives the piezoelectric buzzer to sound.
Electromagnetic buzzers are composed of oscillators, electromagnetic coils, magnets, vibrating diaphragms, and shells. After the power is turned on, the audio signal current generated by the oscillator passes through the electromagnetic coil, causing the electromagnetic coil to generate a magnetic field. Under the interaction between the electromagnetic coil and the magnet, the vibrating diaphragm vibrates periodically and sounds.
The difference between active buzzers and passive buzzers: the word "source" does not refer to the power supply, but to the oscillation source, that is, the active buzzer has an oscillation source while the passive buzzer does not. The one with an oscillation source can make a sound when powered on, while the one without an oscillation source needs a pulse signal to drive it.
Additional knowledge: How to make a simple buzzer
1) Prepare the electromagnet M: Wind 100 turns of wire around an iron bolt about 6 cm long, leaving 5 cm at the end of the wire as a lead, tape the coil with transparent tape to prevent it from loosening, and then tape it to a box with tape, and the electromagnet is ready;
2. Analysis of the buzzer schematic on the development board
From the schematic diagram, we can know that the buzzer is driven by the PWM signal through the GPB0 IO port, and the GPB0 port is a multiplexed IO port. To use it, you must first set it to TOUT0 PWM output mode.
3. Write a buzzer driver suitable for the development board, file name: my2440_pwm.c
/*
static int device_major = PWM_MAJOR; //The main device number dynamically generated by the system |
#cp -f my2440_pwm.c /linux-2.6.30.4/drivers/char //Put the driver source code into the character device of the kernel driver |
#gedit /linux-2.6.30.4/drivers/char/Kconfig //Add PWM buzzer device configuration |
config MY2440_PWM_BEEP |
#gedit /linux-2.6.30.4/drivers/char/Makefile //Add PWM buzzer device configuration |
obj-$(CONFIG_MY2440_PWM_BEEP) += my2440_pwm.o |
5. Configure the kernel and select the PWM buzzer device option
#make menuconfig |
Device Drivers ---> |
6. Compile the kernel and download it to the development board. Note that we don't need to manually create device nodes on the development board now, because we are now using mdev for management (see: Device file system analysis and use for usage), and the driver has also added support for class device interfaces. Some of the drivers mentioned before do not have this, and we will use this method in the future. Now you can see the my2440_pwm device node that is automatically created in the /dev directory, and you can use it directly.
7. Write a test program for the PWM buzzer driver. File name: pwm_test.c
/* |
8. Cross-compile the test application on the development host and put it in the /usr/sbin directory of the file system, then recompile the file system and download it to the development board.
#arm-linux-gcc -o pwm_test pwm_test.c |
9. Run the test program on the development board. You can see that the buzzer will make different frequencies of sound according to the size of the input parameters. Enter 0 to stop the buzzer.
Previous article:uboot porting process on ARM s3c2410
Next article:zc301 camera driver and using serfox and spcaview in S3C2410
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Summary of Problems and Solutions in CCS6 Compilation
- [STM32WB55 review] +thread trial 1
- Guess the question about the list of materials for the undergraduate group of the electronic competition: Sound source localization system
- 【ST NUCLEO-G071RB Review】_01_First impression
- Why use discrete components for preamplification?
- MSP430IO Driver
- How do I design a circuit with such input and output?
- Nexperia Award-winning Live Broadcast: Introduction and Application of Automotive Power MOSFET and GaN Devices in Electric Vehicles
- Nordic Bluetooth sensor monitors guitar temperature and humidity
- EEWORLD University Hall - Altium Designer 22 Electronic Design Introduction Practice 56 Lectures