| STM32 | Systick定时器(第四天) |
| STM32 | STM32F407ZE中断、按键、灯(续第三天) |
1、参考delay_us代码,完成delay_ms的程序
定时器频率换算单位:1GHZ=1000MHZ=1000 000KHZ = 1000 000 000HZ
定时器定时时间:计数个数/f(频率) 或者 (1/f(频率))*计数的个数
500/1MHZ = 500/1000 000 = 0.0005s = 0.5ms = 500us
在1MHZ下,1us计1个数;在100MHZ下,1us计100个数;
01 delay延时项目

# led.h
#ifndef __LED_H#define __LED_H#include "stm32f4xx.h"#define LED0_ON GPIO_ResetBits(GPIOF, GPIO_Pin_9)#define LED0_OFF GPIO_SetBits(GPIOF, GPIO_Pin_9)void Led_Init(void);#endif
# led.c
#include "led.h"/*********************************引脚说明:LED0 -- PF9LED1 -- PF10LED2 -- PE13LED3 -- PE14**********************************/void Led_Init(void){GPIO_InitTypeDef GPIO_InitStruct;//使能GPIOE组时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOF组时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //引脚9 10GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度GPIO_Init(GPIOF, &GPIO_InitStruct);GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //引脚13 14GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; //输出模式GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽输出GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度GPIO_Init(GPIOE, &GPIO_InitStruct);GPIO_SetBits(GPIOF, GPIO_Pin_9);GPIO_SetBits(GPIOF, GPIO_Pin_10);GPIO_SetBits(GPIOE, GPIO_Pin_13);GPIO_SetBits(GPIOE, GPIO_Pin_14);}
# key.h
#ifndef __KEY_H#define __KEY_H#include "stm32f4xx.h"void Key_Init(void);#endif
# key.c
#include "key.h"/*********************************引脚说明:KEY0--PA0**********************************/void Key_Init(void){GPIO_InitTypeDef GPIO_InitStruct;//使能GPIOA组时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //引脚0GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉GPIO_Init(GPIOA, &GPIO_InitStruct);}
# exti.h
#ifndef __EXTI_H#define __EXTI_H#include "stm32f4xx.h"void Exti_PA0_Init(void);#endif
# exti.c
#include "exti.h"/*********************************引脚说明:KEY0--PA0(选择下降沿触发)**********************************/void Exti_PA0_Init(void){GPIO_InitTypeDef GPIO_InitStruct;EXTI_InitTypeDef EXTI_InitStruct;NVIC_InitTypeDef NVIC_InitStruct;//使能SYSCFG及GPIOA时钟:RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能GPIOA组时钟RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//初始化IO口为输入。GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; //引脚0GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; //输入模式GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉GPIO_Init(GPIOA, &GPIO_InitStruct);//设置IO口与中断线的映射关系。SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);EXTI_InitStruct.EXTI_Line = EXTI_Line0; //中断线0EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式EXTI_InitStruct.EXTI_Trigger= EXTI_Trigger_Falling; //下降触发EXTI_InitStruct.EXTI_LineCmd= ENABLE; //中断使能//初始化线上中断,设置触发条件等。EXTI_Init(&EXTI_InitStruct);NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn; //中断通道,可在stm32f4xx.h文件当中查找NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //响应优先级NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //通道使能//配置中断分组(NVIC),并使能中断。NVIC_Init(&NVIC_InitStruct);}void delays(int n){int i,j;for(i=0; i<n; i++)for(j=0; j<30000; j++);}/***************************************************************1、中断服务函数是满足条件后,CPU自行执行的函数不需要主动调用2、中断服务函数是不能传递值与返回值3、STM32的中断服务函数名可在startup_stm32f40_41xxx.s中查找****************************************************************///编写中断服务函数void EXTI0_IRQHandler(void){//判断标志位是否1if(EXTI_GetITStatus(EXTI_Line0) == SET){if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET){//延时一段时间delays(15);if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET){GPIO_ToggleBits(GPIOF, GPIO_Pin_9);}}}//清空中断线0EXTI_ClearITPendingBit(EXTI_Line0);
# delay.h
#ifndef __DELAY_H#define __DELAY_H#include "stm32f4xx.h"void Delay_Init(void);void delay_us(u32 nus);void delay_ms(u32 nms);