ADC.cpp 3.21 KB
Newer Older
Werner Sembach's avatar
Werner Sembach committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * Original File adc-dac-printf.c modified by Lisa Dreier, Margit Haspel and Werner Sembach
 *
 * Original License Header:
 *
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2013 Karl Palsson <karlp@tweak.net.au>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

Werner Sembach's avatar
Cleanup  
Werner Sembach committed
24
25
26
27
28
29
30
31
32
33
#include "ADC.h"

#include <libopencm3/stm32/adc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>

extern "C" void adc_init() {
    rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]);
    rcc_periph_clock_enable(RCC_ADC1);

Werner Sembach's avatar
Werner Sembach committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    // Uncomment pins that are used as ADC
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); //ADC Channel  0 <> GPIO PA0
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); //ADC Channel  1 <> GPIO PA1
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2); //ADC Channel  2 <> GPIO PA2
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3); //ADC Channel  3 <> GPIO PA3
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO4); //ADC Channel  4 <> GPIO PA4
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO5); //ADC Channel  5 <> GPIO PA5
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO6); //ADC Channel  6 <> GPIO PA6
    //gpio_mode_setup(GPIOA, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO7); //ADC Channel  7 <> GPIO PA7
    //gpio_mode_setup(GPIOB, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); //ADC Channel  8 <> GPIO PB0
    //gpio_mode_setup(GPIOB, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); //ADC Channel  9 <> GPIO PB1
    //gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO0); //ADC Channel 10 <> GPIO PC0
    gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO1); //ADC Channel 11 <> GPIO PC1
    //gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO2); //ADC Channel 12 <> GPIO PC2
    //gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO3); //ADC Channel 13 <> GPIO PC3
    //gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO4); //ADC Channel 14 <> GPIO PC4
    //gpio_mode_setup(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO5); //ADC Channel 15 <> GPIO PC5
Werner Sembach's avatar
Cleanup  
Werner Sembach committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    adc_power_off(ADC1);
    adc_disable_scan_mode(ADC1);
    adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_3CYC);

    adc_power_on(ADC1);
}

extern "C" uint16_t adc_get(uint8_t channel) {
        uint8_t channel_array[16];
        channel_array[0] = channel;
        adc_set_regular_sequence(ADC1, 1, channel_array);
        adc_start_conversion_regular(ADC1);
        while (!adc_eoc(ADC1));
        uint16_t reg16 = adc_read_regular(ADC1);
        return reg16;
}