diff --git a/Src/Software/Inc/Base_I2C.h b/Src/Software/Inc/Base_I2C.h new file mode 100644 index 0000000000000000000000000000000000000000..0567a85bef62d43a692c2aaa001a32b4da0edb3d --- /dev/null +++ b/Src/Software/Inc/Base_I2C.h @@ -0,0 +1,37 @@ +#ifndef __Base_I2C_H +#define __Base_I2C_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "stm32f1xx_hal.h" + +/** + * @brief 通过 I2C 接口写入命令数据 + * @param hi2cx: 指向 I2C 句柄的指针,用于指定使用的 I2C 总线实例 + * @param adr: 目标设备的 I2C 地址,标识要与之通信的从设备 + * @param TxData: 指向待发送数据的指针,包含了要写入 I2C 设备的命令或数据内容 + * @param len: 要发送数据的长度,即 TxData 中数据的字节数 + * @param timeout: 操作超时时间,若在该时间内操作未完成,则操作失败,单位取决于具体实现 + * @retval HAL_StatusTypeDef: 操作状态,如 HAL_OK 表示成功,其他值表示失败 + */ +HAL_StatusTypeDef Base_I2C_WriteCmd(uint8_t* hi2cx, uint16_t Adr, uint8_t* TxData, uint8_t Len, uint16_t Timeout); + +/** + * @brief 通过 I2C 接口读取命令响应数据 + * @param hi2cx: 指向 I2C 句柄的指针,用于指定使用的 I2C 总线实例 + * @param adr: 目标设备的 I2C 地址,标识要与之通信的从设备 + * @param RxData: 指向用于存储接收到数据的缓冲区的指针,读取到的数据会存于此 + * @param len: 要读取数据的长度,即期望从 I2C 设备读取的字节数 + * @param timeout: 操作超时时间,若在该时间内操作未完成,则操作失败,单位取决于具体实现 + * @retval HAL_StatusTypeDef: 操作状态,如 HAL_OK 表示成功,其他值表示失败 + */ +HAL_StatusTypeDef Base_I2C_ReadCmd(uint8_t* hi2cx, uint16_t Adr, uint8_t* RxData, uint8_t Len, uint16_t Timeout); + +#ifdef __cplusplus +} +#endif + +#endif /* __Base_I2C_H */ +//作者:韩林杏 diff --git a/Src/Software/Inc/CSB.h b/Src/Software/Inc/CSB.h new file mode 100644 index 0000000000000000000000000000000000000000..9a95f42f63f0c9656c9e5cdbdb71c8b3474c8322 --- /dev/null +++ b/Src/Software/Inc/CSB.h @@ -0,0 +1,13 @@ +#ifndef __CSB_H__ +#define __CSB_H__ + +void HC_SR04_Init(void); +void HC_SR04_W_Trig(uint8_t BitValue); +uint8_t HC_SR04_R_Echo(void); +float HC_SR04_Distance(void); + + + + + +#endif diff --git a/Src/Software/Inc/KALMAN.h b/Src/Software/Inc/KALMAN.h new file mode 100644 index 0000000000000000000000000000000000000000..b5fe2fe185b333aa5a96290fe97eabfd4867ecb2 --- /dev/null +++ b/Src/Software/Inc/KALMAN.h @@ -0,0 +1,21 @@ +#ifndef __KALMAN_H__ +#define __KALMAN_H__ +#include "gpio.h" + + +typedef struct + { + float q; // Э + float r; // Э + float x; // ֵ + float p; // Э + float k; // + }KalmanFilter; +extern KalmanFilter kalman_handle; + + void KalmanFilter_Init(KalmanFilter* kf, float q, float r, float initial_value, float initial_p); + float KalmanFilter_Update(KalmanFilter* kf, float measurement); + void KalmanFilter_GET(float *EX,float *R); + +#endif + diff --git a/Src/Software/Inc/KEY.h b/Src/Software/Inc/KEY.h new file mode 100644 index 0000000000000000000000000000000000000000..45c29ee433ef02ed394ee63521497d27b74a07da --- /dev/null +++ b/Src/Software/Inc/KEY.h @@ -0,0 +1,27 @@ +#ifndef __KEY_H +#define __KEY_H + +// ״̬ + +// ȫְ +typedef enum { + KEY_IDLE, // ״̬ + KEY_DEBOUNCE, // ״̬ + KEY_PRESSED, // ȷ״̬ + KEY_LONG_PRESS // ״̬ +} Key_State; + +// ṹ +typedef struct { + Key_State state; // ǰ״̬ + uint32_t press_time; // ʱ +} Key_Handle; + +extern Key_Handle keys[3]; // ʼ3 + +uint8_t Key_Scan(void); +void KEY_GPIO_Init(void); +void KEY_Lignt(void); + + +#endif diff --git a/Src/Software/Inc/OLED.Font.h b/Src/Software/Inc/OLED.Font.h new file mode 100644 index 0000000000000000000000000000000000000000..3573dca2655a8ea56c384c01808095116e7e211f --- /dev/null +++ b/Src/Software/Inc/OLED.Font.h @@ -0,0 +1,293 @@ +#ifndef __OLED_FONT_H +#define __OLED_FONT_H + +/*OLEDģ⣬8أ16*/ +const uint8_t OLED_F8x16[][16]= +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 + + 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1 + + 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 + + 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00, + 0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3 + + 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00, + 0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4 + + 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00, + 0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5 + + 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00, + 0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6 + + 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 + + 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00, + 0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8 + + 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00, + 0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9 + + 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00, + 0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10 + + 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00, + 0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11 + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12 + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13 + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14 + + 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04, + 0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15 + + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00, + 0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16 + + 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17 + + 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00, + 0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18 + + 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00, + 0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19 + + 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00, + 0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20 + + 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00, + 0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21 + + 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00, + 0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22 + + 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00, + 0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23 + + 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00, + 0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24 + + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00, + 0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25 + + 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00, + 0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26 + + 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00, + 0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27 + + 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00, + 0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28 + + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00, + 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29 + + 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00, + 0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30 + + 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00, + 0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31 + + 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00, + 0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32 + + 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00, + 0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33 + + 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00, + 0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34 + + 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00, + 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35 + + 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00, + 0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36 + + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00, + 0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37 + + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00, + 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38 + + 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00, + 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39 + + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08, + 0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40 + + 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00, + 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41 + + 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00, + 0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42 + + 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00, + 0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43 + + 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00, + 0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44 + + 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00, + 0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45 + + 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08, + 0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46 + + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00, + 0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47 + + 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00, + 0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48 + + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00, + 0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49 + + 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00, + 0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50 + + 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00, + 0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51 + + 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00, + 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52 + + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08, + 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53 + + 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08, + 0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54 + + 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00, + 0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55 + + 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08, + 0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56 + + 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00, + 0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57 + + 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00, + 0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58 + + 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00, + 0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59 + + 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60 + + 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00, + 0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61 + + 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 + + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63 + + 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 + + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00, + 0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65 + + 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00, + 0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66 + + 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00, + 0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67 + + 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00, + 0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68 + + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00, + 0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69 + + 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18, + 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70 + + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00, + 0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71 + + 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00, + 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72 + + 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00, + 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73 + + 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00, + 0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74 + + 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00, + 0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75 + + 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00, + 0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76 + + 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00, + 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77 + + 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00, + 0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78 + + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00, + 0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79 + + 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00, + 0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80 + + 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00, + 0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81 + + 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00, + 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82 + + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00, + 0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83 + + 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00, + 0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84 + + 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00, + 0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85 + + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80, + 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86 + + 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80, + 0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87 + + 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00, + 0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88 + + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80, + 0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89 + + 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00, + 0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90 + + 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02, + 0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91 + + 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92 + + 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00, + 0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93 + + 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 +}; + +#endif diff --git a/Src/Software/Inc/OLED.h b/Src/Software/Inc/OLED.h new file mode 100644 index 0000000000000000000000000000000000000000..62455ad8dd8184720598e4b9454b4ad3f09afc01 --- /dev/null +++ b/Src/Software/Inc/OLED.h @@ -0,0 +1,14 @@ +#ifndef __OLED_H +#define __OLED_H + +void OLED_Init(void); +void OLED_Clear(void); +void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char); +void OLED_ShowString(uint8_t Line, uint8_t Column, char *String); +void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length); +void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length); +void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length); +void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length); +void OLED_ShowFloat(uint8_t Line, uint8_t Column,float Number,uint8_t Length); + +#endif diff --git a/Src/Software/Inc/SEG.h b/Src/Software/Inc/SEG.h new file mode 100644 index 0000000000000000000000000000000000000000..570b6e26c11080acec743eb649684d673e913a1f --- /dev/null +++ b/Src/Software/Inc/SEG.h @@ -0,0 +1,32 @@ +#ifndef __SEG_H__ +#define __SEG_H__ +#include "gpio.h" + + + +void TM1637_WriteBit(uint8_t mBit); +void TM1637_WriteByte(uint8_t Byte); + + +//IICʼ +void TM1637_start(void); + +//IICӦ +void TM1637_ack(void); + +//IICֹͣ +void TM1637_stop(void); + +//дݺ +void TM1637_Write(uint8_t DATA); + +//˳ʾ +void TM1637_display(uint8_t One, uint8_t Two,uint8_t Three,uint8_t Fure,uint8_t Point,uint8_t Lignt_degree); + + +#endif + + + + + diff --git a/Src/Software/Inc/gpio.h b/Src/Software/Inc/gpio.h new file mode 100644 index 0000000000000000000000000000000000000000..132949801612bc8313fee1c4154f2620d10dd0b1 --- /dev/null +++ b/Src/Software/Inc/gpio.h @@ -0,0 +1,50 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.h + * @brief This file contains all the function prototypes for + * the gpio.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __GPIO_H__ +#define __GPIO_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ +void Buzzer_HIGN(uint8_t umber); +void MX_GPIO_Init(void); +void Buzzer_Success(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ GPIO_H__ */ + diff --git a/Src/Software/Inc/i2c.h b/Src/Software/Inc/i2c.h new file mode 100644 index 0000000000000000000000000000000000000000..594b75fcfadc38474d95550e91a44461134ba65d --- /dev/null +++ b/Src/Software/Inc/i2c.h @@ -0,0 +1,53 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.h + * @brief This file contains all the function prototypes for + * the i2c.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __I2C_H__ +#define __I2C_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern I2C_HandleTypeDef hi2c1; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +void MX_I2C1_Init(void); +void enable_software_i2c(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __I2C_H__ */ + diff --git a/Src/Software/Inc/main.h b/Src/Software/Inc/main.h new file mode 100644 index 0000000000000000000000000000000000000000..e371ce5608581d9d51293795141c179bad2c7775 --- /dev/null +++ b/Src/Software/Inc/main.h @@ -0,0 +1,69 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.h + * @brief : Header for main.c file. + * This file contains the common defines of the application. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f1xx_hal.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void Error_Handler(void); + +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +/* Private defines -----------------------------------------------------------*/ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +#ifdef __cplusplus +} +#endif + +#endif /* __MAIN_H */ diff --git a/Src/Software/Inc/stm32f1xx_hal_conf.h b/Src/Software/Inc/stm32f1xx_hal_conf.h new file mode 100644 index 0000000000000000000000000000000000000000..46604be1fd8360cc124d7d3ad4e531ca14ddc4fd --- /dev/null +++ b/Src/Software/Inc/stm32f1xx_hal_conf.h @@ -0,0 +1,391 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f1xx_hal_conf.h + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + * Copyright (c) 2017 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F1xx_HAL_CONF_H +#define __STM32F1xx_HAL_CONF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ + +#define HAL_MODULE_ENABLED + /*#define HAL_ADC_MODULE_ENABLED */ +/*#define HAL_CRYP_MODULE_ENABLED */ +/*#define HAL_CAN_MODULE_ENABLED */ +/*#define HAL_CAN_LEGACY_MODULE_ENABLED */ +/*#define HAL_CEC_MODULE_ENABLED */ +/*#define HAL_CORTEX_MODULE_ENABLED */ +/*#define HAL_CRC_MODULE_ENABLED */ +/*#define HAL_DAC_MODULE_ENABLED */ +/*#define HAL_DMA_MODULE_ENABLED */ +/*#define HAL_ETH_MODULE_ENABLED */ +/*#define HAL_FLASH_MODULE_ENABLED */ +#define HAL_GPIO_MODULE_ENABLED +#define HAL_I2C_MODULE_ENABLED +/*#define HAL_I2S_MODULE_ENABLED */ +/*#define HAL_IRDA_MODULE_ENABLED */ +/*#define HAL_IWDG_MODULE_ENABLED */ +/*#define HAL_NOR_MODULE_ENABLED */ +/*#define HAL_NAND_MODULE_ENABLED */ +/*#define HAL_PCCARD_MODULE_ENABLED */ +/*#define HAL_PCD_MODULE_ENABLED */ +/*#define HAL_HCD_MODULE_ENABLED */ +/*#define HAL_PWR_MODULE_ENABLED */ +/*#define HAL_RCC_MODULE_ENABLED */ +/*#define HAL_RTC_MODULE_ENABLED */ +/*#define HAL_SD_MODULE_ENABLED */ +/*#define HAL_MMC_MODULE_ENABLED */ +/*#define HAL_SDRAM_MODULE_ENABLED */ +/*#define HAL_SMARTCARD_MODULE_ENABLED */ +/*#define HAL_SPI_MODULE_ENABLED */ +/*#define HAL_SRAM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED +/*#define HAL_UART_MODULE_ENABLED */ +/*#define HAL_USART_MODULE_ENABLED */ +/*#define HAL_WWDG_MODULE_ENABLED */ + +#define HAL_CORTEX_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +#define HAL_EXTI_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_PWR_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED + +/* ########################## Oscillator Values adaptation ####################*/ +/** + * @brief Adjust the value of External High Speed oscillator (HSE) used in your application. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined (HSE_VALUE) + #define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +#if !defined (HSE_STARTUP_TIMEOUT) + #define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined (HSI_VALUE) + #define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined (LSI_VALUE) + #define LSI_VALUE 40000U /*!< LSI Typical Value in Hz */ +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz + The real value may vary depending on the variations + in voltage and temperature. */ + +/** + * @brief External Low Speed oscillator (LSE) value. + * This value is used by the UART, RTC HAL module to compute the system frequency + */ +#if !defined (LSE_VALUE) + #define LSE_VALUE 32768U /*!< Value of the External oscillator in Hz*/ +#endif /* LSE_VALUE */ + +#if !defined (LSE_STARTUP_TIMEOUT) + #define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ +#define VDD_VALUE 3300U /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY 15U /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0U +#define PREFETCH_ENABLE 1U + +#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */ +#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */ +#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */ +#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */ +#define USE_HAL_ETH_REGISTER_CALLBACKS 0U /* ETH register callback disabled */ +#define USE_HAL_HCD_REGISTER_CALLBACKS 0U /* HCD register callback disabled */ +#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */ +#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */ +#define USE_HAL_MMC_REGISTER_CALLBACKS 0U /* MMC register callback disabled */ +#define USE_HAL_NAND_REGISTER_CALLBACKS 0U /* NAND register callback disabled */ +#define USE_HAL_NOR_REGISTER_CALLBACKS 0U /* NOR register callback disabled */ +#define USE_HAL_PCCARD_REGISTER_CALLBACKS 0U /* PCCARD register callback disabled */ +#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */ +#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */ +#define USE_HAL_SD_REGISTER_CALLBACKS 0U /* SD register callback disabled */ +#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */ +#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */ +#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */ +#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */ +#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */ +#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */ +#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */ +#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */ + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1U */ + +/* ################## Ethernet peripheral configuration ##################### */ + +/* Section 1 : Ethernet peripheral configuration */ + +/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */ +#define MAC_ADDR0 2U +#define MAC_ADDR1 0U +#define MAC_ADDR2 0U +#define MAC_ADDR3 0U +#define MAC_ADDR4 0U +#define MAC_ADDR5 0U + +/* Definition of the Ethernet driver buffers size and count */ +#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */ +#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */ +#define ETH_RXBUFNB 8U /* 4 Rx buffers of size ETH_RX_BUF_SIZE */ +#define ETH_TXBUFNB 4U /* 4 Tx buffers of size ETH_TX_BUF_SIZE */ + +/* Section 2: PHY configuration section */ + +/* DP83848_PHY_ADDRESS Address*/ +#define DP83848_PHY_ADDRESS 0x01U +/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/ +#define PHY_RESET_DELAY 0x000000FFU +/* PHY Configuration delay */ +#define PHY_CONFIG_DELAY 0x00000FFFU + +#define PHY_READ_TO 0x0000FFFFU +#define PHY_WRITE_TO 0x0000FFFFU + +/* Section 3: Common PHY Registers */ + +#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */ +#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */ + +#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */ +#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */ +#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */ +#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */ +#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */ +#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */ +#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */ +#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */ +#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */ +#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */ + +#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */ +#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */ +#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */ + +/* Section 4: Extended PHY Registers */ +#define PHY_SR ((uint16_t)0x10U) /*!< PHY status register Offset */ + +#define PHY_SPEED_STATUS ((uint16_t)0x0002U) /*!< PHY Speed mask */ +#define PHY_DUPLEX_STATUS ((uint16_t)0x0004U) /*!< PHY Duplex mask */ + +/* ################## SPI peripheral configuration ########################## */ + +/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver +* Activated: CRC code is present inside driver +* Deactivated: CRC code cleaned from driver +*/ + +#define USE_SPI_CRC 0U + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED +#include "stm32f1xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED +#include "stm32f1xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_EXTI_MODULE_ENABLED +#include "stm32f1xx_hal_exti.h" +#endif /* HAL_EXTI_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED +#include "stm32f1xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_ETH_MODULE_ENABLED +#include "stm32f1xx_hal_eth.h" +#endif /* HAL_ETH_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED +#include "stm32f1xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CAN_LEGACY_MODULE_ENABLED + #include "Legacy/stm32f1xx_hal_can_legacy.h" +#endif /* HAL_CAN_LEGACY_MODULE_ENABLED */ + +#ifdef HAL_CEC_MODULE_ENABLED +#include "stm32f1xx_hal_cec.h" +#endif /* HAL_CEC_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED +#include "stm32f1xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED +#include "stm32f1xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED +#include "stm32f1xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED +#include "stm32f1xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED +#include "stm32f1xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED +#include "stm32f1xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED +#include "stm32f1xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED +#include "stm32f1xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED +#include "stm32f1xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED +#include "stm32f1xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED +#include "stm32f1xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED +#include "stm32f1xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED +#include "stm32f1xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_SD_MODULE_ENABLED +#include "stm32f1xx_hal_sd.h" +#endif /* HAL_SD_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED +#include "stm32f1xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED +#include "stm32f1xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED +#include "stm32f1xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED +#include "stm32f1xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED +#include "stm32f1xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED +#include "stm32f1xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED +#include "stm32f1xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED +#include "stm32f1xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED +#include "stm32f1xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_HCD_MODULE_ENABLED +#include "stm32f1xx_hal_hcd.h" +#endif /* HAL_HCD_MODULE_ENABLED */ + +#ifdef HAL_MMC_MODULE_ENABLED +#include "stm32f1xx_hal_mmc.h" +#endif /* HAL_MMC_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#ifdef USE_FULL_ASSERT +/** + * @brief The assert_param macro is used for function's parameters check. + * @param expr If expr is false, it calls assert_failed function + * which reports the name of the source file and the source + * line number of the call that failed. + * If expr is true, it returns no value. + * @retval None + */ +#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t* file, uint32_t line); +#else +#define assert_param(expr) ((void)0U) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F1xx_HAL_CONF_H */ + diff --git a/Src/Software/Inc/stm32f1xx_it.h b/Src/Software/Inc/stm32f1xx_it.h new file mode 100644 index 0000000000000000000000000000000000000000..51fc94c564657b83e0c423696594e306d904eeec --- /dev/null +++ b/Src/Software/Inc/stm32f1xx_it.h @@ -0,0 +1,66 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f1xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F1xx_IT_H +#define __STM32F1xx_IT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Exported types ------------------------------------------------------------*/ +/* USER CODE BEGIN ET */ + +/* USER CODE END ET */ + +/* Exported constants --------------------------------------------------------*/ +/* USER CODE BEGIN EC */ + +/* USER CODE END EC */ + +/* Exported macro ------------------------------------------------------------*/ +/* USER CODE BEGIN EM */ + +/* USER CODE END EM */ + +/* Exported functions prototypes ---------------------------------------------*/ +void NMI_Handler(void); +void HardFault_Handler(void); +void MemManage_Handler(void); +void BusFault_Handler(void); +void UsageFault_Handler(void); +void SVC_Handler(void); +void DebugMon_Handler(void); +void PendSV_Handler(void); +void SysTick_Handler(void); +/* USER CODE BEGIN EFP */ + +/* USER CODE END EFP */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F1xx_IT_H */ diff --git a/Src/Software/Inc/tim.h b/Src/Software/Inc/tim.h new file mode 100644 index 0000000000000000000000000000000000000000..129fecea2a228d459e2a249060893c45e343b9f6 --- /dev/null +++ b/Src/Software/Inc/tim.h @@ -0,0 +1,76 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file tim.h + * @brief This file contains all the function prototypes for + * the tim.c file + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __TIM_H__ +#define __TIM_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ +typedef enum { + MODE_PRECISE, // ׼ģʽ(+) + MODE_ROUGH // ģʽ(+) +} TimerMode; + typedef struct { + uint8_t min_ten; // ֵʮλ + uint8_t min_unit; // ֵĸλ + uint8_t sec_ten; // ʮλ + uint8_t sec_unit; // ĸλ + uint8_t ms_hundred; // İλ + uint8_t ms_ten; // ʮλ +} TimeDigits; +extern uint16_t data; + +extern TIM_HandleTypeDef htim1; +extern TIM_HandleTypeDef htim2; +extern TIM_HandleTypeDef htim3; +extern TIM_HandleTypeDef htim4; +extern TimeDigits digit; +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ +void MX_TIM3_Init(void); +void DWT_Init(void); +void MX_TIM2_Init(void); +void SplitTimeDigits(TimeDigits *digits); +void HAL_Delay_us(uint32_t delay_us) ; +void MX_TIM4_Init(void); +void SwitchMode(TimerMode newMode); +void ResetTimer(void); +void MX_TIM1_Init(void); +void HAL_TIM_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif + +#endif /* __TIM_H__ */ + diff --git a/Src/Software/PWM_CH1.uvprojx b/Src/Software/PWM_CH1.uvprojx new file mode 100644 index 0000000000000000000000000000000000000000..2124cab29b3ba67e35e17b1da36f8b01ea2087f8 --- /dev/null +++ b/Src/Software/PWM_CH1.uvprojx @@ -0,0 +1,695 @@ + + + + 2.1 + +
### uVision Project, (C) Keil Software
+ + + + PWM_CH1 + 0x4 + ARM-ADS + 5060528::V5.06 update 5 (build 528)::ARMCC + 0 + + + STM32F103C8 + STMicroelectronics + Keil.STM32F1xx_DFP.2.2.0 + http://www.keil.com/pack/ + IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE + + + UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128 -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM)) + 0 + $$Device:STM32F103C8$Device\Include\stm32f10x.h + + + + + + + + + + $$Device:STM32F103C8$SVD\STM32F103xx.svd + 0 + 0 + + + + + + + 0 + 0 + 0 + 0 + 1 + + PWM_CH1\ + PWM_CH1 + 1 + 0 + 1 + 1 + 1 + + 1 + 0 + 0 + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 0 + + + 0 + 0 + 0 + 0 + + + 0 + 1 + + + 0 + 0 + 0 + 0 + + 1 + + + + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 3 + + + 0 + + + SARMCM3.DLL + -REMAP + DCM.DLL + -pCM3 + SARMCM3.DLL + + TCM.DLL + -pCM3 + + + + 1 + 0 + 0 + 0 + 16 + + + + + 1 + 0 + 0 + 1 + 1 + 4096 + + 1 + BIN\UL2CM3.DLL + "" () + + + + + 0 + + + + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 1 + 1 + 0 + 1 + 1 + 0 + 0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 0 + 0 + "Cortex-M3" + + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 8 + 0 + 0 + 0 + 0 + 3 + 3 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 1 + 0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0x5000 + + + 1 + 0x8000000 + 0x10000 + + + 0 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x0 + 0x0 + + + 1 + 0x8000000 + 0x10000 + + + 1 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x0 + 0x0 + + + 0 + 0x20000000 + 0x5000 + + + 0 + 0x0 + 0x0 + + + + + + 1 + 4 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 2 + 0 + 0 + 1 + 0 + 5 + 3 + 1 + 1 + 0 + 0 + 0 + + + USE_HAL_DRIVER,STM32F103xB + + ../Core/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include + + + + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + + + + + + + 1 + 0 + 0 + 0 + 1 + 0 + + + + + + + + + + + + + + + Application/MDK-ARM + + + startup_stm32f103xb.s + 2 + startup_stm32f103xb.s + + + + + Application/User/Core + + + OLED.Font.h + 5 + ..\Core\Inc\OLED.Font.h + + + OLED.h + 5 + ..\Core\Inc\OLED.h + + + OELD.c + 1 + ..\Core\Src\OELD.c + + + KEY.c + 1 + ..\Core\Src\KEY.c + + + KEY.h + 5 + ..\Core\Inc\KEY.h + + + CSB.c + 1 + ..\Core\Src\CSB.c + + + CSB.h + 5 + ..\Core\Inc\CSB.h + + + KALMAN.c + 1 + ..\Core\Src\KALMAN.c + + + KALMAN.h + 5 + ..\Core\Inc\KALMAN.h + + + SEG.c + 1 + ..\Core\Src\SEG.c + + + SEG.h + 5 + ..\Core\Inc\SEG.h + + + main.c + 1 + ../Core/Src/main.c + + + gpio.c + 1 + ../Core/Src/gpio.c + + + tim.c + 1 + ../Core/Src/tim.c + + + stm32f1xx_it.c + 1 + ../Core/Src/stm32f1xx_it.c + + + stm32f1xx_hal_msp.c + 1 + ../Core/Src/stm32f1xx_hal_msp.c + + + i2c.c + 1 + ../Core/Src/i2c.c + + + 2 + 0 + 0 + 0 + 0 + 1 + 2 + 2 + 2 + 2 + 11 + + + 1 + + + + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 0 + 2 + 2 + 2 + 2 + 0 + 0 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + + + + Base_I2C.c + 1 + ..\Core\Src\Base_I2C.c + + + Base_I2C.h + 5 + ..\Core\Inc\Base_I2C.h + + + + + Drivers/STM32F1xx_HAL_Driver + + + stm32f1xx_hal_gpio_ex.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c + + + stm32f1xx_hal_i2c.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c + + + 2 + 0 + 0 + 0 + 0 + 1 + 2 + 2 + 2 + 2 + 11 + + + 1 + + + + 2 + 0 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 0 + 2 + 2 + 2 + 2 + 0 + 0 + 2 + 2 + 2 + 2 + 2 + + + + + + + + + + + + stm32f1xx_hal.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c + + + stm32f1xx_hal_rcc.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c + + + stm32f1xx_hal_rcc_ex.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c + + + stm32f1xx_hal_gpio.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c + + + stm32f1xx_hal_dma.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c + + + stm32f1xx_hal_cortex.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c + + + stm32f1xx_hal_pwr.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c + + + stm32f1xx_hal_flash.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c + + + stm32f1xx_hal_flash_ex.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c + + + stm32f1xx_hal_exti.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_exti.c + + + stm32f1xx_hal_tim.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c + + + stm32f1xx_hal_tim_ex.c + 1 + ../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c + + + + + Drivers/CMSIS + + + system_stm32f1xx.c + 1 + ../Core/Src/system_stm32f1xx.c + + + + + ::CMSIS + + + + + + + + + + + + + + + + + + +
diff --git a/Src/Software/Src/Base_I2C.c b/Src/Software/Src/Base_I2C.c new file mode 100644 index 0000000000000000000000000000000000000000..85c4e1242d06d0efae3bc9415cff146d68c38a74 --- /dev/null +++ b/Src/Software/Src/Base_I2C.c @@ -0,0 +1,12 @@ +#include "Base_I2C.h" +#include "i2c.h" + +HAL_StatusTypeDef Base_I2C_WriteCmd(uint8_t* hi2cx, uint16_t Adr, uint8_t* TxData, uint8_t Len, uint16_t Timeout) +{ + return HAL_I2C_Master_Transmit((I2C_HandleTypeDef*)hi2cx, Adr, (uint8_t*)TxData, Len, Timeout); +} + +HAL_StatusTypeDef Base_I2C_ReadCmd(uint8_t* hi2cx, uint16_t Adr, uint8_t* RxData, uint8_t Len, uint16_t Timeout) +{ + return HAL_I2C_Master_Receive((I2C_HandleTypeDef*)hi2cx, Adr, (uint8_t*)RxData, Len, Timeout); +} diff --git a/Src/Software/Src/CSB.c b/Src/Software/Src/CSB.c new file mode 100644 index 0000000000000000000000000000000000000000..cffe8685ad75095f7f61eb0246a3e3afa61ddd78 --- /dev/null +++ b/Src/Software/Src/CSB.c @@ -0,0 +1,55 @@ + +#include "tim.h" + + + +void HC_SR04_W_Trig(uint8_t BitValue) +{ + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,BitValue); + +} +uint8_t HC_SR04_R_Echo(void) +{ + return HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_5); +} + GPIO_InitTypeDef CSB_GPIO_InitStruct = {0}; + +void HC_SR04_Init(void) +{ + + __HAL_RCC_GPIOB_CLK_ENABLE(); + CSB_GPIO_InitStruct.Pin = GPIO_PIN_4; + CSB_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + CSB_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + + HAL_GPIO_Init(GPIOB, &CSB_GPIO_InitStruct); + + CSB_GPIO_InitStruct.Pin = GPIO_PIN_5; + CSB_GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + CSB_GPIO_InitStruct.Pull = GPIO_PULLUP; // ʹ + CSB_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + + HAL_GPIO_Init(GPIOB, &CSB_GPIO_InitStruct); + // KEY2 (PA1) + + + +} +float HC_SR04_Distance(void) +{ + while(HC_SR04_R_Echo()==1); + HC_SR04_W_Trig(1); + HAL_Delay_us(9); + HC_SR04_W_Trig(0); + while(HC_SR04_R_Echo()==0); + __HAL_TIM_SET_COUNTER(&htim2,0); + HAL_TIM_Base_Start(&htim2); + while(HC_SR04_R_Echo()==1) + {}; + HAL_TIM_Base_Stop(&htim2); + + return (__HAL_TIM_GET_COUNTER(&htim2))*340*100/1000000/2; +} + + +// ⺯ڵã diff --git a/Src/Software/Src/KALMAN.c b/Src/Software/Src/KALMAN.c new file mode 100644 index 0000000000000000000000000000000000000000..e75a8a8f5a5d80ee2f7b727a88c7b492f5a05051 --- /dev/null +++ b/Src/Software/Src/KALMAN.c @@ -0,0 +1,46 @@ +#include "kalman.h" +#include "tim.h" +#include "csb.h" +#include "OLED.h" + +KalmanFilter kalman_handle={0}; +uint16_t adc[100]; + +void KalmanFilter_GET(float *EX,float *R) + { + for(int i = 0; i < 100; i++) + { + adc[i]=HC_SR04_Distance(); + *EX += adc[i]; //Rζ˲ֵԤ⣬ʱͲʺô˷óR + HAL_Delay(5); + } + *EX /= 100; + for(int i = 0; i < 100; i++) + { + *R += (adc[i] - *EX) * (adc[i] - *EX); + } + *R/= 100; + } +void KalmanFilter_Init(KalmanFilter* kf, float q, float r, float initial_value, float initial_p) + { + + kf->q = q; + kf->r = r; + kf->x = initial_value; + kf->p = initial_p; + kf->k = 0; + } + +float KalmanFilter_Update(KalmanFilter* kf, float measurement) + { + kf->p = kf->p + kf->q; // Ԥⲽ + kf->k = kf->p / (kf->p + kf->r); // + kf->x = kf->x + kf->k * (measurement - kf->x); //״̬ + kf->p = (1 - kf->k) * kf->p; return kf->x; //P + } + + + + + + \ No newline at end of file diff --git a/Src/Software/Src/KEY.c b/Src/Software/Src/KEY.c new file mode 100644 index 0000000000000000000000000000000000000000..de9dac5910fa82ef63fefa11860985485ec9ee91 --- /dev/null +++ b/Src/Software/Src/KEY.c @@ -0,0 +1,83 @@ +#include "stm32f1xx_hal.h" +#include "KEY.h" +#define KEY1_PIN GPIO_PIN_5 +#define KEY2_PIN GPIO_PIN_6 +#define KEY3_PIN GPIO_PIN_7 +#define KEY_PORT GPIOA + +void KEY_GPIO_Init(void) +{ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + // ʹGPIOʱ + __HAL_RCC_GPIOA_CLK_ENABLE(); + + // KEY1 (PA0) + GPIO_InitStruct.Pin = KEY1_PIN|KEY2_PIN|KEY3_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_PULLUP; // ʹ + HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct); + + + GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct); + HAL_GPIO_WritePin(KEY_PORT, GPIO_PIN_4,GPIO_PIN_SET); + + // KEY2 (PA1) + +} + +void KEY_Lignt(void) +{ + HAL_GPIO_TogglePin(KEY_PORT,GPIO_PIN_4); +} + + + +Key_Handle keys[3] = {0}; // ʼ3 +// ״̬ + +// ״̬ö + +uint8_t Key_Scan(void) { + uint8_t result = 0; + const uint16_t key_pins[3] = {KEY1_PIN, KEY2_PIN, KEY3_PIN}; + + for (uint8_t i = 0; i < 3; i++) { + switch (keys[i].state) { + case KEY_IDLE: + if (HAL_GPIO_ReadPin(KEY_PORT, key_pins[i]) == GPIO_PIN_RESET) { + keys[i].state = KEY_DEBOUNCE; + keys[i].press_time = HAL_GetTick(); + } + break; + + case KEY_DEBOUNCE: + if (HAL_GPIO_ReadPin(KEY_PORT, key_pins[i]) == GPIO_PIN_SET) { + keys[i].state = KEY_IDLE; + } else if (HAL_GetTick() - keys[i].press_time >= 20) { // 20ms + keys[i].state = KEY_PRESSED; + } + break; + + case KEY_PRESSED: + if (HAL_GPIO_ReadPin(KEY_PORT, key_pins[i]) == GPIO_PIN_SET) { + result |= (1 << i); // ̰1/2/4 + keys[i].state = KEY_IDLE; + } else if (i == 0 && (HAL_GetTick() - keys[i].press_time >= 1000)) { + result |= 0x08; // KEY18 + keys[i].state = KEY_LONG_PRESS; + } + break; + + case KEY_LONG_PRESS: + if (HAL_GPIO_ReadPin(KEY_PORT, key_pins[i]) == GPIO_PIN_SET) { + keys[i].state = KEY_IDLE; + } + break; + } + } + return result; +} diff --git a/Src/Software/Src/OELD.c b/Src/Software/Src/OELD.c new file mode 100644 index 0000000000000000000000000000000000000000..791c703045d3b6a706d026965c5ac2b1d05d84be --- /dev/null +++ b/Src/Software/Src/OELD.c @@ -0,0 +1,351 @@ +#include "stm32f1xx_hal.h" +#include "OLED.Font.h" +//#include "OLED_Font.h" +#include "OLED.h" +#include "math.h" + +/**/ + +GPIO_InitTypeDef gpio_init_struct={0}; +#define OLED_W_SCL(x) HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, (GPIO_PinState)(x) ) +#define OLED_W_SDA(x) HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, (GPIO_PinState)(x) ) + + +/*ųʼ*/ +void OLED_I2C_Init(void) +{ + __HAL_RCC_GPIOB_CLK_ENABLE(); + + gpio_init_struct.Pin = GPIO_PIN_8; + gpio_init_struct.Mode = GPIO_MODE_OUTPUT_OD; /* ʽ */ + gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH; /* */ + gpio_init_struct.Pull = GPIO_PULLUP; /* */ + + HAL_GPIO_Init(GPIOB, &gpio_init_struct); /* ʼ1TX */ + + gpio_init_struct.Pin = GPIO_PIN_9; + gpio_init_struct.Mode = GPIO_MODE_OUTPUT_OD; /* */ + HAL_GPIO_Init(GPIOB, &gpio_init_struct); /* ʼ1RX */ + OLED_W_SCL(1); + OLED_W_SDA(1); +} + +/** + * @brief I2Cʼ + * @param + * @retval + */ +void OLED_I2C_Start(void) +{ + OLED_W_SDA(1); + OLED_W_SCL(1); + OLED_W_SDA(0); + OLED_W_SCL(0); +} + +/** + * @brief I2Cֹͣ + * @param + * @retval + */ +void OLED_I2C_Stop(void) +{ + OLED_W_SDA(0); + OLED_W_SCL(1); + OLED_W_SDA(1); +} + +/** + * @brief I2Cһֽ + * @param Byte Ҫ͵һֽ + * @retval + */ +void OLED_I2C_SendByte(uint8_t Byte) +{ + uint8_t i; + for (i = 0; i < 8; i++) + { + OLED_W_SDA(Byte & (0x80 >> i)); + OLED_W_SCL(1); + OLED_W_SCL(0); + } + OLED_W_SCL(1); //һʱӣӦź + OLED_W_SCL(0); +} + +/** + * @brief OLEDд + * @param Command Ҫд + * @retval + */ +void OLED_WriteCommand(uint8_t Command) +{ + OLED_I2C_Start(); + OLED_I2C_SendByte(0x78); //ӻַ + OLED_I2C_SendByte(0x00); //д + OLED_I2C_SendByte(Command); + OLED_I2C_Stop(); +} + +/** + * @brief OLEDд + * @param Data Ҫд + * @retval + */ +void OLED_WriteData(uint8_t Data) +{ + OLED_I2C_Start(); + OLED_I2C_SendByte(0x78); //ӻַ + OLED_I2C_SendByte(0x40); //д + OLED_I2C_SendByte(Data); + OLED_I2C_Stop(); +} + +/** + * @brief OLEDùλ + * @param Y ϽΪԭ㣬·꣬Χ0~7 + * @param X ϽΪԭ㣬ҷ꣬Χ0~127 + * @retval + */ +void OLED_SetCursor(uint8_t Y, uint8_t X) +{ + OLED_WriteCommand(0xB0 | Y); //Yλ + OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //Xλø4λ + OLED_WriteCommand(0x00 | (X & 0x0F)); //Xλõ4λ +} + +/** + * @brief OLED + * @param + * @retval + */ +void OLED_Clear(void) +{ + uint8_t i, j; + for (j = 0; j < 8; j++) + { + OLED_SetCursor(j, 0); + for(i = 0; i < 128; i++) + { + OLED_WriteData(0x00); + } + } +} + +/** + * @brief OLEDʾһַ + * @param Line λãΧ1~4 + * @param Column λãΧ1~16 + * @param Char ҪʾһַΧASCIIɼַ + * @retval + */ +void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char) +{ + uint8_t i; + OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //ùλϰ벿 + for (i = 0; i < 8; i++) + { + OLED_WriteData(OLED_F8x16[Char - ' '][i]); //ʾϰ벿 + } + OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //ùλ°벿 + for (i = 0; i < 8; i++) + { + OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //ʾ°벿 + } +} + +/** + * @brief OLEDʾַ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param String ҪʾַΧASCIIɼַ + * @retval + */ +void OLED_ShowString(uint8_t Line, uint8_t Column, char *String) +{ + uint8_t i; + for (i = 0; String[i] != '\0'; i++) + { + OLED_ShowChar(Line, Column + i, String[i]); + } +} + +/** + * @brief OLEDη + * @retval ֵXYη + */ +uint32_t OLED_Pow(uint32_t X, uint32_t Y) +{ + uint32_t Result = 1; + while (Y--) + { + Result *= X; + } + return Result; +} + +/** + * @brief OLEDʾ֣ʮƣ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param Number Ҫʾ֣Χ0~4294967295 + * @param Length ҪʾֵijȣΧ1~10 + * @retval + */ +void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) +{ + uint8_t i; + for (i = 0; i < Length; i++) + { + OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0'); + } +} + +/** + * @brief OLEDʾ֣ʮƣ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param Number Ҫʾ֣Χ-2147483648~2147483647 + * @param Length ҪʾֵijȣΧ1~10 + * @retval + */ +void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length) +{ + uint8_t i; + uint32_t Number1; + if (Number >= 0) + { + OLED_ShowChar(Line, Column, '+'); + Number1 = Number; + } + else + { + OLED_ShowChar(Line, Column, '-'); + Number1 = -Number; + } + for (i = 0; i < Length; i++) + { + OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0'); + } +} + +/** + * @brief OLEDʾ֣ʮƣ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param Number Ҫʾ֣Χ0~0xFFFFFFFF + * @param Length ҪʾֵijȣΧ1~8 + * @retval + */ +void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) +{ + uint8_t i, SingleNumber; + for (i = 0; i < Length; i++) + { + SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16; + if (SingleNumber < 10) + { + OLED_ShowChar(Line, Column + i, SingleNumber + '0'); + } + else + { + OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A'); + } + } +} + +/** + * @brief OLEDʾ֣ƣ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param Number Ҫʾ֣Χ0~1111 1111 1111 1111 + * @param Length ҪʾֵijȣΧ1~16 + * @retval + */ +void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length) +{ + uint8_t i; + for (i = 0; i < Length; i++) + { + OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0'); + } +} +/** + * @brief OLEDʾ֣ƣ + * @param Line ʼλãΧ1~4 + * @param Column ʼλãΧ1~16 + * @param Number ҪʾСΧ0~1111 1111 1111 1111 + * @param Length ҪС + * @retval + */ +void OLED_ShowFloat(uint8_t Line, uint8_t Column,float Number,uint8_t Length) +{ + uint8_t i=0,j,Number1; + float Number2; + Number1=Number; //Number1=2 + i=(int)log10(Number1) + 1; + OLED_ShowNum(Line,Column,Number,i+1); //i=1 + OLED_ShowChar(Line,Column+i+1,'.'); + Number2=Number-Number1; //Number2=0.1243 + for(j=0;j>loop) & 0x01);//дλ + } + SEG_W_CLK(0); + HAL_Delay_us(200); + SEG_W_DIO(1); + HAL_Delay_us(200); + SEG_W_CLK(1); + HAL_Delay_us(200); + while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == 0x01); + //ȴӦź +} + + + //IICʼ +void TM1637_start(void) +{ + SEG_W_CLK(1); + SEG_W_DIO(1); + HAL_Delay_us(10); + SEG_W_DIO(0); +} + + +//IICӦ +void TM1637_ack(void) +{ + uint8_t i; + SEG_W_CLK(0); + HAL_Delay_us(10); + + while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_7) == 0x01&&(i<250)) + i++; + + SEG_W_CLK(1); + + HAL_Delay_us(10); + + SEG_W_CLK(0); +} + +//IICֹͣ +void TM1637_stop(void) +{ + SEG_W_CLK(0); + HAL_Delay_us(10); + SEG_W_DIO(0); + HAL_Delay_us(10); + SEG_W_CLK(1); + HAL_Delay_us(10); + SEG_W_DIO(1); + HAL_Delay_us(10); +} + +//дݺ +void TM1637_Write(uint8_t DATA) +{ + unsigned char i; + for(i=0;i<8;i++) + { + SEG_W_CLK(0); + if(DATA & 0x01) + SEG_W_DIO(1); + else + SEG_W_DIO(0); + + HAL_Delay_us(10); + + DATA=DATA>>1; + SEG_W_CLK(1); + + HAL_Delay_us(10); + } +} +/** + * @brief ʾ + * @param aΪһ() + * @param bΪڶ() + * @param cΪ() + * @param dΪĸ() + * @param hΪ1ʾΪ0ʾ + * @retval + */ +void TM1637_display(uint8_t One, uint8_t Two,uint8_t Three,uint8_t Fure,uint8_t Point,uint8_t Lignt_degree) +{ + TM1637_start(); + TM1637_Write(0x40);//д+Զַ1+ͨģʽ + TM1637_ack(); + TM1637_stop(); + + TM1637_start(); + + TM1637_Write(0xc0);//ʾ׵ַһLED + TM1637_ack(); + + TM1637_Write(tab[One]); + TM1637_ack(); + + TM1637_Write(tab[Two]|Point<<7);//hΪ1ʱʾʱм + TM1637_ack(); + + TM1637_Write(tab[Three]); + TM1637_ack(); + + TM1637_Write(tab[Fure]); + TM1637_ack(); + + TM1637_stop(); + + TM1637_start(); + TM1637_Write(0x89+Lignt_degree);//ʾ0x89 2/16 0x8F 14/16 + TM1637_ack(); + TM1637_stop(); +} + + + diff --git a/Src/Software/Src/gpio.c b/Src/Software/Src/gpio.c new file mode 100644 index 0000000000000000000000000000000000000000..3cfa5091bf0e78569ec497ff395cce86c4442c3d --- /dev/null +++ b/Src/Software/Src/gpio.c @@ -0,0 +1,90 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file gpio.c + * @brief This file provides code for the configuration + * of all used GPIO pins. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "gpio.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/*----------------------------------------------------------------------------*/ +/* Configure GPIO */ +/*----------------------------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** Configure pins as + * Analog + * Input + * Output + * EVENT_OUT + * EXTI +*/ +GPIO_InitTypeDef BZ_GPIO_InitStruct = {0}; + +void MX_GPIO_Init(void) +{ + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + BZ_GPIO_InitStruct.Pin = GPIO_PIN_15; + BZ_GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + BZ_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + + HAL_GPIO_Init(GPIOB, &BZ_GPIO_InitStruct); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET); + // KEY2 (PA1) + +} + + +void Buzzer_HIGN(uint8_t umber) +{ + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET); + HAL_Delay(50*umber); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET); + + +} +void Buzzer_Success(void) +{ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET); + HAL_Delay(300); + + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET); + HAL_Delay(100); + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET);} +/* USER CODE BEGIN 2 */ + +/* USER CODE END 2 */ diff --git a/Src/Software/Src/i2c.c b/Src/Software/Src/i2c.c new file mode 100644 index 0000000000000000000000000000000000000000..73017bd819e0c883bb9f7feaded309da0eab2e0d --- /dev/null +++ b/Src/Software/Src/i2c.c @@ -0,0 +1,129 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file i2c.c + * @brief This file provides code for the configuration + * of the I2C instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Includes ------------------------------------------------------------------*/ +#include "i2c.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +I2C_HandleTypeDef hi2c1; + +/* I2C1 init function */ +void MX_I2C1_Init(void) +{ + + /* USER CODE BEGIN I2C1_Init 0 */ + + /* USER CODE END I2C1_Init 0 */ + + /* USER CODE BEGIN I2C1_Init 1 */ + + /* USER CODE END I2C1_Init 1 */ + hi2c1.Instance = I2C1; + hi2c1.Init.ClockSpeed = 100000; + hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; //ʱռձѡĬϼ70% + hi2c1.Init.OwnAddress1 = 0; //豸һַ + hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + hi2c1.Init.OwnAddress2 = 0; //豸ڶַ + hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; + if (HAL_I2C_Init(&hi2c1) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN I2C1_Init 2 */ + + /* USER CODE END I2C1_Init 2 */ + +} + +void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspInit 0 */ + + /* USER CODE END I2C1_MspInit 0 */ + + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**I2C1 GPIO Configuration + PB6 ------> I2C1_SCL + PB7 ------> I2C1_SDA + */ + GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7; //Pin6SCL + GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* I2C1 clock enable */ + __HAL_RCC_I2C1_CLK_ENABLE(); + /* USER CODE BEGIN I2C1_MspInit 1 */ + + /* USER CODE END I2C1_MspInit 1 */ + } +} + +void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle) +{ + + if(i2cHandle->Instance==I2C1) + { + /* USER CODE BEGIN I2C1_MspDeInit 0 */ + + /* USER CODE END I2C1_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_I2C1_CLK_DISABLE(); + + /**I2C1 GPIO Configuration + PB6 ------> I2C1_SCL + PB7 ------> I2C1_SDA + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6); + + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7); + + /* USER CODE BEGIN I2C1_MspDeInit 1 */ + + /* USER CODE END I2C1_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ +void enable_software_i2c(void) + { + // HAL_I2C_DeInit(&hi2c1); // رӲI2C + __HAL_RCC_GPIOB_CLK_ENABLE(); + GPIO_InitTypeDef GPIO_InitStruct = {0}; + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; // © + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6|GPIO_PIN_7,GPIO_PIN_SET); +} + + diff --git a/Src/Software/Src/main.c b/Src/Software/Src/main.c new file mode 100644 index 0000000000000000000000000000000000000000..ad0e7b5d9a3795cf69cdd2a1237d4b119022ad04 --- /dev/null +++ b/Src/Software/Src/main.c @@ -0,0 +1,363 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file : main.c + * @brief : Main program body + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "i2c.h" +#include "tim.h" +#include "gpio.h" +#include "i2c.h" +#include "CSB.h" +#include "kalman.h" +#include "oled.h" +#include "SEG.h" +#include "KEY.h" + +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN PTD */ + +/* USER CODE END PTD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ + +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +void SystemClock_Config(void); +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * @brief The application entry point. + * @retval int + */ + + typedef enum { + IDLE, // 空闲状态(允许检测) + FIRST_TRIGGERED, // 首次触发(等待二次触发) + COOLDOWN , // 冷却中(1秒内禁止检测) + SECOND_STOPPED, // 第二次触发完成 + DELAY_AFTER_RESET, // 复位后的延迟状态 + DEAD_ZONE // 按键清零后的死区状态 + } State; + + State distance_state = IDLE; + uint32_t last_trigger_time = 0; // 最后一次触发的时间戳(单位ms) + float Init_X=0,Init_R=0; + KalmanFilter KRM_distance; + uint16_t CSB_Flag=0; + uint32_t first_trigger_time = 0; + uint32_t measured_interval = 0; + uint8_t time_flag=0; + extern uint16_t Filter_DATA; + uint32_t powerOnTime = 0; // 记录上电时间 + uint16_t isSystemStable = 0; // 系统是否已稳定(3秒后) + uint32_t reset_time = 0; // 记录复位时间 + + uint32_t second_stop_time = 0; // 记录第二次触发后的时间 + uint8_t is_obstructed = 0; // 当前是否被遮挡 + uint32_t dead_zone_start_time=0; +int main(void) +{ + + /* USER CODE BEGIN 1 */ + + /* USER CODE END 1 */ + + /* MCU Configuration--------------------------------------------------------*/ + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. *///功能1;手动清零,功能2:延迟 + HAL_Init(); + + /* USER CODE BEGIN Init */ + + /* USER CODE END Init */ + + /* Configure the system clock */ + SystemClock_Config(); + + /* USER CODE BEGIN SysInit */ + + /* USER CODE END SysInit */ + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + /* USER CODE BEGIN 2 */ + OLED_Init(); + /* USER CODE END 2 */ + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + KEY_GPIO_Init(); + HC_SR04_Init(); + HAL_TIM_Init(); + KalmanFilter_Init(&KRM_distance,0.1,2,200,0); + uint16_t c=0,lignt_D=0,TR_flag=1; + enable_software_i2c(); + TM1637_display(0,0,0,0,1, 0); + + + while(1) { + uint32_t current_time = HAL_GetTick(); + uint8_t key_val = Key_Scan(); + + // 上电3秒内不处理距离检测 + if (!isSystemStable) { + if (current_time >= 3000) { // 3秒后标记系统稳定 + isSystemStable = 1; + powerOnTime = current_time; // 记录稳定时间(可选) + + } + else { + // 3秒内只执行其他任务(如按键检测、显示初始化等) + continue; // 跳过后续距离检测逻辑 + } + } + + if(key_val & 0x01) + { + //清零并停止计时器 + // 按键1按下事件 + ResetTimer(); + TR_flag=1; + distance_state = DEAD_ZONE; // 进入死区状态 Buzzer_HIGN(1); + dead_zone_start_time = current_time; + last_trigger_time=0; + time_flag = 0; // 新增:重置超时标志 + Buzzer_HIGN(1); + + } + + if(key_val & 0x02) + { //控制亮度 + // 按键2按下事件 + lignt_D++; + if(lignt_D>=4) + {lignt_D=0;} + + } + if(key_val & 0x04) + { + } + // 状态机处理 + switch (distance_state) { + // ... 原有IDLE和FIRST_TRIGGERED逻辑 + case DEAD_ZONE: + if (current_time - dead_zone_start_time >= 2000) { + distance_state = IDLE; // 死区结束 + } + break; + case DELAY_AFTER_RESET: //进入复位系统 + // 等待1秒后再回到IDLE状态 + if (current_time - reset_time >= 2000) { + distance_state = IDLE; + + } + break; + case SECOND_STOPPED: + // 如果遮挡持续2秒及以上,复位系统 + if (is_obstructed) { + if (current_time - second_stop_time >= 2000) { + ResetTimer(); + TR_flag=1; + Buzzer_HIGN(10); + last_trigger_time=0; + time_flag = 0; // 新增:重置超时标志 + reset_time =current_time; + distance_state = DELAY_AFTER_RESET; + TM1637_display(0, 0, 0, 0,0, lignt_D); + } + } else { + second_stop_time = current_time; // 重置计时 + } + break; + } + if (distance_state == DELAY_AFTER_RESET) { + continue; + } + + + + + if (isSystemStable &&distance_state != COOLDOWN && Filter_DATA < 50&&TR_flag==1) { //如果不是冷却并且触发 + if (distance_state == IDLE) { //并且空闲中 + // 首次触发 + last_trigger_time = current_time; + distance_state = COOLDOWN; // 进入冷却 + HAL_TIM_Base_Start_IT(&htim4); + time_flag = 0; + + // 其他操作(如启动某些动作) + } + else if (distance_state == FIRST_TRIGGERED) { //并且已经触发过一次 + // 二次触发 + HAL_TIM_Base_Stop_IT(&htim4); + Buzzer_Success(); + TR_flag=0; + } + } + Filter_DATA=KalmanFilter_Update(&KRM_distance,data); +//******************************************************************************* + if (distance_state == COOLDOWN) { //计时中 + // Check for timeout condition (4 seconds elapsed with no >100 reading) + if (current_time - last_trigger_time >= 2000) { + if (Filter_DATA < 50) {//正常触发 + // 正常触发 + distance_state = FIRST_TRIGGERED; + time_flag = 0; + } + } + } + + + if (isSystemStable && distance_state != IDLE && Filter_DATA < 50) { + is_obstructed = 1; // 标记当前被遮挡 + } else { + is_obstructed = 0; // 清除遮挡标记 + } + + + // 在第二次触发完成后更新状态 + if (distance_state == FIRST_TRIGGERED && TR_flag == 0) { + distance_state = SECOND_STOPPED; + second_stop_time = current_time; // 记录第二次触发完成时间 + } + TM1637_display(digit.min_unit, digit.sec_ten, digit.sec_unit, digit.ms_hundred,0, lignt_D); + /* USER CODE BEGIN 3 */ +}} + /* USER CODE END 3 */ + + +/** + * @brief System Clock Configuration + * @retval None + */ +void SystemClock_Config(void) +{ + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + Error_Handler(); + } + + /** Initializes the CPU, AHB and APB buses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) + { + Error_Handler(); + } +} + +/* USER CODE BEGIN 4 */ + +/* USER CODE END 4 */ + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } + /* USER CODE END Error_Handler_Debug */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) +{ + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* USER CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ +//// for(int i=0;i<21;i++) +// { +// if(i<=17) +// { +// TM1637_display(i,i+1,i+2,i+3,1); +// } +// else if(i==18) +// { +// TM1637_display(i,i+1,i+2,1,1); +// } +// else if(i==19) +// { +// TM1637_display(i,i+1,1,2,1); +// } +// else if(i==20) +// { +// TM1637_display(i,1,2,3,1); +// } +// +// HAL_Delay(1000); +// +// } + + diff --git a/Src/Software/Src/stm32f1xx_hal_msp.c b/Src/Software/Src/stm32f1xx_hal_msp.c new file mode 100644 index 0000000000000000000000000000000000000000..bcfdc02ba90d3a6c858c7b491e0f73beb925c2a3 --- /dev/null +++ b/Src/Software/Src/stm32f1xx_hal_msp.c @@ -0,0 +1,86 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f1xx_hal_msp.c + * @brief This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN Define */ + +/* USER CODE END Define */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN Macro */ + +/* USER CODE END Macro */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* External functions --------------------------------------------------------*/ +/* USER CODE BEGIN ExternalFunctions */ + +/* USER CODE END ExternalFunctions */ + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_AFIO_CLK_ENABLE(); + __HAL_RCC_PWR_CLK_ENABLE(); + + /* System interrupt init*/ + + /** NOJTAG: JTAG-DP Disabled and SW-DP Enabled + */ + __HAL_AFIO_REMAP_SWJ_NOJTAG(); + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Src/Software/Src/stm32f1xx_it.c b/Src/Software/Src/stm32f1xx_it.c new file mode 100644 index 0000000000000000000000000000000000000000..0a9875d626351a55dc116d57bc584ce653a41a16 --- /dev/null +++ b/Src/Software/Src/stm32f1xx_it.c @@ -0,0 +1,203 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file stm32f1xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ + +/* Includes ------------------------------------------------------------------*/ +#include "main.h" +#include "stm32f1xx_it.h" +/* Private includes ----------------------------------------------------------*/ +/* USER CODE BEGIN Includes */ +/* USER CODE END Includes */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN TD */ + +/* USER CODE END TD */ + +/* Private define ------------------------------------------------------------*/ +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ + +/* USER CODE END PFP */ + +/* Private user code ---------------------------------------------------------*/ +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ + +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/******************************************************************************/ +/* Cortex-M3 Processor Interruption and Exception Handlers */ +/******************************************************************************/ +/** + * @brief This function handles Non maskable interrupt. + */ +void NMI_Handler(void) +{ + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + while (1) + { + } + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** + * @brief This function handles Hard fault interrupt. + */ +void HardFault_Handler(void) +{ + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_HardFault_IRQn 0 */ + /* USER CODE END W1_HardFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Memory management fault. + */ +void MemManage_Handler(void) +{ + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ + /* USER CODE END W1_MemoryManagement_IRQn 0 */ + } +} + +/** + * @brief This function handles Prefetch fault, memory access fault. + */ +void BusFault_Handler(void) +{ + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_BusFault_IRQn 0 */ + /* USER CODE END W1_BusFault_IRQn 0 */ + } +} + +/** + * @brief This function handles Undefined instruction or illegal state. + */ +void UsageFault_Handler(void) +{ + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + while (1) + { + /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ + /* USER CODE END W1_UsageFault_IRQn 0 */ + } +} + +/** + * @brief This function handles System service call via SWI instruction. + */ +void SVC_Handler(void) +{ + /* USER CODE BEGIN SVCall_IRQn 0 */ + + /* USER CODE END SVCall_IRQn 0 */ + /* USER CODE BEGIN SVCall_IRQn 1 */ + + /* USER CODE END SVCall_IRQn 1 */ +} + +/** + * @brief This function handles Debug monitor. + */ +void DebugMon_Handler(void) +{ + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + + /* USER CODE END DebugMonitor_IRQn 1 */ +} + +/** + * @brief This function handles Pendable request for system service. + */ +void PendSV_Handler(void) +{ + /* USER CODE BEGIN PendSV_IRQn 0 */ + + /* USER CODE END PendSV_IRQn 0 */ + /* USER CODE BEGIN PendSV_IRQn 1 */ + + /* USER CODE END PendSV_IRQn 1 */ +} + +/** + * @brief This function handles System tick timer. + */ +void SysTick_Handler(void) +{ + /* USER CODE BEGIN SysTick_IRQn 0 */ + + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +/******************************************************************************/ +/* STM32F1xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32f1xx.s). */ +/******************************************************************************/ + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ diff --git a/Src/Software/Src/system_stm32f1xx.c b/Src/Software/Src/system_stm32f1xx.c new file mode 100644 index 0000000000000000000000000000000000000000..3afa78dbdda1fc9167b401fb71e9d94bc4ee3e72 --- /dev/null +++ b/Src/Software/Src/system_stm32f1xx.c @@ -0,0 +1,406 @@ +/** + ****************************************************************************** + * @file system_stm32f1xx.c + * @author MCD Application Team + * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. + * + * 1. This file provides two functions and one global variable to be called from + * user application: + * - SystemInit(): Setups the system clock (System clock source, PLL Multiplier + * factors, AHB/APBx prescalers and Flash settings). + * This function is called at startup just after reset and + * before branch to main program. This call is made inside + * the "startup_stm32f1xx_xx.s" file. + * + * - SystemCoreClock variable: Contains the core clock (HCLK), it can be used + * by the user application to setup the SysTick + * timer or configure other parameters. + * + * - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must + * be called whenever the core clock is changed + * during program execution. + * + * 2. After each device reset the HSI (8 MHz) is used as system clock source. + * Then SystemInit() function is called, in "startup_stm32f1xx_xx.s" file, to + * configure the system clock before to branch to main program. + * + * 4. The default value of HSE crystal is set to 8 MHz (or 25 MHz, depending on + * the product used), refer to "HSE_VALUE". + * When HSE is used as system clock source, directly or through PLL, and you + * are using different crystal you have to adapt the HSE value to your own + * configuration. + * + ****************************************************************************** + * @attention + * + * Copyright (c) 2017-2021 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ + +/** @addtogroup CMSIS + * @{ + */ + +/** @addtogroup stm32f1xx_system + * @{ + */ + +/** @addtogroup STM32F1xx_System_Private_Includes + * @{ + */ + +#include "stm32f1xx.h" + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_Defines + * @{ + */ + +#if !defined (HSE_VALUE) + #define HSE_VALUE 8000000U /*!< Default value of the External oscillator in Hz. + This value can be provided and adapted by the user application. */ +#endif /* HSE_VALUE */ + +#if !defined (HSI_VALUE) + #define HSI_VALUE 8000000U /*!< Default value of the Internal oscillator in Hz. + This value can be provided and adapted by the user application. */ +#endif /* HSI_VALUE */ + +/*!< Uncomment the following line if you need to use external SRAM */ +#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) +/* #define DATA_IN_ExtSRAM */ +#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ + +/* Note: Following vector table addresses must be defined in line with linker + configuration. */ +/*!< Uncomment the following line if you need to relocate the vector table + anywhere in Flash or Sram, else the vector table is kept at the automatic + remap of boot address selected */ +/* #define USER_VECT_TAB_ADDRESS */ + +#if defined(USER_VECT_TAB_ADDRESS) +/*!< Uncomment the following line if you need to relocate your vector Table + in Sram else user remap will be done in Flash. */ +/* #define VECT_TAB_SRAM */ +#if defined(VECT_TAB_SRAM) +#define VECT_TAB_BASE_ADDRESS SRAM_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#else +#define VECT_TAB_BASE_ADDRESS FLASH_BASE /*!< Vector Table base address field. + This value must be a multiple of 0x200. */ +#define VECT_TAB_OFFSET 0x00000000U /*!< Vector Table base offset field. + This value must be a multiple of 0x200. */ +#endif /* VECT_TAB_SRAM */ +#endif /* USER_VECT_TAB_ADDRESS */ + +/******************************************************************************/ + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_Variables + * @{ + */ + + /* This variable is updated in three ways: + 1) by calling CMSIS function SystemCoreClockUpdate() + 2) by calling HAL API function HAL_RCC_GetHCLKFreq() + 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency + Note: If you use this function to configure the system clock; then there + is no need to call the 2 first functions listed above, since SystemCoreClock + variable is updated automatically. + */ +uint32_t SystemCoreClock = 8000000; +const uint8_t AHBPrescTable[16U] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; +const uint8_t APBPrescTable[8U] = {0, 0, 0, 0, 1, 2, 3, 4}; + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_FunctionPrototypes + * @{ + */ + +#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) +#ifdef DATA_IN_ExtSRAM + static void SystemInit_ExtMemCtl(void); +#endif /* DATA_IN_ExtSRAM */ +#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ + +/** + * @} + */ + +/** @addtogroup STM32F1xx_System_Private_Functions + * @{ + */ + +/** + * @brief Setup the microcontroller system + * Initialize the Embedded Flash Interface, the PLL and update the + * SystemCoreClock variable. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +void SystemInit (void) +{ +#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) + #ifdef DATA_IN_ExtSRAM + SystemInit_ExtMemCtl(); + #endif /* DATA_IN_ExtSRAM */ +#endif + + /* Configure the Vector Table location -------------------------------------*/ +#if defined(USER_VECT_TAB_ADDRESS) + SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ +#endif /* USER_VECT_TAB_ADDRESS */ +} + +/** + * @brief Update SystemCoreClock variable according to Clock Register Values. + * The SystemCoreClock variable contains the core clock (HCLK), it can + * be used by the user application to setup the SysTick timer or configure + * other parameters. + * + * @note Each time the core clock (HCLK) changes, this function must be called + * to update SystemCoreClock variable value. Otherwise, any configuration + * based on this variable will be incorrect. + * + * @note - The system frequency computed by this function is not the real + * frequency in the chip. It is calculated based on the predefined + * constant and the selected clock source: + * + * - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*) + * + * - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(**) + * + * - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(**) + * or HSI_VALUE(*) multiplied by the PLL factors. + * + * (*) HSI_VALUE is a constant defined in stm32f1xx.h file (default value + * 8 MHz) but the real value may vary depending on the variations + * in voltage and temperature. + * + * (**) HSE_VALUE is a constant defined in stm32f1xx.h file (default value + * 8 MHz or 25 MHz, depending on the product used), user has to ensure + * that HSE_VALUE is same as the real frequency of the crystal used. + * Otherwise, this function may have wrong result. + * + * - The result of this function could be not correct when using fractional + * value for HSE crystal. + * @param None + * @retval None + */ +void SystemCoreClockUpdate (void) +{ + uint32_t tmp = 0U, pllmull = 0U, pllsource = 0U; + +#if defined(STM32F105xC) || defined(STM32F107xC) + uint32_t prediv1source = 0U, prediv1factor = 0U, prediv2factor = 0U, pll2mull = 0U; +#endif /* STM32F105xC */ + +#if defined(STM32F100xB) || defined(STM32F100xE) + uint32_t prediv1factor = 0U; +#endif /* STM32F100xB or STM32F100xE */ + + /* Get SYSCLK source -------------------------------------------------------*/ + tmp = RCC->CFGR & RCC_CFGR_SWS; + + switch (tmp) + { + case 0x00U: /* HSI used as system clock */ + SystemCoreClock = HSI_VALUE; + break; + case 0x04U: /* HSE used as system clock */ + SystemCoreClock = HSE_VALUE; + break; + case 0x08U: /* PLL used as system clock */ + + /* Get PLL clock source and multiplication factor ----------------------*/ + pllmull = RCC->CFGR & RCC_CFGR_PLLMULL; + pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; + +#if !defined(STM32F105xC) && !defined(STM32F107xC) + pllmull = ( pllmull >> 18U) + 2U; + + if (pllsource == 0x00U) + { + /* HSI oscillator clock divided by 2 selected as PLL clock entry */ + SystemCoreClock = (HSI_VALUE >> 1U) * pllmull; + } + else + { + #if defined(STM32F100xB) || defined(STM32F100xE) + prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U; + /* HSE oscillator clock selected as PREDIV1 clock entry */ + SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; + #else + /* HSE selected as PLL clock entry */ + if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t)RESET) + {/* HSE oscillator clock divided by 2 */ + SystemCoreClock = (HSE_VALUE >> 1U) * pllmull; + } + else + { + SystemCoreClock = HSE_VALUE * pllmull; + } + #endif + } +#else + pllmull = pllmull >> 18U; + + if (pllmull != 0x0DU) + { + pllmull += 2U; + } + else + { /* PLL multiplication factor = PLL input clock * 6.5 */ + pllmull = 13U / 2U; + } + + if (pllsource == 0x00U) + { + /* HSI oscillator clock divided by 2 selected as PLL clock entry */ + SystemCoreClock = (HSI_VALUE >> 1U) * pllmull; + } + else + {/* PREDIV1 selected as PLL clock entry */ + + /* Get PREDIV1 clock source and division factor */ + prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC; + prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1U; + + if (prediv1source == 0U) + { + /* HSE oscillator clock selected as PREDIV1 clock entry */ + SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; + } + else + {/* PLL2 clock selected as PREDIV1 clock entry */ + + /* Get PREDIV2 division factor and PLL2 multiplication factor */ + prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4U) + 1U; + pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8U) + 2U; + SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / prediv1factor) * pllmull; + } + } +#endif /* STM32F105xC */ + break; + + default: + SystemCoreClock = HSI_VALUE; + break; + } + + /* Compute HCLK clock frequency ----------------*/ + /* Get HCLK prescaler */ + tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4U)]; + /* HCLK clock frequency */ + SystemCoreClock >>= tmp; +} + +#if defined(STM32F100xE) || defined(STM32F101xE) || defined(STM32F101xG) || defined(STM32F103xE) || defined(STM32F103xG) +/** + * @brief Setup the external memory controller. Called in startup_stm32f1xx.s + * before jump to __main + * @param None + * @retval None + */ +#ifdef DATA_IN_ExtSRAM +/** + * @brief Setup the external memory controller. + * Called in startup_stm32f1xx_xx.s/.c before jump to main. + * This function configures the external SRAM mounted on STM3210E-EVAL + * board (STM32 High density devices). This SRAM will be used as program + * data memory (including heap and stack). + * @param None + * @retval None + */ +void SystemInit_ExtMemCtl(void) +{ + __IO uint32_t tmpreg; + /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is + required, then adjust the Register Addresses */ + + /* Enable FSMC clock */ + RCC->AHBENR = 0x00000114U; + + /* Delay after an RCC peripheral clock enabling */ + tmpreg = READ_BIT(RCC->AHBENR, RCC_AHBENR_FSMCEN); + + /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */ + RCC->APB2ENR = 0x000001E0U; + + /* Delay after an RCC peripheral clock enabling */ + tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_IOPDEN); + + (void)(tmpreg); + +/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/ +/*---------------- SRAM Address lines configuration -------------------------*/ +/*---------------- NOE and NWE configuration --------------------------------*/ +/*---------------- NE3 configuration ----------------------------------------*/ +/*---------------- NBL0, NBL1 configuration ---------------------------------*/ + + GPIOD->CRL = 0x44BB44BBU; + GPIOD->CRH = 0xBBBBBBBBU; + + GPIOE->CRL = 0xB44444BBU; + GPIOE->CRH = 0xBBBBBBBBU; + + GPIOF->CRL = 0x44BBBBBBU; + GPIOF->CRH = 0xBBBB4444U; + + GPIOG->CRL = 0x44BBBBBBU; + GPIOG->CRH = 0x444B4B44U; + +/*---------------- FSMC Configuration ---------------------------------------*/ +/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/ + + FSMC_Bank1->BTCR[4U] = 0x00001091U; + FSMC_Bank1->BTCR[5U] = 0x00110212U; +} +#endif /* DATA_IN_ExtSRAM */ +#endif /* STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ diff --git a/Src/Software/Src/tim.c b/Src/Software/Src/tim.c new file mode 100644 index 0000000000000000000000000000000000000000..3c35093d79018a904de32d83237380928885f2a3 --- /dev/null +++ b/Src/Software/Src/tim.c @@ -0,0 +1,388 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file tim.c + * @brief This file provides code for the configuration + * of the TIM instances. + ****************************************************************************** + * @attention + * + * Copyright (c) 2025 STMicroelectronics. + * All rights reserved. + * + * This software is licensed under terms that can be found in the LICENSE file + * in the root directory of this software component. + * If no LICENSE file comes with this software, it is provided AS-IS. + * + ****************************************************************************** + */ +/* USER CODE END Header */ +/* Includes ------------------------------------------------------------------*/ +#include "tim.h" +#include "oled.h" +#include "CSB.h" +#include "KALMAN.h" +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +TIM_HandleTypeDef htim1; // ijTIM1ľ +TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; +TIM_HandleTypeDef htim4; + + + +volatile TimerMode currentMode = MODE_ROUGH; // Ĭϴģʽ +volatile uint16_t milliseconds = 0; // (0-999) +volatile uint8_t seconds = 0; // (0-59) +volatile uint8_t minutes = 0; // (0-99) +volatile uint8_t ten_seconds = 0; // ʮ루0~5 +volatile uint8_t hundred_ms = 0; // ٺ루0~9 + +// ʱṹ +/* TIM2 init function */ + +extern KalmanFilter KRM_distance; +void HAL_TIM_Init(void) +{ + MX_TIM2_Init(); + MX_TIM1_Init(); + MX_TIM4_Init(); +} + +void MX_TIM1_Init(void) +{ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + + __HAL_RCC_TIM1_CLK_ENABLE(); + htim1.Instance = TIM1; // ʹTIM1 + htim1.Init.Prescaler = 7200 - 1; // 72MHz / 7200 = 10kHz + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + htim1.Init.Period = 1000 - 1; // 10kHz / 1000 = 10Hz (100ms) + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + htim1.Init.RepetitionCounter = 0; // ߼ʱУTIM1/TIM8 + + // ʼTIM1 + if (HAL_TIM_Base_Init(&htim1) != HAL_OK) + { + Error_Handler(); + } + + // ʱԴڲʱӣ + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + + // ģʽãĬϼɣ + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + + // TIM1ж + HAL_NVIC_SetPriority(TIM1_UP_IRQn, 1, 1); // TIM1ж TIM1_UP_IRQn + HAL_NVIC_EnableIRQ(TIM1_UP_IRQn); // ʹTIM1ж + HAL_TIM_Base_Start_IT(&htim1); +} +void MX_TIM2_Init(void) +{ + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + __HAL_RCC_TIM2_CLK_ENABLE(); + /* USER CODE END TIM2_Init 1 */ + htim2.Instance = TIM2; + htim2.Init.Prescaler = 72-1; + htim2.Init.CounterMode = TIM_COUNTERMODE_UP; + htim2.Init.Period = 65536-1; + htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim2) != HAL_OK) + { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ + +} +void MX_TIM3_Init(void) +{ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + __HAL_RCC_TIM3_CLK_ENABLE(); + htim3.Instance = TIM3; + htim3.Init.Prescaler = 7200-1; // 72MHz/7200 = 10kHz + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 1000-1; // 10kHz/1000 = 10Hz (100ms) + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + + // NVICж + HAL_NVIC_SetPriority(TIM3_IRQn, 2, 2); + HAL_NVIC_EnableIRQ(TIM3_IRQn); + + // ʼʱ + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) + { + Error_Handler(); + } + + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } +} + + + +void MX_TIM4_Init(void) +{ + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + TIM_MasterConfigTypeDef sMasterConfig = {0}; + + __HAL_RCC_TIM4_CLK_ENABLE(); + htim4.Instance = TIM4; + htim4.Init.Prescaler = 7200 - 1; // 72MHz/7200 = 10kHz + htim4.Init.CounterMode = TIM_COUNTERMODE_UP; + htim4.Init.Period = 1000 - 1; // 10kHz/1000 = 10Hz (100ms) + htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim4) != HAL_OK) + { + Error_Handler(); + } + + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK) + { + Error_Handler(); + } + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) + { + Error_Handler(); + } + HAL_NVIC_SetPriority(TIM4_IRQn, 3, 3); // ȼʵ + HAL_NVIC_EnableIRQ(TIM4_IRQn); // ʹTIM4ж +} + +void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) +{ + + if(tim_baseHandle->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspInit 0 */ + + /* USER CODE END TIM2_MspInit 0 */ + /* TIM2 clock enable */ + __HAL_RCC_TIM2_CLK_ENABLE(); + /* USER CODE BEGIN TIM2_MspInit 1 */ + + + /* USER CODE END TIM2_MspInit 1 */ + } +} + +void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) +{ + + if(tim_baseHandle->Instance==TIM2) + { + /* USER CODE BEGIN TIM2_MspDeInit 0 */ + + /* USER CODE END TIM2_MspDeInit 0 */ + /* Peripheral clock disable */ + /* USER CODE BEGIN TIM2_MspDeInit 1 */ + + /* USER CODE END TIM2_MspDeInit 1 */ + } +} + +uint16_t Filter_DATA=0; + +/************************************** +*******************************************************************************************/ +void ResetTimer(void) +{ + HAL_TIM_Base_Stop_IT(&htim4); + digit.min_unit=0; + digit.ms_hundred=0; + digit.sec_ten=0; + digit.sec_unit=0; +} + +/****************************************************/ +void TIM1_UP_IRQHandler(void) +{ + HAL_TIM_IRQHandler(&htim1); // HALжϴ +} +uint16_t data=0; +void TIM4_IRQHandler(void) { + HAL_TIM_IRQHandler(&htim4); +} +/***************************************************** typedef struct { + uint8_t min_ten; // ֵʮλ + uint8_t min_unit; // ֵĸλ + uint8_t sec_ten; // ʮλ + uint8_t sec_unit; // ĸλ + uint8_t ms_hundred; // İλ + uint8_t ms_ten; // ʮλ*/ + +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) +{ + if (htim->Instance == TIM4) { + digit.ms_hundred ++; + if (digit.ms_hundred >= 10) { + digit.ms_hundred = 0; + digit.sec_unit++; + + if (digit.sec_unit >= 10) { + digit.sec_unit = 0; + digit.sec_ten++; + + if (digit.sec_ten >= 6) { // 60=1 + digit.sec_ten = 0; + digit.min_unit++; + + if (digit.min_unit >= 10) { // 10 + digit.min_unit = 9; // Ϊ9:59.9 + digit.sec_ten = 5; + digit.sec_unit = 9; + digit.ms_hundred = 9; + HAL_TIM_Base_Stop_IT(&htim4); // ֹͣʱ + } + } + } + } + } + else if (htim->Instance == TIM1) + { data=HC_SR04_Distance(); + if(data>=220) + {data=220;} + Filter_DATA=KalmanFilter_Update(&KRM_distance,data); + + } + + } + + +void SwitchMode(TimerMode newMode) +{ + // ֹͣǰʱ + HAL_TIM_Base_Stop_IT(&htim4); + + // ģʽ + currentMode = newMode; + if(currentMode == MODE_PRECISE) { + // ׼ģʽ: 1msж + htim4.Instance->PSC = 720 - 1; // 72MHz/720 = 1k + htim4.Instance->ARR = 1000 - 1; // 1MHz/1000 = 100Hz (1ms) + } else { + // ģʽ: 1sж + htim4.Instance->PSC = 7200 - 1; // 72MHz/7200 = 10kHz + htim4.Instance->ARR = 10000 - 1; // 10kHz/10000 = 1Hz (1s) + } + + // HALøº + __HAL_TIM_SET_AUTORELOAD(&htim4, htim4.Instance->ARR); + __HAL_TIM_SET_PRESCALER(&htim4, htim4.Instance->PSC); + + // ³ʼʱ + __HAL_TIM_SET_COUNTER(&htim4, 0); + // ʱ +// HAL_TIM_Base_Start_IT(&htim4); + + // üʱ + milliseconds = 0; + seconds = 0; + minutes = 0; +} + + +//ֱӽṹȥ +TimeDigits digit={0}; +void SplitTimeDigits(TimeDigits *digits) +{ + + if(currentMode == MODE_PRECISE) { + // ׼ģʽ: ֽ롢 + digits->ms_hundred = milliseconds / 10; + digits->ms_ten = milliseconds % 10; + + digits->sec_ten = seconds / 10; + digits->sec_unit = seconds % 10; + } else { + // ģʽ: ֽ֡ + digits->min_ten = minutes / 10; + digits->min_unit = minutes % 10; + + digits->sec_ten = seconds / 10; + digits->sec_unit = seconds % 10; + } + + +} +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ +//void DWT_Init(void) +// { +// CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; +// DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; +// DWT->CYCCNT=0; +//} +//void HAL_Delay_us(uint32_t us) +// { +// uint32_t sys_clk = HAL_RCC_GetHCLKFreq(); +// uint32_t ticks = us * (sys_clk / 1000000); +// uint32_t start = DWT->CYCCNT; +// while ((DWT->CYCCNT - start) < ticks); +//} + + +void HAL_Delay_us(uint32_t delay_us) + { + HAL_TIM_Base_Start(&htim2); // ʱ + __HAL_TIM_SET_COUNTER(&htim2, 0); // + if(delay_us>65535) + delay_us=65535; + while (__HAL_TIM_GET_COUNTER(&htim2) < delay_us); // ȴﵽĿֵ + HAL_TIM_Base_Stop(&htim2); // ֹͣʱ +} +