led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
/* the macro definition to trigger the led on or off
* 1 - off
- 0 - on
*/
#define ON 0
#define OFF 1
#define LED1(a) if (a) \
GPIO_SetBits(GPIOC,GPIO_Pin_13);\
else \
GPIO_ResetBits(GPIOC,GPIO_Pin_13)
#define LED2(a) if (a) \
GPIO_SetBits(GPIOB,GPIO_Pin_14);\
else \
GPIO_ResetBits(GPIOB,GPIO_Pin_14)
void LED_GPIO_Config(void);
#endif /* __LED_H */
led.c
#include "led.h"
/*************** ÅäÖÃLEDÓõ½µÄI/O¿Ú *******************/
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE); // ʹÄÜPC¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //Ñ¡Ôñ¶ÔÓ¦µÄÒý½Å
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); //³õʼ»¯PC¶Ë¿Ú
GPIO_SetBits(GPIOC, GPIO_Pin_13 ); // ¹Ø±ÕËùÓÐLED
}
main.c
#include "stm32f10x.h"
#include "led.h"
void Delay(__IO u32 nCount);
int main(void)
{
SystemInit(); // ÅäÖÃϵͳʱÖÓΪ72M
LED_GPIO_Config(); //LED ¶Ë¿Ú³õʼ»¯
while (1)
{
LED1( ON ); // ÁÁ
Delay(0x200000);
LED1( OFF ); // Ãð
Delay(0x200000);
LED2( ON );
Delay(0x200000);
LED2( OFF );
}
}
void Delay(__IO u32 nCount)
{
for(; nCount != 0; nCount--);
}
#STM32使用姿势 1、main.c里include stm32f10x.h 2、配置系统时钟/为72M
#GPIO使用流程 ##GPIO初始化 ###使能时钟 ###选择引脚 ###配置输入输出方式 ###配置IO口速度 ###调用GPIO口初始化函数
#将IO口置为高电平或低电平 GPIO_SetBits(GPIOC,GPIO_Pin_13); GPIO_ResetBits(GPIOC,GPIO_Pin_13);
#使用示例
#define LED1(a) if (a) \
GPIO_SetBits(GPIOC,GPIO_Pin_13);\
else \
GPIO_ResetBits(GPIOC,GPIO_Pin_13)
其中define里面换行需使用斜杆\