#include "stm32f10x.h"
uint8_t seg_code[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
void delay_ms(uint32_t ms) {
for (uint32_t i = 0; i < ms * 8000; i++);
}
void GPIO_Config(void) {
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN;
// PORTA as output for segments
GPIOA->CRL = 0x33333333; // PA0–PA7 output push-pull 50MHz
// PORTB as input (data source)
GPIOB->CRL = 0x44444444; // PB0–PB7 input floating
// PORTC pins 0–2 as output (digit select)
GPIOC->CRL &= ~0x00000FFF;
GPIOC->CRL |= 0x00000333;
}
void display_digit(uint8_t digit, uint8_t pos) {
// Turn off all digits
GPIOC->ODR &= ~(0x07);
// Output segment pattern
GPIOA->ODR = seg_code[digit];
// Enable current position
GPIOC->ODR |= (1 << pos);
delay_ms(5); // small multiplexing delay
}
int main(void) {
uint8_t value, hundreds, tens, ones;
GPIO_Config();
while (1) {
value = GPIOB->IDR & 0xFF; // read byte from PORTB
hundreds = value / 100;
tens = (value % 100) / 10;
ones = value % 10;
// multiplex display
display_digit(hundreds, 0);
display_digit(tens, 1);
display_digit(ones, 2);
}
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6