first commit

This commit is contained in:
2025-02-19 14:46:14 +08:00
commit cde431e41c
221 changed files with 204599 additions and 0 deletions

261
APP/DS18B20.c Normal file
View File

@ -0,0 +1,261 @@
#include "DS18B20.h"
#define DS18B20_PORT GPIOC
#define DS18B20_PIN GPIO_PIN_1
void DS18B20_OUTPUT(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = DS18B20_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DS18B20_PORT, &GPIO_InitStruct);
}
void DS18B20_INPUT(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = DS18B20_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DS18B20_PORT, &GPIO_InitStruct);
}
void DS18B20_Reset(void) {
DS18B20_OUTPUT();
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(750);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
Delay_us(15);
}
uint8_t DS18B20_Check(void)
{
uint8_t retry=0;
DS18B20_INPUT();
while ((HAL_GPIO_ReadPin(DS18B20_PORT, DS18B20_PIN))&&retry<200)
{
retry++;
Delay_us(1);
};
if(retry>=200) return 0;
else retry=0;
while (!(HAL_GPIO_ReadPin(DS18B20_PORT, DS18B20_PIN))&&retry<240)
{
retry++;
Delay_us(1);
};
Delay_us(480);
if(retry>=300) return 0;
return 1;
}
uint8_t DS18B20_Read_Bit(void) // read one bit
{
uint8_t data;
DS18B20_OUTPUT();
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(2);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
DS18B20_INPUT();
Delay_us(12);
if((HAL_GPIO_ReadPin(DS18B20_PORT, DS18B20_PIN))) data=1;
else data=0;
Delay_us(50);
return data;
}
uint8_t DS18B20_ReadByte(void) {
uint8_t byte = 0;
uint8_t j=0;
for (uint8_t i = 0; i < 8; i++) {
j=DS18B20_Read_Bit();
byte=(j<<7)|(byte>>1);
}
return byte;
}
void DS18B20_Write_Bit(uint8_t bit) {
DS18B20_OUTPUT();
if (bit == 1) {
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(2);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
Delay_us(60);
}
else {
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(60);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
Delay_us(2);
}
}
void DS18B20_WriteByte(uint8_t data) {
DS18B20_OUTPUT();
for (uint8_t i = 0; i < 8; i++)
{
if (data & 0x01)
{
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(2);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
Delay_us(60);
}
else
{
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_RESET);
Delay_us(60);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
Delay_us(2);
}
data >>= 1;
}
}
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Reset();
DS18B20_Check();
DS18B20_WriteByte(0xcc);// skip rom
DS18B20_WriteByte(0x44);// convert
}
uint8_t DS18B20_Init()
{
Delay_Init() ;
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = DS18B20_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(DS18B20_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
HAL_Delay(1000);
DS18B20_Reset();
return DS18B20_Check();
}
void DS18B20_Stop() {
DS18B20_OUTPUT();
HAL_GPIO_WritePin(DS18B20_PORT, DS18B20_PIN, GPIO_PIN_SET);
}
float DS18B20_GetTemperature() {
uint8_t Hight_Byte = 0;
uint8_t Low_Byte = 0;
uint16_t value = 0;
float temperture = 0;
DS18B20_Start();
DS18B20_Reset();
uint8_t check = DS18B20_Check();
// printf("check %d",check);
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0xBE);
Low_Byte = DS18B20_ReadByte();
Hight_Byte = DS18B20_ReadByte();
DS18B20_Stop();
value = Hight_Byte;
value = (value << 8) + Low_Byte;
if ((value & 0xf800) == 0xf800) {
value = (~value)+1;
temperture = value*(-0.0625);
}
else {
temperture = value*(0.0625);
}
// printf("Temperature 111: %.2f\n", temperture);
return temperture;
}
//
// void DS18B20_SearchROM(uint8_t *roms, uint8_t *count) {
// uint8_t bit_value, complement_bit;
// uint8_t rom[8];
// uint8_t last_discrepancy = 0;
// uint8_t rom_index = 0;
// uint8_t search_complete = 0;
//
// *count = 0; // 设备数量清零
// printf("Starting DS18B20 SearchROM...\n");
//
// while (!search_complete) {
// DS18B20_Reset();
// // uint8_t aaa=DS18B20_Check();
// if (!DS18B20_Check()) { // 复位总线,检测是否有设备存在
// return; // 没有设备,直接返回
// }
//
// DS18B20_WriteByte(0xF0); // 发送 "搜索 ROM" 命令
//
// uint8_t bit_position = 1;
// uint8_t discrepancy_marker = 0; // 记录新的冲突位置
//
// for (uint8_t i = 0; i < 64; i++) {
// bit_value = DS18B20_Read_Bit();
// complement_bit = DS18B20_Read_Bit();
//
// if (bit_value == 1 && complement_bit == 1) {
// printf("No more devices found, stopping search.\n");
// return;
// }
//
// uint8_t chosen_bit;
// if (bit_value == 0 && complement_bit == 0) {
// // **发生冲突,选择路径**
// if (bit_position < last_discrepancy) {
// // 沿用之前的选择
// chosen_bit = (rom[i / 8] >> (i % 8)) & 0x01;
// } else if (bit_position == last_discrepancy) {
// // 选择 1 继续遍历新路径
// chosen_bit = 1;
// } else {
// // 选择 0 并记录新的冲突
// chosen_bit = 0;
// discrepancy_marker = bit_position;
// }
// } else {
// // 读取到 0 或 1直接存储
// chosen_bit = bit_value;
// }
//
// if (chosen_bit) {
// rom[i / 8] |= (1 << (i % 8));
// } else {
// rom[i / 8] &= ~(1 << (i % 8));
// }
//
// DS18B20_Write_Bit(chosen_bit);
// bit_position++;
// }
//
// // 存储找到的ROM地址
// for (uint8_t j = 0; j < 8; j++) {
// roms[rom_index * 8 + j] = rom[j];
// }
// rom_index++;
//
// // **搜索结束条件**
// if (discrepancy_marker == 0) {
// search_complete = 1;
// }
//
// last_discrepancy = discrepancy_marker;
// }
//
// *count = rom_index; // 返回找到的设备数量
// }
//

