#include "stm32f4xx.h"
// Define the GPIO pins for the 7-segment display (PA0 to PA8 for segments A to G)
#define SEG_A_PIN (1 << 0) // PA0
#define SEG_B_PIN (1 << 1) // PA1
#define SEG_C_PIN (1 << 4) // PA4
#define SEG_D_PIN (1 << 5) // PA5
#define SEG_E_PIN (1 << 6) // PA6
#define SEG_F_PIN (1 << 7) // PA7
#define SEG_G_PIN (1 << 8) // PA8
// Define digit control pins (PB0 to PB3 for 4 digits)
#define DIGIT_4_PIN (1 << 0) // PB0, DIGIT 4
#define DIGIT_3_PIN (1 << 1) // PB1, DIGIT 3
#define DIGIT_2_PIN (1 << 2) // PB2, DIGIT 2
#define DIGIT_1_PIN (1 << 5) // PB5, DIGIT 1
// Corrected segment patterns for digits 9, 0, 3
#define DIGIT_9_PATTERN (SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN | SEG_F_PIN | SEG_G_PIN) // 9
#define DIGIT_0_PATTERN (SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN | SEG_E_PIN | SEG_F_PIN) // 0
#define DIGIT_3_PATTERN (SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN | SEG_G_PIN) // 3
// Function to display a digit on the 7-segment display
void display_digit(uint32_t digit_pattern, uint32_t digit_pin) {
// Turn off all digits
GPIOB->ODR &= ~(DIGIT_1_PIN | DIGIT_2_PIN | DIGIT_3_PIN | DIGIT_4_PIN);
// Set the segment pattern for the digit
GPIOA->ODR &= ~0x1FF; // Clear PA0-PA8 (segments)
GPIOA->ODR |= digit_pattern;
// Turn on the desired digit
GPIOB->ODR |= digit_pin;
}
// Function for delay to create visible effect
void delay(void) {
for (volatile int i = 0; i < 1000000; i++); // Adjust delay for refresh rate
}
int main(void) {
// Enable GPIOA and GPIOB clocks
RCC->AHB1ENR |= 0x03;
// Set PA0-PA8 (segments) and PB0-PB3 (digit control) to output mode
GPIOA->MODER |= 0x55555555; // Set PA0, PA1, PA4, PA5, PA6, PA7, PA8 to output mode
GPIOB->MODER |= 0x55; // Set PB0, PB1, PB2, PB3 to output mode
// Time for each digit: 1.33 seconds
for (int i = 0; i < 2; i++) {
// Display 9 on digit 2 (PB2)
display_digit(DIGIT_9_PATTERN, DIGIT_2_PIN);
delay();
// Display 0 on digit 3 (PB1)
display_digit(DIGIT_0_PATTERN, DIGIT_3_PIN);
delay();
// Display 3 on digit 4 (PB0)
display_digit(DIGIT_3_PATTERN, DIGIT_4_PIN);
delay();
}
return 0;
}Loading
st-nucleo-c031c6
st-nucleo-c031c6