利用已经建立的工程模板,在Keil for ARM V5中编写代码,使用直流电机电路,实现如下功能:配置定时器1输出PWM,通过查询方式获取按键是否按下,KEY1按下增加占空比;KEY2按下减少占空比。
main.c
#include "stm32f10x.h"
#include "st.h"
#include "delay.h"
#include "time.h"
#include "motor.h"
int p = 100;
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
motor_Init();
TIM1_Init(500 - 1 , 7200 - 1);
ST_Init();
MOTOR1 = 1;
MOTOR2 = 0;
while(1)
{
}
}
st.c
#include "st.h"
extern int p;
void ST_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC , GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC , GPIO_PinSource1);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1; //ÒªÅäÖõÄÖжÏÏß
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler() //ÍâÖжÏ
{
p = p + 10;
TIM_SetCompare1(TIM1, p);
EXTI_ClearITPendingBit(EXTI_Line0);
}
void EXTI1_IRQHandler() //ÍâÖжÏ
{
p = p - 10;
TIM_SetCompare1(TIM1, p);
EXTI_ClearITPendingBit(EXTI_Line1);
}
st.h
#ifndef __ST_H
#define __ST_H
#include "sys.h"
#define MOTOR1 PBout(13)
#define MOTOR2 PBout(14)
void ST_Init(void);
#endif
time.c
#include "time.h"
#include "stm32f10x.h"
void TIM1_Init(u16 arr , u16 psc)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
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(TIM1, & TIM_TimeBaseStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = 250;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1 , TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
time.h
#ifndef __TIME_H
#define __TIME_H
#include "sys.h"
void TIM1_Init(u16 arr , u16 psc);
#endif
motor.h
#ifndef __MOTOR_H
#define __MOTOR_H
#include "sys.h"
#define MOTOR1 PBout(13)
#define MOTOR2 PBout(14)
void motor_Init(void);
#endif
motor.c
#include "motor.h"
void motor_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}