Create a new project with the structure shown below:
The timer.h and timer.c files are from the previous section.
Add declarations of two variables and two functions in timer.h, as shown below:
#ifndef __NXPLPC11xx_TIME_H__
#define __NXPLPC11xx_TIME_H__
extern uint16_t cycle;
extern uint8_t duty;
extern void T16B0_init(void);
extern void T16B0_delay_ms(uint16_t ms);
extern void T16B0_delay_us(uint16_t us);
extern void T16B0_cnt_init(void);
extern void T16B0_CAP_Init(void);
extern void T16B0M0_PWM_Init(void);
extern void T16B0M0_PWM_SET(void);
#endif
In the timer.c file, add two variables and two functions:
uint16_t cycle; // cycle, unit: microseconds
uint8_t duty; // duty cycle, range 1~99
void T16B0M0_PWM_Init(void)
{
if((duty>=100)&&(duty<=0))return; //If the duty cycle value is not a number between 1 and 99, exit the function
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); //Enable TIM16B0 clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16); // Enable IOCON clock
LPC_IOCON->PIO0_8 &= ~0x07;
LPC_IOCON->PIO0_8 |= 0x02; //Set P0.8 pin to MAT0
LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<16); // Disable IOCON clock
LPC_TMR16B0->TCR = 0x02; //Reset timer (bit 1: write 1 to reset)
LPC_TMR16B0->PR = SystemCoreClock/1000000-1; //1 microsecond TC+1
LPC_TMR16B0->PWMC= 0x01; //Set MAT0 as PWM output pin
LPC_TMR16B0->MCR = 0x02<<9; //Reset TC when setting MR3 match, that is, use MR3 as period register
LPC_TMR16B0->MR3 = cycle; //Set cycle
LPC_TMR16B0->MR0 = cycle/100*(100-duty); //Set duty cycle
LPC_TMR16B0->TCR = 0x01; //Start the timer
}
void T16B0M0_PWM_SET(void)
{
if((duty>=100)&&(duty<=0))return; //If the duty cycle value is not a number between 1 and 99, exit the function
LPC_TMR16B0->MR0 = cycle/100*(100-duty); //Set duty cycle
}
Lines 1 and 2 define two variables, namely period and duty cycle. The period unit is microseconds because in the following PWM initialization function, the TC increment time will be set to 1 microsecond.
Lines 3 to 18 define a PWM initialization function and set the PWM output pin to P0.8.
Line 14 sets MR3 as the period register.
Line 15, write cycle
Line 16, write the duty cycle value.
Lines 19 to 23 are the PWM duty cycle setting functions
Line 22, enter the duty cycle value
In the main.c file, enter the following code:
#include “lpc11xx.h”
#include “timer.h”
void delay_ms(uint16_t ms)
{
uint16_t i,j;
for(i=0;i<5000;i++)
for(j=0;j
}
int main()
{
cycle = 10000;
duty = 99;
T16B0M0_PWM_Init();
while(1)
{
delay_ms(50);
if(duty<50)duty=99;
T16B0M0_PWM_SET();
duty–;
}
}
The main function defines a period of 10000 microseconds, or 100 Hz, and an initial duty cycle of 99. Then, in the while loop, the duty cycle is changed every 50 milliseconds, and the duty cycle changes cyclically from 99 to 50.
Take an oscilloscope and you can see a 100Hz signal on the P0.8 pin, with the duty cycle constantly changing from 99 to 50.
If you don't have an oscilloscope, you can connect the P0.8 pin to the pin connected to the LED, and you can see the changes in the brightness of the LED light.
Previous article:LPC1114 watchdog_how to start and feed the dog
Next article:LPC1114 general purpose timer - capture function
Recommended ReadingLatest update time:2024-11-16 19:33
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- How to Make LED Bulbs Dimmable
- (All delivered, order number can be checked) The shortlist is here - 2020 ST MEMS Sensor Creative Design Competition
- [Content synchronization] A quick review of RVB2601 article synchronization specifications to avoid repeated returns and modifications
- This indicator has an EVM indicator in the previous high-speed rail or subway PA.
- Domestic MCU replacement: a pitfall
- Can the power supply master give me some advice on how to calculate the secondary winding wire diameter of the flyback switching power supply transformer?
- 【GD32L233C-START Review】Driver of serial digital tube display module
- [ESP32-S2-Kaluga-1 Review] Development Environment Selection and Construction
- Do foldable electronic devices make sense?
- Shanghai recruits photoelectric sensor digital front-end design engineers (400,000-600,000)