ADC implementation fairly working
This commit is contained in:
parent
40e7c07f20
commit
62a9a434c8
Binary file not shown.
@ -9,6 +9,7 @@ RM := rm -rf
|
|||||||
# All of the sources participating in the build are defined here
|
# All of the sources participating in the build are defined here
|
||||||
-include sources.mk
|
-include sources.mk
|
||||||
-include startup/subdir.mk
|
-include startup/subdir.mk
|
||||||
|
-include src/drivers/subdir.mk
|
||||||
-include src/subdir.mk
|
-include src/subdir.mk
|
||||||
-include subdir.mk
|
-include subdir.mk
|
||||||
-include objects.mk
|
-include objects.mk
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
"src/drivers/adc.o"
|
||||||
"src/main.o"
|
"src/main.o"
|
||||||
"src/stm32f4xx_it.o"
|
"src/stm32f4xx_it.o"
|
||||||
"src/syscalls.o"
|
"src/syscalls.o"
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -15,6 +15,7 @@ C_DEPS :=
|
|||||||
|
|
||||||
# Every subdirectory with source files must be described here
|
# Every subdirectory with source files must be described here
|
||||||
SUBDIRS := \
|
SUBDIRS := \
|
||||||
|
src/drivers \
|
||||||
src \
|
src \
|
||||||
startup \
|
startup \
|
||||||
|
|
||||||
|
54
UAV-ControlSystem/inc/drivers/adc.h
Normal file
54
UAV-ControlSystem/inc/drivers/adc.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* ADC.h
|
||||||
|
*
|
||||||
|
* Created on: 13 sep. 2016
|
||||||
|
* Author: Philip
|
||||||
|
*/
|
||||||
|
/**********************************************************************
|
||||||
|
* NAME: adc.h *
|
||||||
|
* PURPOSE: Set up and read from ADC *
|
||||||
|
* INFORMATION: *
|
||||||
|
* How to use this driver is explained in page 107 of HAL driver *
|
||||||
|
* Enable the ADC clock *
|
||||||
|
* Enable the GPIO clock for the pin wanted *
|
||||||
|
* Configure the GPIO pin as analog input *
|
||||||
|
* Configure the ADC speed (prescaler/sampling time) *
|
||||||
|
* Enable continuous measurement mode *
|
||||||
|
* *
|
||||||
|
* Read more at: www.visualgdb.com/tutorials/arm/stm32/adc/ *
|
||||||
|
* *
|
||||||
|
* GLOBAL VARIABLES: *
|
||||||
|
* Variable Type Description *
|
||||||
|
* -------- ---- ----------- *
|
||||||
|
* *
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
#ifndef DRIVERS_ADC_H_
|
||||||
|
#define DRIVERS_ADC_H_
|
||||||
|
|
||||||
|
#include "stm32f4xx.h"
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF: *
|
||||||
|
* INFORMATION: *
|
||||||
|
***********************************************************************/
|
||||||
|
void adc_configure();
|
||||||
|
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF: *
|
||||||
|
* INFORMATION: *
|
||||||
|
***********************************************************************/
|
||||||
|
void adc_pin_config(ADC_HandleTypeDef * g_AdcHandle, uint32_t adc_channel);
|
||||||
|
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
* BRIEF: *
|
||||||
|
* INFORMATION: *
|
||||||
|
***********************************************************************/
|
||||||
|
uint32_t adc_read_int(ADC_HandleTypeDef * g_AdcHandle);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* DRIVERS_ADC_H_ */
|
77
UAV-ControlSystem/src/drivers/adc.c
Normal file
77
UAV-ControlSystem/src/drivers/adc.c
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* ADC.c
|
||||||
|
*
|
||||||
|
* Created on: 13 sep. 2016
|
||||||
|
* Author: Philip
|
||||||
|
*/
|
||||||
|
#include <drivers/adc.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void adc_configure()
|
||||||
|
{
|
||||||
|
__GPIOC_CLK_ENABLE();
|
||||||
|
__ADC1_CLK_ENABLE();
|
||||||
|
}
|
||||||
|
|
||||||
|
void adc_pin_config(ADC_HandleTypeDef * g_AdcHandle, uint32_t adc_channel)
|
||||||
|
{
|
||||||
|
//ADC_HandleTypeDef g_AdcHandle;
|
||||||
|
GPIO_InitTypeDef gpioInit;
|
||||||
|
|
||||||
|
// TODO!!!!
|
||||||
|
if (adc_channel == ADC_CHANNEL_12) gpioInit.Pin = GPIO_PIN_2;
|
||||||
|
else gpioInit.Pin = GPIO_PIN_1;
|
||||||
|
|
||||||
|
|
||||||
|
gpioInit.Mode = GPIO_MODE_ANALOG;
|
||||||
|
gpioInit.Pull = GPIO_NOPULL;
|
||||||
|
HAL_GPIO_Init(GPIOC,&gpioInit);
|
||||||
|
|
||||||
|
HAL_NVIC_SetPriority(ADC_IRQn, 0,0);
|
||||||
|
HAL_NVIC_EnableIRQ(ADC_IRQn);
|
||||||
|
|
||||||
|
ADC_ChannelConfTypeDef adcChannel;
|
||||||
|
|
||||||
|
|
||||||
|
g_AdcHandle->Instance = ADC1;
|
||||||
|
g_AdcHandle->Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
|
||||||
|
g_AdcHandle->Init.Resolution = ADC_RESOLUTION12b;
|
||||||
|
g_AdcHandle->Init.ScanConvMode = DISABLE;
|
||||||
|
g_AdcHandle->Init.ContinuousConvMode = ENABLE;
|
||||||
|
g_AdcHandle->Init.DiscontinuousConvMode = DISABLE;
|
||||||
|
g_AdcHandle->Init.NbrOfDiscConversion = 0;
|
||||||
|
g_AdcHandle->Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONV_T1_CC1;
|
||||||
|
g_AdcHandle->Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||||
|
g_AdcHandle->Init.NbrOfConversion = 1;
|
||||||
|
g_AdcHandle->Init.DMAContinuousRequests = ENABLE;
|
||||||
|
g_AdcHandle->Init.EOCSelection = DISABLE;
|
||||||
|
|
||||||
|
HAL_ADC_Init(g_AdcHandle);
|
||||||
|
|
||||||
|
adcChannel.Channel = adc_channel; // ex ADC_CHANNEL_12
|
||||||
|
adcChannel.Rank = 1;
|
||||||
|
adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES;
|
||||||
|
adcChannel.Offset = 0;
|
||||||
|
|
||||||
|
if (HAL_ADC_ConfigChannel(g_AdcHandle, &adcChannel) != HAL_OK)
|
||||||
|
{
|
||||||
|
asm("bkpt 255");
|
||||||
|
}
|
||||||
|
|
||||||
|
HAL_ADC_Start(g_AdcHandle);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t adc_read_int(ADC_HandleTypeDef * g_AdcHandle)
|
||||||
|
{
|
||||||
|
uint32_t value = 0;
|
||||||
|
if (HAL_ADC_PollForConversion(g_AdcHandle, 100) == HAL_OK)
|
||||||
|
{
|
||||||
|
value = HAL_ADC_GetValue(g_AdcHandle);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -9,14 +9,23 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <drivers/adc.h>
|
||||||
#include "stm32f4xx.h"
|
#include "stm32f4xx.h"
|
||||||
|
|
||||||
/* Private function prototypes -----------------------------------------------*/
|
/* Private function prototypes -----------------------------------------------*/
|
||||||
static void SystemClock_Config(void);
|
static void SystemClock_Config(void);
|
||||||
static void Error_Handler(void);
|
static void Error_Handler(void);
|
||||||
|
|
||||||
|
ADC_HandleTypeDef adc_testinput_handle; // For example battery voltage
|
||||||
|
ADC_HandleTypeDef adc_test2input_handle;
|
||||||
|
uint32_t g_ADCValue;
|
||||||
|
uint32_t g_ADC2;
|
||||||
|
int g_MeasurementNumber;
|
||||||
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
// Comment?
|
||||||
HAL_Init();
|
HAL_Init();
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
@ -25,6 +34,11 @@ int main(void)
|
|||||||
/* Configure the system clock to 100 MHz */
|
/* Configure the system clock to 100 MHz */
|
||||||
SystemClock_Config();
|
SystemClock_Config();
|
||||||
|
|
||||||
|
adc_configure();
|
||||||
|
//adc_pin_config(&adc_testinput_handle,ADC_CHANNEL_12);
|
||||||
|
adc_pin_config(&adc_testinput_handle,ADC_CHANNEL_11);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*##-1- Enable GPIOA Clock (to be able to program the configuration registers) */
|
/*##-1- Enable GPIOA Clock (to be able to program the configuration registers) */
|
||||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||||
@ -50,6 +64,13 @@ int main(void)
|
|||||||
i++;
|
i++;
|
||||||
/* Insert a 100ms delay */
|
/* Insert a 100ms delay */
|
||||||
HAL_Delay(100);
|
HAL_Delay(100);
|
||||||
|
|
||||||
|
|
||||||
|
// ADC part
|
||||||
|
//g_ADCValue = adc_read_int(&adc_testinput_handle);
|
||||||
|
HAL_Delay(10);
|
||||||
|
g_ADC2 = adc_read_int(&adc_test2input_handle);
|
||||||
|
g_MeasurementNumber++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(;;);
|
for(;;);
|
||||||
@ -117,23 +138,9 @@ static void SystemClock_Config(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigureADC()
|
|
||||||
{
|
|
||||||
GPIO_InitTypeDef gpioInit;
|
|
||||||
__GPIOC_CLK_ENABLE();
|
|
||||||
__ADC1_CLK_ENABLE();
|
|
||||||
|
|
||||||
gpioInit.Pin = GPIO_PIN_1;
|
|
||||||
gpioInit.Mode = GPIO_MODE_ANALOG;
|
|
||||||
gpioInit.Pull = GPIO_NOPULL;
|
|
||||||
HAL_GPIO_Init(GPIOC,&gpioInit);
|
|
||||||
|
|
||||||
HAL_NVIC_SetPriority(ADC_IRQn,0,0);
|
|
||||||
HAL_NVIC_EnableIRQ(ADC_IRQn);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief This function is executed in case of error occurrence.
|
* @brief This function is executed in case of error occurrence.
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="1489874316490887047" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="1758116234699011172" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||||
</provider>
|
</provider>
|
||||||
@ -16,7 +16,7 @@
|
|||||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
|
||||||
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="1489874316490887047" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
<provider class="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" console="false" env-hash="1758116234699011172" id="fr.ac6.mcu.ide.build.CrossBuiltinSpecsDetector" keep-relative-paths="false" name="Ac6 SW4 STM32 MCU Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||||
</provider>
|
</provider>
|
||||||
|
Reference in New Issue
Block a user