|
|
|
/*
|
|
|
|
* @Author: your name
|
|
|
|
* @Date: 2022-04-17 21:03:48
|
|
|
|
* @LastEditTime: 2022-04-20 21:32:40
|
|
|
|
* @LastEditors: Please set LastEditors
|
|
|
|
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
|
|
* @FilePath: \undefinede:\项目\兼职项目\MotorPid\stm32\USER\main.c
|
|
|
|
*/
|
|
|
|
#include "led.h"
|
|
|
|
#include "delay.h"
|
|
|
|
#include "sys.h"
|
|
|
|
#include "usart.h"
|
|
|
|
#include "wifi.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "oled.h"
|
|
|
|
#include "motor.h"
|
|
|
|
#include "encoder.h"
|
|
|
|
#include "key.h"
|
|
|
|
|
|
|
|
struct UserInfo userInfo;
|
|
|
|
|
|
|
|
// 函数声明
|
|
|
|
void Sys_Init(void);
|
|
|
|
void processingTasks(void);
|
|
|
|
void OLED_Display(void);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: main函数
|
|
|
|
* @param {*}
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
Sys_Init();
|
|
|
|
delay_ms(1000);
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
processingTasks();
|
|
|
|
OLED_Display();
|
|
|
|
delay_ms(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: 处理任务
|
|
|
|
* @param {*}
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
void processingTasks(void)
|
|
|
|
{
|
|
|
|
int key = 0;
|
|
|
|
key = KEY_Scan(0);
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case WKUP_PRES:
|
|
|
|
userInfo.diversion = ~userInfo.diversion;
|
|
|
|
// 方向
|
|
|
|
if(userInfo.diversion && userInfo.targetSpeed > 0) userInfo.targetSpeed = 0 - userInfo.targetSpeed;
|
|
|
|
else if(userInfo.diversion == 0) userInfo.targetSpeed = abs(userInfo.targetSpeed);
|
|
|
|
break;
|
|
|
|
case KEY1_PRES:
|
|
|
|
userInfo.targetSpeed++;
|
|
|
|
if(userInfo.targetSpeed > 20) userInfo.targetSpeed = 20;
|
|
|
|
break;
|
|
|
|
case KEY0_PRES:
|
|
|
|
userInfo.targetSpeed--;
|
|
|
|
if(userInfo.targetSpeed < -20) userInfo.targetSpeed = -20;
|
|
|
|
break;
|
|
|
|
case KEY2_PRES:
|
|
|
|
if(userInfo.run == 0) userInfo.run = 1;
|
|
|
|
else userInfo.run = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: 系统初始化
|
|
|
|
* @param {*}
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
void Sys_Init(void)
|
|
|
|
{
|
|
|
|
delay_init();
|
|
|
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
|
|
|
uart_init(115200);
|
|
|
|
PWM_Init(7199,0);
|
|
|
|
TIM3_Int_Init(5000, 71); // 定时器3
|
|
|
|
Encoder_Init_TIM2();
|
|
|
|
KEY_Init();
|
|
|
|
OLED_Init();
|
|
|
|
OLED_Clear();
|
|
|
|
// 初始化为0
|
|
|
|
userInfo.targetSpeed = 0;
|
|
|
|
userInfo.run = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description: Oled显示
|
|
|
|
* @param {*}
|
|
|
|
* @return {*}
|
|
|
|
*/
|
|
|
|
void OLED_Display(void)
|
|
|
|
{
|
|
|
|
char str[50];
|
|
|
|
if(userInfo.diversion) sprintf(str, "speed:%d,pwm_val:%d,target:%d CW ", abs(userInfo.currentSpeed), abs(userInfo.pwm_val), userInfo.targetSpeed);
|
|
|
|
else sprintf(str, "speed:%d,pwm_val:%d,target:%d REV ", abs(userInfo.currentSpeed), abs(userInfo.pwm_val), userInfo.targetSpeed);
|
|
|
|
OLED_ShowString(0,0, str, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|