重点:GPIO端口配置
代码:
#include "stm32f10x.h"
#define Led1_On GPIO_ResetBits(GPIOD, GPIO_Pin_2);//低电平点灯
#define Led1_Off GPIO_SetBits(GPIOD, GPIO_Pin_2);
#define Led2_On GPIO_ResetBits(GPIOD, GPIO_Pin_3);
#define Led2_Off GPIO_SetBits(GPIOD, GPIO_Pin_3);
#define Led3_On GPIO_ResetBits(GPIOD, GPIO_Pin_4);
#define Led3_Off GPIO_SetBits(GPIOD, GPIO_Pin_4);
#define Led4_On GPIO_ResetBits(GPIOD, GPIO_Pin_7);
#define Led4_Off GPIO_SetBits(GPIOD, GPIO_Pin_7);
void LED_Init(void);
void Delay(__IO uint32_t nConut);
/******LED灯端口配置******/
void LED_Init()
{
GPIO_InitTypeDef GPIO_Structure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_Structure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;
GPIO_Structure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Structure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_Structure);
}
int main()
{
LED_Init();
while(1){
Led1_On;
Delay(800000);
Led1_Off;
Delay(800000);
Led2_On;
Delay(800000);
Led2_Off;
Delay(800000);
Led3_On;
Delay(800000);
Led3_Off;
Delay(800000);
Led4_On;
Delay(800000);
Led4_Off;
Delay(800000);
}
}
/******延时函数******/
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
笔记:
1. 开发板的GPIO管脚采用低电平点灯,管脚只要输出低电平即可点灯,处理器功耗低。
串接的电阻放在左边可以减弱人触摸到LED时导入到处理器的静电。