stm32库函数写一个流水灯的 程序
以下是一个使用stm32库函数编写的流水灯程序:
#include "stm32f10x.h"
void Delay(__IO uint32_t nCount);
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
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_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while (1)
{
// 点亮第一个LED
GPIO_SetBits(GPIOB, GPIO_Pin_0);
Delay(0xFFFFF);
// 依次点亮其他LED
GPIO_SetBits(GPIOB, GPIO_Pin_1);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_5);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_6);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_8);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_10);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_11);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_13);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_14);
Delay(0xFFFFF);
GPIO_SetBits(GPIOB, GPIO_Pin_15);
Delay(0xFFFFF);
// 依次熄灭LED
GPIO_ResetBits(GPIOB, GPIO_Pin_15);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_13);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_11);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_10);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_6);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
Delay(0xFFFFF);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
Delay(0xFFFFF);
}
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
此代码使用GPIOB的12个引脚来控制12个LED灯,代码中使用了GPIO_SetBits和GPIO_ResetBits函数来控制LED灯的点亮和熄灭,使用Delay函数来控制流水灯的速度。在主循环中,依次点亮每个LED灯,然后依次熄灭每个LED灯。
原文地址: http://www.cveoy.top/t/topic/bckv 著作权归作者所有。请勿转载和采集!