## 项目说明
基于梁山派开发板实现的多媒体控制旋钮,通过上位机python代码接收串口数据实现对PC端的控制
## PCB设计说明
PCB是以梁山派拓展板的形式进行设计
主要使用的硬件:
按钮x1
LCD显示屏x1
AS5600磁性编码器x1
带磁环的无刷电机x1(本来想做力反馈,奈何能力有限,PID算法研究中)
还留下了不少硬件,方便日后拓展新功能
## 软件说明
主要代码实现逻辑:
**代码块:**
```
/*!
\file main.c
\brief led spark with systick
\version 2016-08-15, V1.0.0, firmware for GD32F4xx
\version 2018-12-12, V2.0.0, firmware for GD32F4xx
\version 2020-09-30, V2.1.0, firmware for GD32F4xx
\version 2022-03-09, V3.0.0, firmware for GD32F4xx
*/
/*
Copyright (c) 2022, GigaDevice Semiconductor Inc.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "gd32f4xx.h"
#include "systick.h"
#include <stdio.h>
#include "main.h"
#include "bsp_led.h"
#include "sys.h"
#include "bsp_usart.h"
#include "bsp_key.h"
#include "bsp_base_timer.h"
#include "bsp_pwm.h"
#include "stdlib.h"
#include "string.h"
#include "bsp_IIC.h"
#include "math.h"
#include "lcd.h"
#include "lcd_init.h"
#include "bsp_dma.h"
/*!
\brief toggle the led every 500ms
\param[in] none
\param[out] none
\retval none
*/
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int btnmode = 1;
int main(void)
{
systick_config();
usart_gpio_config(115200U);
key_gpio_config();
pwm_config(240,16000);
i2c_config();
IIC_Start();
delay_1ms(100);
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2); // 优先级分组
LCD_Init();//屏幕初始化
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);//清全屏为黑色
float oldAng = AS5600_receive_data();
float newAng = oldAng;
while(1){
newAng = AS5600_receive_data();
if (newAng - oldAng>-0.2 && newAng - oldAng < 0.2)
{
newAng = oldAng;
}else{
oldAng = newAng;
}
if (btnmode == 1)
{
LCD_ShowString(8*8,16*3,"Mode: 1",WHITE,BLACK,32,0);
LCD_ShowString(8*5,16*6," VolumeCtrl ",WHITE,BLACK,24,0);
}else{
LCD_ShowString(8*8,16*3,"Mode: 2",WHITE,BLACK,32,0);
LCD_ShowString(8*5,16*6,"MouseScrolling",WHITE,BLACK,24,0);
}
LCD_ShowFloatNum1(8*8,16*10,newAng,6,WHITE,BLACK,32);
delay_1ms(10);
printf("s%d%.2fe",btnmode,newAng);
}
}
void BSP_KEY_EXTI_IRQHANDLER(void)
{
if(exti_interrupt_flag_get(BSP_KEY_EXTI_LINE)==SET )
{
if(gpio_input_bit_get(BSP_KEY_PORT,BSP_KEY_PIN)==SET)
{
btnmode++;
btnmode = btnmode % 2;
//gpio_bit_toggle(PORT_LED2,PIN_LED2);
}
}
exti_interrupt_flag_clear(BSP_KEY_EXTI_LINE);
}
```
旋转角度读取:通过IIC通信,读取AS5600编码器数据,再通过串口输出
功能切换:配置引脚外部中断函数,按下按键时,切换btnmode变量控制串口输出数据
## 附件说明
硬件项目代码:智能旋钮.zip
上位机代码:上位机.py
为了方便使用,这里也提供了一个打包好的上位机代码:上位机.exe
## PS
视频里的飞线是因为元件焊到最后才发现板子有问题,部分画了焊盘的位置没有焊盘,导致部分引脚悬空
一抓就抓住了五个板子里唯一的残次品,这个手气也不知道算好还是算差 -.-