feat : add ds18b20 driver

This commit is contained in:
chenyf 2024-06-13 16:01:47 +08:00
parent 23c061a6e9
commit a848fcce7d
17 changed files with 738 additions and 89 deletions

View File

@ -1 +1 @@
[]
[{"node":"GPIOA.IDR","expanded":true,"format":0},{"node":"GPIOB.IDR","expanded":true,"format":0},{"node":"GPIOC","expanded":true,"format":0,"pinned":false},{"node":"GPIOD.IDR","expanded":true,"format":0}]

View File

@ -1,6 +1,7 @@
{
"files.associations": {
"stm32f407xx.h": "c",
"stm32f4xx_hal_gpio.h": "c"
"stm32f4xx_hal_gpio.h": "c",
"ds18b20.h": "c"
}
}

View File

@ -56,6 +56,7 @@ void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void TIM2_IRQHandler(void);
void USART1_IRQHandler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */

View File

@ -0,0 +1,24 @@
/** function comment
* @Author: 1343619937@qq.com
* @Date: 2023-09-28 13:35:51
* @LastEditors: 1343619937@qq.com
* @LastEditTime: 2023-09-28 13:36:11
* @FilePath: \Code\User\type_define.h
* @Description: ,`customMade`, koroFileHeader查看配置 : https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*
*/
#ifndef TYPE_DEFINE_H
#define TYPE_DEFINE_H
#ifndef u8
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
#endif
#ifndef true
#define true 1
#define false 0
#endif
#endif

View File

