stm32F103R6课设
设计内容
使用STM32F103微处理器和八个LED灯、一个按键、异步串口、12864LCD,直流电机等模块和元器件在Proteus软件中搭建一个控制系统,实现以下功能:
1、一键长按系统启动/关闭/启动…循环,系统关闭时所有模块停止工作;
2、系统启动后电机以50%占空比转动,短按占空比以50%-100%-0%-50%
…循环;
3、上电后LCD显示欢迎提示,系统启动后每隔1s发送占空比数据和电机正转反转状态至LCD显示;
4、一个LED做系统状态指示灯,系统启动后一直点亮,系统关闭后熄灭;系统开始工作后其余七个LED灯显示流水灯效果,速度与占空比成比例关系;系统关闭时七个LED熄灭
#include "stm32f10x.h"
#include "lcd12864.h"
#include <stdbool.h>
int power_flag = 0;
volatile uint32_t systemTime = 0;
int sysInitFlag =1;
int sysInitFinishFlag = 1;
int choice=0;
int PWM_flag = 0;
int Sleep_flag = 0;
int Enter_Sleep = 0;
volatile uint32_t Task_counter=0;
volatile uint32_t LED_flag=0;
volatile uint32_t LED_section= 0x0004;
#define IN1_PIN GPIO_Pin_10
#define IN2_PIN GPIO_Pin_11
#define ENA_PIN GPIO_Pin_9
void waterfall_light();
void Display_State();
void Wake_Sleep();
void Config_ClockCmd(){
// Enable the GPIOA peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // 使能Timer3时钟
// Enable the wakeup pin (WKUP) and configure it for rising edge detection
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_WakeUpPinCmd(ENABLE);
}
void Config_LED(){
GPIO_InitTypeDef LED_InitStructure;
/* GPIOD Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure PD0 and PD2 in output pushpull mode */
LED_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 |
GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&LED_InitStructure);
}
void Config_DCinit(){
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IN1_PIN | IN2_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // ??????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void TIM2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// ??TIM3??
// ??TIM3????
TIM_TimeBaseStructure.TIM_Period = 999; // ??????1000?????
TIM_TimeBaseStructure.TIM_Prescaler = 79; // ?????80,???????1MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// ??TIM3??1??PWM??
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500; // ??????0%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/