重构完代码结构了

This commit is contained in:
2025-07-26 01:22:52 +08:00
parent 47e0b8419f
commit 501a9ddff4
108 changed files with 224 additions and 2017 deletions

BIN
assets/User_code/device/.DS_Store vendored Normal file

Binary file not shown.

View File

View File

@@ -0,0 +1,301 @@
#include "bmp280_i2c.h"
#include "bsp/i2c.h"
#define I2C_Handle BSP_I2C_GetHandle(BSP_I2C_BMP280)
/**
* @brief 读寄存器
* @param regAdd 寄存器开始地址
* @param pdata 存储数据的指针
* @param size 寄存器个数
* @retval 无
*/
void bmp280_readReg(uint8_t regAdd, uint8_t *pdata, uint8_t size) {
HAL_I2C_Mem_Read(I2C_Handle, BMP280_I2C_ADDR << 1, regAdd, I2C_MEMADD_SIZE_8BIT, pdata, size, 1000);
}
/**
* @brief 写1个寄存器
* @param regAdd 寄存器开始地址
* @param pdata 存储数据的指针
* @retval 0 写入成功
* 1 写入失败
*/
uint8_t bmp280_writeReg(uint8_t regAdd, uint8_t *pdata) {
if (HAL_I2C_Mem_Write(I2C_Handle, BMP280_I2C_ADDR << 1, regAdd, I2C_MEMADD_SIZE_8BIT, pdata, 1, 1000) == HAL_OK) {
return 0;
}
return 1;
}
/**
* @brief 读取设备物理id用于调试
* @param 无
* @retval 设备id
*/
uint8_t bmp280_get_id(void) {
uint8_t temp = 0;
bmp280_readReg(BMP280_ID, &temp, 1);
return temp;
}
/**
* @brief 重启设备
* @param 无
* @retval 0 重启成功
* 1 重启失败
*/
uint8_t bmp280_reset(void) {
uint8_t temp = 0xB6;
return bmp280_writeReg(BMP280_RESET, &temp);
}
/**
* @brief 获取设备状态
* @param 无
* @retval 0 空闲
* 1 正在测量或者正在复制
*/
uint8_t bmp280_getStatus(void) {
uint8_t temp = 0;
bmp280_readReg(BMP280_STATUS, &temp, 1);
return (temp & 0x09) ? 1 : 0;
}
/**
* @brief 工作模式设置推荐
* @param mode 0 睡眠模式
* 1 单次测量模式,测量完成后回到休眠模式
* 2 连续测量模式
* @retval 0 设置成功
* 1 设置失败
*/
uint8_t bmp280_setMode(uint8_t mode)
{
uint8_t temp=0;
bmp280_readReg(BMP280_CTRL_MEAS,&temp,1);
switch(mode)
{
case 0:
temp&=0xFC;
break;
case 1:
temp&=0xFC;
temp|=0x01;
break;
case 2:
temp&=0xFC;
temp|=0x03;
break;
default:
return 1;
}
return bmp280_writeReg(BMP280_CTRL_MEAS,&temp);
}
/**
* @brief 过采样设置
* @param mode temp&press 0 禁用
* 1 过采样×1
* 2 过采样×2
* 3 过采样×4
* .. .....
* 5 过采样×16
* @retval 0 设置成功
* 1 设置失败
*/
uint8_t bmp280_setOversampling(uint8_t osrs_p,uint8_t osrs_t)
{
uint8_t temp=0;
bmp280_readReg(BMP280_CTRL_MEAS,&temp,1);
temp&=0xE3;
osrs_p = osrs_p<<2;
osrs_p&= 0x1C;
temp|=osrs_p;
temp&=0x1F;
osrs_t = osrs_t<<5;
osrs_t&= 0xE0;
temp|=osrs_t;
return bmp280_writeReg(BMP280_CTRL_MEAS,&temp);
}
/**
* @brief 滤波器系数和采样间隔时间设置
* @param Standbyt 0 0.5ms filter 0 关闭滤波器
* 1 62.5ms 1 2
* 2 125ms 2 4
* 3 250ms 3 8
* 4 500ms 4 16
* 5 1000ms
* 6 2000ms
* 7 4000ms
* @retval 0 设置成功
* 1 设置失败
*/
uint8_t bmp280_setConfig(uint8_t Standbyt,uint8_t filter)
{
uint8_t temp=0;
temp = Standbyt<<5;
filter&=0x07;
filter=filter<<2;
temp|=filter;
return bmp280_writeReg(BMP280_CONFIG,&temp);
}
/**
* @brief 获取校准系数
* @param calib 储存系数的结构体
* @retval 无
*/
void bmp280_getCalibration(bmp280_calib *calib)
{
uint8_t buf[20];
bmp280_readReg(BMP280_DIGT,buf,6);
calib->dig_t1 =(uint16_t)(bmp280_msblsb_to_u16(buf[1], buf[0]));
calib->dig_t2 =(int16_t)(bmp280_msblsb_to_u16(buf[3], buf[2]));
calib->dig_t3 =(int16_t)(bmp280_msblsb_to_u16(buf[5], buf[4]));
bmp280_readReg(BMP280_DIGP,buf,18);
calib->dig_p1 = (uint16_t)(bmp280_msblsb_to_u16(buf[1], buf[0]));
calib->dig_p2 =(int16_t)(bmp280_msblsb_to_u16(buf[3], buf[2]));
calib->dig_p3 =(int16_t)(bmp280_msblsb_to_u16(buf[5], buf[4]));
calib->dig_p4 =(int16_t)(bmp280_msblsb_to_u16(buf[7], buf[6]));
calib->dig_p5 =(int16_t)(bmp280_msblsb_to_u16(buf[9], buf[8]));
calib->dig_p6 =(int16_t)(bmp280_msblsb_to_u16(buf[11], buf[10]));
calib->dig_p7 =(int16_t)(bmp280_msblsb_to_u16(buf[13], buf[12]));
calib->dig_p8 =(int16_t)(bmp280_msblsb_to_u16(buf[15], buf[14]));
calib->dig_p9 =(int16_t)(bmp280_msblsb_to_u16(buf[17], buf[16]));
}
/**
* @brief 获取温度
* @param calib 系数的结构体
* *temperature 温度值指针
* *t_fine 精细分辨率温度值指针
* @retval 无
*/
void bmp280_getTemperature(bmp280_calib *calib,double *temperature,int32_t *t_fine)
{
uint8_t buf[3];
uint32_t data_xlsb;
uint32_t data_lsb;
uint32_t data_msb;
int32_t uncomp_temperature;
double var1, var2;
bmp280_readReg(BMP280_TEMP,buf,3);
data_msb = (int32_t)buf[0] << 12;
data_lsb = (int32_t)buf[1] << 4;
data_xlsb = (int32_t)buf[2] >> 4;
uncomp_temperature = (int32_t)(data_msb | data_lsb | data_xlsb);
var1 = (((double) uncomp_temperature) / 16384.0 - ((double) calib->dig_t1) / 1024.0) *
((double) calib->dig_t2);
var2 =
((((double) uncomp_temperature) / 131072.0 - ((double) calib->dig_t1) / 8192.0) *
(((double) uncomp_temperature) / 131072.0 - ((double) calib->dig_t1) / 8192.0)) *
((double) calib->dig_t3);
*t_fine = (int32_t) (var1 + var2);
*temperature = (var1 + var2) / 5120.0;
}
/**
* @brief 获取气压
* @param calib 系数的结构体
* *pressure 气压值指针
* *t_fine 精细分辨率温度值指针
* @retval 无
*/
void bmp280_getPressure(bmp280_calib *calib,double *pressure,int32_t *t_fine)
{
uint8_t buf[3];
uint32_t data_xlsb;
uint32_t data_lsb;
uint32_t data_msb;
int32_t uncomp_pressure;
double var1, var2;
bmp280_readReg(BMP280_PRES,buf,3);
data_msb = (uint32_t)buf[0] << 12;
data_lsb = (uint32_t)buf[1] << 4;
data_xlsb = (uint32_t)buf[2] >> 4;
uncomp_pressure = (data_msb | data_lsb | data_xlsb);
var1 = ((double) *t_fine / 2.0) - 64000.0;
var2 = var1 * var1 * ((double) calib->dig_p6) / 32768.0;
var2 = var2 + var1 * ((double) calib->dig_p5) * 2.0;
var2 = (var2 / 4.0) + (((double) calib->dig_p4) * 65536.0);
var1 = (((double)calib->dig_p3) * var1 * var1 / 524288.0 + ((double)calib->dig_p2) * var1) /
524288.0;
var1 = (1.0 + var1 / 32768.0) * ((double) calib->dig_p1);
*pressure = 1048576.0 - (double)uncomp_pressure;
*pressure = (*pressure - (var2 / 4096.0)) * 6250.0 / var1;
var1 = ((double)calib->dig_p9) * *pressure * *pressure / 2147483648.0;
var2 = *pressure * ((double)calib->dig_p8) / 32768.0;
*pressure = *pressure + (var1 + var2 + ((double)calib->dig_p7)) / 16.0;
}
/**
* @brief 初始化
* @param *calib 系数的结构体指针
* @retval 0 设置成功
* 1 设置失败
*/
uint8_t bmp280_init(bmp280_calib *calib)
{
uint8_t rslt;
rslt = bmp280_get_id();
if(rslt == BMP2_CHIP_ID)
{
bmp280_getCalibration(calib);
rslt = bmp280_setOversampling(5,2);
if(rslt)
{
return 1;
}
rslt = bmp280_setConfig(0,4);
if(rslt)
{
return 1;
}
rslt = bmp280_setMode(2);
if(rslt)
{
return 1;
}
}
else
{
return 1;
}
return 0;
}
/**
* @brief 获取最终数据
* @param *calib 系数的结构体指针
* @retval 无
*/
void bmp280_getdata(bmp280_calib *calib,float *temperature,float *pressure)
{
double temp_T,temp_P;
int32_t t_fine;
bmp280_getTemperature(calib,&temp_T,&t_fine);
bmp280_getPressure(calib,&temp_P,&t_fine);
*temperature = (float)temp_T;
*pressure = (float)temp_P;
}

