Difference between revisions of "Nucleo Boards for Control Systems"

From RobolaboWiki
Jump to: navigation, search
Line 76: Line 76:
 
Main parameters:
 
Main parameters:
 
<ul>
 
<ul>
<li><b>Clock Source:</b> reloj a tomar como referencia por el timer. Debemos seleccionar la opción Internal clock. Además, debemos asegurarnos que la frecuencia del reloj usada es la deseada, en la pestaña de clock configuration.</li>
+
<li><b>Clock Source:</b> Clock of the timer. The Internal clock option must be selected. Moreover, it must be checked that the frequency of the clock that is being used is the one desired. This could be checked within the clock configuration tab of the STM32CubeIDE. </li>
<li><b>Channel x:</b> configuración deseada para el canal x del timer. Debemos seleccionar la opción “PWM Generation CHx”, donde x será el número del canal en cuestión.</li>
+
<li><b>Channel x:</b> Configuration for the channel x of the timer. The option “PWM Generation CHx” must be selected, in which x will be the number of the channel to configure.</li>
<li><b>Prescaler (PSC):</b> Parámetro para configurar el registro de PSC.</li>
+
<li><b>Prescaler (PSC):</b> Parameter to introduce the value of the PSC register.</li>
<li><b>Counter Period:</b> Parámetro para configurar el valor del registro ARR.</li>
+
<li><b>Counter Period:</b> Parameter to set the value of the ARR register.</li>
<li><b>Pulse (16 bits):</b> parámetro para modificar el registro de CCR.</li>
+
<li><b>Pulse (16 bits):</b> Parameter to mofidy the CCR register.</li>
<li><b>Mode: </b> Debemos seleccionar PWM mode 1.</li>
+
<li><b>Mode: </b> The PWM mode 1 must be selected.</li>
 
</ul>
 
</ul>
  
Además, en nuestro código debemos activar el timer ya configurado e inicializado de la siguiente manera:
+
Additionally, within the code, the timer to use must be initialized following the structure below:
 +
 
 
<pre>
 
<pre>
 
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
 
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
Line 90: Line 91:
  
  
Podemos modificar el valor de CCR (aceder al registro) mediante código de la siguiente manera:
+
The CCR value can be modified by introducing the following instruction:  
 
<pre>
 
<pre>
 
htimx.Instance->CCR1 = new_ccr;
 
htimx.Instance->CCR1 = new_ccr;
 
</pre>
 
</pre>
  
donde CCR1 indica que estamos accediendo al registro del canal 1, new_ccr es el nuevo valor entero que queremos establecer y htimx es la instancia correspondiente al timer x (por ejemplo, htim2 o htim3).
+
in which CCR1 indicates that we are accessing to the channel 1 register, new_ccr is the new to set and htimx refers to the timer x (for example, htim2 or htim3).
  
De igual manera, para modificar el registro del canal 2:  
+
In the same way, to modify the register of the channel 2:  
 
<pre>
 
<pre>
 
htimx.Instance->CCR2 = new_ccr;
 
htimx.Instance->CCR2 = new_ccr;

Revision as of 12:21, 6 February 2023


GPIO read and write

Read from a GPIO

HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_9);

Write to a GPIO

- High level:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

- Low level:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);


Encoder

Theory


Encoder Configuration in STM32CubeIDE


PWM

Theory



Main registers

Prescaler (PSC)

The prescaler is a register that divides the timer clock frequency to obtain a specific frequency. Specifically, the frequency reduction is applied using the following formula:

f_PWM = f_CLK / (PSC + 1)

Therefore, if the frequency to set is the same as the frequency of the clock, the PSC register must be set at PSC=0. The STM32 timers allow a prescaler value up to 65535, through 16 bits registers.


Auto Reload Register (ARR) and Capture Compare Register (CCR)

The ARR register storages the maximum value that the counter can reach before going back to zero. Moreover, the CCR registers the value of the corresponding timer counter after which the output PWM signal goes from high to low level. Every channel of the timer used to generate the PWM will have a different CCR register (for example, CCR1 and CCR2 for channels 1 and 2). In this way, the value of the CCR together with the value of the ARR define the width of the pulses and, therefore, the duty cycle of the PWM signal:

Duty_Cycle (%) = 100 * (CCR / ARR)

CNT

This is the register that storages the value of the counter used to generate the PWM.

PWM Configuration in STM32CubeIDE

To configure a timer as PWM, the first thing to do is to select one of the timers that could be configured as a PWM generator. Additionally, also the channels that will be used as PWM output must be decided.

Advice: As different PWM outputs are needed, it is recommended to use different channels of the same timer to the extent possible.

The main timers that can be set as PWM are: TIM2, TIM3, TIM4 and TIM5, with 4 different channels each of them.

Advice: It is recommended to use the timer and the corresponding channels that match directly with the pins that feed the H-bridge to modulate the speed of the motor. Otherwise, the PWM output signals must be bridged with the H-bridge input signals.

Therefore, the pins that the channels of the timer are using need to be checked in the "Pinout" section of the STM32CubeIDE.

Once the channels of the timer have been activated, some parameters and the configuration of the timer must be changed.

Main parameters:

  • Clock Source: Clock of the timer. The Internal clock option must be selected. Moreover, it must be checked that the frequency of the clock that is being used is the one desired. This could be checked within the clock configuration tab of the STM32CubeIDE.
  • Channel x: Configuration for the channel x of the timer. The option “PWM Generation CHx” must be selected, in which x will be the number of the channel to configure.
  • Prescaler (PSC): Parameter to introduce the value of the PSC register.
  • Counter Period: Parameter to set the value of the ARR register.
  • Pulse (16 bits): Parameter to mofidy the CCR register.
  • Mode: The PWM mode 1 must be selected.

Additionally, within the code, the timer to use must be initialized following the structure below:

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);


The CCR value can be modified by introducing the following instruction:

htimx.Instance->CCR1 = new_ccr;

in which CCR1 indicates that we are accessing to the channel 1 register, new_ccr is the new to set and htimx refers to the timer x (for example, htim2 or htim3).

In the same way, to modify the register of the channel 2:

htimx.Instance->CCR2 = new_ccr;

Serial Port