STM32 | Systick定时器(第四天源码解析)

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 10  GPIO_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 14  GPIO_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;    //引脚0  GPIO_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;    //引脚0  GPIO_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;      //中断线0  EXTI_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){
    //判断标志位是否1  if(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);      }      }  }  //清空中断线0  EXTI_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);
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qt历险记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值