View File

@@ -0,0 +1,49 @@
#pragma once
/*底层接口定义*/
#include "bsp/i2c.h"
#include "stdint.h"
#define BMP280_I2C_ADDR 0x76 // BMP280 默认 I2C 地址
/*寄存器地址*/
#define BMP280_ID 0xD0 // 设备ID地址
#define BMP280_RESET 0xE0 // 设备重启
#define BMP280_STATUS 0xF3 // 设备状态
#define BMP280_CTRL_MEAS 0xF4 // 数据采集和模式设置
#define BMP280_CONFIG 0xF5 // 采样速率,滤波器和接口设置
#define BMP280_DIGT 0x88 // 温度校准系数起始位置
#define BMP280_DIGP 0x8E // 气压校准系数起始位置
#define BMP280_TEMP 0xFA // 温度储存起始位置
#define BMP280_PRES 0xF7 // 气压储存起始位置
#define BMP2_CHIP_ID 0x58 // 设备ID地址
#define bmp280_msblsb_to_u16(msb, lsb) (((uint16_t)msb << 8) | ((uint16_t)lsb))
typedef struct {
unsigned short dig_t1;
signed short dig_t2;
signed short dig_t3;
unsigned short dig_p1;
signed short dig_p2;
signed short dig_p3;
signed short dig_p4;
signed short dig_p5;
signed short dig_p6;
signed short dig_p7;
signed short dig_p8;
signed short dig_p9;
} bmp280_calib;
uint8_t bmp280_get_id(void);
uint8_t bmp280_reset(void);
uint8_t bmp280_getStatus(void);
uint8_t bmp280_setMode(uint8_t mode);
uint8_t bmp280_setOversampling(uint8_t osrs_p, uint8_t osrs_t);
uint8_t bmp280_setConfig(uint8_t Standbyt, uint8_t filter);
void bmp280_getCalibration(bmp280_calib *calib);
void bmp280_getTemperature(bmp280_calib *calib, double *temperature, int32_t *t_fine);
void bmp280_getPressure(bmp280_calib *calib, double *pressure, int32_t *t_fine);
uint8_t bmp280_init(bmp280_calib *calib);
void bmp280_getdata(bmp280_calib *calib, float *temperature, float *pressure);

