所需要实现的功能
通过按键捕获,长按来切换三种模式(为了写程序的方便,直接朴取正点原子给出的输入捕获例程)模式2:通过短按来切换led1的相位,实现视觉上的延迟模式3:led0闪烁作为计时器,通过短按切换led1的状态
模式1 默认状态为pwm占空比每次增大0.2,长按key0 pwm占空比每次减小0.2
u16 terminal=300;
u16 led0pwmval=0;
u16 key0pwmval=300;
u8 dir=1;
static int M=4;
static int N=1;
u16 key;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
LED_Init();
KEY_Init();
if(key==0)
{
if(led0pwmval<300)
{
delay_ms(10);
if(dir)
led0pwmval++;
else
led0pwmval--;
if(led0pwmval>terminal*N*0.2)
{
dir=0;
N++;
}
if(led0pwmval==0)
dir=1;
TIM_SetCompare1(TIM14,led0pwmval);
}
else
TIM_SetCompare1(TIM14,0);
}
```c
else
{
if(key0pwmval>0)
{
delay_ms(10);
if(dir)
key0pwmval--;
else
key0pwmval++;
if(key0pwmval<terminal*M*0.2)
{
dir=0;
M--;
}
if(key0pwmval>=300)
dir=1;
TIM_SetCompare1(TIM14,key0pwmval);
}
else
{
TIM_SetCompare1(TIM14,terminal);
}
}
模式2 :通过短按来切换led1的相位,实现视觉上的延迟
long long temp=0;
extern u8 K2;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
LED_Init();
KEY_Init();
TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1);
if(temp>=100&&temp<=500)
{
temp=0;
if(++K2==4)
K2=0;
}
static u8 i=0;
static u8 n=0;
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
if(MODE==2)
{
if(i==1)
LED0=0;
if(i==2)
LED0=1;
if(n==K2)
LED1=0;
if(n==K2+1)
LED1=1;
if(++i>4) i=0;
if(++n>4) n=0;
}
}
模式3:led0闪烁作为计时器,通过短按切换led1的状态
u8 K3;
if(temp>=100&&temp<=500)
{
temp=0;
K3++;
if(K3>3)
K3=1;
}
else
{
temp=0;
}
static u8 j=0;
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
if(MODE==3)
{
++j;
LED0=!LED0;
if(K3==1)
{
if(j==2)
{
LED1=!LED1;
j=0;
}
}
else if(K3==2)
{
if(j==3)
{
LED1=!LED1;
j=0;
}
}
else if(K3==3)
{
if(j==1)
{
LED1=!LED1;
j=0;
}
}
if(j>3)
j=0;
}
值得注意的是:
正点原子给出的代码中,按键扫描函数中对wk_up进行了初始化,而我们在输入捕获的函数中需要用到wk_up判断长按短按,所以在main函数中同时初始化key_init和tim5的话,按键将会冲突;另一方面led0在pwm模式和后面两个模式中都有用到,不能同时进行初始化,所以我们在进入该模式时对io口和定时器进行初始化
u8 IO_Switch=0;
if(IO_Switch==0)
{
IO_Switch=1;
TIM14_PWM_Init1(500-1,84-1);
TIM_Cmd(TIM3,DISABLE);
}
if(IO_Switch==1)
{
IO_Switch=2;
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM14,DISABLE);
LED_Init();
TIM3_Int_Init(10000-1,8400-1);
}
if(IO_Switch==2)
{
IO_Switch=0;
TIM_SetCounter(TIM3,0);
TIM3_Int_Init(5000-1,8400-1);
}
相关代码有待完善 还请大佬指正
下面贴上源码 main
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "timer.h"
#include "key.h"
#include "pwm.h"
#include "stm32f4xx.h"
#include "usart.h"
extern u8 TIM5CH1_CAPTURE_STA;
extern u32 TIM5CH1_CAPTURE_VAL;
u8 K2;
u8 K3;
u8 MODE;
int main(void)
{
u16 terminal=300;
u16 key0pwmval=300;
u16 led0pwmval=0;
u8 dir=1;
u16 init=0;
static int M=4;
static int N=1;
u16 key;
long long temp=0;
u8 IO_Switch=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart_init(115200);
LED_Init();
KEY_Init();
K2=1;
K3=1;
MODE=1;
TIM5_CH1_Cap_Init(0XFFFFFFFF,84-1);
while(1)
{
if(TIM5CH1_CAPTURE_STA&0X80)
{
temp=TIM5CH1_CAPTURE_STA&0X3F;
temp*=0XFFFFFFFF;
temp+=TIM5CH1_CAPTURE_VAL;
temp=temp/1000;
TIM5CH1_CAPTURE_STA=0;
}
if(temp>500)
{
temp=0;
MODE++;
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM14,DISABLE);
}
if(MODE>3)
{
MODE=1;
TIM_Cmd(TIM14,ENABLE);
TIM_Cmd(TIM3,DISABLE);
}
switch(MODE)
{
case 1:
if(IO_Switch==0)
{
IO_Switch=1;
TIM14_PWM_Init1(500-1,84-1);
TIM_Cmd(TIM3,DISABLE);
}
key=KEY_Scan(1);
if(key==0)
{
if(led0pwmval<300)
{
delay_ms(10);
if(dir)
led0pwmval++;
else
led0pwmval--;
if(led0pwmval>terminal*N*0.2)
{
dir=0;
N++;
}
if(led0pwmval==0)
dir=1;
TIM_SetCompare1(TIM14,led0pwmval);
}
else
TIM_SetCompare1(TIM14,0);
}
else
{
if(key0pwmval>0)
{
delay_ms(10);
if(dir) key0pwmval--;
else key0pwmval++;
if(key0pwmval<terminal*M*0.2)
{
dir=0;
M--;
}
if(key0pwmval>=300)
dir=1;
TIM_SetCompare1(TIM14,key0pwmval);
}
else
{
TIM_SetCompare1(TIM14,terminal);
}
}
break;
case 2:
if(IO_Switch==1)
{
IO_Switch=2;
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM14,DISABLE);
LED_Init();
TIM3_Int_Init(10000-1,8400-1);
}
if(temp>=100&&temp<=500)
{
temp=0;
if(++K2==4)
K2=0;
}
break;
case 3:
if(IO_Switch==2)
{
IO_Switch=0;
TIM_SetCounter(TIM3,0);
TIM3_Int_Init(5000-1,8400-1);
}
if(temp>=100&&temp<=500)
{
temp=0;
K3++;
if(K3>3)
K3=1;
}
else
{
temp=0;
}
break;
}
}
}
timer。c
#include "timer.h"
#include "led.h"
#include "delay.h"
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void)
{
static u8 i=0;
static u8 n=0;
static u8 j=0;
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
{
if(MODE==2)
{
if(i==1)
LED0=0;
if(i==2)
LED0=1;
if(n==K2)
LED1=0;
if(n==K2+1)
LED1=1;
if(++i>4) i=0;
if(++n>4) n=0;
}
else if(MODE==3)
{
++j;
LED0=!LED0;
if(K3==1)
{
if(j==2)
{
LED1=!LED1;
j=0;
}
}
else if(K3==2)
{
if(j==3)
{
LED1=!LED1;
j=0;
}
}
else if(K3==3)
{
if(j==1)
{
LED1=!LED1;
j=0;
}
}
if(j>3)
j=0;
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
TIM_ICInitTypeDef TIM5_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 TIM5CH1_CAPTURE_STA=0;
u32 TIM5CH1_CAPTURE_VAL;
void TIM5_IRQHandler(void)
{
if((TIM5CH1_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X40)
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)
{
TIM5CH1_CAPTURE_STA|=0X80;
TIM5CH1_CAPTURE_VAL=0XFFFFFFFF;
}
else TIM5CH1_CAPTURE_STA++;
}
}
if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X40)
{
TIM5CH1_CAPTURE_STA|=0X80;
TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
}
else
{
TIM5CH1_CAPTURE_STA=0;
TIM5CH1_CAPTURE_VAL=0;
TIM5CH1_CAPTURE_STA|=0X40;
TIM_Cmd(TIM5,DISABLE );
TIM_SetCounter(TIM5,0);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);
TIM_Cmd(TIM5,ENABLE );
}
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
}
pwm.c
#include "pwm.h"
#include "led.h"
#include "usart.h"
void TIM14_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
void TIM14_PWM_Init1(u32 arr,u32 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
TIM14_GPIO_Init();
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM14,ENABLE);
TIM_Cmd(TIM14, ENABLE);
}
key.c
#include "key.h"
#include "delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up=1;
if(mode)key_up=1;
if(key_up&&(KEY0==0||KEY1==0||KEY2==0))
{
delay_ms(10);
key_up=0;
if(KEY0==0)return 1;
else if(KEY1==0)return 2;
else if(KEY2==0)return 3;
}
else if(KEY0==1&&KEY1==1&&KEY2==1)
key_up=1;
return 0;
}
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
timer.h
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM5_CH1_Cap_Init(u32 arr,u16 psc);
extern u8 K2;
extern u8 K3;
extern u8 MODE;
#endif
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define KEY2 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define KEY0_PRES 1
#define KEY1_PRES 2
#define KEY2_PRES 3
#define WKUP_PRES 4
void KEY_Init(void);
u8 KEY_Scan(u8);
#endif
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PFout(9)
#define LED1 PFout(10)
void LED_Init(void);
#endif
pwm.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM14_GPIO_Init(void);
void TIM14_PWM_Init1(u32 arr,u32 psc);
void TIM14_PWM_Init2(u32 arr,u32 psc);
#endif
timer.h
#ifndef _TIMER_H
#define _TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM5_CH1_Cap_Init(u32 arr,u16 psc);
extern u8 K2;
extern u8 K3;
extern u8 MODE;
#endif
|