@ -0,0 +1,270 @@
#include "uart_console.h"
#include "debug.h"
typedef struct uart_config
{
char *name;
void (*fun)(u8, void *);
} uart_config;
#define NAME_SIZE 20
#define PAR_SIZE 100
typedef struct uart_console
{
char fun_name[NAME_SIZE];
int par_int[PAR_SIZE];
float par_float[PAR_SIZE];
uart_config *cfg;
// private
u8 pos;
u8 step;
u8 par_num;
u8 fun_ord;
u8 dot : 1; // С<><D0A1>
u8 nega : 1; // <20><><EFBFBD><EFBFBD>
} uart_console;
/*_______________________________________Driver___________________________________________________________*/
static u8 str_cmp(char *char1, char *char2, u8 len)
{
for (int i = 0; i < NAME_SIZE; i++)
{
if ((*char1 == *char2) && *char1 == '\0')
{
return 1;
}
if (*char1 != *char2)
{
return 0;
}
char1++;
char2++;
}
return 1;
}
/** function comment
* @description:
* @param {uart_console} *tis
* @return {*}
*/
u8 uart_console_name_analysis(uart_console *tis)
{
uart_config *cfg = tis->cfg;
u8 ord = 0;
while (!(str_cmp(cfg[ord].name, "end", 3)))
{
if (str_cmp(cfg[ord].name, tis->fun_name, tis->pos))
{
return ord;
}
ord++;
}
return 255;
}
extern uart_console console;
/** function comment
* @description:
* @return {*}
*/
void uart_console_put_char(u8 data)
{
uart_console *tis = &console;
if (data == '@')
{
tis->step = 0;
tis->pos = 0;
return;
}
switch (tis->step)
{
case 0:
if (data == '(')
{
tis->fun_ord = uart_console_name_analysis(tis);
// Printf("name is %d\n", tis->fun_ord);
if (tis->fun_ord != 255)
{
tis->nega = 0;
tis->step++;
tis->pos = 0;
tis->par_num = 0;
tis->dot = 0;
tis->par_int[0] = 0;
// Printf("name is %d\n", tis->fun_ord);
}
else
{
tis->step = 0;
tis->pos = 0;
}
}
else
{
tis->fun_name[tis->pos] = data;
tis->pos++;
tis->fun_name[tis->pos] = '\0';
if (tis->pos == NAME_SIZE)
{
Printf("error: uart console buffer overflow\n");
tis->pos = 0;
}
}
break;
case 1:
if (data == ')')
{
tis->cfg[tis->fun_ord].fun(tis->par_num, tis);
tis->step = 0;
return;
}
else
{
tis->step++;
}
case 2:
if (data == ')')
{
while (tis->par_float[tis->pos] > 1)
{
tis->par_float[tis->pos] = tis->par_float[tis->pos] / 10.0;
}
tis->par_float[tis->pos] = (float)tis->par_int[tis->pos] + tis->par_float[tis->pos];
if (tis->nega == 1)
{
Printf("nega\n");
tis->par_float[tis->pos] = tis->par_float[tis->pos] * (-1.0);
tis->par_int[tis->pos] = tis->par_int[tis->pos] * (-1);
}
tis->par_num++;
tis->cfg[tis->fun_ord].fun(tis->par_num, tis);
tis->step = 0;
return;
}
else if (data == '.')
{
tis->dot = 1;
return;
}
else if (data == '-')
{
tis->nega = 1;
return;
}
else if (data == ',')
{
while (tis->par_float[tis->pos] > 1)
{
tis->par_float[tis->pos] = tis->par_float[tis->pos] / 10.0;
}
tis->par_float[tis->pos] = (float)tis->par_int[tis->pos] + tis->par_float[tis->pos];
if (tis->nega == 1)
{
Printf("nega\n");
tis->par_float[tis->pos] = tis->par_float[tis->pos] * (-1.0);
tis->par_int[tis->pos] = tis->par_int[tis->pos] * (-1);
}
tis->nega = 0;
tis->dot = 0;
tis->pos++;
tis->par_num++;
tis->par_float[tis->pos] = 0.0;
tis->par_int[tis->pos] = 0;
if (tis->par_num == PAR_SIZE)
{
Printf("error : uart console par overflow\n");
tis->par_num = 0;
}
return;
}
else if (data < '0' || data > '9')
{
Printf("error: uart console illegal parameter\n");
return;
}
if (!tis->dot)
{
tis->par_int[tis->pos] = tis->par_int[tis->pos] * 10 + data - '0';
// Printf("%d\n",tis->par_int[tis->pos]);
}
else
{
tis->par_float[tis->pos] = tis->par_float[tis->pos] * 10 + data - '0';
}
default:
break;
}
}
/*__________________________________________________________________________________________________*/
/*__________________________________________user________________________________________________________*/
static void test_cmd(u8 par_num, void *par)
{
uart_console *tis = (uart_console *)par;
Printf("test cmd %d\n", par_num);
for (int i = 0; i < par_num; i++)
{
Printf(" %d,%f\n", tis->par_int[i], tis->par_float[i]);
}
}
static void pwm_set(u8 par_num, void *par)
{
uart_console *tis = (uart_console *)par;
extern void duty_set(unsigned char ch, unsigned char _duty);
duty_set((unsigned char)tis->par_int[0], (unsigned char)tis->par_int[1]);
}
static void test_ds18b20(u8 par_num, void *par){
uart_console *tis = (uart_console *)par;
#include "ds18b20.h"
#include "debug.h"
float temp = ds18b20_read_temp(0);
Printf("temp = %f\n", temp);
ds18b20_start_convert(0);
// ds18b20_rst(0);
// ds18b20_write_data(0, 0xcc);
}
static uart_config cfg[] = {
{"test", test_cmd},
{"pwm", pwm_set},
{"ds18b20",test_ds18b20},
{"end", NULL},
};
uart_console console = {
.step = 0,
.cfg = cfg,
};
// int main()
// {
// u8 data[] = "@test123(12,3214,2183,0.232434,-12321)";
// for (int i = 0; i < sizeof(data); i++)
// {
// uart_console_put_char(data[i]);
// }
// }

View File

@ -0,0 +1,7 @@
#ifndef UART_CONSOLE_H
#define UART_CONSOLE_H
#include "type_define.h"
#include "stdio.h"
void uart_console_put_char(u8 data);
#endif

View File

@ -133,6 +133,10 @@ void Printf(const char *format, ...)
{
float valflt = va_arg(ap, double);
printfloat(valflt);
float dot = valflt - (int)valflt;
if(dot == 0){
PutChar('0');
}
format++;
break;
}

View File

