#include <stm32f10x.h>
uint8_t seg8[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
void delay(void) { uint16_t i; for (i = 0; i < 20000; i++); }
void ser_in(uint8_t data) { uint8_t i; for (i = 0; i < 8; i++) { GPIOB->BRR = GPIO_BRR_BR9; // SCLK=PB9 if (data & 0x80) GPIOB->BSRR = GPIO_BSRR_BS7; // DIO=PB7 else GPIOB->BRR = GPIO_BRR_BR7; data <<= 1; GPIOB->BSRR = GPIO_BSRR_BS9; } }
void par_out(void) { GPIOB->BRR = GPIO_BRR_BR8; // RCLK=PB8 GPIOB->BSRR = GPIO_BSRR_BS8; }
void seg_scan(void) { uint8_t i; uint32_t numbuf = (32767 - RTC->DIVL) * 1000 / 32768; // 小数部分: DIV的范围是0~32767 (向下计数) numbuf += RTC->CNTL * 1000; // 整数部分 for (i = 0; i <= 7; i++) { if (i == 3) ser_in(seg8[numbuf % 10] & 0x7f); // 显示小数点 else ser_in(seg8[numbuf % 10]); ser_in(1 << i); par_out(); delay(); numbuf /= 10; } }
int main(void) { // 配置RTC时钟 RCC->APB1ENR = RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN; PWR->CR = PWR_CR_DBP; // 允许写入后备寄存器 if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0) { // 若RTC还没有打开, 则初始化RTC if ((RCC->BDCR & RCC_BDCR_LSEON) == 0) RCC->BDCR |= RCC_BDCR_LSEON; // 开外部低速晶振LSE RCC->BDCR |= RCC_BDCR_RTCEN; // 打开RTC外设, 但暂不开始走时 RTC->CRL |= RTC_CRL_CNF; // 进入RTC配置模式 RTC->PRLH = 0; RTC->PRLL = 32767; // 定时1s (PRLH和PRLL寄存器只能写不能读) //RTC->CNTH = 0; //RTC->CNTL = 50; // 初始时间 RTC->CRL &= ~RTC_CRL_CNF; // 保存设置 RCC->BDCR |= RCC_BDCR_RTCSEL_0; // 选LSE作为RTC时钟, 准备走时 while ((RTC->CRL & RTC_CRL_RTOFF) == 0); // 等待设置生效 // RTC从此处开始走时 } // 配置GPIO口 RCC->APB2ENR = RCC_APB2ENR_IOPBEN; GPIOB->CRL = 0x30000000; GPIOB->CRH = 0x00000033; while (1) seg_scan(); }
|