Charles Andre
/
Register_level_analog
some success, still errors
Diff: main.cpp
- Revision:
- 108:b8d05666d95f
- Parent:
- 102:780b4ecac614
- Child:
- 109:20827b1e5195
--- a/main.cpp Wed Nov 27 06:49:46 2019 +0000 +++ b/main.cpp Thu Nov 28 22:26:38 2019 +0000 @@ -9,79 +9,52 @@ #include <AnalogIn.h> #include <AnalogOut.h> +#include "stm32l1xx_hal.h" +#include "stm32l1xx_hal_def.h" +#include "stm32l1xx_hal_adc_ex.h" + AnalogOut v_src(GPIO0); AnalogIn therm(GPIO2); #define SLEEP_TIME 50 // (msec) #define PRINT_AFTER_N_LOOPS 20 + +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(ADC1_IRQn, 0, 0); - HAL_NVIC_EnableIRQ(ADC1_IRQn); - - ADC_ChannelConfTypeDef adcChannel; - // error here - g_AdcHandle.Instance = ADC1; - - g_AdcHandle.Init.ClockPrescaler = ADC1_CLOCKPRESCALER_PCLK_DIV2; - g_AdcHandle.Init.Resolution = ADC1_RESOLUTION_12B; - g_AdcHandle.Init.ScanConvMode = DISABLE; - g_AdcHandle.Init.ContinuousConvMode = ENABLE; - g_AdcHandle.Init.DiscontinuousConvMode = DISABLE; - g_AdcHandle.Init.NbrOfDiscConversion = 0; - g_AdcHandle.Init.ExternalTrigConvEdge = ADC1_EXTERNALTRIGCONVEDGE_NONE; - g_AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; - g_AdcHandle.Init.DataAlign = ADC1_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_11; - adcChannel.Rank = 1; - adcChannel.SamplingTime = ADC_SAMPLETIME_480CYCLES; - adcChannel.Offset = 0; - - if (HAL_ADC_ConfigChannel(&g_AdcHandle, &adcChannel) != HAL_OK) - { - asm("bkpt 255"); - } + + } // main() runs in its own thread in the OS int main() { - GPIO_InitTypeDef GPIO_InitStructure; //Variable used to setup the GPIO pins - SystemReport sys_state( SLEEP_TIME * PRINT_AFTER_N_LOOPS /* Loop delay time in ms */); - v_src = 1.0; /* Going to use a digital output as V_src */ - float max = 0; - while (true) { - thread_sleep_for(SLEEP_TIME); - v_src = 1.0; - float raw_analog_val = 0; - - for(int i = 0; i < 10; i++) { - raw_analog_val += therm.read(); - } - raw_analog_val /= 10; - printf("Raw Analog Percentage 10 read AVG %f\n", raw_analog_val); - if (raw_analog_val > max) { - max = raw_analog_val; - printf("NEW MAX: %f\n", max); - thread_sleep_for(3000); - } - } + SystemInit(); + /* Setup GPIO for LEDs */ + RCC->APB2ENR |= 1 << 6; /* Enable GPIOE clock */ + + /* Setup and initialize ADC converter */ + RCC->APB2ENR |= 1 << 9; /* Enable ADC1 clock */ + //GPIOC->CRL &= 0xFFF0FFFF; /* Configure PC4 as ADC.14 input */ + ADC1->SQR1 = 0x00000000; /* Regular channel 1 conversion */ + ADC1->SQR2 = 0x00000000; /* Clear register */ + ADC1->SQR3 = 14 << 0; /* SQ1 = channel 14 */ + ADC1->SMPR1 = 5 << 12; /* Channel 14 sample time is 55.5 cyc */ + ADC1->SMPR2 = 0x00000000; /* Clear register */ + ADC1->CR1 = 1 << 8; /* Scan mode on */ + ADC1->CR2 = (1 << 20) | /* Enable external trigger */ + (7 << 17) | /* EXTSEL = SWSTART */ + (1 << 1) | /* Continuous conversion */ + (1 << 0) ; /* ADC enable */ + ADC1->CR2 |= 1 << 3; /* Initialize calibration registers */ + while (ADC1->CR2 & (1 << 3)); /* Wait for initialization to finish */ + ADC1->CR2 |= 1 << 2; /* Start calibration */ + while (ADC1->CR2 & (1 << 2)); /* Wait for calibration to finish */ + ADC1->CR2 |= 1 << 22; /* Start first conversion */ }