ADC.h 3.07 KB
Newer Older
uepls's avatar
uepls committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * ADC.h
 *
 *    Author: Max Zuidberg
 *     Email: m.zuidberg@icloud.com
 */

#ifndef ADC_H_
#define ADC_H_


/*
 * stdbool.h:               Boolean definitions for the C99 standard
 * stdint.h:                Variable definitions for the C99 standard
 * inc/hw_types.h:          Macros for hardware access, both direct and via the
 *                          bit-band region.
 * inc/hw_memmap.h:         Macros defining the memory map of the Tiva C Series
 *                          device. This includes defines such as peripheral
 *                          base address locations such as GPIO_PORTF_BASE.
 * inc/hw_ints.h:           Macros defining the interrupt assignments
 * driverlib/sysctl.h:      Defines and macros for the System Control API of
 *                          DriverLib. This includes API functions such as
 *                          SysCtlClockSet.
 * driverlib/gpio.h:        Defines and macros for GPIO API of DriverLib. This
 *                          includes API functions such as GPIOPinConfigure.
 * driverlib/adc.h          Definitions for using the ADC driver
 * System.h:                Access to current CPU clock and other functions.
 */
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include "System.h"


// Defines for direct register access
// Base address for the System Control Registers (Datasheet page 237)
#define SYS_CTL_BASE 0x400FE000

// Offset to add to SYS_CTL_BASE to access RCGCADC Register.
// (Datasheet page 352)
#define SYS_CTL_RCGCADC_OFFSET 0x638

// Actual RCGCADC Register
#define SYS_CTL_RCGCADC_REG HWREG(SYS_CTL_BASE + SYS_CTL_RCGCADC_OFFSET)

// Defines for readability using adcInputPinMapping
#define AIN_PERIPH  0
#define AIN_PORT    1
#define AIN_PIN     2


class ADC
{
public:
    ADC();
    virtual ~ADC();
    void init(System *sys, uint32_t base, uint32_t sampleSeq, uint32_t analogInput);
    void setHWAveraging(uint32_t averaging);
    uint32_t read();
    float readVolt();

private:
    bool adcEnabled;
    uint32_t adcBase, adcSampleSeq, adcInput, adcReading[1];
    const uint32_t adcInputPinMapping[12][3] =
                   {{SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_3},
                    {SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_2},
                    {SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_1},
                    {SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_0},
                    {SYSCTL_PERIPH_GPIOD, GPIO_PORTD_BASE, GPIO_PIN_3},
                    {SYSCTL_PERIPH_GPIOD, GPIO_PORTD_BASE, GPIO_PIN_2},
                    {SYSCTL_PERIPH_GPIOD, GPIO_PORTD_BASE, GPIO_PIN_1},
                    {SYSCTL_PERIPH_GPIOD, GPIO_PORTD_BASE, GPIO_PIN_0},
                    {SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_5},
                    {SYSCTL_PERIPH_GPIOE, GPIO_PORTE_BASE, GPIO_PIN_4},
                    {SYSCTL_PERIPH_GPIOB, GPIO_PORTB_BASE, GPIO_PIN_4},
                    {SYSCTL_PERIPH_GPIOB, GPIO_PORTB_BASE, GPIO_PIN_5}};
};


#endif /* ADC_H_ */