MCP3208-STM32-ADC-Integration-main

by Marwen Maghrebi
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "mcp3208.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
MCP3208_HandleTypeDef hmcp3208;
/* USER CODE END PTD */

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

/* USER CODE BEGIN PV */
#define VREF 2.5 // Reference voltage
#define ADC_RESOLUTION 4096  // 12-bit ADC

const float V_REF = 2.5f;    // Adjust based on your reference voltage
const float V_REF2 = 5.0f;    // Adjust based on your reference voltage

const int ADC_MAX_VALUE = 4095; // MCP3208 is a 12-bit ADC
const int ADC_MAX_VALUE2 = 1024; // MCP3208 is a 12-bit ADC

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void print_uart(const char* str)
{
    HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), HAL_MAX_DELAY);
}

float adc_to_voltage(uint16_t adc_value)
{
    return (float)adc_value * VREF / ADC_RESOLUTION ;

}

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config();
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
    // Initialize MCP3208
     hmcp3208.hspi = &hspi1;
     hmcp3208.CS_GPIO_Port = GPIOA;
     hmcp3208.CS_Pin = GPIO_PIN_4;
     MCP3208_Init(&hmcp3208);
    
     char temp[10];
     int ctr = 0;
  /* USER CODE END 2 */
    
 /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      if (ctr == 0)
      {
          HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r", 2, HAL_MAX_DELAY);
          HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r              Single Ended (V)           |    Differential (V)\n\r", 68, HAL_MAX_DELAY);
          HAL_UART_Transmit(&huart1, (uint8_t*)"   0    1    2    3    4    5    6    7  |   0     1    2    3\n\r", 66, HAL_MAX_DELAY);
          HAL_UART_Transmit(&huart1, (uint8_t*)"------------------------------------------------------------------------\n\r", 66, HAL_MAX_DELAY);
          HAL_UART_Transmit(&huart1, (uint8_t*)"\n\r", 2, HAL_MAX_DELAY);
      }

      ctr++;
      if (ctr == 10)
      {
          ctr = 0;
      }

      // Read single-ended values
      for (int i = 0; i < 8; i++)
      {
          uint16_t value = MCP3208_AnalogRead(&hmcp3208, i);
          float voltage = (value * V_REF) / ADC_MAX_VALUE; // Convert to voltage
          sprintf(temp, "%5.2f", voltage);  // Format to 2 decimal places with 5 characters (including leading spaces)
          HAL_UART_Transmit(&huart1, (uint8_t*)temp, strlen(temp), HAL_MAX_DELAY);
      }
      HAL_UART_Transmit(&huart1, (uint8_t*)" | ", 2, HAL_MAX_DELAY);

      // Read differential values
      for (int i = 0; i < 4; i++)
      {
          int16_t diff_value = MCP3208_AnalogReadDif(&hmcp3208, i);
          float diff_voltage = (diff_value * V_REF) / ADC_MAX_VALUE; // Convert to voltage
          sprintf(temp, "%5.2f ", diff_voltage); // Format to 2 decimal places with a single space after the value
          HAL_UART_Transmit(&huart1, (uint8_t*)temp, strlen(temp), HAL_MAX_DELAY);
      }
      HAL_UART_Transmit(&huart1, (uint8_t*)"\r\n", 2, HAL_MAX_DELAY);
      HAL_Delay(1000); // Delay for stability
  }
  /* USER CODE END 3 */
}
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