View File

@@ -0,0 +1,5 @@
oled_i2c,bsp/i2c
bmp280_i2c,bsp/i2c
pc_uart,bsp/uart
key_gpio,bsp/gpio_exti
servo,bsp/servo_pwm
1 oled_i2c bsp/i2c
2 bmp280_i2c bsp/i2c
3 pc_uart bsp/uart
4 key_gpio bsp/gpio_exti
5 servo bsp/servo_pwm

View File

@@ -0,0 +1 @@
servo,测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息
1 servo 测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息测试消息

View File

@@ -0,0 +1,15 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define DEVICE_OK (0)
#define DEVICE_ERR (-1)
#define DEVICE_ERR_NULL (-2)
#define DEVICE_ERR_INITED (-3)
#define DEVICE_ERR_NO_DEV (-4)
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,65 @@
/* Includes ----------------------------------------------------------------- */
#include "key_gpio.h"
#include "device.h"
#include "bsp/gpio_exti.h"
#include "gpio.h"
/* Private define ----------------------------------------------------------- */
#define DEBOUNCE_TIME_MS 20
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function -------------------------------------------------------- */
/* 外部声明标志位(标志位) */
volatile uint8_t key_flag = 0; // 1=按下0=松开
volatile uint8_t key_exti = 0;
volatile uint8_t key_pressed = 0; // 全局标志位
static uint32_t last_debounce_time = 0; // 消抖
/* Private function -------------------------------------------------------- */
static void KEY_Interrupt_Callback(void) {
// 切换标志位状态
key_flag = !key_flag;
key_exti = 1;
}
/* Exported functions ------------------------------------------------------- */
void KEY_Process(void)
{
BSP_GPIO_RegisterCallback(BSP_GPIO_USER_KEY, BSP_GPIO_EXTI_CB, KEY_Interrupt_Callback);
if(key_exti == 1)
{
uint32_t now = HAL_GetTick();
// 检查是否超过消抖时间
if ((now - last_debounce_time) > DEBOUNCE_TIME_MS) {
// 更新有效状态(假设按下为低电平)
if(key_flag == 0)
{
key_pressed = DEVICE_KEY_RELEASED;
}
if(key_flag == 1)
{
key_pressed = DEVICE_KEY_PRESSED;
}
}
else
{
HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);
}
last_debounce_time = now; // 重置消抖计时器
key_exti = 0;
}
else
{
}
}
uint8_t KEY_Get_State(void) {
return key_pressed;
}

