3218 views|6 replies

23

Posts

0

Resources
The OP
 

[ST NUCLEO-G071RB Review]_08_PWM timer control three-color LED experiment [Copy link]

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
2GPIOPin 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]
  1. #include "stm32g0xx.h" // Device header #include<stdlib.h>
  2. void mdelay(int ms) { RCC-&gt;APBENR1 |= RCC_APBENR1_TIM6EN; //Enable TIM6 TIM6-&gt;PSC = SystemCoreClock / 1000 - 1; //Prescaled timer clock is 1000Hz TIM6-&gt;ARR = ms; //Number of cycles TIM6-&gt;CR1 |= TIM_CR1_OPM; //Single pulse mode TIM6-&gt;CR1 |= TIM_CR1_CEN; //Start timerwhile(TIM6-&gt;CR1 &amp; TIM_CR1_CEN); //Wait for timer to end } void rgb_init(void) { /* Initialize IO pins */ RCC-&gt;IOPENR |= RCC_IOPENR_GPIOCEN; //Enable GPIOC GPIOC-&gt;MODER &amp;= ~(0x3F&lt;&lt;16); GPIOC-&gt;MODER |= (0x2A&lt;&lt;16); //Set PC8 9 10 is AF mode GPIOC-&gt;AFR[1] &amp;= ~(0xFFF&lt;&lt;0); GPIOC-&gt;AFR[1] |= (0x222&lt;&lt;0); //Set PC8 9 10 to AF2 /* Initialize timer TIM1 */ RCC-&gt;APBENR2 |= RCC_APBENR2_TIM1EN; //Enable TIM1 clock TIM1-&gt;CR1 = (1&lt;&lt;7); //Allow auto-reload TIM1-&gt;CCMR1 = (0x6&lt;&lt;4) | (1&lt;&lt;3) | (0x6&lt;&lt;12) | (1&lt;&lt;11); //Set CH1 CH2 to PWM mode TIM1-&gt;CCMR2 = (0x6&lt;&lt;4) | (1&lt;&lt;3); //Set CH3 to PWM mode TIM1-&gt;CCER |= (1&lt;&lt;0) | (1&lt;&lt;4) | (1&lt;&lt;8); //Enable output function of CH1,2,3 TIM1-&gt;BDTR |= (1&lt;&lt;15); //Enable the main output function of the timer TIM1-&gt;PSC = 99; //Division value TIM1-&gt;ARR = 255; //Count the maximum value, determine the PWM period TIM1-&gt;CNT = 0; //Reset the timer counter TIM1-&gt;CR1 |= (1&lt;&lt;0); //Start the timer } int main(void) { int rgb; rgb_init(); while(1){ rgb = rand(); TIM1-&gt;CCR1 = rgb&amp;0xff; //CH1 comparison value, determine CH1 duty cycle TIM1-&gt;CCR2 = (rgb&gt;&gt;8)&amp;0xff; //CH2 comparison value, determine CH2 duty cycle TIM1-&gt;CCR3 = (rgb&gt;&gt;16)&amp;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

图片2.png (57.77 KB, downloads: 0)

图片2.png

图片3.png (762.25 KB, downloads: 0)

图片3.png

图片4.png (181.05 KB, downloads: 0)

图片4.png
This post is from stm32/stm8

Latest reply

Hmmmm  Details Published on 2019-1-23 09:54
 

1366

Posts

6

Resources
2
 
1. I appreciate the use of registers, which will give you a deeper understanding of this MCU in the review. 2. Why don't you use cubemx or the official library interface?
This post is from stm32/stm8

Comments

When evaluating a chip, you can only see the difference from other chips from the perspective of the underlying registers. If the registers are OK, it will be much easier to use the library functions. The most important thing is that the project is too small, and the functions are implemented by a few lines of code, and you can control the details yourself.  Details Published on 2019-1-22 11:30
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 

23

Posts

0

Resources
3
 
RCSN posted on 2019-1-22 10:37 1. The author deserves praise for using register operation, so that he can have a deeper understanding of this MCU in the evaluation. 2. Why doesn't the author use cubemx? Or...
To evaluate the chip, we should start from the perspective of the underlying registers to see the difference with other chips. If the registers are fine, it will be much simpler to use the library functions. The most important thing is that the project is too small, and the functions implemented by a few lines of code are controlled by the details.
This post is from stm32/stm8

Comments

Well, I look forward to your continued review posts.  Details Published on 2019-1-22 13:19
 
 

1366

Posts

6

Resources
4
 
lvxinn2006 posted on 2019-1-22 11:30 To evaluate the chip, we should start from the perspective of the underlying registers to see the difference with other chips. If the registers are fine, it will be simple to use the library functions...
Well, I look forward to your continued evaluation posts.
This post is from stm32/stm8
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 
 

1w

Posts

204

Resources
5
 
The color is very beautiful.
This post is from stm32/stm8
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

Through a layer of paper, it looks a bit like the moon in different colors  Details Published on 2019-1-23 09:39
 
 
 

23

Posts

0

Resources
6
 
[quote]okhxyyo posted on 2019-1-22 21:29 The color is very beautiful. Like it, like it, like it, looks a bit like the moon in different colors through a layer of paper
This post is from stm32/stm8

Comments

Hmmmm  Details Published on 2019-1-23 09:54
 
 
 

1w

Posts

204

Resources
7
 
lvxinn2006 posted on 2019-1-23 09:39 Through a layer of paper, it looks a bit like the moon in different colors
Hmmmm
This post is from stm32/stm8
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list