SAM4E MCU Tour - 5. LED Breathing and PWM

Publisher:敬亭山人Latest update time:2017-01-07 Source: eefocusKeywords:SAM4E Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In high frequency conditions, a good use of PWM is to control the output power by controlling the duty cycle, such as controlling the fan speed, the brightness of the LED light, etc. This time, we use the interrupt function of PWM to dynamically change the duty cycle of the pulse to achieve the effect of a breathing light.

 

1. Implementation ideas

PWM can choose to have the counter generate an interrupt at the end of the period (when aligned at the center of the period, it may also choose to generate an interrupt at the center of the period), and can dynamically adjust the duty cycle, period, polarity and other properties during operation. So you can dynamically change the duty cycle in the interrupt handler to change the brightness of the LED light. 
This time, channel 0 and pin PA0 will also be used.

 

2. PWM Setting

A higher frequency clock is needed here, so the main clock is divided by 32 (12.5 kHz). The counter period is 400, that is, the output pulse frequency is 125000/400 = 312.5 Hz. At the same time, the corresponding interrupt needs to be enabled. 
The main configuration code of PWM is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
#define PERIOD_VALUE 400
 
/* Clock selection */
PWM->PWM_CH_NUM[0].PWM_CMR = PWM_CMR_CPRE_MCK_DIV_32;
/* Enable interrupts */
PWM->PWM_IER1 = PWM_IER1_CHID0;
/* Period and duty cycle*/
PWM->PWM_CH_NUM[0].PWM_CPRD= PWM_CPRD_CPRD(PERIOD_VALUE);  
PWM->PWM_CH_NUM[0].PWM_CDTY = PWM_CDTY_CDTY(0);
/* Enable interrupt */
NVIC_ClearPendingIRQ(PWM_IRQn);
NVIC_SetPriority(PWM_IRQn, 0);
NVIC_EnableIRQ(PWM_IRQn);

 

 

3. PWM interrupt processing

After each cycle, an interrupt is generated. Then in the interrupt handling function, change the duty cycle. It should be noted that when PWM is enabled, the duty cycle needs to be changed by writing to the PWM duty cycle modification register (PWM_CDTYUPD). By default, the modification takes effect in the next cycle.  
For better results, you can set an interval between two breaths. 
Note that the interrupt generated needs to be pulled low by reading PWM_ISR1. The interrupt handling function is posted in the complete code below.


Keywords:SAM4E Reference address:SAM4E MCU Tour - 5. LED Breathing and PWM

Previous article:SAM4E MCU Tour - 6. LED Flashing Button Control
Next article:SAM4E MCU Tour - 4. LED Flashing PWM

Recommended ReadingLatest update time:2024-11-16 13:58

How to distinguish between 8-bit and 16-bit microcontrollers
1. The mainstream MCU includes CPU, 4KB RAM, 128KB ROM, 2 16-bit timers/counters, 4 8-bit parallel ports, full-duplex serial port, ADC/DAC, SPI, I2C, ISP, and IAP. 2. The system structure is simple, easy to use, and modularized; 3. The single chip microcomputer has high reliability and can work for 10^6~10^7 hours wi
[Microcontroller]
How to distinguish between 8-bit and 16-bit microcontrollers
89C51 MCU timer/counter, P1 port programming example
There is a light-emitting diode connected to the P1.0 port of the 89C51 microcontroller. When P1.0 outputs a low level, the light-emitting diode lights up, and when it outputs a high level, it goes out. There is a switch connected to P1.7. When the switch is closed, P1.7 is at a low level, and when it is disconnected,
[Microcontroller]
Design of multi-chip microcomputer DC power supply control board
Abstract: This paper introduces a DC power supply control board system with single-chip microcomputer controlling thyristor trigger pulse as the core. The system realizes the synchronous generation, phase shifting, driving and system overvoltage/overcurrent protection of thyristor trigger pulse signal. The user can
[Microcontroller]
Design of multi-chip microcomputer DC power supply control board
LCD driver AY0438 and its interface design with PIC microcontroller
1. Overview AY0438 is a complete MCOS display driver produced by Microchip. It can directly drive LCD display modules under the control of a single-chip microcomputer or a microprocessor. It has a simple structure and is easy to use. Especially in driving 32-segment LCD displays, it can show its exquis
[Microcontroller]
LCD driver AY0438 and its interface design with PIC microcontroller
MCU reset circuit problem
VCC ——     | capacitance     |     ————RST     | resistance     |   land   The circuit is as shown above (buttons are not drawn). Let me first explain the principle: when VCC is powered on, the capacitor is charged (there will be a charging current during the charging process, and the current is th
[Microcontroller]
A6_A7 GPS module 51 single chip microcomputer routine
1. Prepare a STC89C52 minimum system board      2. Burn the code (burn the code first and then connect the wires to prevent the code from being unable to be downloaded after connecting the wires) 3. Power the module and turn on the module 4. Connect:     STC89C52 A6&A7     GND - GND     TXD/P3.1- U_RXD     RXD/P3.0- U
[Microcontroller]
Functional implementation of 51 single chip microcomputer timer/counter
The 8051 microcontroller has two 16-bit timers/counters: T0 and T1. Timer usage: delay and pulse width measurement Reading method: software reading and interrupt official: T (initial value) = 2^N-timing time/machine cycle time Machine cycle time = 12/fosc N: represents the number of timers, there are 13, 16, and 8
[Microcontroller]
Functional implementation of 51 single chip microcomputer timer/counter
51 MCU Series - I2C Communication Method - Application of 24C02 (E2PROM)
The simulation circuit is as follows: code show as below: i2c.h #ifndef _I2C_H //Fixed format for writing header files #define _I2C_H //Fixed format for writing header files #include reg52.h sbit SCL=P1^2; //Pin definition of E2PROM24C02 sbit SDA=P1^3; //Pin definition of E2PROM24C02 sbit WP=P1^4; //Read and w
[Microcontroller]
51 MCU Series - I2C Communication Method - Application of 24C02 (E2PROM)
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号