View File

@@ -0,0 +1,21 @@
#ifndef KEY_GPIO_H
#define KEY_GPIO_H
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include "main.h"
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
///* KEY按键状态设置用 */
typedef enum
{
DEVICE_KEY_RELEASED, //按键释放
DEVICE_KEY_PRESSED, //按键按下
} DEVICE_KEY_Status_t;
void KEY_Process(void);
uint8_t KEY_Get_State(void);
#endif

View File

@@ -0,0 +1,267 @@
/* Includes ----------------------------------------------------------------- */
#include "device/oled_i2c.h"
#include "bsp/i2c.h"
#include <string.h>
#include <stdint.h>
/* Private define ----------------------------------------------------------- */
#define OLED_I2C_ADDR 0x78 // OLED I2C 地址
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
/* Private variables -------------------------------------------------------- */
static uint8_t oled_buffer[OLED_WIDTH * OLED_HEIGHT / 8];
static struct {
uint8_t x_min;
uint8_t x_max;
uint8_t y_min;
uint8_t y_max;
uint8_t dirty; // 标志是否有脏区域
} dirty_rect = {0, 0, 0, 0, 0};
/* Private function prototypes ---------------------------------------------- */
static void OLED_WriteCommand(uint8_t cmd) {
uint8_t data[2] = {0x00, cmd};
HAL_I2C_Master_Transmit(BSP_I2C_GetHandle(BSP_I2C_OLED), OLED_I2C_ADDR, data, 2, HAL_MAX_DELAY);
}
static void OLED_WriteData(uint8_t *data, uint16_t size) {
uint8_t buffer[size + 1];
buffer[0] = 0x40;
memcpy(&buffer[1], data, size);
HAL_I2C_Master_Transmit(BSP_I2C_GetHandle(BSP_I2C_OLED), OLED_I2C_ADDR, buffer, size + 1, HAL_MAX_DELAY);
}
static void OLED_MarkDirty(uint8_t x, uint8_t y);
static void OLED_UpdateDirtyScreen(void);
static const uint8_t oled_font[95][8] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,}, /* " ", 0 */
{0x00,0x00,0x00,0xcf,0xcf,0x00,0x00,0x00,}, /* "!", 1 */
{0x00,0x0c,0x06,0x00,0x0c,0x06,0x00,0x00,}, /* """, 2 */
{0x24,0xe4,0x3c,0x27,0xe4,0x3c,0x27,0x24,}, /* "#", 3 */
{0x00,0x20,0x46,0xf9,0x9f,0x62,0x04,0x00,}, /* "$", 4 */
{0x06,0x09,0xc6,0x30,0x0c,0x63,0x90,0x60,}, /* "%", 5 */
{0x00,0x00,0x6e,0x91,0xa9,0x46,0xa0,0x00,}, /* "&", 6 */
{0x00,0x00,0x00,0x1c,0x0e,0x00,0x00,0x00,}, /* "'", 7 */
{0x00,0x00,0x3c,0x42,0x81,0x00,0x00,0x00,}, /* "(", 8 */
{0x00,0x00,0x00,0x81,0x42,0x3c,0x00,0x00,}, /* ")", 9 */
{0x00,0x10,0x54,0x38,0x38,0x54,0x10,0x00,}, /* "*", 10 */
{0x00,0x10,0x10,0xfc,0x10,0x10,0x00,0x00,}, /* "+", 11 */
{0x00,0x00,0x00,0xc0,0x60,0x00,0x00,0x00,}, /* ",", 12 */
{0x00,0x00,0x10,0x10,0x10,0x10,0x00,0x00,}, /* "-", 13 */
{0x00,0x00,0x00,0x00,0xc0,0xc0,0x00,0x00,}, /* ".", 14 */
{0x00,0x00,0x00,0xc0,0x38,0x07,0x00,0x00,}, /* "/", 15 */
{0x00,0x00,0x7c,0x92,0x8a,0x7c,0x00,0x00,}, /* "0", 16 */
{0x00,0x00,0x00,0x84,0xfe,0x80,0x00,0x00,}, /* "1", 17 */
{0x00,0x00,0x8c,0xc2,0xa2,0x9c,0x00,0x00,}, /* "2", 18 */
{0x00,0x00,0x44,0x92,0x92,0x6c,0x00,0x00,}, /* "3", 19 */
{0x00,0x20,0x38,0x24,0xfe,0x20,0x00,0x00,}, /* "4", 20 */
{0x00,0x00,0x5e,0x92,0x92,0x62,0x00,0x00,}, /* "5", 21 */
{0x00,0x00,0x78,0x94,0x92,0x62,0x00,0x00,}, /* "6", 22 */
{0x00,0x00,0x82,0x62,0x1a,0x06,0x00,0x00,}, /* "7", 23 */
{0x00,0x00,0x6c,0x92,0x92,0x6c,0x00,0x00,}, /* "8", 24 */
{0x00,0x00,0x8c,0x52,0x32,0x1c,0x00,0x00,}, /* "9", 25 */
{0x00,0x00,0x00,0x6c,0x6c,0x00,0x00,0x00,}, /* ":", 26 */
{0x00,0x00,0x80,0xec,0x6c,0x00,0x00,0x00,}, /* ";", 27 */
{0x00,0x00,0x10,0x28,0x44,0x00,0x00,0x00,}, /* "<", 28 */
{0x00,0x00,0x24,0x24,0x24,0x24,0x00,0x00,}, /* "=", 29 */
{0x00,0x00,0x00,0x44,0x28,0x10,0x00,0x00,}, /* ">", 30 */
{0x00,0x00,0x0c,0xa2,0x92,0x1c,0x00,0x00,}, /* "?", 31 */
{0x00,0x3c,0x42,0x99,0xa5,0xa2,0x3c,0x00,}, /* "@", 32 */
{0x00,0xe0,0x1c,0x12,0x12,0x1c,0xe0,0x00,}, /* "A", 33 */
{0x00,0xfe,0x92,0x92,0x9c,0x90,0x60,0x00,}, /* "B", 34 */
{0x00,0x38,0x44,0x82,0x82,0x82,0x44,0x00,}, /* "C", 35 */
{0x00,0xfe,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "D", 36 */
{0x00,0xfe,0x92,0x92,0x92,0x92,0x92,0x00,}, /* "E", 37 */
{0x00,0xfe,0x12,0x12,0x12,0x12,0x02,0x00,}, /* "F", 38 */
{0x00,0x7c,0x82,0x92,0x92,0x72,0x00,0x00,}, /* "G", 39 */
{0x00,0xfe,0x10,0x10,0x10,0x10,0xfe,0x00,}, /* "H", 40 */
{0x00,0x82,0x82,0xfe,0x82,0x82,0x00,0x00,}, /* "I", 41 */
{0x00,0x82,0x82,0x7e,0x02,0x02,0x00,0x00,}, /* "J", 42 */
{0x00,0xfe,0x10,0x28,0x44,0x82,0x00,0x00,}, /* "K", 43 */
{0x00,0xfe,0x80,0x80,0x80,0x80,0x00,0x00,}, /* "L", 44 */
{0xfc,0x02,0x04,0xf8,0x04,0x02,0xfc,0x00,}, /* "M", 45 */
{0x00,0xfe,0x04,0x18,0x30,0x40,0xfe,0x00,}, /* "N", 46 */
{0x00,0x7c,0x82,0x82,0x82,0x82,0x7c,0x00,}, /* "O", 47 */
{0x00,0x00,0xfe,0x12,0x12,0x0c,0x00,0x00,}, /* "P", 48 */
{0x00,0x00,0x3c,0x42,0xc2,0xbc,0x00,0x00,}, /* "Q", 49 */
{0x00,0x00,0xfe,0x32,0x52,0x8c,0x00,0x00,}, /* "R", 50 */
{0x00,0x00,0x4c,0x92,0x92,0x64,0x00,0x00,}, /* "S", 51 */
{0x00,0x02,0x02,0xfe,0x02,0x02,0x00,0x00,}, /* "T", 52 */
{0x00,0x7e,0x80,0x80,0x80,0x80,0x7e,0x00,}, /* "U", 53 */
{0x00,0x0c,0x30,0xc0,0x30,0x0c,0x00,0x00,}, /* "V", 54 */
{0x7c,0x80,0x80,0x78,0x80,0x80,0x7c,0x00,}, /* "W", 55 */
{0x00,0x84,0x48,0x30,0x30,0x48,0x84,0x00,}, /* "X", 56 */
{0x00,0x06,0x08,0xf0,0x08,0x06,0x00,0x00,}, /* "Y", 57 */
{0x00,0x00,0xc2,0xa2,0x92,0x8e,0x00,0x00,}, /* "Z", 58 */
{0x00,0x00,0xfe,0x82,0x82,0x82,0x00,0x00,}, /* "[", 59 */
{0x00,0x00,0x06,0x18,0x60,0x80,0x00,0x00,}, /* "\", 60 */
{0x00,0x00,0x82,0x82,0x82,0xfe,0x00,0x00,}, /* "]", 61 */
{0x00,0x30,0x0c,0x02,0x0c,0x30,0x00,0x00,}, /* "^", 62 */
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,}, /* "_", 63 */
{0x00,0x00,0x04,0x0c,0x18,0x00,0x00,0x00,}, /* "`", 64 */
{0x00,0x00,0x60,0x90,0x90,0xe0,0x00,0x00,}, /* "a", 65 */
{0x00,0x00,0xf8,0xa0,0xe0,0x00,0x00,0x00,}, /* "b", 66 */
{0x00,0x00,0x60,0x90,0x90,0x00,0x00,0x00,}, /* "c", 67 */
{0x00,0x00,0xe0,0xa0,0xf8,0x00,0x00,0x00,}, /* "d", 68 */
{0x00,0x00,0x70,0xa8,0xa8,0x90,0x00,0x00,}, /* "e", 69 */
{0x00,0x00,0x10,0xf8,0x14,0x00,0x00,0x00,}, /* "f", 70 */
{0x00,0x00,0xd8,0xa4,0x7c,0x00,0x00,0x00,}, /* "g", 71 */
{0x00,0x00,0xf8,0x20,0xe0,0x00,0x00,0x00,}, /* "h", 72 */
{0x00,0x00,0x00,0xe8,0x00,0x00,0x00,0x00,}, /* "i", 73 */
{0x00,0x00,0x40,0x90,0x74,0x00,0x00,0x00,}, /* "j", 74 */
{0x00,0x00,0xf8,0x60,0x90,0x00,0x00,0x00,}, /* "k", 75 */
{0x00,0x00,0x78,0x80,0x80,0x00,0x00,0x00,}, /* "l", 76 */
{0x00,0xe0,0x10,0xe0,0x10,0xe0,0x00,0x00,}, /* "m", 77 */
{0x00,0x00,0xf0,0x10,0x10,0xe0,0x00,0x00,}, /* "n", 78 */
{0x00,0x00,0x60,0x90,0x90,0x60,0x00,0x00,}, /* "o", 79 */
{0x00,0x00,0xf0,0x48,0x48,0x30,0x00,0x00,}, /* "p", 80 */
{0x00,0x00,0x30,0x48,0x48,0xf0,0x00,0x00,}, /* "q", 81 */
{0x00,0x00,0x00,0xf0,0x20,0x10,0x00,0x00,}, /* "r", 82 */
{0x00,0x00,0x90,0xa8,0xa8,0x48,0x00,0x00,}, /* "s", 83 */
{0x00,0x10,0x10,0xf8,0x90,0x90,0x00,0x00,}, /* "t", 84 */
{0x00,0x00,0x78,0x80,0x80,0xf8,0x00,0x00,}, /* "u", 85 */
{0x00,0x18,0x60,0x80,0x60,0x18,0x00,0x00,}, /* "v", 86 */
{0x00,0x38,0xc0,0x38,0xc0,0x38,0x00,0x00,}, /* "w", 87 */
{0x00,0x88,0x50,0x20,0x50,0x88,0x00,0x00,}, /* "x", 88 */
{0x00,0x8c,0x50,0x20,0x10,0x0c,0x00,0x00,}, /* "y", 89 */
{0x00,0x88,0xc8,0xa8,0x98,0x88,0x00,0x00,}, /* "z", 90 */
{0x00,0x00,0x10,0x7c,0x82,0x00,0x00,0x00,}, /* "{", 91 */
{0x00,0x00,0x00,0xfe,0x00,0x00,0x00,0x00,}, /* "|", 92 */
{0x00,0x00,0x00,0x82,0x7c,0x10,0x00,0x00,}, /* "}", 93 */
{0x00,0x08,0x04,0x04,0x08,0x10,0x10,0x08,}, /* "~", 94 */
};
/* Exported functions ------------------------------------------------------- */
void OLED_Init(void) {
OLED_WriteCommand(0xAE); // 关闭显示
OLED_WriteCommand(0x20); // 设置内存寻址模式
OLED_WriteCommand(0x10); // 页寻址模式
OLED_WriteCommand(0xB0); // 设置页起始地址
OLED_WriteCommand(0xC8); // 设置COM扫描方向
OLED_WriteCommand(0x00); // 设置低列地址
OLED_WriteCommand(0x10); // 设置高列地址
OLED_WriteCommand(0x40); // 设置显示起始行
OLED_WriteCommand(0x81); // 设置对比度
OLED_WriteCommand(0xFF); // 最大对比度
OLED_WriteCommand(0xA1); // 设置段重映射
OLED_WriteCommand(0xA6); // 正常显示
OLED_WriteCommand(0xA8); // 设置多路复用比率
OLED_WriteCommand(0x3F); // 1/64
OLED_WriteCommand(0xA4); // 输出跟随 RAM 内容
OLED_WriteCommand(0xD3); // 设置显示偏移
OLED_WriteCommand(0x00); // 无偏移
OLED_WriteCommand(0xD5); // 设置显示时钟分频比/振荡频率
OLED_WriteCommand(0xF0); // 高频
OLED_WriteCommand(0xD9); // 设置预充电周期
OLED_WriteCommand(0x22); // 修复缺少分号
OLED_WriteCommand(0xDA); // 设置COM引脚硬件配置
OLED_WriteCommand(0x12); // 修复缺少分号
OLED_WriteCommand(0xDB); // 设置VCOMH电压
OLED_WriteCommand(0x20); // 修复缺少分号
OLED_WriteCommand(0x8D); // 设置充电泵
OLED_WriteCommand(0x14); // 修复缺少分号
OLED_WriteCommand(0xAF); // 打开显示
}
void OLED_Clear(void) {
memset(oled_buffer, 0, sizeof(oled_buffer));
dirty_rect.x_min = 0;
dirty_rect.x_max = OLED_WIDTH - 1;
dirty_rect.y_min = 0;
dirty_rect.y_max = OLED_HEIGHT - 1;
dirty_rect.dirty = 1;
OLED_UpdateScreen();
}
void OLED_UpdateScreen(void) {
OLED_UpdateDirtyScreen();
}
void OLED_DrawPixel(uint8_t x, uint8_t y, uint8_t color) {
if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return;
if (color) {
if (!(oled_buffer[x + (y / 8) * OLED_WIDTH] & (1 << (y % 8)))) {
oled_buffer[x + (y / 8) * OLED_WIDTH] |= (1 << (y % 8));
OLED_MarkDirty(x, y);
}
} else {
if (oled_buffer[x + (y / 8) * OLED_WIDTH] & (1 << (y % 8))) {
oled_buffer[x + (y / 8) * OLED_WIDTH] &= ~(1 << (y % 8));
OLED_MarkDirty(x, y);
}
}
}
void OLED_DrawChar(uint8_t x, uint8_t y, char ch, uint8_t color) {
if (ch < ' ' || ch > '~') return;
if (x >= OLED_WIDTH || y >= OLED_HEIGHT || x + 8 > OLED_WIDTH || y + 8 > OLED_HEIGHT) {
return;
}
const uint8_t *font_data = oled_font[ch - ' '];
for (uint8_t i = 0; i < 8; i++) {
uint8_t column_data = font_data[i];
for (uint8_t j = 0; j < 8; j++) {
if (column_data & (1 << j)) {
OLED_DrawPixel(x + i, y + j, color);
} else {
OLED_DrawPixel(x + i, y + j, !color);
}
}
}
}
void OLED_DrawString(uint8_t x, uint8_t y, const char *str, uint8_t color) {
while (*str) {
OLED_DrawChar(x, y, *str, color);
x += 8;
if (x + 8 > OLED_WIDTH) {
x = 0;
y += 8;
}
if (y + 8 > OLED_HEIGHT) {
break;
}
str++;
}
}
/* Private functions -------------------------------------------------------- */
static void OLED_MarkDirty(uint8_t x, uint8_t y) {
if (!dirty_rect.dirty) {
dirty_rect.x_min = x;
dirty_rect.x_max = x;
dirty_rect.y_min = y;
dirty_rect.y_max = y;
dirty_rect.dirty = 1;
} else {
if (x < dirty_rect.x_min) dirty_rect.x_min = x;
if (x > dirty_rect.x_max) dirty_rect.x_max = x;
if (y < dirty_rect.y_min) dirty_rect.y_min = y;
if (y > dirty_rect.y_max) dirty_rect.y_max = y;
}
}
static void OLED_UpdateDirtyScreen(void) {
if (!dirty_rect.dirty) return;
uint8_t y_start = dirty_rect.y_min / 8;
uint8_t y_end = dirty_rect.y_max / 8;
for (uint8_t i = y_start; i <= y_end; i++) {
OLED_WriteCommand(0xB0 + i);
OLED_WriteCommand(dirty_rect.x_min & 0x0F);
OLED_WriteCommand(0x10 | (dirty_rect.x_min >> 4));
uint8_t width = dirty_rect.x_max - dirty_rect.x_min + 1;
OLED_WriteData(&oled_buffer[dirty_rect.x_min + i * OLED_WIDTH], width);
}
dirty_rect.dirty = 0;
}