14
APP/DS18B20.h Normal file
View File

@ -0,0 +1,14 @@
//
// Created by IRIS on 25-2-10.
//
#ifndef DS18B20_H
#define DS18B20_H
#include "mymain.h"
// void DS18B20_Init(void);
float DS18B20_GetTemperature();
uint8_t DS18B20_Init();
void DS18B20_SearchROM(uint8_t *roms, uint8_t *count);
#endif //DS18B20_H

172
APP/IRIS_Method.c Normal file
View File

@ -0,0 +1,172 @@
/**
******************************************************************************
* @file : IRIS_Method.c
* @author : xin
* @brief : None
* @attention : None
* @date : 2024/2/1
******************************************************************************
*/
//
// Created by xin on 2024/2/1.
//
#include "IRIS_Method.h"
int32_t IRIS_Protocol_Pack(uint8_t Command, uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData) {
if (PackData == NULL || (LenthofIn != 0 && BufferIn == NULL)) {
return -1;
}
PackData[0] = 0x55;
PackData[1] = 0xAA;
PackData[2] = Command;
uint16_t datalenth = LenthofIn;
PackData[3] = (datalenth >> 8) & 0xFF;
PackData[4] = datalenth & 0xFF;
if (LenthofIn != 0) {
memcpy(&PackData[5], BufferIn, LenthofIn);
}
uint16_t crcbytelenth = LenthofIn;
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, crcbytelenth);
PackData[LenthofIn + 5] = (CRC_CRC >> 8) & 0xFF;
PackData[LenthofIn + 6] = CRC_CRC & 0xFF;
return LenthofIn + 7;
}
int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut) {
if (PackData == NULL || BufferOut == NULL) {
return ERROR_INPUT;
}
if (PackData[0] != 0x55 || PackData[1] != 0xAA) {
return ERROR_HEADER;
}
uint16_t LenthofOut = PackData[4] + (PackData[3] << 8); //减去CRC的两个字节
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) {
return ERROR_CRC;
}
}
if (LenthofOut == 0) {
return 0;
}
*Command = PackData[2];
memcpy(BufferOut, &PackData[5], LenthofOut);
return LenthofOut;
}
int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut) {
if (PackData == NULL || BufferOut == NULL) {
return ERROR_INPUT;
}
if (PackData[0] != 0x55 || PackData[1] != 0xAA) {
return ERROR_HEADER;
}
if (PackData[2] != Command) {
return ERROR_COMMAND;
}
uint16_t LenthofOut = PackData[4] + (PackData[3] << 8);
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) {
return ERROR_CRC;
}
}
if (LenthofOut == 0) {
return 0;
}
memcpy(BufferOut, &PackData[5], LenthofOut);
return LenthofOut;
}
int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn) {
if (PackData == NULL) {
return ERROR_INPUT;
}
uint16_t i = 0;
for (i = 0; i < LenthofIn; i++) {
if (PackData[i] == 0x55 && PackData[i + 1] == 0xAA) {
break;
}
}
if (i == LenthofIn) {
//清空数据
memset(PackData, 0, LenthofIn);
return 0;
}
uint16_t LenthofOut = LenthofIn - i;
memcpy(PackData, &PackData[i], LenthofOut);
return LenthofOut;
}
int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn) {
if (PackData == NULL) {
return ERROR_INPUT;
}
if (LenthofIn < 7) {
return ERROR_NOT_ENOUGH_DATA;
/* code */
}
if (PackData[0] != 0x55 || PackData[1] != 0xAA) {
return ERROR_HEADER;
}
uint16_t LenthofOut = PackData[4] + (PackData[3] << 8);
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) {
return ERROR_CRC;
}
}
return 1;
}
uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize) {
const uint8_t *pBytesArray = (const uint8_t *) pBuffer;
uint16_t poly = 0x8408;
uint16_t crc = 0;
uint8_t carry;
uint8_t i_bits;
uint16_t j;
for (j = 0; j < bufferSize; j++) {
crc = crc ^ pBytesArray[j];
for (i_bits = 0; i_bits < 8; i_bits++) {
carry = crc & 1;
crc = crc / 2;
if (carry) {
crc = crc ^ poly;
}
}
}
return crc;
}

