Configuración ADC integrada de Blue Bridge Cup
En primer lugar, recuerde que el pin y la función correspondiente
pin ADC1 es el octavo canal de PB0 ADC1
Luego visitamos la biblioteca de firmware oficial para encontrar el archivo correspondiente a la configuración ADC. La
ruta es la siguiente
biblioteca de firmware STM32 v3.5 \ STM32F10x_StdPeriph_Lib_V3.5.0 \ Project \ STM32F10x_StdPeriph_Examples \ ADC \ ADC1_DMA \ main.c
tomamos su main.c Contenido y lugar de configuración de ADC1 Entra,
ponlo en la función de inicialización que nosotros mismos editamos
#include adc.h
void my_adc_init()
{
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
Luego coloque la estructura en
ADC_InitTypeDef ADC_InitStructure;
Luego tenemos que hacer algunos cambios. El
oficial es inicializar el canal ADC_14
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1,
Queremos cambiar al canal ADC_8
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1,
Luego necesitamos inicializar el puerto GPIO usamos el reloj PB0 APB2,
que se puede encontrar en el archivo fuente
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
Cambiar a PB0
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
Entonces no olvides el reloj
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
¿Cómo obtenemos este valor al final?
Primero, definimos una variable de 16 bits
y luego usamos una función oficial ya definida
ADC_GetConversionValue (ADC1)
Esta función es difícil de recordar si desea escribirla para recordarla delante del medio y Value Get una pequeña nota de ella es probablemente una buena conversión
diseñada para tomar el valor ADC,
usamos esta función para asignárselo
ADC_value=ADC_GetConversionValue(ADC1);
Entonces podemos obtener el valor de nuestro ADC.
A continuación se muestra el código completo
adc.c code
#include "adc.h"
void My_ADC_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure PB0 (ADC Channel8) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel18 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
A continuación se muestra el código de ADC.H
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
void My_ADC_Init(void);
#endif
Código de función principal
int main(void)
{
TIM2_Config();
SysTick_Config(SystemCoreClock/1000);
LED_Init();
BEEP_Init();
TIM2_Config();
STM3210B_LCD_Init();
USART_Config();
LCD_Clear(Blue);
TIM3_PWM_Init();
LCD_SetBackColor(Yellow);
LCD_DisplayStringLine(Line0," hello world ");
RTC_Configuration();
Time_Adjust(23,59,55);
printf("hello world");
My_ADC_Init();
while(1)
{
ADC_value=ADC_GetConversionValue(ADC1);
printf("ADC = %d\n",ADC_value);
}
}