stm32双adc交错采集一个通道数据库函数代码
时间: 2023-11-06 17:05:07 浏览: 179
adc.c stm32 双通道采集 型号 RBT6
以下是一个基于STM32的双ADC交错采集一个通道的代码,可以将采集到的数据存储到数据库中:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
// Function prototypes
void ADC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Configuration(void);
void USART_Configuration(void);
void Delay(__IO uint32_t nCount);
// Global variables
uint16_t ADC1ConvertedValue[2];
uint16_t ADC2ConvertedValue[2];
char str[100];
int main(void)
{
// Initialize peripherals
ADC_Configuration();
GPIO_Configuration();
TIM_Configuration();
USART_Configuration();
// Start ADC conversions
ADC_SoftwareStartConv(ADC1);
ADC_SoftwareStartConv(ADC2);
// Infinite loop
while (1)
{
// Wait for conversions to complete
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
while (ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
// Read ADC values
ADC1ConvertedValue[0] = ADC_GetConversionValue(ADC1);
ADC2ConvertedValue[0] = ADC_GetConversionValue(ADC2);
// Wait for next conversion
Delay(0xFFFFF);
// Wait for conversions to complete
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
while (ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
// Read ADC values
ADC1ConvertedValue[1] = ADC_GetConversionValue(ADC1);
ADC2ConvertedValue[1] = ADC_GetConversionValue(ADC2);
// Compute average ADC value
uint32_t ADC1AvgValue = (ADC1ConvertedValue[0] + ADC1ConvertedValue[1]) / 2;
uint32_t ADC2AvgValue = (ADC2ConvertedValue[0] + ADC2ConvertedValue[1]) / 2;
// Store data into database
sprintf(str, "INSERT INTO adc_data (timestamp, channel1, channel2) VALUES (%d, %d, %d)", HAL_GetTick(), ADC1AvgValue, ADC2AvgValue);
// Execute SQL query to store data into database
// Wait for next conversion
Delay(0xFFFFF);
}
}
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
// Enable ADC1 and ADC2 clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
// ADC common configuration
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
// ADC1 configuration
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// ADC2 configuration
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC2;
ADC_Init(ADC2, &ADC_InitStructure);
// Enable ADC1 and ADC2
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
// Calibrate ADC1 and ADC2
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Single;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
while (ADC_GetCalibrationStatus(ADC1) != RESET);
ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC2);
while (ADC_GetCalibrationStatus(ADC2) != RESET);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Enable GPIOA and GPIOC clocks
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
// Configure PA0 (ADC1 channel 0) and PC1 (ADC2 channel 11) as analog inputs
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Enable TIM1 clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
// TIM1 configuration
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 83;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// TIM1 PWM channel 1 configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
// TIM1 PWM channel 2 configuration
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
// Enable TIM1
TIM_Cmd(TIM1, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// Enable USART2 and GPIOA clocks
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
// GPIOA configuration
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Connect USART2 TX and RX pins to GPIOA pins
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
// USART2 configuration
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
// Enable USART2
USART_Cmd(USART2, ENABLE);
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
```
注意:上述代码仅供参考,需要根据具体的硬件平台和数据库接口进行修改和适配。
阅读全文