【代码】 #include <stm32f10x.h>
#define _BV(n) (1 << (n))
uint32_t n = 12345678; uint8_t segdisp[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90}; // 共阳数码管段码表,取反后就是共阴的段码
void delay() { int i; for (i = 0; i < 2500; i++); }
void init() { int i; RCC->APB2ENR |= _BV(2) | _BV(3); // 打开GPIOA和GPIOB的时钟 // 把PA0~7都设置为输出模式(速率50MHz) for (i = 0; i < 8; i++) { GPIOA->CRL |= _BV(4 * i + 1); GPIOA->CRL |= _BV(4 * i); } // 把PB8~15都设置为输出模式 for (i = 0; i < 8; i++) { GPIOB->CRH |= _BV(4 * i + 1); GPIOB->CRH |= _BV(4 * i); } }
int main() { init(); while (1) { GPIOB->ODR = ~segdisp[n % 10] << 8; GPIOA->ODR = ~_BV(7); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 100 / 10] << 8; GPIOA->ODR = ~_BV(6); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 1000 / 100] << 8; GPIOA->ODR = ~_BV(5); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 10000 / 1000] << 8; GPIOA->ODR = ~_BV(4); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 100000 / 10000] << 8; GPIOA->ODR = ~_BV(3); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 1000000 / 100000] << 8; GPIOA->ODR = ~_BV(2); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n % 10000000 / 1000000] << 8; GPIOA->ODR = ~_BV(1); delay(); GPIOA->ODR = 0; GPIOB->ODR = ~segdisp[n / 10000000] << 8; GPIOA->ODR = ~_BV(0); delay(); GPIOA->ODR = 0; } }
void SystemInit() { }
|