// Insert compiler directives to set optimization to zero
// We do not want the compiler to optimize out any code
#pragma GCC push_options
#pragma GCC optimize("O0")
//Include header file.
//IN STM32 microcontroller programming, the header files provided by the
//manufacture (STmicroelectronics) contain the neccessary definitions,
//constants, and function phototypesmspecific to the microcontrollers peripherals
//and features
#include "stm32c0xx.h"
#include "myUARTconfig.h"
#include "myReadBusFrequencies.h"
#include "stdio.h"
#include "mySysTick.h"
#include "myClockConfig.h"
#include "myADCconfig.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 Bus Frequencies
unsigned int SYSCLK;
unsigned int HCLK;
unsigned int APBCLK;
int main(void)
{
//FOR LEDS ON BREADBOARD
RCC-> IOPENR |= (1UL << 1);
// Configure PB2 to PB9 as output pins
GPIOB ->MODER = 0x5555550;
//Enable the GPIOB Peripheral
RCC->IOPENR |= (1UL << 3);
//Configure PDO
GPIOD->MODER &=~ (3UL);//CLEAR BITS 1 AND O
GPIOD-> MODER |= (1UL << 4);
//Configure the bus frequencies according to settings in the myClockConfig
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 bus frequencies
SYSCLK = GetSYSCLK();
HCLK = GetHCLKFreq();
APBCLK = GetPCLKFreq();
printf("SYSCLK:%dHz, HCLK:%dHz, APBCLK:%dHz\n", SYSCLK, HCLK, APBCLK);
while(1)
{
//Toggle the state of PD0
GPIOD -> ODR ^= (1UL);
//Read the ADC value
adcval = readADC();
//Send it to the LEDS on GPIOB (PB2 to PB9)
GPIOB->ODR = (adcval << 2); //starts from PB2 - PB9
//Calculate the anol input voltage read by the potentiometer
Vin = (adcval * 3.3) / 4095; // 4095 = 2^12
// dtostr(float_value, min_width, num_digits_after_decimal, where_to_store_string)
dtostrf(Vin, 4, 3, Vin_string );
print("The analogue voltage is %s volts\n", Vin_string);
// call a 1 second delay
myDelay(1000);
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6