Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Now that all the peripheral pins are in their respective modes, They will need to be configured more specifically and the actual coding can begin.

Triggering and reading ADC input

The microcontroller will need to tell the ADC to start converting the voltage value and then receive the digital input over SPI.

The SPI communication will need to be further configured. To configure it go to the .ioc file, find SPI1 in the Connectivity menu on the left:

The data size is the length of each message in bits, to make communication easier, set this to 8 bits.

In the ADC data sheet there is information on controlling the ADC from the microcontroller in Sections 5.0 and 6.1.

Specifically check the following configuration parameters:

  • Clock polarity

  • If the ADC sends Most or Least significant bit first (MSB/LSB)

  • How to select which channel the ADC should measure from and in which mode (Will be needed later)

  • How the communication interaction works.

It is good to note that the MCU can run much faster than the ADC, so adding a prescaler of 16 or greater in the configuration of SPI1 will reduce the speed it needs to operate at.

For this circuit board, you can check which ADC channel the potentiometer is connected to from the schematic. The mode is differential if it is comparing 2 signals, single if it comparing one signal to the ground input.

The Hal library automatically generates the base code required to run the configuration you set when you save the .ioc file. You do want the IDE to generate code for you.

Configure the Timer (PWM)

To configure it go to the .ioc file, find TIM1 in the Timers menu on the left:

To start ensure that the channel (pin connected to timer) you chose is in the “PWM Generation CH#” mode. The clock source should be “Internal Clock” as we are using the clock that is built into the MCU.

The Prescaler and Period of the timer will need to be configured to meet the 50Hz. (period of 20ms) and 1ms to 2ms on-time (5-10% duty cycle) required to drive the motor.

The timer works as a counter, it counts up to the set counter period value before changing the output. The maximum number it can count to is 65535, or the maximum 2 byte number (uint16_t of 0xFFFF). The prescaler will slow down the system clock by dividing by (prescaler + 1) that way the speed is unaffected if prescaler = 0.

The equations based on the input clock speed are:

For this system the input clock speed is 48.0MHz. and the desired frequency is 50.0Hz.

Hint: A lower prescaler value with a higher counter period value will produce more accurate results.

Coding in STM32 Cube IDE

Once you have generated the code for the SPI and TIM configuration, you can find those values and setting will show up in the Core->Src directory in the project. Since you have configured those

To find the functions you will use to send and read messages from the ADC, find the stm32f0xx_hal_spi.h file in the Drivers->STM32F0xx_HAL_Driver->Inc directory and read the function prototypes at the bottom or visit the HAL User Manual and look for the SPI input and output functions (IO Operations).

It is good to note that this ADC requires full duplex mode SPI communication, meaning that information will be sent to the ADC and read from it at the same time. You will need to use the SPI function that can both send and receive data.

 

Converting ADC value to PWM signal

The main behavior of the system will be written here. The microcontroller will need to receive the digital input over SPI

Best Practices

For the Firmware team we follow a naming and code style guide to make our code more uniform and readable

  • No labels