#pragma GCC push_options
#pragma GCC optimize ("O0")
#include "stm32c0xx.h"
#include "myClockConfig.h"
#include "mySysTick.h"
#include "myADCconfig.h"
#include "myReadBusFrequencies.h"
#include "myUARTconfig.h"
#include "stdio.h"
//Set variables for the ADC
unsigned int adcval;
float Vin;
//Set the variable to hold the analogue voltage represented as a string
char Vin_string[];
//Set variables to contain the BUS frequencies
unsigned int SYSCLK;
unsigned int HCLK;
unsigned int APBCLK;
int main(void)
{
// Enable the GPIOB peripheral
RCC -> IOPENR |= (1UL << 1);
// Configure PB2 to PB9 as output pins
GPIOB ->MODER = 0x5555550;
//Enable the GPIOD peripheral
RCC -> IOPENR |= (1UL << 3);
//Configure PD2 as an output pin
GPIOD ->MODER = (1UL << 4);
//Configure the bus frequqncies according to the settings in the myClockConfig.c file
SystemClock_Config();
//Configure the UART for serial communications on UART2
myUARTconfig ();
//Configure the ADC according to the settings in the myADCconfig.c file
configADCpins();
initADC();
//Read the BUS frequencies
SYSCLK = GetSYSCLK();
HCLK = GetHCLKFreq();
APBCLK = GetPCLKFreq();
printf("SYSCLK:%dHz, HCLK:%dHz, APBCLK:%dHz\n", SYSCLK, HCLK, APBCLK);
while (1)
{
//Read the ADC value
adcval = readADC();
//Send it to the LEDS on GPIOB (PB2 to PB13)
GPIOB->ODR = (adcval << 2);
// Calculate the analogue input voltage read by the potentiometer
Vin = (adcval * 3.3) / 63;
//Because STM32C031C6 does not have a Floating Point Unit (FPU) and the "printf"
//function limitations within the context of the embedded environment, floating point
//numbers cannot be printed. Therefore the floating point number has to first be
//converted into a string before being printed via the USART. The function below
//allows for the conversion of the float to a string.
// dtostrf(float_value, min_width, num_digits_after_decimal, where_to_store_string)
dtostrf(Vin, 4, 3, Vin_string);
printf("The analogue voltage is %s volts\n", Vin_string);
//Call a 1 second delay
myDelay(1000);
}
}