#include "stm32c031xx.h"
#define DIO_HIGH() (GPIOA->BSRR = (1 << 0))
#define DIO_LOW() (GPIOA->BSRR = (1 << 16))
#define CLK_HIGH() (GPIOA->BSRR = (1 << 1))
#define CLK_LOW() (GPIOA->BSRR = (1 << 17))
void DELAY(uint32_t us) {
for(volatile uint32_t i = 0; i < us; i++);
}
void START(void) {
CLK_HIGH();
DIO_HIGH();
DELAY(30);
DIO_LOW();
DELAY(30);
CLK_LOW();
}
void STOP(void)
{
CLK_LOW();
DIO_LOW();
DELAY(30);
CLK_HIGH();
DELAY(30);
DIO_HIGH();
DELAY(30);
}
void ACK (void) {
CLK_LOW();
DIO_HIGH();
DELAY(30);
CLK_HIGH();
DELAY(30);
CLK_LOW();
}
void DATA_IN(uint8_t byte) {
for(int BD = 0; BD < 8; BD++) {
CLK_LOW();
DELAY(30);
if(byte & 0x01) {
DIO_HIGH();
}else {
DIO_LOW();
}
DELAY(30);
CLK_HIGH();
DELAY(30);
byte >>= 1;
}
CLK_LOW();
DIO_HIGH();
DELAY(30);
CLK_HIGH();
DELAY(30);
CLK_LOW();
}
int main(void) {
RCC->IOPENR = 1;
GPIOA->MODER &= ~((3 << 0) | (3 << 2));
GPIOA->MODER |= (1 << 0) | (1 << 2);
GPIOA->OTYPER |= (1 << 0) | (1 << 1);
GPIOA->PUPDR &= ~((3 << 0) | (3 << 2));
GPIOA->PUPDR |= ((1 << 0) | (1 << 2));
CLK_HIGH();
DIO_HIGH();
DELAY(60);
volatile int SEGR = 0;
volatile int POSITION = 0;
volatile uint8_t SEG1 = 0;
volatile int SEG2 = 0;
volatile int SEG3 = 0;
volatile int SEG4 = 0;
uint8_t NUM[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
volatile int counter = 0;
while(1) {
counter = 0;
START();
DATA_IN(0x40);
STOP();
START();
DATA_IN(0xC0);
DATA_IN(0x00);
DATA_IN(0x00);
DATA_IN(0x00);
DATA_IN(0x00);
STOP();
START();
DATA_IN(0x8F);
STOP();
DELAY(100000);
SEG1 = 0;
SEG2 = 0;
SEG3 = 0;
SEG4 = 0;
while (counter < 4) {
if(counter == 3) {
for(volatile int num3 = 1; num3 < 10; num3++){
SEG4 = NUM[num3];
for(volatile int num2 = 0; num2 < 10; num2++){
SEG3 = NUM[num2];
for(volatile int num1 = 0; num1 < 10; num1++) {
SEG2 = NUM[num1];
for(volatile int num = 0; num < 10; num++) {
SEG1 = NUM[num];
START();
DATA_IN(0x40);
STOP();
START();
DATA_IN(0xC0);
DATA_IN(SEG4);
DATA_IN(SEG3);
DATA_IN(SEG2);
DATA_IN(SEG1);
STOP();
START();
DATA_IN(0x8F);
STOP();
DELAY(5000);
}
}
}
}
}else if(counter == 2) {
for(volatile int num2 = 1; num2 < 10; num2++){
SEG3 = NUM[num2];
for(volatile int num1 = 0; num1 < 10; num1++) {
SEG2 = NUM[num1];
for(volatile int num = 0; num < 10; num++) {
SEG1 = NUM[num];
START();
DATA_IN(0x40);
STOP();
START();
DATA_IN(0xC0);
DATA_IN(SEG4);
DATA_IN(SEG3);
DATA_IN(SEG2);
DATA_IN(SEG1);
STOP();
START();
DATA_IN(0x8F);
STOP();
DELAY(5000);
}
}
}
} else if(counter == 1) {
for(volatile int num1 = 1; num1 < 10; num1++){
SEG2 = NUM[num1];
for(volatile int num = 0; num < 10; num++) {
SEG1 = NUM[num];
START();
DATA_IN(0x40);
STOP();
START();
DATA_IN(0xC0);
DATA_IN(SEG4);
DATA_IN(SEG3);
DATA_IN(SEG2);
DATA_IN(SEG1);
STOP();
START();
DATA_IN(0x8F);
STOP();
DELAY(5000);
}
}
}else {
for(volatile int num = 0; num < 10; num++) {
SEG1 = NUM[num];
START();
DATA_IN(0x40);
STOP();
START();
DATA_IN(0xC0);
DATA_IN(SEG4);
DATA_IN(SEG3);
DATA_IN(SEG2);
DATA_IN(SEG1);
STOP();
START();
DATA_IN(0x8F);
STOP();
DELAY(5000);
}
}
counter++;
}
}
}