View File

@@ -0,0 +1,26 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
/* Exported constants ------------------------------------------------------- */
#define OLED_COLOR_BLACK 0
#define OLED_COLOR_WHITE 1
/* Exported functions prototypes -------------------------------------------- */
void OLED_Init(void);
void OLED_Clear(void);
void OLED_UpdateScreen(void);
void OLED_DrawPixel(uint8_t x, uint8_t y, uint8_t color);
void OLED_DrawString(uint8_t x, uint8_t y, const char *str, uint8_t color);
void OLED_DrawChar(uint8_t x, uint8_t y, char ch, uint8_t color);
void OLED_ShowChinese(uint8_t x, uint8_t y, uint8_t index);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,57 @@
/* Includes ----------------------------------------------------------------- */
#include "pc_uart.h"
#include <string.h>
#include "bsp\uart.h"
#include "device.h"
#define UART_HANDLE BSP_UART_GetHandle(BSP_UART_PC)
#define AI_LEN_RX_BUFF (sizeof(UART_RxData_t))
static bool rx_flag = false;
static uint8_t rxbuf[AI_LEN_RX_BUFF];
static void UART_RxCpltCallback(void) { rx_flag = true; }
int UART_Init(UART_t *huart)
{
UNUSED(huart);
//注册回调函数
HAL_UART_RegisterCallback(UART_HANDLE, BSP_UART_RX_CPLT_CB, UART_RxCpltCallback);
return DEVICE_OK
}
int UART_StartReceive(UART_t *huart)
{
UNUSED(huart);
HAL_UART_Receive_DMA(UART_HANDLE, rxbuf, AI_LEN_RX_BUFF);
return DEVICE_OK;
}
bool UART_IsReceiveComplete(void)
{
return rx_flag;
}
int8_t UART_ParseData(UART_t *huart)
{
memcpy(&huart->rx_data, rxbuf, sizeof(UART_RxData_t));
}
void UART_PackTx(UART_t *huart, UART_TxData_t *tx_data)
{
memcpy(tx_data, huart->tx_data, sizeof(UART_TxData_t));
}
int8_t UART_StartSend(UART_t *huart)
{
if (HAL_UART_Transmit_DMA(UART_HANDLE, huart->tx_data, sizeof(UART_TxData_t)) == HAL_OK)
{
return DEVICE_OK
}
return DEVICE_ERR;
}

