ADC_F450.cpp
#include "Adc_F450.hpp"
#include "main.h"
#include <rtthread.h>
/*
STM32 所用管脚和ADC通道
PA4 --- ADC1_IN4 --- ADC24V 有
PA6 --- ADC1_IN6 --- ADCI24V 有
PC4 --- ADC1_IN14 --- ADC HALL1 有
PC5 --- ADC1_IN15 --- ADC HALL2 有
PC0 --- ADC1_IN10 --- ADC 5V 有
PC1 --- ADC1_IN11 --- ADC 3.3V 4GV 无
*/
void Adc_F450::Init()
{
/* system clocks configuration */
/* enable GPIOC clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOC);
/* enable ADC clock */
rcu_periph_clock_enable(RCU_ADC0);
/* enable DMA clock */
rcu_periph_clock_enable(RCU_DMA1);
/* config ADC clock */
adc_clock_config(ADC_ADCCK_PCLK2_DIV8);
/* GPIO configuration */
/* config the GPIO as analog mode */
gpio_mode_set(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_4|GPIO_PIN_6);
gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);
/* DMA configuration */
/* ADC_DMA_channel configuration */
dma_single_data_parameter_struct dma_single_data_parameter;
/* ADC DMA_channel configuration */
dma_deinit(DMA1, DMA_CH0);
/* initialize DMA single data mode */
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)adcValueU;
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_32BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 6;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH0, &dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI0);
/* enable DMA circulation mode */
dma_circulation_enable(DMA1, DMA_CH0);
/* enable DMA channel */
dma_channel_enable(DMA1, DMA_CH0);
/* ADC configuration */
/* ADC mode config */
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
/* ADC channel length config */
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);
/* ADC regular channel config */
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_15);
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_11, ADC_SAMPLETIME_15);
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_4, ADC_SAMPLETIME_15);
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_6, ADC_SAMPLETIME_15);
adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_14, ADC_SAMPLETIME_15);
adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_15, ADC_SAMPLETIME_15);
/* ADC trigger config */
adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_T0_CH0);
adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, EXTERNAL_TRIGGER_DISABLE);
/* ADC DMA function enable */
adc_dma_request_after_last_enable(ADC0);
adc_dma_mode_enable(ADC0);
/* enable ADC interface */
adc_enable(ADC0);
/* wait for ADC stability */
rt_thread_mdelay(1);
/* ADC calibration and reset calibration */
adc_calibration_enable(ADC0);
/* enable ADC software trigger */
adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
}
void Adc_F450::SetRatio(uint8_t n, float _ratio)
{
adcRatio[n] = _ratio;
}
void Adc_F450::PeriodRun(void)
{
uint8_t i;
for(i = 0; i < 6; i++)
{
adcValueF[i] = adcValueU[i] * adcRatio[i];
}
}
ADC_F450.hpp
#pragma once
#include "Bsp/bsp.hpp"
#include "LIB/LIB_MATH/M_math.hpp"
class Adc_F450:public Adc
{
public:
Adc_F450(){}
void Init(void);
void SetRatio(uint8_t n, float _ratio);
void PeriodRun();
};
ADC.hpp
#pragma once
#include <stdint.h>
class Adc
{
public:
Adc(){}
uint32_t adcValueU[6];
float adcValueF[6];
float adcRatio[6];
virtual void Init(void){}
virtual void SetRatio(uint8_t n, float _ratio){}
virtual void PeriodRun(){}
};
BSP_F450.cpp
#include "BSP_F450.hpp"
#include "main.h"
#include "IO_F450.hpp"
BSP_F450::BSP_F450()
{
// mcuClock = new McuClock_F4();
led_s = new IO_F450(GPIOD,GPIO_PIN_13);
debugUart = new UART0_F450();
LoRa = new UART1_F450();
sewagePump = new PWM_T0CH0_F450();
cleanWaterPump = new PWM_T0CH2_F450();
cleanWaterValve = new IO_F450(GPIOC,GPIO_PIN_3);
acRelay = new IO_F450(GPIOE,GPIO_PIN_7);
masterSwitch = new IO_F450(GPIOC,GPIO_PIN_3);
emergencyStopSwitch = new IO_F450(GPIOC,GPIO_PIN_3);
PowerSwitch = new IO_F450(GPIOB,GPIO_PIN_0);
powerControl = new IO_F450(GPIOB,GPIO_PIN_1);
cleanWaterLevel[0] = new IO_F450(GPIOD,GPIO_PIN_9);
cleanWaterLevel[1] = new IO_F450(GPIOD,GPIO_PIN_10);
sewageWaterLevel[0] = new IO_F450(GPIOD,GPIO_PIN_11);
sewageWaterLevel[1] = new IO_F450(GPIOD,GPIO_PIN_12);
power_4G = new IO_F450(GPIOC,GPIO_PIN_12);
ws2812b_Spi = new MasterSPI_F450();
gAdc = new Adc_F450();
}
void BSP_F450::Init(void)
{
// mcuClock->Init();
led_s->Init();
debugUart->Init();
LoRa->Init();
sewagePump->Init(20000);
cleanWaterPump->Init(20000);
ws2812b_Spi->Init();
gAdc->Init();
voltage_5V = &gAdc->adcValueF[0];
voltage_4G = &gAdc->adcValueF[1];
voltage_bus = &gAdc->adcValueF[2];
current_bus = &gAdc->adcValueF[3];
current_Charge[0] = &gAdc->adcValueF[4];
current_Charge[1] = &gAdc->adcValueF[5];
gAdc->SetRatio(0,0.001611328125);// x / 4096 * 3.3 * 2.0
gAdc->SetRatio(1,0.001611328125);// x / 4096 * 3.3 * 2.0
gAdc->SetRatio(2,0.012890625);//x / 4096 * 3.3 * 16.0
gAdc->SetRatio(3,1);//
gAdc->SetRatio(4,1);//
gAdc->SetRatio(5,1);//
}
//uint32_t HAL_GetTick(void)
//{
return osKernelGetTickCount();
//}
extern "C"
{
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 */
}
}
BSP_F450.hpp
#pragma once
#include "BSP_config.h"
#include "Bsp/BSP.hpp"
#include "McuClock_F450.hpp"
#include "LED_S_F450.hpp"
#include "IO_F450.hpp"
#include "MasterSPI_F450.hpp"
#ifdef BSP_USING_ADC1
#include "Adc_F4.hpp"
#endif
#ifdef BSP_USING_UART0
#include "Uart0_F450.hpp"
#endif
#ifdef BSP_USING_UART1
#include "Uart1_F450.hpp"
#endif
#include "PWM_F450.hpp"
#include "ADC_F450.hpp"
//#include "PB_F7.hpp"
//#include "HALL_F7.hpp"
//#include "Uart3_F7.hpp"
//#include "ENC_HALL_F7.hpp"
//#include "LEDS_F7.hpp"
//#include "CAN1_F7.hpp"
//#include "IIC1_F7.hpp"
class BSP_F450:public BSP
{
public:
BSP_F450();
void Init(void);
};
BSP.hpp
#pragma once
#include "Bsp/McuClock.hpp"
#include "Bsp/IO.hpp"
#include "Bsp/LED.hpp"
#include "Bsp/UART.hpp"
#include "bsp/pwm.hpp"
#include "bsp/MasterSPI.hpp"
#include "bsp/ADC.hpp"
//#include "adc.hpp"
//#include "../Bsp/CAN.hpp"
//#include "../Bsp/IIC.hpp"
class BSP
{
public:
BSP(){}
McuClock *mcuClock;
IO *led_s;
UART *debugUart;
UART *LoRa;
PWM *sewagePump;
PWM *cleanWaterPump;
IO *cleanWaterValve;
IO *acRelay;
IO *masterSwitch;
IO *emergencyStopSwitch;
IO *PowerSwitch;
IO *powerControl;
IO *cleanWaterLevel[2];
IO *sewageWaterLevel[2];
IO *power_4G;
MasterSPI *ws2812b_Spi;
Adc *gAdc;
float *voltage_bus;
float *current_bus;
float *voltage_5V;
float *voltage_4G;
float *current_Charge[2];
// Adc *gAdc;
// UART *gUSB_UART;
// UART *gUSB_UART;
// CAN *gcan;
// IIC *gIIC;
virtual void Init(void){}
};
版权声明:本文为CSDN博主「cuitsj」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cuuitsj/article/details/122941185
暂无评论