60
APP/IRIS_Method.h Normal file
View File

@ -0,0 +1,60 @@
/**
******************************************************************************
* @file : IRIS_Method.h
* @author : xin
* @brief : None
* @attention : None
* @date : 2024/2/1
******************************************************************************
*/
//
// Created by xin on 2024/2/1.
//
#ifndef IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H
#define IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H
#define ERROR_NOT_ENOUGH_DATA -200
#define ERROR_HEADER -300
#define ERROR_COMMAND -400
#define ERROR_INPUT -500
#define ERROR_CRC -600
#include <stdint.h>
//#include<Arduino.h>
#include "mymain.h"
// 成功返回打包后的数据长度
// -1: Error
// 成功返回打包后的数据长度
int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData);
// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令 BufferOut 是输出
// 下位机使用的打包函数 Command 是输出
// 成功返回解包后的数据长度
// 0: 该命令返回无参数
// 错误返回ERRor
// 成功返回解包后的数据长度
int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut);
// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令输入 BufferOut 是输出 上位机使用
// 成功返回解包后的数据长度
// 0: 该命令返回无参数
// 错误返回ERRor
// 成功返回解包后的数据长度
int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut);
// 定义裁切命令
// 成功返回裁切后的数据长度
// -1: Error
int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn );
// 检查数据是否有效
// 有效返回值1
// 错误返回ERRor
int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn );
// 返回CRC校验值
uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize);
#endif //IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H

