[stm32f407]定时器使用

1.定时器定时串口打印

main.c

#include "stm32f4xx.h"                  // Device header
#include "serial.h"
#include "delay.h"
#include "tim.h"

extern uint16_t count;

int main(void)
{
    Serial_Init();

    TIM_Init();
  
    printf("123success");
  
  while (1)
  {    
    printf("count=%d\r\n",count);

    if((count%10)==1){     
      printf("interrupt\r\n");
    }
          
    if(count>1000){
        count=0;
    }    
  }
}

void TIM3_IRQHandler(){
    if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET){
        count++;
        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
    }       
}

TIM.c

#include "stm32f4xx.h"                  // Device header




#define PSC 1680-1


uint16_t count=0;

void TIM_Init(){
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
    
    TIM_TimeBaseInitTypeDef TimeBaseInitStructure;
    TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
    TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;  
    TimeBaseInitStructure.TIM_Period=1000-1;
    TimeBaseInitStructure.TIM_Prescaler=PSC;
    TIM_TimeBaseInit(TIM3,&TimeBaseInitStructure);
    
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//深入理解
    
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
    NVIC_Init(&NVIC_InitStructure);
    
    TIM_Cmd(TIM3,ENABLE);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值