LED
led.c
#include "led.h" //初始化PF9和PF10为输出口.并使能这两个口的时钟 //LED IO初始化 void LED_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟 //GPIOF9,F10初始化设置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;//LED0和LED1对应IO口 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化GPIO GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭 }
led.h
#ifndef __LED_H #define __LED_H #include "sys.h" //LED端口定义 #define LED0 PFout(9) // DS0 #define LED1 PFout(10) // DS1 void LED_Init(void);//初始化 #endif
main.c
#include "sys.h" #include "delay.h" #include "usart.h" #include "led.h" int main(void) { delay_init(168); //初始化延时函数 LED_Init(); //初始化LED端口 /**下面是通过直接操作库函数的方式实现IO控制**/ while(1) { GPIO_ResetBits(GPIOF,GPIO_Pin_9); //LED0对应引脚GPIOF.9拉低,亮 等同LED0=0; GPIO_SetBits(GPIOF,GPIO_Pin_10); //LED1对应引脚GPIOF.10拉高,灭 等同LED1=1; delay_ms(500); //延时300ms GPIO_SetBits(GPIOF,GPIO_Pin_9); //LED0对应引脚GPIOF.0拉高,灭 等同LED0=1; GPIO_ResetBits(GPIOF,GPIO_Pin_10); //LED1对应引脚GPIOF.10拉低,亮 等同LED1=0; delay_ms(500); //延时300ms } } /** *******************下面注释掉的代码是通过 位带 操作实现IO口控制************************************** int main(void) { delay_init(168); //初始化延时函数 LED_Init(); //初始化LED端口 while(1) { LED0=0; //LED0亮 LED1=1; //LED1灭 delay_ms(500); LED0=1; //LED0灭 LED1=0; //LED1亮 delay_ms(500); } } ************************************************************************************************** **/ /** *******************下面注释掉的代码是通过 直接操作寄存器 方式实现IO口控制************************************** int main(void) { delay_init(168); //初始化延时函数 LED_Init(); //初始化LED端口 while(1) { GPIOF->BSRRH=GPIO_Pin_9;//LED0亮 GPIOF->BSRRL=GPIO_Pin_10;//LED1灭 delay_ms(500); GPIOF->BSRRL=GPIO_Pin_9;//LED0灭 GPIOF->BSRRH=GPIO_Pin_10;//LED1亮 delay_ms(500); } } ************************************************************************************************** **/