STM32流水灯的几种实现方法

#include “stm32f10x.h”
void RCC_Configuration(void);//2
void GPIO_Configuration(void);//GPIO
void Delay(u32 count)
{
u32 i=0;
for(;i<count;i++);
}
int main(void)
{ RCC_Configuration();//3
LED_Init();

while(1)

{
GPIO_SetBits(GPIOA,GPIO_Pin_0);//第一灯亮

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_0);	//第一灯灭

	Delay(800000); //延时

	GPIO_SetBits(GPIOA,GPIO_Pin_1); //第二灯亮

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_1);	 //第二灯灭

	Delay(800000); //延时

	GPIO_SetBits(GPIOA,GPIO_Pin_2); //第三灯亮

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_2);	 //第三灯灭
	
	Delay(800000); //延时

	GPIO_SetBits(GPIOA,GPIO_Pin_3); //第四灯亮

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_3);	 //第四灯灭

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_4);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_4);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_5);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_5);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_6);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_6);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_7);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_7);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_8);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_8);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_9);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_9);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_10);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_10);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_11);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_11);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_12);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_12);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_13);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_13);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_14);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_14);	//

	Delay(800000); //延时
	
	GPIO_SetBits(GPIOA,GPIO_Pin_15);//

	Delay(800000); //延时

	GPIO_ResetBits(GPIOA,GPIO_Pin_15);	//

  Delay(800000); //延时

}
}
void LED_Init(void)//GPIO
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_InitTypeDef   GPIO_InitStructure;         //结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; 

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

}
void RCC_Configuration(void) //1
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
	
	RCC_HCLKConfig(RCC_SYSCLK_Div1); 
 	RCC_PCLK2Config(RCC_HCLK_Div1); 
 	RCC_PCLK1Config(RCC_HCLK_Div2);

	FLASH_SetLatency(FLASH_Latency_2);
	FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

	RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

	RCC_PLLCmd(ENABLE);

	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	
	while(RCC_GetSYSCLKSource() != 0x08);
}

}

流水灯还有几中实现方法如
用函数GPIO_Write 位置1 再左移 实现流水灯

猜你喜欢

转载自blog.csdn.net/qq_42339969/article/details/88817817