STM32——库函数版——数码管流动显示程序

数码管流动显示

我是用遍历两个数组让数码管流动显示数字
main.c

#include "sys.h"
#include "delay.h"
#include "num.h"

int main(void)
{ 
	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);	//系统时钟设置PLL作为系统时钟
	delay_init();			//初始化延时函数

	NUM_FLOW();  //数码管流动显示
}

num.c

#include "num.h"
#include "delay.h"

//动态数码管段码
//unsigned char seg[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};

//流动数码管
u8 smgduan[4]={0xF9,0xB0,0xF9,0x99};//段选
u8 smgwei[4]={0xfE,0xfd,0xfb,0xf7}; //位选

//数码管初始化
void NUM_Init(void){

	GPIO_InitTypeDef  GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);	 //使能GPIO端口时钟,数码管显示GPIO使能配置
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	//位选使能
	//段码设置
	GPIO_InitStructure.GPIO_Pin = NUM_STATIC_GPIO;    //数码管段选显示GPIO引脚配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;  //最高输出速率50MHz	
	GPIO_Init(GPIOD, &GPIO_InitStructure);  //IO口初始化
	//位选设置
	GPIO_InitStructure.GPIO_Pin = NUM_STATIC_BIT;    //数码管位选显示GPIO引脚配置
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //推挽输出
	GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;  //最高输出速率50MHz	
	GPIO_Init(GPIOC, &GPIO_InitStructure);  //IO口初始化
	GPIO_ResetBits(GPIOC, NUM_STATIC_BIT);   //给数码管位码置0
}
//数码管流动显示
void NUM_FLOW(void){
	uint16_t i;
	NUM_Init();
	while(1)
	{
		for(i=0;i<4;i++)
		{
			GPIO_Write(GPIOD,0xff);
			GPIO_Write(GPIOC,0xff);
			GPIO_Write(GPIOC,smgwei[i]);  //位选输出
			GPIO_Write(GPIOD,smgduan[i]);  //段码输出
			delay_ms(10);
		}
	}	
	
}

num.h

#include "sys.h"

//段码引脚
#define NUM_STATIC_GPIO  GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7  
#define NUM_STATIC_BIT GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
//数码管流动显示
void NUM_FLOW(void);



发布了13 篇原创文章 · 获赞 8 · 访问量 3230

猜你喜欢

转载自blog.csdn.net/qq_45844792/article/details/105162149