stm32-spi-integration-with-adc128s102

by Marwen Maghrebi
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <math.h>
#include <stdio.h>
#include <string.h>

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart1;

/* USER CODE BEGIN 0 */
#define REF 5.0
int readADC(int chan)
{
uint8_t txData[2] = {chan << 3, 0};
uint8_t rxData[2];
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi1, txData, rxData, 2, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
return (rxData[0] << 8) | rxData[1];
}
/* USER CODE END 0 */

int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();

while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_UART_Transmit(&huart1, (uint8_t*)" \n\r ", 3, HAL_MAX_DELAY);
for (int ch = 0; ch < 8; ch++)
{
int newCh = (ch == 7) ? 0 : ch + 1;
int val = readADC(newCh);
float reading = (val * (REF / 4096));
char MSG[60];
sprintf(MSG,"ch %d = reading %.2f Volt",ch , reading);
HAL_UART_Transmit(&huart1, (uint8_t*)MSG, sizeof(MSG), HAL_MAX_DELAY);
HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r", 3, HAL_MAX_DELAY);
}
HAL_Delay(2000);
}
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00