@ -21,6 +21,15 @@ void gpio_output_set(GPIO_TypeDef *gpio, uint16_t pin)
void gpio_init()
{
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
// fan read
gpio_input_set(GPIOD, GPIO_PIN_4);
gpio_input_set(GPIOD, GPIO_PIN_5);
@ -29,12 +38,12 @@ void gpio_init()
gpio_input_set(GPIOB, GPIO_PIN_4);
gpio_input_set(GPIOB, GPIO_PIN_3);
// pwm output
// gpio_output_set(GPIOC, GPIO_PIN_6);
// gpio_output_set(GPIOC, GPIO_PIN_7);
// gpio_output_set(GPIOC, GPIO_PIN_8);
// gpio_output_set(GPIOC, GPIO_PIN_9);
gpio_output_set(GPIOC, GPIO_PIN_6);
gpio_output_set(GPIOC, GPIO_PIN_7);
gpio_output_set(GPIOC, GPIO_PIN_8);
gpio_output_set(GPIOC, GPIO_PIN_9);
gpio_output_set(GPIOA, GPIO_PIN_5);
// gpio_output_set(GPIOA, GPIO_PIN_5);
}
// void start_time()
// {
@ -78,6 +87,8 @@ void gpio_save_speed()
SAVE_SPEED(speed[3], cnt[3]);
SAVE_SPEED(speed[4], cnt[4]);
SAVE_SPEED(speed[5], cnt[5]);
#include "debug.h"
// Printf("speed:%d,%d,%d,%d,%d\n", speed[0], speed[1], speed[2], speed[3], speed[4]);
}
void gpio_scan()
{
@ -106,40 +117,46 @@ void gpio_scan()
}
}
static char duty[4] = {90, 60, 40, 10};
static char now = 0;
static volatile int duty[4] = {90, 60, 40, 10};
static volatile int now = 0;
#define PWM_OUT(duty, now, io_deal) \
if (duty < now) \
if (duty <= now) \
{ \
io_deal = 1; \
io_deal = 0; \
} \
else \
{ \
io_deal = 0; \
io_deal = 1; \
}
void gpio_pwm_output()
{
// static char cnt = 0;
// if (cnt < 10)
// {
// cnt++;
// return;
// }
// cnt = 0;
// PAout(5) = 1;
// PCout(6) = 1;
// PCout(7) = 1;
// HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
// PCout(8) = 1;
// PCout(9) = 1;
// PAout(5) = 0;
// PWM_OUT(duty[0], now, PCout(6));
// PWM_OUT(duty[1], now, PCout(7));
// PWM_OUT(duty[2], now, PCout(8));
// PWM_OUT(duty[3], now, PCout(9));
// now++;
// if (now == 99)
// {
// now = 0;
// }
static char cnt = 0;
if (cnt < 10)
{
cnt++;
return;
}
cnt = 0;
PWM_OUT(duty[0], now, PCout(6));
PWM_OUT(duty[1], now, PCout(7));
PWM_OUT(duty[2], now, PCout(8));
PWM_OUT(duty[3], now, PCout(9));
now++;
if (now == 100)
{
now = 0;
}
}
void duty_set(unsigned char ch, unsigned char _duty)
{
if (ch >= sizeof(duty))
{
return;
}
duty[ch] = _duty;
}
void soft_gpio_function()
{
gpio_pwm_output();
gpio_scan();
}

View File

@ -62,13 +62,20 @@ void putChar(char c)
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
unsigned char RX_buf;
extern void uart_console_put_char(unsigned char data);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uart_console_put_char(RX_buf);
HAL_UART_Receive_IT(&huart1, (unsigned char *)&RX_buf, 1); // 接收一个字节
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
* @brief The application entry point.
* @retval int
*/
int main(void)
{
@ -99,6 +106,10 @@ int main(void)
/* USER CODE BEGIN 2 */
extern void gpio_init();
gpio_init();
HAL_TIM_Base_Start_IT(&htim2);
extern void ds18b20_task_init();
ds18b20_task_init();
HAL_Delay(3);
/* USER CODE END 2 */
/* Infinite loop */
@ -108,27 +119,31 @@ int main(void)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// Printf(".");
extern void ds18b20_task_read();
ds18b20_task_read();
// HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
@ -144,9 +159,8 @@ void SystemClock_Config(void)
}
/** 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.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_DIV4;
@ -159,10 +173,10 @@ void SystemClock_Config(void)
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
@ -177,9 +191,9 @@ static void MX_TIM2_Init(void)
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 84-1;
htim2.Init.Prescaler = 84 - 1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000-1;
htim2.Init.Period = 1000 - 1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
@ -200,14 +214,13 @@ static void MX_TIM2_Init(void)
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
@ -231,27 +244,26 @@ static void MX_USART1_UART_Init(void)
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
HAL_UART_Receive_IT(&huart1, (unsigned char *)&RX_buf, 1);
/* USER CODE END USART1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
@ -259,9 +271,9 @@ static void MX_GPIO_Init(void)
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
@ -273,15 +285,15 @@ void Error_Handler(void)
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
#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)
* @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(unsigned char *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,

View File

@ -156,6 +156,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart)
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USART1 interrupt Init */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
@ -185,6 +188,8 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
/* USART1 interrupt DeInit */
HAL_NVIC_DisableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_MspDeInit 1 */
/* USER CODE END USART1_MspDeInit 1 */

View File

