1.题目要求
在数码管上实现滚动显示学号
2.参考代码(中断方法)
/******************************学号显示************************
* 学号显示
***********************************************************************/
#include "sys.h"
#include "delay.h"
#include "led.h"
#define uchar unsigned char
// 你的学号(10是不显示)
int StudentID[] = {1,2,3,4,5,6,7,8,9};
// 计算学号长度
int IDLength = (int)sizeof(StudentID) / sizeof(StudentID[0]);
// 时间秒
u8 second = 0;
/****************普通按键初始化函数********************
* 通用定时器中断初始化
* 这里时钟选择为APB1的2倍,而APB1为36M
* arr:自动重装值。
* psc:时钟预分频数
* 这里使用的是定时器3!
******************************************************/
void TimerxInit(u16 arr, u16 psc)
{
RCC->APB1ENR |= 1 << 1; // TIM3时钟使能
TIM3->ARR = arr; // 设定计数器自动重装值,10为1ms
TIM3->PSC = psc; // 预分频器7200,得到10KHZ的计数时钟
TIM3->DIER |= 1 << 0; // 允许更新中断
TIM3->CR1 |= 0x01; // 使能定时器3
MY_NVIC_Init(1, 3, TIM3_IRQChannel, 2); // 抢占1,子优先级3,组2
}
/****************定时器3的中断函数********************
* 定时器3的中断函数
* 每次中断,second加一
******************************************************/
void TIM3_IRQHandler(void)
{
if (TIM3->SR & 0x0001) // 溢出中断
{
second = (second++) % ((2 ^ 32) - 1);
}
TIM3->SR &= ~(1 << 0); // 清除中断标志位
}
// 板子初始化
void InitBand()
{
Stm32_Clock_Init(6);
delay_init(72);
TimerxInit(9999, 7199); // 10Khz的计数频率,计数到10000表示1s
LED_Init();
LED_SEL = 0;
}
/***************************学号显示函数*****************************/
void ShowStudentID(int ID[])
{ // 变量定义
uchar i, icode;
// 8位显示
for (i = 0; i < 8; i++)
{
// 第i位数码管显示数字(second用来移动数字)
icode = ID[(i + second) % (IDLength)];
// 显示数字
SetLed(i, icode);
// 频率控制(显示亮度控制,数值越大显示越亮)
delay_us(200);
}
}
/***************************主函数*****************************/
int main()
{
// 板子初始化
InitBand();
// 主循环
while (TRUE)
{
// 学号显示
ShowStudentID(StudentID);
}
}