#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); //第2组:最高2位用于指定抢占式优先级,最低2位用于指定响应优先级
uart_init(115200); //串口初始化为115200
TIM3_Int_Init(arr,psc); //10Khz的计数频率,计数到100为10ms (arr+1)*(psc+1)/72
while(1)
{
key = KEY_Scan(0);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
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(GPIOB,GPIO_Pin_8);
num2 = 1;
}
else
{
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
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 "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); //时钟使能,APB2可以工作在72MHz下,而APB1最大是36MHz。
//定时器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_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_7;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>