Skip to content

Conversation

@LT-733
Copy link

@LT-733 LT-733 commented Nov 9, 2025

The previous commits added the following functionalities:

  1. Configured pins for SPI1 to communicate with ADC
  2. Set the prescalar and the timer period to optimize resolution
  3. Wrote code for CH1 to send and receive signals to/from ADC
  4. Wrote code for PWM to compare the data output from the ADC

The configuration can build without bugs. Hence we are ready for reviewing.

Copy link

@EemanAleem EemanAleem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work Leon! Mostly minor changes since you have the logic down, however, you need to start the TIM in order to use it wuld be the major change requested. Additionally, your bit configuration for txData is incorrect, so that should be revisited.

//this is because 0x90 = 0b10010000
//where 1 = sgl/diff, 0 = d2, 0 = d1, 1 = d0
uint8_t txData[3] = {0x01, 0xD0, 0x00};//send
uint8_t rxData[3] = {0};//receive

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txData and rxData do not need to be re-declared every time the ADC channel needs to be read; move these outside the looping section of the main code.

uint16_t Read_ADC_Channel1(void){
//this is because 0x90 = 0b10010000
//where 1 = sgl/diff, 0 = d2, 0 = d1, 1 = d0
uint8_t txData[3] = {0x01, 0xD0, 0x00};//send

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try looking at the datasheet again for the config for the second indexed value in txData!

//pull Chip Select to low, as the slave receives (master sends out) data on low edge
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
//now we use spi1 to transmit/receive data by HAL_SPI_TransmitReceive()
HAL_SPI_TransmitReceive(&hspi1, txData, rxData, 3, HAL_MAX_DELAY);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Ensure you use no magic numbers (i.e. assign every value some variable for code clarity).
  • Utilize the error code you receive from the HAL_SPI_TransmitReceive, and determine whether to flag the error or continue with the rest of the code!

//pull Chip Select back to high, as it is supposed to idle in high
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
//reconstruct the output into a 10 bit value of type uint16_t
uint16_t adcOut = ((rxData[1] & 0x03) << 8) | rxData[2];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is correct, ensure you don't have redundant declarations every time a section of code is looped! So have the declaration of adcOut outside the main while loop :)

Or tbh, you could just return the calculation instead of needing to allocate memory for adcOut.

uint32_t Convert_to_PWM(uint16_t adcin){
//we begin with minimum 1ms, all the way up to 2ms by maxing out adcin as 1023.
//notice the formula base value + (adcread * delta)/10-bit-max
return 3000 + (uint32_t)adcin * (6000 - 3000) / 1023;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer to comment on line 68 on no magic numbers.

MX_SPI1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to start your selected timer's channel (using HAL), otherwise you cannot use it!

Copy link

@EemanAleem EemanAleem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor changes requested. For the transmission bits, you need to use channel 0 for the transmission from the ADC, not channel 1.

//where 1 = sgl/diff, 0 = d2, 0 = d1, 1 = d0
uint8_t txData[MCP3004_TRANSFER_BYTES] = {
MCP3004_START_BIT,
MCP3004_SINGLE_ENDED|MCP3004_CHANNEL_1,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txData[1] should only be MCP3004_SINGLE_ENDED! txData[2] should have the channel used for the ADC which is 0 here. The TIM channel is 1, the one for the ADC is 0.

I would suggest keeping the channel name generic (i.e. MCP3004_CHANNEL) and setting it to 0x00. Then if we're being verbose, txData[2] = MCP3004_CHANNEL.

HAL_StatusTypeDef spiStatus;

//pull Chip Select to low, as the slave receives (master sends out) data on low edge
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you've defined variables in lines 39 and 40 for GPIOB and GPIO_PIN_8, use them here in line 94!

Copy link

@EemanAleem EemanAleem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to WARG!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants