Recently, I have been working on a portable energy storage BMS project, using the LKS32MC45x series MCU from ChipON Semiconductor as the main control chip. Since the project involves implementing an LED breathing light control, the peripherals used include GPIO and the general-purpose timer UTimer. Below is a description of the driver for the LED breathing light control.
1. Module Initialization Configuration
The configuration of PWM mainly includes: the initialization configuration of GPIO and the initialization configuration of the UTimer module.
1GPIO initialization configuration
Taking P2_12 (Timer4-CH1) as an example, the LKS32MC45x series MCU requires configuring the IO as output (OUTPUT).
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStruct.GPIO_PODEna = DISABLE; GPIO_InitStruct.GPIO_PFLT = DISABLE;
GPIO_Init(GPIO2, &GPIO_InitStruct); |
Next, configure the P2_12 (Timer4-CH1) pin to operate in the multiplexed TIMER4 mode.
GPIO_PinAFConfig(GPIO2, GPIO_PinSource_12, GPIO_AF_TIMER4); // P2.12 is remapped as the output mode for Timer4CH1 |
2Initialization configuration of the UTimer module
The initialization configuration of the UTimer4 module is as follows. Note that the system clock frequency is 192MHz. The parameter configuration is as follows:
- Divide the system clock frequency (CLK_DIV) by 8 to obtain the operating frequency of UTimer4 as 24MHz.
- Set the counter threshold (TH) to 24000, which resets the counter every 1ms, meaning the PWM frequency is 1kHz.
- Set the working mode of Timer4 Channel 1 (CH1_MODE) to compare mode, with the output polarity (CH1_POL) set to 1.
- Enable the zero interrupt for Timer4 (UTIMER_IE_ZERO).
TIM_InitStruct.EN = ENABLE; // Enable the Timer module as a whole, active high TIM_InitStruct.ETON = 0; // Timer counter enable configuration: 0 - automatic operation, 1 - wait for external event to trigger counting TIM_InitStruct.CLK_DIV = UTIMER_Clk_Div8; // Timer counter clock division setting, 192,000,000/8 = 24,000,000 Hz; TIM_InitStruct.CLK_SRC = UTIMER_CLK_SRC_MCLK; // Timer clock source TIM_InitStruct.CH1_POL = 1; // Output polarity control for Timer channel 1 in compare mode, determines the output value when the counter resets to zero TIM_InitStruct.CH1_MODE = UTIMER_MODE_CMP; // Work mode selection for Timer Channel 1, default value is 0 TIM_InitStruct.TH = 24000; // Timer counter threshold. TIM_InitStruct.CMP1 = 24000; // When Timer Channel 1 operates in compare mode, a compare event occurs when the counter value equals CMP1. TIM_InitStruct.FLT = 0; // Selection of signal filter width for channel 0/1. The value range is 0~255. TIM_InitStruct.IE = UTIMER_IE_ZERO; // Enable Timer module zero interrupt
UTIMER_Init(UTIMER4, &TIM_InitStruct); |
3Interrupted initialization configuration
Since the interrupt function is required, it is necessary to enable the UART interrupt and set its interrupt priority, for example:
NVIC_SetPriority(TIMER4_IRQn, 1); // Set the interrupt priority for UTimer4 NVIC_EnableIRQ(TIMER4_IRQn); // Enable UTimer4 interrupt |
2. Implementation of the breathing light
1Turn on the breathing light
To enable the breathing light, you need to activate UTimer4 and configure the IO pins to the multiplexed TIMER mode.
For channel 1 of Timer4, when the counter resets to zero, a signal level is output to TIMER4_CH1. When the counter reaches UTIMER4_CMP1, triggering a compare event, the signal level toggles, outputting another signal level to the IO port TIMER4_CH1. Therefore, by modifying the value of CMP1 in the interrupt handler function of UTimer4, the duty cycle of the PWM can be adjusted.
The content of the interrupt handler function is as follows:
void TIMER4_IRQHandler(void) { static int Timer_flag = 0; if (UTIMER4_IF & BIT2) // Check if UTimer4 has triggered a zero-reset interrupt { UTIMER4_IF = BIT2; // Clear the UTimer interrupt flag bit if(LED_PWM_UpDown) { LED_PWM_CMP += LED_PWM_Step; // Duty cycle increases if(LED_PWM_CMP > LED_PWM_TH) // When the duty cycle reaches 100%, start decreasing the duty cycle { LED_PWM_CMP = LED_PWM_TH; LED_PWM_UpDown = 0; } } else { if (LED_PWM_CMP > LED_PWM_Low) { LED_PWM_CMP -= LED_PWM_Step; // Duty cycle decreases } else // When the duty cycle is too small, start increasing the duty cycle { LED_PWM_UpDown=1; } } UTIMER4_CMP1 = LED_PWM_CMP; // Assign value to CMP1 } } |
2Turn off the breathing light
To turn off the breathing light, you need to disable the clock for UTimer4 and configure the IO pin as a regular IO. Finally, set the IO pin to the level that turns off the LED.
UTIMER_Disable(UTIMER4); GPIO_PinAFConfig(GPIO2, GPIO_PinSource_12, GPIO_AF_GPIO); // Set P2.12 to GPIO mode GPIO_ResetBits(GPIO2, GPIO_Pin_12); |
3. Summary
The above is an explanation of the software driver for configuring the UTimer module of ChipON LKS32MC45x to control a breathing light. If you want to learn more, feel free to leave a comment below or send us an email:atu.sh@wpi-group.com。
4. References
- LKS32M45x User Manual: LKS32MC45x_UM_v1.52.pdf.
- LKS32M45x Peripheral Example Project: lks32mc45x_demo_prj_v2.9.
If you have more needs, please contact the ATU department of WPI-Group: atu.sh@wpi-group.com Author: Miss May
For more information, please scan the QR code to follow us!

