Configuration ADC intégrée Blue Bridge Cup
Tout d'abord, rappelez-vous que la broche et la fonction
correspondante La broche ADC1 est le 8ème canal du PB0 ADC1
Ensuite, nous visitons la bibliothèque de firmware officielle pour trouver le fichier correspondant à la configuration ADC. Le
chemin est le suivant:
bibliothèque de firmware STM32 v3.5 \ STM32F10x_StdPeriph_Lib_V3.5.0 \ Project \ STM32F10x_StdPeriph_Examples \ ADC \ ADC1_DMA \ main.c
nous prenons leur main.c Contenu et place de la configuration ADC1 Entrez,
mettez-le dans la fonction d'initialisation que nous avons éditée nous-mêmes
#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);
}
Ensuite, mettez la structure dans
ADC_InitTypeDef ADC_InitStructure;
Ensuite, nous devons faire quelques changements. L'
officiel est d'initialiser le canal ADC_14
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1,
Nous voulons passer au canal ADC_8
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1,
Ensuite, nous devons initialiser le port GPIO, nous utilisons l'horloge PB0 APB2,
qui peut être trouvée dans le fichier source
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);
Changer en 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);
Alors n'oubliez pas l'horloge
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
Comment obtenir cette valeur à la fin?
Tout d'abord, nous définissons une variable 16 bits
, puis nous utilisons une fonction officielle déjà définie
ADC_GetConversionValue (ADC1)
Cette fonction est difficile à retenir si vous voulez l'écrire pour vous en souvenir devant le milieu et Value Get a little note of it is probablement très bien Conversion
conçue pour prendre la valeur ADC,
nous utilisons cette fonction pour lui attribuer
ADC_value=ADC_GetConversionValue(ADC1);
Ensuite, nous pouvons obtenir la valeur de notre ADC.
Vous trouverez ci-dessous le code complet code
adc.c
#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);
}
Ci-dessous le code ADC.H
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
void My_ADC_Init(void);
#endif
Code de la fonction principale
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);
}
}