@ -56,6 +56,7 @@
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim2;
extern UART_HandleTypeDef huart1;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
@ -207,15 +208,26 @@ void TIM2_IRQHandler(void)
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
extern void gpio_scan();
gpio_scan();
extern void gpio_pwm_output();
gpio_pwm_output();
/* USER CODE BEGIN TIM2_IRQn 1 */
extern void soft_gpio_function();
soft_gpio_function();
/* USER CODE END TIM2_IRQn 1 */
}
/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@ -0,0 +1,123 @@
/**
* @ Author: Yifan Chen
* @ Create Time: 2024-06-13 09:04:07
* @ Modified by: Yifan Chen
* @ Modified time: 2024-06-13 15:03:03
* @ Description:
*/
#include "ds18b20.h"
#include <stdio.h>
extern void ds18b20_io_out(unsigned char ch);
extern void ds18b20_write(unsigned char ch, unsigned char value);
extern unsigned char ds18b20_read(unsigned char ch);
__attribute__((weak)) void delay_us(unsigned int us)
{
for (volatile int i = 0; i < us; i++)
{
for (volatile int j = 0; j < 13; j++)
{
;
}
}
}
/*
DS18B20写入数据
*/
void ds18b20_write_data(unsigned char ch, unsigned char byte)
{
unsigned char tempIndex, tempData;
// ds18b20_io_out(ch);
for (tempIndex = 1; tempIndex <= 8; tempIndex++)
{
tempData = (byte & 0x01);
byte >>= 1;
if (tempData == 1)
{
ds18b20_write(ch, 0); // 低电平
delay_us(2);
ds18b20_write(ch, 1); // 高电平
delay_us(60); // 延时60us
}
else
{
ds18b20_write(ch, 0); // 低电平
delay_us(60); // 延时60us
ds18b20_write(ch, 1); // 高电平
delay_us(2);
}
}
}
/*
DS18B20一位数据
*/
unsigned char ds18b20_read_bit(unsigned char ch)
{
unsigned char data = 0;
ds18b20_write(ch, 0); // 低电平
delay_us(2);
ds18b20_write(ch, 1); // 高电平
delay_us(15);
data = ds18b20_read(ch);
// ds18b20_io_out(ch); // 设置为输出
ds18b20_write(ch, 1); // 高电平
delay_us(45);
return data;
}
/*
DS18B20的数据
*/
unsigned char ds18b20_read_data(unsigned char ch)
{
unsigned char i, j, data = 0;
for (i = 1; i <= 8; i++)
{
j = ds18b20_read_bit(ch);
data = (j << 7) | (data >> 1);
}
return data;
}
/**
* return 0 : rst success return 1 : rst failure
*/
char ds18b20_rst(unsigned char io)
{
// ds18b20_io_out(io);
ds18b20_write(io, 0);
delay_us(480);
ds18b20_write(io, 1);
delay_us(120);
unsigned char val = ds18b20_read(io);
// ds18b20_io_out(io);
ds18b20_write(io, 1);
delay_us(360);
return val;
}
float ds18b20_read_temp(unsigned char ch)
{
unsigned short temp = 0;
ds18b20_rst(ch);
ds18b20_write_data(ch, 0xcc);
ds18b20_write_data(ch, 0xbe);
delay_us(1000);
temp = ds18b20_read_data(ch);
temp |= (ds18b20_read_data(ch) << 8);
float temp_f = 0.0625 * (float)temp;
return temp_f;
}
void ds18b20_start_convert(unsigned char ch)
{
ds18b20_rst(ch);
ds18b20_write_data(ch, 0xcc);
ds18b20_write_data(ch, 0x44);
}
void ds18b20_io_init(unsigned char ch)
{
ds18b20_io_out(ch);
ds18b20_write(ch, 1);
}

View File

@ -0,0 +1,17 @@
/**
* @ Author: Yifan Chen
* @ Create Time: 2024-06-13 09:38:54
* @ Modified by: Yifan Chen
* @ Modified time: 2024-06-13 15:21:22
* @ Description:
*/
#ifndef DS18B20_H
#define DS18B20_H
extern float ds18b20_read_temp(unsigned char ch);
extern void ds18b20_start_convert(unsigned char ch);
extern void ds18b20_io_init(unsigned char ch);
extern char ds18b20_rst(unsigned char io);
extern unsigned char ds18b20_read_data(unsigned char ch);
extern void ds18b20_write_data(unsigned char ch, unsigned char byte);
#endif

View File

