LPC1114 General Purpose Timer - PWM

Publisher:innovator8Latest update time:2016-12-10 Source: eefocusKeywords:lpc1114 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Create a new project with the structure shown below:

New Construction

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:

  1. uint16_t cycle; // cycle, unit: microseconds

  2. uint8_t duty; // duty cycle, range 1~99

  3. void T16B0M0_PWM_Init(void)

  4. {

  5.    if((duty>=100)&&(duty<=0))return; //If the duty cycle value is not a number between 1 and 99, exit the function

  6.    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); //Enable TIM16B0 clock

  7.    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16); // Enable IOCON clock

  8.    LPC_IOCON->PIO0_8 &= ~0x07;

  9.    LPC_IOCON->PIO0_8 |= 0x02; //Set P0.8 pin to MAT0

  10.    LPC_SYSCON->SYSAHBCLKCTRL &= ~(1<<16); // Disable IOCON clock

  11.    LPC_TMR16B0->TCR = 0x02; //Reset timer (bit 1: write 1 to reset)

  12.    LPC_TMR16B0->PR = SystemCoreClock/1000000-1; //1 microsecond TC+1

  13.    LPC_TMR16B0->PWMC= 0x01; //Set MAT0 as PWM output pin

  14.    LPC_TMR16B0->MCR = 0x02<<9; //Reset TC when setting MR3 match, that is, use MR3 as period register

  15.    LPC_TMR16B0->MR3 = cycle; //Set cycle

  16.    LPC_TMR16B0->MR0 = cycle/100*(100-duty); //Set duty cycle

  17.    LPC_TMR16B0->TCR = 0x01; //Start the timer

  18. }

  1. void T16B0M0_PWM_SET(void)

  2. {

  3.    if((duty>=100)&&(duty<=0))return; //If the duty cycle value is not a number between 1 and 99, exit the function

  4.    LPC_TMR16B0->MR0 = cycle/100*(100-duty); //Set duty cycle

  5. }

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:

  1. #include “lpc11xx.h”

  2. #include “timer.h”

  1. void delay_ms(uint16_t ms)

  2. {

  3.    uint16_t i,j;

  1.    for(i=0;i<5000;i++)

  2.       for(j=0;j

  3. }

  1. int main()

  2. {

  3.    cycle = 10000;

  4.    duty = 99;

  5.    T16B0M0_PWM_Init();

  1.    while(1)

  2.    {

  3.       delay_ms(50);

  4.       if(duty<50)duty=99;

  5.       T16B0M0_PWM_SET();

  6.       duty–;

  7.    }

  8. }

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.


Keywords:lpc1114 Reference address:LPC1114 General Purpose Timer - PWM

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

PIC microcontroller PWM principle
 1. What is PWM What is PWM? Professionally speaking, it is pulse width modulation. In layman's terms, it is the continuous output of high and low levels. For example, the microcontroller outputs a high level for 40ms, then a low level for 60ms, and then continuously and periodically outputs high and low levels alte
[Microcontroller]
Current regeneration methods and their differences when PWM drives a brushed motor
This article will discuss the current regeneration method when PWM drives a brushed motor and the difference in current regeneration when PWM drives a brushed motor. Current regeneration method in PWM drive of brushed motor In the PWM drive process of the brushed motor, there are two main methods for
[Embedded]
Current regeneration methods and their differences when PWM drives a brushed motor
Principle and application of active clamp PWM controller LM5025
1. Introduction LM5025 is a PWM converter dedicated to active clamping. Its internal driver can drive P-channel or N-channel clamping switches. Compared with traditional winding or RDC clamping technology, active clamping technology has the characteristics of high efficiency and high power density. LM5025 has
[Power Management]
Principle and application of active clamp PWM controller LM5025
Temperature measurement system based on TMP04 digital temperature sensor with PWM output
  1 Introduction   The main output modes of digital temperature sensors include PWM, SPI, I2C, SMBus, etc. Almost all mainstream microcontrollers support this interface mode. This article takes the PWM output mode as an example to discuss the temperature measurement scheme of PIC microcontroller for this output mode
[Microcontroller]
Temperature measurement system based on TMP04 digital temperature sensor with PWM output
PWM control circuit principle and circuit design FPGA
In the DC servo control system, the traditional PWM control circuit composed of dedicated integrated chips or small and medium-sized digital integrated circuits often has the disadvantages of complex circuit design, large size, poor anti-interference ability, difficult design, and long des
[Analog Electronics]
PWM control circuit principle and circuit design FPGA
Analysis of Pulse Width Modulation (PWM) Design and Application Principles
Pulse Width Modulation (or PWM for short) is a simple concept with many applications. Let's look at a square wave. A waveform has a certain frequency. The frequency and period of a waveform are opposite to each other, that is, frequency = 1/period, and period = 1/frequency. A period of 1s means a frequenc
[Embedded]
Analysis of Pulse Width Modulation (PWM) Design and Application Principles
AVR microcontroller fast PWM timer 0 generation mode TIME0_fast PWM program
/*AVR fast PWM timer 0 generation mode*/ /*PWM wave generation mechanism is that TCNT0 of timer 0 keeps counting. When TCNT0=OCR0, a low level appears on OC0 pin. When timer 0=TOP maximum value, */ /*OC0 pin will be pulled high and overflow interrupt will be generated. You can modify the value of OCR0 in terminal ser
[Microcontroller]
Common methods and precautions for microcontroller output PWM
The application of PWM can be said to be very wide, controlling motor speed, light brightness, communication modulation and many other fields. My friends have asked me a lot about PWM, and I have been using PWM recently. Here I will share some information about PWM. What is PWM? PWM: Pulse Width Modulation, pulse widt
[Microcontroller]
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号