用C语言写一个STM32F407数码管驱动
时间: 2023-02-25 19:25:43 浏览: 186
STM32实例程序 2.数码管
5星 · 资源好评率100%
下面是一个STM32F407数码管驱动的简单C语言代码:
```
#include "stm32f4xx.h"
//定义数码管的端口和引脚
#define SEG_PORT GPIOA
#define SEG_PIN GPIO_Pin_0
void delay(uint32_t time)
{
while (time--);
}
void Display(uint8_t num)
{
//清空数码管端口
SEG_PORT->ODR = 0x00;
//根据数字选择对应的灯亮起
switch (num)
{
case 0:
SEG_PORT->ODR = 0x3F;
break;
case 1:
SEG_PORT->ODR = 0x06;
break;
case 2:
SEG_PORT->ODR = 0x5B;
break;
case 3:
SEG_PORT->ODR = 0x4F;
break;
case 4:
SEG_PORT->ODR = 0x66;
break;
case 5:
SEG_PORT->ODR = 0x6D;
break;
case 6:
SEG_PORT->ODR = 0x7D;
break;
case 7:
SEG_PORT->ODR = 0x07;
break;
case 8:
SEG_PORT->ODR = 0x7F;
break;
case 9:
SEG_PORT->ODR = 0x6F;
break;
default:
break;
}
delay(0xfffff);
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//初始化GPIOA的0号引脚
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = SEG_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SEG_PORT, &GPIO_InitStructure);
while (1)
{
//循环显示数字0-9
for (uint8_t i = 0; i < 10; i++)
{
Display(i);
}
}
}
```
请注意,这仅仅是一个
阅读全文