18
APP/LED.c Normal file
View File

@ -0,0 +1,18 @@
//
// Created by hu123456 on 2024/1/24.
//
#include "LED.h"
void led_init()
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

17
APP/LED.h Normal file
View File

@ -0,0 +1,17 @@
//
// Created by hu123456 on 2024/1/24.
//
#ifndef FOC_N_LED_H
#define FOC_N_LED_H
#include "mymain.h"
#define LED_ON HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 0)
#define LED_OFF HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, 1)
void led_init();
#endif //FOC_N_LED_H

47
APP/Shutter.c Normal file
View File

@ -0,0 +1,47 @@
//
// Created by IRIS on 25-2-10.
//
#include "Shutter.h"
#define Shutter_port GPIOD
#define Shutter1_pin GPIO_PIN_1
#define Shutter2_pin GPIO_PIN_0
void Shutter_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOD_CLK_ENABLE();
GPIO_InitStruct.Pin = Shutter1_pin | Shutter2_pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Shutter_port, &GPIO_InitStruct);
}
void Shutter_Open(uint8_t shutter)
{
if (shutter == Shutter_1)
{
HAL_GPIO_WritePin(Shutter_port, Shutter1_pin, 1);
}
else if (shutter == Shutter_2)
{
HAL_GPIO_WritePin(Shutter_port, Shutter2_pin, 1);
}
}
void Shutter_Close(uint8_t shutter)
{
if (shutter == Shutter_1)
{
HAL_GPIO_WritePin(Shutter_port, Shutter1_pin, 0);
}
else if (shutter == Shutter_2)
{
HAL_GPIO_WritePin(Shutter_port, Shutter2_pin, 0);
}
}

19
APP/Shutter.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by IRIS on 25-2-10.
//
#ifndef SHUTTER_H
#define SHUTTER_H
#include "mymain.h"
#define Shutter_1 1
#define Shutter_2 2
void Shutter_init();
void Shutter_Open(uint8_t shutter);
void Shutter_Close(uint8_t shutter);
#endif //SHUTTER_H

53
APP/communication.c Normal file
View File

@ -0,0 +1,53 @@
//
// Created by IRIS on 25-2-7.
//
#include "communication.h"
#define maxValue 65535
//
void Send_Shutter_Time(uint32_t shutter_time) {
uint32_t tt = shutter_time*1000;
// printf("t %d \n",t);
// printf("tt %d \n",tt);
uint8_t t_buff[4];
t_buff[0] = *((uint8_t *)&tt+3);
t_buff[1] = *((uint8_t *)&tt+2);
t_buff[2] = *((uint8_t *)&tt+1);
t_buff[3] = *((uint8_t *)&tt);
// HAL_UART_Transmit(&huart1,t_buff,4,100);
__HAL_UART_ENABLE(&huart2);
HAL_UART_Transmit(&huart2,t_buff,4,100);
}
uint32_t Opt_Snenser(int persent, uint16_t *data,uint32_t Size,uint32_t Shutter_Now) {
int maxvalue=maxValue*1.0*persent / 100;
int maxvaluenow = 0;
uint32_t shutter_time;
uint32_t max_shutter_time = 100;
uint32_t min_shutter_time = 10;
for (int i = 0; i < Size; i++) {
if (data[i] > maxvaluenow) {
maxvaluenow = data[i];
}
}
if (Shutter_Now<=min_shutter_time || Shutter_Now>=max_shutter_time) return 0;
if (maxvaluenow<maxvalue*0.95 || maxvaluenow>maxvalue) {
shutter_time = Shutter_Now * maxvaluenow / maxvalue;
Send_Shutter_Time(shutter_time);
HAL_Delay(1000);
return shutter_time;
}
return 0;
}

11
APP/communication.h Normal file
View File

