Difference between revisions of "Nucleo Boards for Control Systems"

From RobolaboWiki
Jump to: navigation, search
Line 2: Line 2:
  
  
<h1> Lectura y escritura de pines </h1>
+
<h1> GPIO read and write </h1>
<h2> Lectura</h2>
+
 
 +
<h2> Read from a GPIO</h2>
 
<pre>
 
<pre>
 
HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_9);
 
HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_9);
 
</pre>
 
</pre>
<h2> Escritura</h2>
+
<h2> Write to a GPIO</h2>
- A nivel alto:
+
- High level:
 
<pre>
 
<pre>
 
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
 
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
 
</pre>
 
</pre>
  
- A nivel bajo:
+
- Low level:
 
<pre>
 
<pre>
 
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
 
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
Line 20: Line 21:
  
 
<h1> Encoder </h1>
 
<h1> Encoder </h1>
<h2> Teoría </h2>
+
<h2> Theory </h2>
  
  
<h2>Configuración de encoder en STM32CubeIDE</h2>
+
<h2> Encoder Configuration in STM32CubeIDE</h2>
  
  
Line 29: Line 30:
 
<h1> PWM </h1>
 
<h1> PWM </h1>
  
<h2> Teoría </h2>
+
<h2> Theory </h2>
  
  
Line 35: Line 36:
  
  
<h2> Principales registros </h2>
+
<h2> Main registers </h2>
 
<h3> Prescaler (PSC) </h3>
 
<h3> Prescaler (PSC) </h3>
El preescalado es un registro que especifica entre cuánto se debe dividir la frecuencia de referencia del reloj usado por el timer.
+
 
En concreto, la reducción  de frecuencia se aplica de la siguiente manera:
+
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)
 
f_PWM = f_CLK / (PSC + 1)
  
Por lo tanto, hay que tener en cuenta que si queremos mantener la misma frecuencia que la del reloj de referencia debemos fijar PSC=0.
+
Therefore, if the frequency to set is the same as the frequency of the clock, the PSC register must be set at PSC=0.
Los timers del STM32 admiten valores de preescalado de hasta 65535, mediante registros de 16 bits.
+
The STM32 timers allow a prescaler value up to 65535, through 16 bits registers.  
  
<h3> Auto Reload Register (ARR) y Capture Compare Register (CCR) </h3>
+
 
El registro ARR almacena el valor máximo que debe alcanzar el contador antes de volver a cero.  
+
<h3> Auto Reload Register (ARR) and Capture Compare Register (CCR) </h3>
Por otra parte, CCR registra valor del contador del timer asociado a partir del cual la señal PWM de salida pasa de nivel alto a nivel bajo.  
+
The ARR register storages the maximum value that the counter can reach before going back to zero.  
Cada canal del timer usado para la generación de señal PWM tendrá un registro CCR diferente (por ejemplo CCR1 y CCR para canales 1 y 2).  
+
Moreover, the CCR registers the value of the corresponding timer counter after which the output PWM signal goes from high to low level.  
De esta manera, el valor de CCR junto con el valor de ARR definen la anchura de los pulsos y con ello el ciclo de trabajo de la señal PWM:
+
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)
 
Duty_Cycle (%) = 100 * (CCR / ARR)
  
 
<h3> CNT </h3>
 
<h3> CNT </h3>
Es el registro que almacena el valor actual del contador usado para la generación PWM.  
+
This is the register that storages the value of the counter used to generate the PWM.  
 +
 
 +
<h2>PWM Configuration in STM32CubeIDE</h2>
  
<h2>Configuración de PWM en STM32CubeIDE</h2>
+
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.
  
Para configurar el timer como PWM primero debemos seleccionar uno de los posibles timers configurables como generador PWM.
+
<b>Advice:</b> As different PWM outputs are needed, it is recommended to use different channels of the same timer to the extent possible.
También debemos decidir los canles que queremos usar como salidas PWM.
+
  
<b>Consejo:</b> Al usar varias salidas PWM es recomendable usar diferentes canales del mismo timer en la medida de lo posible.
+
The main timers that can be set as PWM are: TIM2, TIM3, TIM4 and TIM5, with 4 different channels each of them.  
  
Los principales timers configurables como PWM son el TIM2, TIM3, TIM4 y TIM5, con 4 canales independientes cada uno.  
+
<b>Advice:</b> 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.  
  
<b>Consejo:</b>  Es recomendable usar el timer y los canales que se correspondan directamente con los pines que alimentan las entradas del Puente H para modular la velocidad del motor. En caso contrario deberemos puentear las salidas del timer PWM con las entradas del puente H.
+
Therefore, the pins that the channels of the timer are using need to be checked in the "Pinout" section of the STM32CubeIDE.
  
Por lo tanto, deberemos buscar los pines asociados a los canales del timer a usar en la vista “Pinout” del entorno de desarrollo.
+
Once the channels of the timer have been activated, some parameters and the configuration of the timer must be changed.  
  
Una vez hemos activado los canales del timer necesarios, debemos modificar la configuración y parámetros del timer.
+
Main parameters:
Los principales parámetros
+
 
<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> 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>

Revision as of 12:12, 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: 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.
  • Channel x: 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.
  • Prescaler (PSC): Parámetro para configurar el registro de PSC.
  • Counter Period: Parámetro para configurar el valor del registro ARR.
  • Pulse (16 bits): parámetro para modificar el registro de CCR.
  • Mode: Debemos seleccionar PWM mode 1.

Además, en nuestro código debemos activar el timer ya configurado e inicializado de la siguiente manera:

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);


Podemos modificar el valor de CCR (aceder al registro) mediante código de la siguiente manera:

htimx.Instance->CCR1 = new_ccr;

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).

De igual manera, para modificar el registro del canal 2:

htimx.Instance->CCR2 = new_ccr;

Serial Port