some success, still errors

Committer:
candre97
Date:
Thu Nov 28 22:28:30 2019 +0000
Revision:
109:20827b1e5195
Parent:
108:b8d05666d95f
using line 62-66 instead of analog read. Maybe use API for initialization

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 82:abf1b1785bd7 1 /* mbed Microcontroller Library
mbed_official 82:abf1b1785bd7 2 * Copyright (c) 2018 ARM Limited
mbed_official 82:abf1b1785bd7 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 82:abf1b1785bd7 4 */
mbed_official 82:abf1b1785bd7 5
Jonathan Austin 0:2757d7abb7d9 6 #include "mbed.h"
mbed_official 100:ec006d6f3cb6 7 #include "platform/mbed_thread.h"
mbed_official 82:abf1b1785bd7 8 #include "stats_report.h"
candre97 102:780b4ecac614 9 #include <AnalogIn.h>
candre97 102:780b4ecac614 10 #include <AnalogOut.h>
Jonathan Austin 0:2757d7abb7d9 11
candre97 108:b8d05666d95f 12 #include "stm32l1xx_hal.h"
candre97 108:b8d05666d95f 13 #include "stm32l1xx_hal_def.h"
candre97 108:b8d05666d95f 14 #include "stm32l1xx_hal_adc_ex.h"
candre97 108:b8d05666d95f 15
candre97 102:780b4ecac614 16 AnalogOut v_src(GPIO0);
candre97 102:780b4ecac614 17 AnalogIn therm(GPIO2);
candre97 102:780b4ecac614 18
candre97 102:780b4ecac614 19 #define SLEEP_TIME 50 // (msec)
candre97 102:780b4ecac614 20 #define PRINT_AFTER_N_LOOPS 20
Jonathan Austin 0:2757d7abb7d9 21
candre97 108:b8d05666d95f 22
candre97 108:b8d05666d95f 23 static void SystemClock_Config(void)
candre97 108:b8d05666d95f 24 {
candre97 108:b8d05666d95f 25
candre97 108:b8d05666d95f 26 }
candre97 108:b8d05666d95f 27
candre97 102:780b4ecac614 28 void ConfigureADC()
candre97 102:780b4ecac614 29 {
candre97 102:780b4ecac614 30
candre97 108:b8d05666d95f 31
candre97 108:b8d05666d95f 32
candre97 102:780b4ecac614 33 }
mbed_official 88:bea4f2daa48c 34
Jonathan Austin 1:846c97078558 35 // main() runs in its own thread in the OS
mbed_official 82:abf1b1785bd7 36 int main()
mbed_official 82:abf1b1785bd7 37 {
candre97 108:b8d05666d95f 38 SystemInit();
candre97 108:b8d05666d95f 39 /* Setup GPIO for LEDs */
candre97 108:b8d05666d95f 40 RCC->APB2ENR |= 1 << 6; /* Enable GPIOE clock */
candre97 108:b8d05666d95f 41
candre97 108:b8d05666d95f 42 /* Setup and initialize ADC converter */
candre97 108:b8d05666d95f 43 RCC->APB2ENR |= 1 << 9; /* Enable ADC1 clock */
candre97 108:b8d05666d95f 44 //GPIOC->CRL &= 0xFFF0FFFF; /* Configure PC4 as ADC.14 input */
candre97 108:b8d05666d95f 45 ADC1->SQR1 = 0x00000000; /* Regular channel 1 conversion */
candre97 108:b8d05666d95f 46 ADC1->SQR2 = 0x00000000; /* Clear register */
candre97 108:b8d05666d95f 47 ADC1->SQR3 = 14 << 0; /* SQ1 = channel 14 */
candre97 108:b8d05666d95f 48 ADC1->SMPR1 = 5 << 12; /* Channel 14 sample time is 55.5 cyc */
candre97 108:b8d05666d95f 49 ADC1->SMPR2 = 0x00000000; /* Clear register */
candre97 108:b8d05666d95f 50 ADC1->CR1 = 1 << 8; /* Scan mode on */
candre97 108:b8d05666d95f 51 ADC1->CR2 = (1 << 20) | /* Enable external trigger */
candre97 108:b8d05666d95f 52 (7 << 17) | /* EXTSEL = SWSTART */
candre97 108:b8d05666d95f 53 (1 << 1) | /* Continuous conversion */
candre97 108:b8d05666d95f 54 (1 << 0) ; /* ADC enable */
candre97 108:b8d05666d95f 55 ADC1->CR2 |= 1 << 3; /* Initialize calibration registers */
candre97 108:b8d05666d95f 56 while (ADC1->CR2 & (1 << 3)); /* Wait for initialization to finish */
candre97 108:b8d05666d95f 57 ADC1->CR2 |= 1 << 2; /* Start calibration */
candre97 108:b8d05666d95f 58 while (ADC1->CR2 & (1 << 2)); /* Wait for calibration to finish */
candre97 108:b8d05666d95f 59 ADC1->CR2 |= 1 << 22; /* Start first conversion */
candre97 109:20827b1e5195 60 int AD_val;
candre97 109:20827b1e5195 61
candre97 109:20827b1e5195 62 // this part instead of API analog_read
candre97 109:20827b1e5195 63 for (;;) { /* Loop forever */
candre97 109:20827b1e5195 64 if (ADC1->SR & (1 << 1)) { /* If conversion has finished */
candre97 109:20827b1e5195 65 AD_val = ADC1->DR & 0x0FFF; /* Read AD converted value */
candre97 109:20827b1e5195 66 ADC1->CR2 |= 1 << 22; /* Start new conversion */
candre97 109:20827b1e5195 67 }
candre97 109:20827b1e5195 68 }
Jonathan Austin 0:2757d7abb7d9 69 }