@ -0,0 +1,11 @@
//
// Created by IRIS on 25-2-7.
//
#ifndef COMMUNICATION_H
#define COMMUNICATION_H
#include "mymain.h"
void Send_Shutter_Time(uint32_t shutter_time);
uint32_t Opt_Snenser(int persent, uint16_t *data,uint32_t Size,uint32_t Shutter_Now);
#endif //COMMUNICATION_H

53
APP/data_analyse.c Normal file
View File

@ -0,0 +1,53 @@
//
// Created by IRIS on 25-2-12.
//
#include "data_analyse.h"
#define WINDOW_SIZE 5 // 高斯滑动窗口大小(建议为奇数)
#define SIGMA 1.5 // 平滑程度,值越大平滑越强
// 计算高斯权重
void compute_gaussian_weights(double weights[], int size, double sigma) {
double sum = 0.0;
int half_size = size / 2;
for (int i = -half_size; i <= half_size; i++) {
weights[i + half_size] = exp(- (i * i) / (2 * sigma * sigma)); // 高斯分布公式
sum += weights[i + half_size];
}
// 归一化,使权重之和为 1
for (int i = 0; i < size; i++) {
weights[i] /= sum;
}
}
// SGI 平滑算法 (适用于 uint16_t 数据)
void sgi_smoothing(uint16_t data[], uint16_t result[], int size) {
double weights[WINDOW_SIZE];
compute_gaussian_weights(weights, WINDOW_SIZE, SIGMA);
int half_size = WINDOW_SIZE / 2;
for (int i = 0; i < size; i++) {
double sum = 0.0;
double weight_sum = 0.0;
for (int j = -half_size; j <= half_size; j++) {
int idx = i + j;
if (idx >= 0 && idx < size) { // 确保索引有效
sum += data[idx] * weights[j + half_size];
weight_sum += weights[j + half_size];
}
}
// 四舍五入并转换为 uint16_t
result[i] = (uint16_t)round(sum / weight_sum);
}
}
void sgi(uint16_t *data, uint16_t *result) {
int size = sizeof(data) / sizeof(data[0]);
sgi_smoothing(data, result, size);
}

11
APP/data_analyse.h Normal file
View File

@ -0,0 +1,11 @@
//
// Created by IRIS on 25-2-12.
//
#ifndef DATA_ANALYSE_H
#define DATA_ANALYSE_H
#include "mymain.h"
void sgi(uint16_t *data, uint16_t *result);
#endif //DATA_ANALYSE_H

22
APP/delay.c Normal file
View File

@ -0,0 +1,22 @@
//
// Created by IRIS on 25-2-10.
//
#include "delay.h"
void Delay_Init() {
HAL_TIM_Base_Start(&htim5); //启动定时器
}
void Delay_us(uint32_t us) {
uint32_t Time_Now;
uint32_t Ts;
__HAL_TIM_SetCounter(&htim5, 0);
while(Ts < us) {
Time_Now = __HAL_TIM_GET_COUNTER(&htim5);
Ts = Time_Now * 0.1;
}
}

12
APP/delay.h Normal file
View File

@ -0,0 +1,12 @@
//
// Created by IRIS on 25-2-10.
//
#ifndef DELAY_H
#define DELAY_H
#include "mymain.h"
void Delay_us(uint32_t us);
void Delay_Init();
#endif //DELAY_H

305
APP/mymain.c Normal file
View File

