// Insert compiler directives to set the optimization to zero
// We do not want the compiler to optimize out any code
#pragma GCC push_options
#pragma GCC optimize ("O0")
// Include the header file.
#include "stm32c0xx.h"
// Include function declarations
void myDelay(unsigned int val);
// Declare variables
int valuea[] = {0x03A0, 0x0200, 0x01C0, 0x0340, 0x0260, 0x0360, 0x03E0, 0x0200, 0x03E0, 0x0360};
int valueb[] = {0x0300, 0x0100, 0x0300, 0x0300, 0x0100, 0x0200, 0x0200, 0x0300, 0x0300, 0x0300};
int length = sizeof(valuea) / sizeof(valuea[0]);
int count;
// Increase diplay value only when button pressed
int main(void)
{
// Enable GPIOA, GPIOB, GPIOD
RCC->IOPENR |= 11UL;
// Configure related pins as an output pin / push-pull
GPIOA -> MODER = 0x00055400;
GPIOB -> MODER = 0x00050000;
// Configure related pins as an input pin / pull-up
GPIOD -> MODER = 0x00000000;
GPIOD -> PUPDR = 0x00000000;
GPIOA -> OTYPER = 0x00000000;
GPIOB -> OTYPER = 0x00000000;
setup();
count = 0;
GPIOA -> BSRR = (0x03E0 << 16); // Turn off LED
GPIOB -> BSRR = (0x0300 << 16);
GPIOA -> BSRR = valuea[count]; // Turn on LED
GPIOB -> BSRR = valueb[count];
do{
myDelay(100000);
if (((GPIOD -> IDR) & 0x01) == 0){ // Check if increment button pressed
myDelay(1000); // Debounce
if (((GPIOD->IDR) & 0x01) == 0){
GPIOA -> BSRR = (0x03E0 << 16); // Turn off LED
GPIOB -> BSRR = (0x0300 << 16);
GPIOA -> BSRR = valuea[count]; // Turn on LED
GPIOB -> BSRR = valueb[count];
count ++;
if (count == length){
count = 0;
}
}
}
if (((GPIOD -> IDR) & 0x02) == 0){ // Check if decrement button pressed
myDelay(1000); // Debounce
if (((GPIOD->IDR) & 0x02) == 0){
if (count == 0) {
count = length - 1; // Wrap to max
} else {
count--;
}
GPIOA -> BSRR = (0x03E0 << 16); // Turn off LED
GPIOB -> BSRR = (0x0300 << 16);
GPIOA -> BSRR = valuea[count]; // Turn on LED
GPIOB -> BSRR = valueb[count];
}
}
} while(1);
}
// Add function definitions here
void myDelay(unsigned int val)
{
int i;
for (i = 0; i < val; i++);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, STM32 BERR CLASS!");
}