基于stm32的led点阵屏的代码
时间: 2023-11-03 15:32:18 浏览: 44
以下是一个基于STM32的LED点阵屏的代码示例,实现了16×16LED点阵动态显示“光电子系欢迎您”字样,滚动速度可以通过调整定时器的时间来控制:
```
#include "stm32f10x.h"
#include "delay.h"
//定义16×16LED点阵数组
const unsigned char LED_Matrix[32] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //空白行
0x20, 0x10, 0x08, 0x04, 0x02, 0x7F, 0x00, 0x00, //光
0x00, 0x00, 0x7E, 0x02, 0x02, 0x7E, 0x00, 0x00, //电
0x02, 0x02, 0x02, 0x02, 0x7F, 0x02, 0x02, 0x02, //子
0x7F, 0x40, 0x40, 0x7E, 0x40, 0x40, 0x40, 0x7F //系欢迎您
};
//定义定时器中断处理函数
void TIM3_IRQHandler(void) {
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
static unsigned char pos = 0; //当前显示位置
static unsigned char row = 0; //当前显示行
static unsigned char speed = 0; //滚动速度,0为慢,1为快
static unsigned char delay = 0; //延时计数器
if(delay++ >= speed) { //根据滚动速度调整延时时间
delay = 0;
for(unsigned char i = 0; i < 16; i++) {
GPIO_Write(GPIOB, LED_Matrix[pos + i * 2] << row); //设置LED点阵的行
GPIO_Write(GPIOC, LED_Matrix[pos + i * 2 + 1] << row);
}
if(++row >= 8) { //当前行显示完毕,切换到下一行
row = 0;
if(++pos >= 16) pos = 0; //当前页显示完毕,切换到下一页
}
}
}
}
int main(void) {
//初始化GPIO口和定时器
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 1000; //定时器周期为1ms
TIM_TimeBaseStructure.TIM_Prescaler = 7199; //定时器分频系数为7200
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, ENABLE);
//进入死循环
while(1) {
//在循环中调整滚动速度
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) {
delay_ms(10);
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET) {
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == RESET);
if(++speed >= 2) speed = 0;
}
}
}
}
```
在这个代码中,使用了定时器中断来控制LED点阵的显示。定时器的周期为1ms,定时器中断处理函数中根据滚动速度调整延时时间,并依次显示每个字形。同时,还在程序中添加了滚动速度调整的功能,通过按下开发板上的按键来切换滚动速度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)