@ -0,0 +1,305 @@
////////// Created by lijie on 2025/2/6.
#include "mymain.h"
#define USART2_RX_BUFFER_SIZE 516
#define USART_REC_LEN 200
#define USART_EN_RX 1
#define RXBUFFERSIZE 1
uint16_t g_usart_rx_sta = 0;
uint8_t g_usart_rx_buf[USART_REC_LEN];
uint8_t g_rx_buffer[RXBUFFERSIZE];
//平均次数
uint8_t Target_Average_Times = 1;
uint16_t Receive_Data_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint16_t USART2_RX_Buffer[USART2_RX_BUFFER_SIZE];
uint16_t Average_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint16_t Moving_Buffer[10][USART2_RX_BUFFER_SIZE - 1];
uint16_t Moving_Average_Buffer[USART2_RX_BUFFER_SIZE - 1];
uint32_t Receive_Data_Count = 0;
uint32_t Last_Receive_Data_Count = 0;
uint32_t shutter_time = 20;
uint32_t mode = 0;
float Temperature = 0;
/* Send_Data_Type
* 0 发送原始数据
* 1 发送多次平均数据
* 2 发送sgi数据
* 3 发送多次均数据
* 4 发送滑动平均数据
*/
uint32_t Send_Data_Type = 0;
uint32_t sn = 1699;
void Communication_Init() {
__HAL_UART_DISABLE(&huart2);
HAL_UART_Receive_DMA(&huart2, (uint8_t *)USART2_RX_Buffer, USART2_RX_BUFFER_SIZE*2);
USART2->CR3 |= USART_CR3_DMAT; // 启用DMA传输
}
void Send_Data(uint8_t command,uint8_t *pData, uint16_t Size) {
if (__HAL_DMA_GET_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7))
{
__HAL_DMA_CLEAR_FLAG(&g_dma_handle, DMA_FLAG_TCIF3_7);
HAL_UART_DMAStop(&huart1);
}
uint8_t send_buff[1024*2];
uint32_t send_lenth;
send_lenth = IRIS_Protocol_Pack(command,Size,pData,send_buff);
// printf("Send_Data: %s\n", send_buff);
HAL_UART_Transmit_DMA(&huart1,(uint8_t *)send_buff,send_lenth);
}
void mymain()
{
// HAL_Delay(5000);
HAL_NVIC_SetPriorityGrouping(2);
HAL_UART_Receive_IT(&huart1, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
setvbuf(stdout, NULL, _IONBF, 0);
dma_init(DMA2_Stream7, DMA_REQUEST_USART1_TX);
/////
// uint8_t p[4] = {0x00,0x01,0x5f,0x90};
// HAL_UART_Transmit(&huart2,p,4,100);
DS18B20_Init();
// led_init();
Communication_Init();
// __HAL_UART_DISABLE(&huart2);
// HAL_UART_Receive_DMA(&huart2, (uint8_t *)USART2_RX_Buffer, USART2_RX_BUFFER_SIZE*2);
// USART2->CR3 |= USART_CR3_DMAT; // 启用DMA传输
// LED_OFF;
// // HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,0);
// LED_ON;
uint32_t Average_Times = 0;
uint32_t Moving_Average_Times = 0;
uint32_t a=0;
while (1) {
while (mode == 0) {
commander_run();
if (Receive_Data_Count != Last_Receive_Data_Count) {
uint32_t a = Opt_Snenser(90, Receive_Data_Buffer ,USART2_RX_BUFFER_SIZE,shutter_time );
if (a !=0) shutter_time = a;
else mode = 1;
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
}
while(mode == 1) {
//处理指令
commander_run();
//处理数据
if (Receive_Data_Count != Last_Receive_Data_Count)
{
// Send_Data((uint8_t *)Receive_Data_Buffer,USART2_RX_BUFFER_SIZE*2);
//2多次平均
Average_Times ++;
if(Average_Times <= Target_Average_Times ) {
for (uint32_t i = 0; i < USART2_RX_BUFFER_SIZE; i++) {
Average_Buffer[i] = (Average_Buffer[i] * (Average_Times-1) + Receive_Data_Buffer[i]) / Average_Times;
}
}else {
Average_Times = 0;
}
//滑动平均
memcpy(&Moving_Average_Buffer[Moving_Average_Times % 10], Receive_Data_Buffer, USART2_RX_BUFFER_SIZE*2);
Moving_Average_Times ++;
for (uint32_t i = 0; i < USART2_RX_BUFFER_SIZE; i++) {
for (uint32_t j = 0; j < 10; j++) {
Moving_Average_Buffer[i] += Moving_Buffer[j][i] / 10;
}
}
}
//按命令发送数据
if((Send_Data_Type & 0x8000) !=0)
{
Send_Data_Type = Send_Data_Type & 0x7FFF;
uint16_t sgi_result[USART2_RX_BUFFER_SIZE];
// printf("AAA");
switch (Send_Data_Type)
{
case 0:
Send_Data(0x61,(uint8_t *)Receive_Data_Buffer,515*2);
break;
case 1:
Send_Data(0x61,(uint8_t *)Average_Buffer,515*2);
break;
case 2:
sgi(Receive_Data_Buffer,sgi_result);
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
break;
case 3:
sgi(Average_Buffer,sgi_result);
Send_Data(0x61,(uint8_t *)sgi_result,515*2);
break;
case 4:
Send_Data(0x61,(uint8_t *)Moving_Average_Buffer,515*2);
break;
}
Send_Data_Type = Send_Data_Type & 0x7FFF;
}
// if ( a == 1300 * 15) {
// Temperature = DS18B20_GetTemperature();
// printf("Temperatureqqq: %f\n", Temperature);
// a = 0;
// }
a++;
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
// 当DMA接收完成时这个回调函数会被触发
if (huart->Instance == USART2)
{
__HAL_UART_DISABLE(&huart2);
memcpy(Receive_Data_Buffer, USART2_RX_Buffer+2, USART2_RX_BUFFER_SIZE*2);
Receive_Data_Count ++;
HAL_UART_Receive_DMA(&huart2, (uint8_t *)USART2_RX_Buffer, USART2_RX_BUFFER_SIZE*2);
}
if(huart->Instance == USART1)
{
g_usart_rx_buf[g_usart_rx_sta] = g_rx_buffer[0];
g_usart_rx_sta++;
HAL_UART_Receive_IT(&huart1, (uint8_t *)g_rx_buffer, RXBUFFERSIZE);
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
__HAL_UART_ENABLE(&huart2);
}
}
void commander_run(void)
{
if(g_usart_rx_sta > 7)
{
uint16_t data_length = g_usart_rx_sta & 0x3fff;
uint8_t data_type = 0;
uint8_t command_data[USART_REC_LEN] = {0};
// data_length = IRIS_Cut_Befor_Header(g_usart_rx_buf,data_length);
int ret = IRIS_STM32_Protocol_Unpack(g_usart_rx_buf,data_length,&data_type,command_data);
if (ret > 0) {
switch(data_type)
{
// 获取设备信息
case 0x50: {
// printf("hello \n");
uint8_t str[8];
str[0] = 'I';
str[1] = 'S';
str[2] = '3';
str[3] = '-';
str[4] = '0'+ sn/1000%10;
str[5] = '0'+ sn/100%10;
str[6] = '0'+ sn/10%10;
str[7] = '0'+ sn%10;
// printf("shutter_time %d\n",shutter_time);
Send_Data(0x50,str,8);
break;
}
//设置曝光时间
case 0x51: {
shutter_time = command_data[0]<<24 | command_data[1]<<16 | command_data[2]<<8 | command_data[3];
Send_Shutter_Time(shutter_time);
// memset()
Send_Data(0x51,command_data,4);
break;
}
//自动曝光
case 0x52:{
mode = 0;
Send_Data(0x52,command_data,1);
break;
}
//获取曝光时间
case 0x53:{
uint8_t st[5];
// memcpy(st,&shutter_time,4);
st[3] = shutter_time & 0xff;
st[2] = (shutter_time >> 8) & 0xff;
st[1] = (shutter_time >> 16) & 0xff;
st[0] = (shutter_time >> 24) & 0xff;
st[4] = Send_Data_Type & 0x7FFF;
// printf("shutter_time %d\n",shutter_time);
Send_Data(0x53,st,5);
break;
}
//获取温度
case 0x54: {
// 将Temperature的值转换为uint8_t数组并将其作为参数传递给Send_Data函数。
uint8_t temp[4];
Temperature = DS18B20_GetTemperature();
memcpy(&temp[0],(uint8_t *)&Temperature + 3,1);
memcpy(&temp[1],(uint8_t *)&Temperature + 2,1);
memcpy(&temp[2],(uint8_t *)&Temperature + 1,1);
memcpy(&temp[3],(uint8_t *)&Temperature + 0,1);
Send_Data(0x54,temp,4);
break;
}
//开启快门
case 0x55: {
Shutter_Open(command_data[0]);
Send_Data(0x55,command_data,1);
break;
}
//关闭快门
case 0x56: {
Shutter_Close(command_data[0]);
Send_Data(0x56,command_data,1);
break;
}
//设置数据处理方式
case 0x60: {
Send_Data_Type = command_data[0];
Target_Average_Times = command_data[1]<<24 | command_data[2]<<16 | command_data[3]<<8 | command_data[4];
Send_Data(0x60,command_data,5);
break;
}
//获取数据
case 0x61: {
Send_Data_Type = Send_Data_Type | 0x8000;
break;
}
}
}
memset(g_usart_rx_buf,0,200);
g_usart_rx_sta=0;
}
}

29
APP/mymain.h Normal file
View File

@ -0,0 +1,29 @@
//
// Created by hu123456 on 2024/1/16.
//
#ifndef FOC_N_MYMAIN_H
#define FOC_N_MYMAIN_H
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"
#include "LED.h"
#include "usart1_tx_dma.h"
#include "communication.h"
#include "delay.h"
#include "IRIS_Method.h"
#include "Shutter.h"
#include "DS18B20.h"
#include "delay.h"
#include "tim.h"
#include "data_analyse.h"
void mymain();
void commander_run(void);
#endif //FOC_N_MYMAIN_H

50
APP/usart1_tx_dma.c Normal file
View File

@ -0,0 +1,50 @@
//
// Created by hu123456 on 2024/8/19.
//
#include "usart1_tx_dma.h"
DMA_HandleTypeDef g_dma_handle;
void dma_init(DMA_Stream_TypeDef *dma_stream_handle, uint32_t ch)
{
if ((uint32_t)dma_stream_handle > (uint32_t)DMA2)
{
__HAL_RCC_DMA2_CLK_ENABLE();
}
else
{
__HAL_RCC_DMA1_CLK_ENABLE();
}
__HAL_LINKDMA(&huart1,hdmatx, g_dma_handle);
/* Tx DMA???? */
g_dma_handle.Instance = dma_stream_handle;
g_dma_handle.Init.Request = ch;
g_dma_handle.Init.Direction = DMA_MEMORY_TO_PERIPH;
g_dma_handle.Init.PeriphInc = DMA_PINC_DISABLE;
g_dma_handle.Init.MemInc = DMA_MINC_ENABLE;
g_dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
g_dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
g_dma_handle.Init.Mode = DMA_NORMAL;
g_dma_handle.Init.Priority = DMA_PRIORITY_MEDIUM;
g_dma_handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
g_dma_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
g_dma_handle.Init.MemBurst = DMA_MBURST_SINGLE;
g_dma_handle.Init.PeriphBurst = DMA_PBURST_SINGLE;
HAL_DMA_DeInit(&g_dma_handle);
HAL_DMA_Init(&g_dma_handle);
}

11
APP/usart1_tx_dma.h Normal file
View File

@ -0,0 +1,11 @@
//
// Created by hu123456 on 2024/8/19.
//
#ifndef H750_S15909_USART1_TX_DMA_H
#define H750_S15909_USART1_TX_DMA_H
#include "mymain.h"
extern DMA_HandleTypeDef g_dma_handle;
void dma_init(DMA_Stream_TypeDef *dma_stream_handle, uint32_t ch);
#endif //H750_S15909_USART1_TX_DMA_H