LED控制端口初始化
//初始化PA8和PD2为输出口.并使能这两个口的时钟
//LED IO初始化
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能APB2外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_8); //PA8输出高
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);//使能APB2外设时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD, GPIO_Pin_2); //PD2输出高
}
主函数
#include "stm32f10x.h"
int main(void)
{
SystemInit(); //系统时钟初始化为72M SYSCLK_FREQ_72MHz
delay_init(72);
LED_Init();
while(1)
{
LED0=!LED0;
LED1=!LED1; //通过位带操作实现
delay_ms(50);
}
}
对于库函数中各函数的使用可以使用《STM32库函数代码自动生成器》生成。