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 3 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. and 1ms to 2ms on-time required to drive the motor.

Coding in STM32 Cube IDE

Once you have generated the code for the SPI and TIM (timer) 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