1、点亮LED
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量。
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE); //使能GPIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_PiN_12 | GPIO_Pin_13;
GPIO_InitStructure_GPIO_Mode = GPIO_Mode_OUT; //通用输出
GPIO_InitStructure_GPIO_OType = GPIO_Type_PP; //推挽输出
GPIO_InitStructure_GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_IniteStructure_GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOH, &GPIO_InitStructure);
while(1)
{
GPIO_SetBits(GPIOH, GPIO_Pin12 | GPIO_Pin_13);//点亮LED
delay_ms(1000);
GPIO_ResetBits (GPIOH, GPIO_Pin12 | GPIO_Pin_16); //熄灭LED
delay_ms(1000);
}
}
2、蜂鸣器
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体变量
RCC_GetClocksFreq(&RCC_Clocks); //获取芯片时钟向时钟
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); //滴答定时器初始化
Delay_us_Init(24); //微秒级延时初始化
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE); //使能GPIOE时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure_GPIO_Mode = GPIO_Mode_OUT; //通用输出
GPIO_InitStructure_GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure_GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure_GPIO_Speed = GPIO_Speed_100MHz; //输出速率为100MHz
GPIO_Init(GPIOH, &GPIO_InitStructure); //调用初始化函数
while(1)
{
GPIO_SetBits(GPIOH, GPIO_Pin_5); //使能PH5引脚为高电平,打开蜂鸣器
delay_ms(100); //延时
GPIO_ResetBits(GPIOH, GPIO_Pin_5); //使能PH5引脚为低电平,关闭蜂鸣器
delay_ms(100); //延时
}
}
3、按键中断
void Exti_Configure()
{
GPIO_InitTypeDef GPIO_InitStructrue;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE); //开启GPIO组时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); //系统配置时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //选择端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; //输入
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化配置
//GPIO与中断关联
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource4);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource5);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource6);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource7);
EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line5 | EXTI_Line6 | EXTI_Line7; // 选择中断线
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断触发
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //下降沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //使能中断线
EXTI_Init(&EXTI_TypeDefStructure); //初始化中断线配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; // 现在中断向量
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 8; // 响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断向量
NVIC_Init(&NVIC_TypeDefStructure); //初始化中断向量配置
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_8_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_IntiStructure.NVIC_IRQChannelSubPriority = 7;
NVIC_Init(&NVIC_TypeDefStructure);
}
void EXTI4_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line))
{
if (GPIO_ReadInputDataBit(GPIOI, GPIO_Pin_4) == 0)
{
LED1 = !LED1;
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line5))
{
LED2 = !LED2;
EXTI_ClearITPendingBit(EXTI_Line5);
}
if (EXTI_GetITStatus(EXTI_Line6))
{
LED3 = !LED3;
EXTI_ClearITPendingBit(EXTI_Line6);
}
if (EXTI_GetITStatus(EXTI_Line7))
{
LED4 = !LED4;
EXTI_ClearITPendingBit(EXTI_Line7);
}
}
4、串口中断点亮灯
void USART1_Config(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO结构体定义
USART_InitTypeDef USART_InitStructure; // 串口结构体定义
NVIC_InitTypeDef NVIC_InitStructure; //中断结构体定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //使能USART1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIO时钟
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //引脚映射P9
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); //引脚映射P10
// GPIOA9端口初始化结构参数设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIOA10端口初始化结构参数设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 串口初始化结构体参数设置
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_EXNE, ENABLE);
// 中断初始化结构体参数设置
NVIC_InitStructure.NVIC_IRQChannel = USART1_ITQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
if (USART1_Rx_Flag == RESET)
{
USART1_RxBuf[USART1_LengthCut] = USART_ReceiveData(USART1);
USART1_LengthCut++;
if (USART1_LengthCut >= USART1_RXSIZE)
{
USART1_LengthCut = 0;
USART_Rx_Flag = SetBitMask;
}
}
USART_ClearTPendingBit(USART1, USART_IT_RXNE);
}
}
void USART1_SendByte(uint8_t src)
{
USART_SendData(USART1, src);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void USART1_SendString(char* src)
{
unit16_t Tx_cut = 0;
do
{
USART1_SendByte(*(src + Tx_cut));
Tx_cut++;
}
while(*(src + Tx_cut) != '\0');
}
int main(void)
{
delay_Init();
LED_Configure();
USART1_Config(115200);
while(1)
{
if (KEY1 == 0)
{
Delay_ms(20);
while(KEY1 == 0)
{
LED1 = !LED1;
strlcpy(USART1_TxBuf, "LED OK\n", 11);
USART1_SendString(USART1_TxBuf);
}
}
if (KEY2 == 0)
{
Delay_ms(20);
while(KEY2 == 0)
{
LED2= !LED2;
strlcpy(USART1_TxBuf, "LED OK\n", 11);
USART1_SendString(USART1_TxBuf);
}
}
}
}
5、定时器
void TIM_Configure(unsigned int arr, unsigned short psc) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
void TIM4_IRQHandler(void) {
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET) {
LED2 = !LED2;
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
}
}
int main(void)
{
led_configure();
TIM_Configure(10000-1, 840-1);
while(1){}
}