使用Keil5和STM32芯片,使用C语言实现0-9数码管显示的代码以及仿真
时间: 2024-02-25 22:55:03 浏览: 47
好的,以下是使用Keil5和STM32芯片,使用C语言实现0-9数码管显示的代码以及仿真。
代码如下:
```c
#include "stm32f10x.h"
void delay(uint32_t time);
int main(void)
{
uint8_t num[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; // 数码管0-9显示的编码
uint8_t i = 0;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能GPIOB时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; // 数码管对应的引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化GPIOB
while(1)
{
for(i = 0; i < 10; i++) // 循环显示0-9
{
GPIO_Write(GPIOB, num[i] << 1); // 显示数字
delay(1000); // 延时1s
}
}
}
void delay(uint32_t time)
{
while(time--);
}
```
仿真步骤如下:
1. 打开Keil5软件,创建一个新的工程。
2. 选择对应的芯片型号和工作频率,点击“OK”按钮。
3. 在“Project”菜单下选择“Options for Target”选项。
4. 在弹出的窗口中选择“Debug”选项卡。
5. 在“Settings”选项卡中选择“ST-Link”调试器,并勾选“Serial Wire Viewer”和“Serial Wire Output”。
6. 点击“OK”按钮保存设置。
7. 在工程文件夹中找到“startup_stm32f10x_md_vl.s”文件,双击打开。
8. 在文件中找到“_stack_top”符号,并在其下方插入以下代码:
```
.word 0x20001000
```
9. 保存文件并关闭。
10. 在工程中添加上面的代码文件,编译工程并下载到STM32芯片中。
11. 打开“Debug”菜单下的“Start/Stop Debug Session”选项。
12. 点击“Run”按钮启动仿真。
13. 可以观察到数码管0-9依次显示。
注意:以上代码和步骤仅供参考,具体实现可能会有所不同。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.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)