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);

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 05:30:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 05:30:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 05:30:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 05:30:06       18 阅读

热门阅读

  1. 100 天机器学习指南

    2024-03-25 05:30:06       17 阅读
  2. 字符串的冒泡排序 C语言

    2024-03-25 05:30:06       18 阅读
  3. flutter搭建mac开发环境

    2024-03-25 05:30:06       19 阅读
  4. 为什么Rust语言不支持三元表达式?

    2024-03-25 05:30:06       17 阅读
  5. 设计模式,策略模式

    2024-03-25 05:30:06       20 阅读
  6. ElasticSearch插件安装及配置

    2024-03-25 05:30:06       16 阅读
  7. 设计模式--建造者模式(Builder Pattern)

    2024-03-25 05:30:06       20 阅读