View File

@@ -0,0 +1,50 @@
/*
UART通讯模板
*/
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdbool.h>
#include <stdint.h>
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
typedef struct
{
uint8_t head;
uint8_t data;
uint8_t crc;
} UART_RxData_t;
typedef struct
{
uint8_t head;
uint8_t data;
uint8_t crc;
} UART_TxData_t;
typedef struct
{
UART_RxData_t rx_data; // Received data buffer
UART_TxData_t tx_data; // Transmit data buffer
} UART_t;
/* Exported functions prototypes -------------------------------------------- */
int UART_Init(UART_t *huart);
int UART_StartReceive(UART_t *huart);
bool UART_IsReceiveComplete(void);
int8_t UART_ParseData(UART_t *huart);
void UART_PackTx(UART_t *huart, UART_TxData_t *tx_data);
int8_t UART_StartSend(UART_t *huart);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,36 @@
/* Includes ----------------------------------------------------------------- */
#include "main.h"
#include "servo.h"
#include "bsp/servo_pwm.h"
/* Private define ----------------------------------------------------------- */
#define MIN_CYCLE 0.5f //change begin
#define MAX_CYCLE 2.5f
#define ANGLE_LIMIT 180 //change end
/* Private macro ------------------------------------------------------------ */
/* Private typedef ---------------------------------------------------------- */
/* Private variables -------------------------------------------------------- */
/* Private function -------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
int serve_Init(BSP_PWM_Channel_t ch)
{
if(BSP_PWM_Start(ch)!=0){
return -1;
}else return 0;
}
int set_servo_angle(BSP_PWM_Channel_t ch,float angle)
{
if (angle < 0.0f || angle > ANGLE_LIMIT) {
return -1; // ÎÞЧµÄ½Ç¶È
}
float duty_cycle=MIN_CYCLE+(MAX_CYCLE-MIN_CYCLE)*(angle/ANGLE_LIMIT);
if(BSP_PWM_Set(ch,duty_cycle)!=0){
return -1;
}else return 0;
}

View File

@@ -0,0 +1,41 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include "tim.h"
#include "bsp/bsp.h"
#include "bsp/servo_pwm.h"
/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
extern int serve_Init(BSP_PWM_Channel_t ch);
extern int set_servo_angle(BSP_PWM_Channel_t ch,float angle);
#ifdef __cplusplus
}
#endif