修改之前的闪烁代码即可,我们假设有8个LED
#include“stm32f10x.h”
#include"Delay.h"
int main
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE)
GPIO_InitTypeDef GPIO_InitStructure //定义结构体变量
GPIO_InitStructure .GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
//p0~p7按位或操作 我们也可以这样写:=GPIO_Pin_All;把16个端口都配置为推挽输出模式
GPIO_InitStructure .GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure );//初始化外设
while(1)
{
GPIO_Write(GPIOA,~0x0001);//0000 0000 0000 0001 因为是低电平点亮我们按位取反~ 这样第一个LED点亮其他的都熄灭
Delay_ms(500);//调用自己添加的Delay函数
GPIO_Write(GPIOA,~0x0002);//0000 0000 0000 0010
Delay_ms(500);
GPIO_Write(GPIOA,~0x0004);//0000 0000 0000 0100
Delay_ms(500);
GPIO_Write(GPIOA,~0x0008);//0000 0000 0000 1000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0010);//0000 0000 0001 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0020);//0000 0000 0010 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0040);//0000 0000 0100 0000
Delay_ms(500);
GPIO_Write(GPIOA,~0x0080);//0000 0000 1000 0000
Delay_ms(500);
}
}
实验现象:依次点亮