初级会员

- 积分
- 116
- 金钱
- 116
- 注册时间
- 2019-10-16
- 在线时间
- 18 小时
|
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 KEY0_PRES 1 //KEY0按下
- #define KEY1_PRES 2 //KEY1按下
- void KEY_Init(void);//IO初始化
- #endif
复制代码
key.c
- #include "key.h"
- #include "delay.h"
- void KEY_Init(void) //IO初始化
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
- GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE4,3
- //初始化 WK_UP-->GPIOA.0 下拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0
- }
复制代码
pwm.h
- #ifndef __PWM_H
- #define __PWM_H
- #include "sys.h"
- void TIM3_PWM_Init1(u16 arr,u16 psc);
- void TIM3_PWM_Init2(u16 arr,u16 psc);
- #endif
复制代码
pwm.c
- #include "pwm.h"
- void TIM3_PWM_Init1(u16 arr,u16 psc)//PA.7
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstruct;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitstruct.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInitstruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInitstruct.TIM_Period=arr;
- TIM_TimeBaseInitstruct.TIM_Prescaler=psc;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitstruct);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
- TIM_OC2Init(TIM3, &TIM_OCInitStructure); //初始化 TIM3 OC1
-
- TIM_Cmd(TIM3, ENABLE);
-
- TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
- }
- void TIM3_PWM_Init2(u16 arr,u16 psc)//PA.6
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstruct;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
-
-
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
- TIM_TimeBaseInitstruct.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInitstruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInitstruct.TIM_Period=arr;
- TIM_TimeBaseInitstruct.TIM_Prescaler=psc;
- TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitstruct);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //初始化 TIM3 OC1
-
- TIM_Cmd(TIM3, ENABLE);
-
- TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
- }
复制代码
timer.h
- #ifndef __TIMER_H
- #define __TIMER_H
- #include "sys.h"
- void TIM2_Init(u16 arr,u16 psc);
- #endif
复制代码
timer.c
- #include "timer.h"
- #include "led.h"
- void TIM2_Init(u16 arr,u16 psc)
- {
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstruct;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
-
- TIM_TimeBaseInitstruct.TIM_ClockDivision=TIM_CKD_DIV1;
- TIM_TimeBaseInitstruct.TIM_CounterMode=TIM_CounterMode_Up;
- TIM_TimeBaseInitstruct.TIM_Period=arr;
- TIM_TimeBaseInitstruct.TIM_Prescaler=psc;
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitstruct);
-
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //使能按键TIME2所在的中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能TIM3中断通道
- NVIC_Init(&NVIC_InitStructure);
- TIM_Cmd(TIM2, ENABLE);
-
- }
- void TIM2_IRQHandler()
- {
- extern u16 count;
-
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- {
- LED1=!LED1;//忘了这里影响了判断
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
- count++;
- }
-
- }
复制代码
main.c
- #include "stm32f10x.h"
- #include "delay.h"
- #include "pwm.h"
- #include "timer.h"
- u16 count=0;
- u16 mark1=0;
- u16 mark2=0;
- u16 flag1=0;
- u16 flag2=0;
- int main()
- {
- delay_init();
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- TIM3_PWM_Init1(199,7199);
- TIM3_PWM_Init2(199,7199);
-
- TIM2_Init(9,7199);
-
- while(1)
- {
-
- if(KEY0==0)
- {
- if(flag1==0)
- {
- mark1=count;
- flag1=1;
- }
- if(count-mark1>=0)
- {
- TIM_SetCompare1(TIM3,185);
- }
- }
- else
- {
- TIM_SetCompare1(TIM3,185);
- flag1=0;
- }
-
- if(KEY1==0)
- {
- if(flag2==0)
- {
- mark2=count;
- flag2=1;
- }
- if(count-mark1>=0)
- {
- TIM_SetCompare1(TIM3,185);
- }
- }
- else
- {
- TIM_SetCompare1(TIM3,185);
- flag2=0;
- }
- }
-
-
复制代码
|
|