This post was last edited by lvxinn2006 on 2019-1-22 17:15 The development board evaluated in this eventST NUCLEO-G071RB is provided by STMicroelectronics. Thanks to STMicroelectronics for its support for EEWorld's evaluation!
【Purpose】
· MasterThe principle and use of PWMTimer
· Master the application of PWM in controlling the brightness of LEDs
[Experimental environment] · NUCLEO-G071RB development board · Tricolor LED lamp module · Keil MDK-ARM (Keil uVision 5.25.2.0) · Keil.STM32G0xx_DFP.1.0.0.pack
[Experimental environment] · NUCLEO-G071RB development board · Tricolor LED lamp module · Keil MDK-ARM (Keil uVision 5.25.2.0) · Keil.STM32G0xx_DFP.1.0.0.pack
【Experimental information】
· NUCLEO-G071RB development board schematic diagram
· STM32G071x8/xB Data Sheet
· STM32G071 chip user reference manual
【ExperimentalAnalysis】
1、Electrical connection relationship
RedR LED ——> PC9
GreenG LED ——> PC8
BlueB LED ——> PC10
2、GPIOPin Configuration
As can be seen from the above figure, the three pins PC8, PC9 and PC10 correspond to TIM1_CH1, TIM1_CH2 and TIM1_CH3 respectively, so all three pins need to be configured to AF2 mode. So it is necessary to configure these three pins into the corresponding AF mode, that is, AF2]. 3. Enable TIM1 timer 400089. Set it as shown in the figure. You can enable the clock of TIM1 by configuring bit 11. 4. Configure the timer The advanced control timer (TIM1) consists of a 16-bit auto-reload counter driven by a programmable prescaler and can be used for a variety of purposes, including measuring the length of input pulses, capturing input signals, or outputting waveforms (including output compare, PWM, and dead-band insertion complementary PWM). The pulse length and waveform period can be modulated within a few microseconds. By setting the timer prescaler and RCC clock controller, the timer's working clock cycle can be controlled at the millisecond level. The advanced control timer (TIM1) and the general timer are completely independent and do not share any resources. The principle of the advanced control timer is as follows: 400090 This experiment mainly uses this timer to generate PWM signals. By controlling the PWM duty cycle, using signals with different duty cycles to drive three colors of LED lights, you can control the brightness level of the three LED lights to match different colors. In the application of timer output PWM signal, the most important registers are: ARR - count reload register, determines the PWM period; CCRx - compare match register, determines the PWM duty cycle of different pins; CNT ——Timer counter, the timer counter can be self-incrementing or self-decrementing. During the change process, it is constantly compared with CCRx. Once CNT reaches the value of CCRx, it can drive the corresponding output pin to reverse the level. The description of PWM related configuration is as follows: 400091
[Experimental code[font =Tahoma]】
- #include "stm32g0xx.h" // Device header #include<stdlib.h>
- void mdelay(int ms) { RCC->APBENR1 |= RCC_APBENR1_TIM6EN; //Enable TIM6 TIM6->PSC = SystemCoreClock / 1000 - 1; //Prescaled timer clock is 1000Hz TIM6->ARR = ms; //Number of cycles TIM6->CR1 |= TIM_CR1_OPM; //Single pulse mode TIM6->CR1 |= TIM_CR1_CEN; //Start timerwhile(TIM6->CR1 & TIM_CR1_CEN); //Wait for timer to end } void rgb_init(void) { /* Initialize IO pins */ RCC->IOPENR |= RCC_IOPENR_GPIOCEN; //Enable GPIOC GPIOC->MODER &= ~(0x3F<<16); GPIOC->MODER |= (0x2A<<16); //Set PC8 9 10 is AF mode GPIOC->AFR[1] &= ~(0xFFF<<0); GPIOC->AFR[1] |= (0x222<<0); //Set PC8 9 10 to AF2 /* Initialize timer TIM1 */ RCC->APBENR2 |= RCC_APBENR2_TIM1EN; //Enable TIM1 clock TIM1->CR1 = (1<<7); //Allow auto-reload TIM1->CCMR1 = (0x6<<4) | (1<<3) | (0x6<<12) | (1<<11); //Set CH1 CH2 to PWM mode TIM1->CCMR2 = (0x6<<4) | (1<<3); //Set CH3 to PWM mode TIM1->CCER |= (1<<0) | (1<<4) | (1<<8); //Enable output function of CH1,2,3 TIM1->BDTR |= (1<<15); //Enable the main output function of the timer TIM1->PSC = 99; //Division value TIM1->ARR = 255; //Count the maximum value, determine the PWM period TIM1->CNT = 0; //Reset the timer counter TIM1->CR1 |= (1<<0); //Start the timer } int main(void) { int rgb; rgb_init(); while(1){ rgb = rand(); TIM1->CCR1 = rgb&0xff; //CH1 comparison value, determine CH1 duty cycle TIM1->CCR2 = (rgb>>8)&0xff; //CH2 comparison value, determine CH2 duty cycle TIM1->CCR3 = (rgb>>16)&0xff; //CH3 comparison value, determine CH3 duty cycle mdelay(100); } }
复制代码 【Experimental phenomenon】
· Three-colorLEDwill keep changing colors
This content is created by EEWORLD forum user lvxinn2006. If you need to reprint or use it for commercial purposes, you must obtain the author’s consent and indicate the source