用stm32f106在proteus中连接一个8*8的led点阵并使用
时间: 2024-06-09 13:04:48 浏览: 19
首先,需要选择一个合适的8x8的LED点阵模块,并将其添加到Proteus中。然后,需要将模块与STM32F106微控制器进行连接。以下是一个简单的连接示意图:
![image.png](attachment:image.png)
其中,A~H是点阵的8个列,1~8是点阵的8行,OE是输出使能,CLK是时钟,和DIN是数据输入。
接下来,需要编写C代码来控制点阵的显示。以下是一个简单的示例代码:
```c
#include "stm32f10x.h"
// 定义点阵的列引脚
#define LED_COL_A GPIO_Pin_0
#define LED_COL_B GPIO_Pin_1
#define LED_COL_C GPIO_Pin_2
#define LED_COL_D GPIO_Pin_3
#define LED_COL_E GPIO_Pin_4
#define LED_COL_F GPIO_Pin_5
#define LED_COL_G GPIO_Pin_6
#define LED_COL_H GPIO_Pin_7
// 定义点阵的行引脚
#define LED_ROW_1 GPIO_Pin_8
#define LED_ROW_2 GPIO_Pin_9
#define LED_ROW_3 GPIO_Pin_10
#define LED_ROW_4 GPIO_Pin_11
#define LED_ROW_5 GPIO_Pin_12
#define LED_ROW_6 GPIO_Pin_13
#define LED_ROW_7 GPIO_Pin_14
#define LED_ROW_8 GPIO_Pin_15
// 定义点阵的控制引脚
#define LED_OE GPIO_Pin_6
#define LED_CLK GPIO_Pin_7
#define LED_DIN GPIO_Pin_8
// 定义字模数据
const uint8_t FONT[][8] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // 空白
{0x00, 0x00, 0x3C, 0x42, 0x42, 0x3C, 0x00, 0x00}, // A
{0x00, 0x00, 0x7E, 0x4A, 0x4A, 0x34, 0x00, 0x00}, // B
{0x00, 0x00, 0x3C, 0x42, 0x42, 0x24, 0x00, 0x00}, // C
{0x00, 0x00, 0x7E, 0x42, 0x42, 0x3C, 0x00, 0x00}, // D
{0x00, 0x00, 0x7E, 0x4A, 0x4A, 0x4A, 0x00, 0x00}, // E
{0x00, 0x00, 0x7E, 0x09, 0x09, 0x09, 0x00, 0x00}, // F
{0x00, 0x00, 0x3C, 0x42, 0x4A, 0x3C, 0x00, 0x00}, // G
{0x00, 0x00, 0x7E, 0x08, 0x08, 0x7E, 0x00, 0x00}, // H
{0x00, 0x00, 0x00, 0x42, 0x7E, 0x42, 0x00, 0x00}, // I
{0x00, 0x00, 0x20, 0x40, 0x40, 0x3E, 0x00, 0x00}, // J
{0x00, 0x00, 0x7E, 0x08, 0x14, 0x62, 0x00, 0x00}, // K
{0x00, 0x00, 0x7E, 0x40, 0x40, 0x40, 0x00, 0x00}, // L
{0x00, 0x00, 0x7E, 0x02, 0x0C, 0x02, 0x7E, 0x00}, // M
{0x00, 0x00, 0x7E, 0x02, 0x0C, 0x10, 0x7E, 0x00}, // N
{0x00, 0x00, 0x3C, 0x42, 0x42, 0x3C, 0x00, 0x00}, // O
{0x00, 0x00, 0x7E, 0x09, 0x09, 0x06, 0x00, 0x00}, // P
{0x00, 0x00, 0x3C, 0x42, 0x52, 0x3C, 0x00, 0x00}, // Q
{0x00, 0x00, 0x7E, 0x09, 0x19, 0x66, 0x00, 0x00}, // R
{0x00, 0x00, 0x26, 0x4A, 0x4A, 0x32, 0x00, 0x00}, // S
{0x00, 0x00, 0x01, 0x01, 0x7E, 0x01, 0x01, 0x00}, // T
{0x00, 0x00, 0x3F, 0x40, 0x40, 0x3F, 0x00, 0x00}, // U
{0x00, 0x00, 0x0F, 0x30, 0x40, 0x30, 0x0F, 0x00}, // V
{0x00, 0x00, 0x1F, 0x60, 0x1C, 0x60, 0x1F, 0x00}, // W
{0x00, 0x00, 0x77, 0x08, 0x08, 0x77, 0x00, 0x00}, // X
{0x00, 0x00, 0x07, 0x08, 0x70, 0x08, 0x07, 0x00}, // Y
{0x00, 0x00, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00}, // Z
};
// 延时函数
void delay(uint32_t count)
{
uint32_t i;
for (i = 0; i < count; i++);
}
// 发送一个字节到点阵
void send_byte(uint8_t byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
// 设置数据引脚
if (byte & 0x80)
GPIO_SetBits(GPIOB, LED_DIN);
else
GPIO_ResetBits(GPIOB, LED_DIN);
// 拉高时钟引脚
GPIO_SetBits(GPIOB, LED_CLK);
// 延时一段时间
delay(10);
// 拉低时钟引脚
GPIO_ResetBits(GPIOB, LED_CLK);
// 移位
byte <<= 1;
}
}
// 发送一帧数据到点阵
void send_frame(uint8_t frame[8])
{
uint8_t i;
for (i = 0; i < 8; i++)
{
// 选择列
switch (i)
{
case 0: GPIO_ResetBits(GPIOA, LED_COL_H); break;
case 1: GPIO_ResetBits(GPIOA, LED_COL_G); break;
case 2: GPIO_ResetBits(GPIOA, LED_COL_F); break;
case 3: GPIO_ResetBits(GPIOA, LED_COL_E); break;
case 4: GPIO_ResetBits(GPIOA, LED_COL_D); break;
case 5: GPIO_ResetBits(GPIOA, LED_COL_C); break;
case 6: GPIO_ResetBits(GPIOA, LED_COL_B); break;
case 7: GPIO_ResetBits(GPIOA, LED_COL_A); break;
}
// 发送一行数据
send_byte(frame[i]);
// 拉高时钟引脚
GPIO_SetBits(GPIOB, LED_CLK);
// 延时一段时间
delay(10);
// 拉低时钟引脚
GPIO_ResetBits(GPIOB, LED_CLK);
// 拉高所有列
GPIO_SetBits(GPIOA, LED_COL_A | LED_COL_B | LED_COL_C | LED_COL_D | LED_COL_E | LED_COL_F | LED_COL_G | LED_COL_H);
}
}
int main(void)
{
// 初始化GPIO
GPIO_InitTypeDef GPIO_InitStructure;
// 使能GPIOA和GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置LED列引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = LED_COL_A | LED_COL_B | LED_COL_C | LED_COL_D | LED_COL_E | LED_COL_F | LED_COL_G | LED_COL_H;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置LED行引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = LED_ROW_1 | LED_ROW_2 | LED_ROW_3 | LED_ROW_4 | LED_ROW_5 | LED_ROW_6 | LED_ROW_7 | LED_ROW_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置LED控制引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = LED_OE | LED_CLK | LED_DIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 初始化所有引脚状态
GPIO_SetBits(GPIOA, LED_COL_A | LED_COL_B | LED_COL_C | LED_COL_D | LED_COL_E | LED_COL_F | LED_COL_G | LED_COL_H);
GPIO_SetBits(GPIOB, LED_ROW_1 | LED_ROW_2 | LED_ROW_3 | LED_ROW_4 | LED_ROW_5 | LED_ROW_6 | LED_ROW_7 | LED_ROW_8 | LED_OE | LED_CLK | LED_DIN);
// 循环显示字母
uint8_t i;
while (1)
{
for (i = 1; i <= 26; i++)
{
send_frame(FONT[i]);
delay(100000);
}
}
}
```
以上代码的功能是不断显示字母表中的字母。首先,我们定义了点阵的引脚,以及一个字模数组。然后,我们编写了三个函数:
- `delay`:简单的延时函数,用于延时一段时间。
- `send_byte`:将一个字节发送到点阵上。
- `send_frame`:将一帧数据发送到点阵上。
在 `main` 函数中,我们先初始化了所有引脚的状态,然后使用 `send_frame` 函数循环显示字母表中的字母。在每一帧数据中,我们选择一列,然后将当前行的数据发送到点阵上。
需要注意的是,由于点阵是共阴极的,因此我们需要将行引脚拉低才能点亮对应的LED。此外,由于点阵输出的是负逻辑信号,因此我们需要将字节的每一位反转一下再发送到点阵上。
最后,我们需要将代码烧录到STM32F106微控制器中,并在Proteus中运行仿真。可以看到,点阵会不断显示字母表中的字母,如下图所示:
![image.png](attachment:image.png)
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)