@ -0,0 +1,96 @@
/**
* @ Author: Yifan Chen
* @ Create Time: 2024-06-13 09:29:29
* @ Modified by: Yifan Chen
* @ Modified time: 2024-06-13 15:15:46
* @ Description:
*/
#include "stm32f4xx_hal.h"
#include "stm32f407xx.h"
/**
* GPIO
*/
__weak void gpio_input_set(GPIO_TypeDef *gpio, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(gpio, &GPIO_InitStruct);
}
__weak void gpio_output_set(GPIO_TypeDef *gpio, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(gpio, &GPIO_InitStruct);
}
void ds18b20_io_out(unsigned char ch)
{
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
if (ch == 0)
{
gpio_output_set(GPIOD, GPIO_PIN_14);
}
else if (ch == 1)
{
gpio_output_set(GPIOD, GPIO_PIN_15);
}
else if (ch == 2)
{
gpio_output_set(GPIOD, GPIO_PIN_0);
}
else if (ch == 3)
{
gpio_output_set(GPIOD, GPIO_PIN_1);
}
else if (ch == 4)
{
gpio_output_set(GPIOE, GPIO_PIN_7);
}
}
void ds18b20_write(unsigned char ch, unsigned char value)
{
if (ch == 0)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, value);
}
else if (ch == 1)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, value);
}else if (ch == 2){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0, value);
}else if(ch ==3){
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, value);
}
else if(ch == 4){
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, value);
}
}
unsigned char ds18b20_read(unsigned char ch)
{
if (ch == 0)
{
return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14);
}
else if (ch == 1)
{
return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15);
}
else if (ch == 2)
{
return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_0);
}
else if (ch == 3)
{
return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_1);
}
else if (ch == 4)
{
return HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_7);
}
}

View File

@ -0,0 +1,53 @@
#include "ds18b20.h"
/**
* DS18B20 soft_gpio对DS18B20的影响
* 18B20的数据18B20数据是否产生变化error
*/
#if 0
#define DS18B20_NUMBER 5
void ds18b20_task_init()
{
for (int i = 0; i < DS18B20_NUMBER; i++)
{
ds18b20_io_init(i);
}
for (volatile int i = 0; i < 10 * 1000 * 1000; i++)
{
;
}
for (int i = 0; i < DS18B20_NUMBER; i++)
{
ds18b20_start_convert(i);
}
}
float temp_keep[DS18B20_NUMBER];
void ds18b20_task_read()
{
static int first = 100;
#include "debug.h"
for (int i = 0; i < DS18B20_NUMBER; i++)
{
float temp = ds18b20_read_temp(i);
// ds18b20_start_convert(i);
if (first > 0)
{
temp_keep[i] = temp;
Printf("%f,", temp);
}
else
{
if (temp_keep[i] != temp)
{
Printf("[error %d,%f,%f]", i, temp, temp_keep[i]);
}
}
}
if (first > 0)
{
first--;
Printf("\r\n");
}
// Printf("\r\n");
}
#endif

View File

@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Wed Jun 12 16:44:57 CST 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Thu Jun 13 08:13:32 CST 2024]
##########################################################################################################################
# ------------------------------------------------
@ -59,7 +59,11 @@ Core/Src/system_stm32f4xx.c \
Core/Src/sysmem.c \
Core/Src/syscalls.c \
Core/Src/debug.c \
Core/Src/gpio/gpio_scan.c
Core/Src/gpio/soft_gpio.c \
Core/Src/console/uart_console.c \
Core/Src/temp_sensor/ds18b20_hal.c \
Core/Src/temp_sensor/ds18b20.c \
Core/Src/temp_sensor/ds18b20_task.c
# ASM sources
ASM_SOURCES = \
@ -124,7 +128,9 @@ C_INCLUDES = \
-IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F4xx/Include \
-IDrivers/CMSIS/Include \
-ICore/Src/gpio
-ICore/Src/gpio \
-ICore/Src/console \
-ICore/Src/temp_sensor
# compile gcc flags

View File

@ -40,6 +40,7 @@ NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
NVIC.TIM2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
PA10.Locked=true
PA10.Mode=Asynchronous
@ -86,7 +87,7 @@ ProjectManager.ToolChainLocation=
ProjectManager.UAScriptAfterPath=
ProjectManager.UAScriptBeforePath=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART1_UART_Init-USART1-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_TIM2_Init-TIM2-false-HAL-true
RCC.48MHZClocksFreq_Value=84000000
RCC.AHBFreq_Value=168000000
RCC.APB1CLKDivider=RCC_HCLK_DIV4