stm32f103c8t6的动态七段共阴数码
时间: 2024-01-22 18:16:25 浏览: 66
动态七段共阴数码管是一种常见的显示器件,常用于显示数字和一些简单的字符。对于stm32f103c8t6这款微控制器,我们可以通过控制IO口的高低电平来控制数码管的显示。
以下是一个简单的示例代码,演示如何使用stm32f103c8t6控制动态七段共阴数码管显示数字:
```c
#include "stm32f10x.h"
// 定义数码管的引脚
#define SEG_A GPIO_Pin_0
#define SEG_B GPIO_Pin_1
#define SEG_C GPIO_Pin_2
#define SEG_D GPIO_Pin_3
#define SEG_E GPIO_Pin_4
#define SEG_F GPIO_Pin_5
#define SEG_G GPIO_Pin_6
#define SEG_DP GPIO_Pin_7
// 定义数字0-9的编码
uint8_t digitCode[10] = {
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // 数字0
SEG_B | SEG_C, // 数字1
SEG_A | SEG_B | SEG_D | SEG_E | SEG_G, // 数字2
SEG_A | SEG_B | SEG_C | SEG_D | SEG_G, // 数字3
SEG_B | SEG_C | SEG_F | SEG_G, // 数字4
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // 数字5
SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, // 数字6
SEG_A | SEG_B | SEG_C, // 数字7
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, // 数字8
SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G // 数字9
};
int main(void)
{
// 初始化GPIO口
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G | SEG_DP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
while (1)
{
// 显示数字0-9
for (int i = 0; i < 10; i++)
{
GPIO_Write(GPIOA, digitCode[i]);
delay_ms(1000); // 延时1秒
}
}
}
// 延时函数,单位毫秒
void delay_ms(uint32_t ms)
{
ms *= 1000;
while (ms--)
{
__NOP();
}
}
```
这段代码使用了GPIO口控制数码管的引脚,通过循环显示数字0-9,每个数字显示1秒钟。你可以根据自己的需求修改代码,实现其他功能。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)