标题: STM32定时器中断实验 [打印本页]

作者: wwj103250    时间: 2018-3-26 00:00
标题: STM32定时器中断实验
mian函数

#include "stm32f10x.h"
#include "usart.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include "key.h"
#include "timer.h"
#define up 1
#define down 0

int main(void)
{
    u8  key = 0;
    u8  key1 = 0;
    u8  num0 = 0;
    u8  num1 = 0;
    u8  num2 = 0;
    u8  speed = 1;
    u16 arrLowest = 189;
    u16  arrMax= 409;
    u16 arr =299;
    u16 psc = 359;
    LED_Init();
    delay_init();
    KEY_Init();
    DJ_Init();
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    uart_init(115200);   //串口初始化为115200
    TIM3_Int_Init(arr,psc);//10Khz的计数频率,计数到100为10ms (arr+1)*(psc+1)/72  
    while(1)
    {   
        key = KEY_Scan(0);
        if(key == KEY0_PRES)
        {
            if(num0 == 0)
            {
                num0 = 1;
                LED1=0;
                GPIO_ResetBits(GPIOB,GPIO_Pin_6);
            }
            else
            {
                num0 = 0;
                LED1=1;
                GPIO_SetBits(GPIOB,GPIO_Pin_6);
            }
        }           
        if(key == KEY1_PRES)
        {
            if(num2 == 0)
            {
                GPIO_SetBits(GPIOG,GPIO_Pin_9);
                num2 = 1;
            }
            else
            {
                GPIO_ResetBits(GPIOG,GPIO_Pin_9);
                num2 = 0;
            }
        }
        while(key == WKUP_PRES)
        {                       
            if(speed == down)
            {               
                arr+=2;
                TIM3_Int_Init(arr,psc);                 
                delay_ms(50);
                key1 = KEY_Scan(0);
                if(arr > arrMax)
                {
                    speed = up;
                }
                if(key1 == WKUP_PRES)//关闭变速
                {
                    LED1 = 0;
                    key = 0;
                }
            }
            if(speed == up)
            {                  
                arr-=2;            
                TIM3_Int_Init(arr,psc);                 
                delay_ms(50);
                if(arr < arrLowest)
                {   
                    speed = down;
                }
                key1 = KEY_Scan(0);
                if(key1 == WKUP_PRES)
                {
                    LED1 = 0;
                    key = 0;
                }   
            }
        }      
    }
}



#include "timer.h"
#include "led.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
    //定时器TIM3初始化
    TIM_TimeBaseStructure.TIM_Period = arr;//自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler = psc;//预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
    TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //使能指定的TIM3中断,允许更新中断
    //中断优先级NVIC设置
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;//TIM3中断
    NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//响应优先级
    NVIC_Init(&NVIC_InitStructure);
    TIM_Cmd(TIM3,ENABLE);//使能TIMx
}
void TIM3_IRQHandler(void)//TIM3中断函数
{
    if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)//检查TIM3更新中断发生与否
    {
        TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除TIMx更新中断标志
        LED0=!LED0;
    }
}

void DJ_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);//使能GPIOG
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_4;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStruct);
    GPIO_SetBits(GPIOB,GPIO_Pin_6);
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOG,&GPIO_InitStruct);
    GPIO_SetBits(GPIOG,GPIO_Pin_9);
}





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1