#include "dianji.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);
}
//②主函數(具體操作控制)
#include "stm32f10x.h"
#include "usart.h"
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include "key.h"
#include "dianji.h"
#define up 1
#define down 0
int main(void)
{
vu8 key = 0;
vu8 key1 = 0;
vu8 num0 = 0;
vu8 num1 = 0;
vu8 num2 = 0;
vu8 speed = 1;
vu16 arrLowest = 189;
vu16 arrMax= 409;
vu16 arr =299;
vu16 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;
}
}
}
}
}
|