posilani dat
Dependencies: FatFileSystemCpp mbed PowerControl USBHostLite
ADC_full/adc.h@0:16fd37cf4a7c, 2015-02-13 (annotated)
- Committer:
- jkaderka
- Date:
- Fri Feb 13 08:20:27 2015 +0000
- Revision:
- 0:16fd37cf4a7c
first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jkaderka | 0:16fd37cf4a7c | 1 | /* mbed Library - ADC |
jkaderka | 0:16fd37cf4a7c | 2 | * Copyright (c) 2010, sblandford |
jkaderka | 0:16fd37cf4a7c | 3 | * released under MIT license http://mbed.org/licence/mit |
jkaderka | 0:16fd37cf4a7c | 4 | */ |
jkaderka | 0:16fd37cf4a7c | 5 | |
jkaderka | 0:16fd37cf4a7c | 6 | #ifndef MBED_ADC_H |
jkaderka | 0:16fd37cf4a7c | 7 | #define MBED_ADC_H |
jkaderka | 0:16fd37cf4a7c | 8 | |
jkaderka | 0:16fd37cf4a7c | 9 | #include "mbed.h" |
jkaderka | 0:16fd37cf4a7c | 10 | #define XTAL_FREQ 12000000 |
jkaderka | 0:16fd37cf4a7c | 11 | #define MAX_ADC_CLOCK 13000000 |
jkaderka | 0:16fd37cf4a7c | 12 | #define CLKS_PER_SAMPLE 64 |
jkaderka | 0:16fd37cf4a7c | 13 | |
jkaderka | 0:16fd37cf4a7c | 14 | class ADC { |
jkaderka | 0:16fd37cf4a7c | 15 | public: |
jkaderka | 0:16fd37cf4a7c | 16 | |
jkaderka | 0:16fd37cf4a7c | 17 | //Initialize ADC with ADC maximum sample rate of |
jkaderka | 0:16fd37cf4a7c | 18 | //sample_rate and system clock divider of cclk_div |
jkaderka | 0:16fd37cf4a7c | 19 | //Maximum recommened sample rate is 184000 |
jkaderka | 0:16fd37cf4a7c | 20 | ADC(int sample_rate, int cclk_div); |
jkaderka | 0:16fd37cf4a7c | 21 | |
jkaderka | 0:16fd37cf4a7c | 22 | //Enable/disable ADC on pin according to state |
jkaderka | 0:16fd37cf4a7c | 23 | //and also select/de-select for next conversion |
jkaderka | 0:16fd37cf4a7c | 24 | void setup(PinName pin, int state); |
jkaderka | 0:16fd37cf4a7c | 25 | |
jkaderka | 0:16fd37cf4a7c | 26 | //Return enabled/disabled state of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 27 | int setup(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 28 | |
jkaderka | 0:16fd37cf4a7c | 29 | //Enable/disable burst mode according to state |
jkaderka | 0:16fd37cf4a7c | 30 | void burst(int state); |
jkaderka | 0:16fd37cf4a7c | 31 | |
jkaderka | 0:16fd37cf4a7c | 32 | //Select channel already setup |
jkaderka | 0:16fd37cf4a7c | 33 | void select(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 34 | |
jkaderka | 0:16fd37cf4a7c | 35 | //Return burst mode enabled/disabled |
jkaderka | 0:16fd37cf4a7c | 36 | int burst(void); |
jkaderka | 0:16fd37cf4a7c | 37 | |
jkaderka | 0:16fd37cf4a7c | 38 | /*Set start condition and edge according to mode: |
jkaderka | 0:16fd37cf4a7c | 39 | 0 - No start (this value should be used when clearing PDN to 0). |
jkaderka | 0:16fd37cf4a7c | 40 | 1 - Start conversion now. |
jkaderka | 0:16fd37cf4a7c | 41 | 2 - Start conversion when the edge selected by bit 27 occurs on the P2.10 / EINT0 / NMI pin. |
jkaderka | 0:16fd37cf4a7c | 42 | 3 - Start conversion when the edge selected by bit 27 occurs on the P1.27 / CLKOUT / |
jkaderka | 0:16fd37cf4a7c | 43 | USB_OVRCRn / CAP0.1 pin. |
jkaderka | 0:16fd37cf4a7c | 44 | 4 - Start conversion when the edge selected by bit 27 occurs on MAT0.1. Note that this does |
jkaderka | 0:16fd37cf4a7c | 45 | not require that the MAT0.1 function appear on a device pin. |
jkaderka | 0:16fd37cf4a7c | 46 | 5 - Start conversion when the edge selected by bit 27 occurs on MAT0.3. Note that it is not |
jkaderka | 0:16fd37cf4a7c | 47 | possible to cause the MAT0.3 function to appear on a device pin. |
jkaderka | 0:16fd37cf4a7c | 48 | 6 - Start conversion when the edge selected by bit 27 occurs on MAT1.0. Note that this does |
jkaderka | 0:16fd37cf4a7c | 49 | not require that the MAT1.0 function appear on a device pin. |
jkaderka | 0:16fd37cf4a7c | 50 | 7 - Start conversion when the edge selected by bit 27 occurs on MAT1.1. Note that this does |
jkaderka | 0:16fd37cf4a7c | 51 | not require that the MAT1.1 function appear on a device pin. |
jkaderka | 0:16fd37cf4a7c | 52 | When mode >= 2, conversion is triggered by edge: |
jkaderka | 0:16fd37cf4a7c | 53 | 0 - Rising edge |
jkaderka | 0:16fd37cf4a7c | 54 | 1 - Falling edge |
jkaderka | 0:16fd37cf4a7c | 55 | */ |
jkaderka | 0:16fd37cf4a7c | 56 | void startmode(int mode, int edge); |
jkaderka | 0:16fd37cf4a7c | 57 | |
jkaderka | 0:16fd37cf4a7c | 58 | //Return startmode state according to mode_edge=0: mode and mode_edge=1: edge |
jkaderka | 0:16fd37cf4a7c | 59 | int startmode(int mode_edge); |
jkaderka | 0:16fd37cf4a7c | 60 | |
jkaderka | 0:16fd37cf4a7c | 61 | //Start ADC conversion |
jkaderka | 0:16fd37cf4a7c | 62 | void start(void); |
jkaderka | 0:16fd37cf4a7c | 63 | |
jkaderka | 0:16fd37cf4a7c | 64 | //Set interrupt enable/disable for pin to state |
jkaderka | 0:16fd37cf4a7c | 65 | void interrupt_state(PinName pin, int state); |
jkaderka | 0:16fd37cf4a7c | 66 | |
jkaderka | 0:16fd37cf4a7c | 67 | //Return enable/disable state of interrupt for pin |
jkaderka | 0:16fd37cf4a7c | 68 | int interrupt_state(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 69 | |
jkaderka | 0:16fd37cf4a7c | 70 | //Attach custom interrupt handler replacing default |
jkaderka | 0:16fd37cf4a7c | 71 | void attach(void(*fptr)(void)); |
jkaderka | 0:16fd37cf4a7c | 72 | |
jkaderka | 0:16fd37cf4a7c | 73 | //Restore default interrupt handler |
jkaderka | 0:16fd37cf4a7c | 74 | void detach(void); |
jkaderka | 0:16fd37cf4a7c | 75 | |
jkaderka | 0:16fd37cf4a7c | 76 | //Append custom interrupt handler for pin |
jkaderka | 0:16fd37cf4a7c | 77 | void append(PinName pin, void(*fptr)(uint32_t value)); |
jkaderka | 0:16fd37cf4a7c | 78 | |
jkaderka | 0:16fd37cf4a7c | 79 | //Unappend custom interrupt handler for pin |
jkaderka | 0:16fd37cf4a7c | 80 | void unappend(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 81 | |
jkaderka | 0:16fd37cf4a7c | 82 | //Append custom global interrupt handler |
jkaderka | 0:16fd37cf4a7c | 83 | void append(void(*fptr)(int chan, uint32_t value)); |
jkaderka | 0:16fd37cf4a7c | 84 | |
jkaderka | 0:16fd37cf4a7c | 85 | //Unappend custom global interrupt handler |
jkaderka | 0:16fd37cf4a7c | 86 | void unappend(void); |
jkaderka | 0:16fd37cf4a7c | 87 | |
jkaderka | 0:16fd37cf4a7c | 88 | //Set ADC offset to a value 0-7 |
jkaderka | 0:16fd37cf4a7c | 89 | void offset(int offset); |
jkaderka | 0:16fd37cf4a7c | 90 | |
jkaderka | 0:16fd37cf4a7c | 91 | //Return current ADC offset |
jkaderka | 0:16fd37cf4a7c | 92 | int offset(void); |
jkaderka | 0:16fd37cf4a7c | 93 | |
jkaderka | 0:16fd37cf4a7c | 94 | //Return value of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 95 | int read(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 96 | |
jkaderka | 0:16fd37cf4a7c | 97 | //Return DONE flag of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 98 | int done(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 99 | |
jkaderka | 0:16fd37cf4a7c | 100 | //Return OVERRUN flag of ADC on pin |
jkaderka | 0:16fd37cf4a7c | 101 | int overrun(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 102 | |
jkaderka | 0:16fd37cf4a7c | 103 | //Return actual ADC clock |
jkaderka | 0:16fd37cf4a7c | 104 | int actual_adc_clock(void); |
jkaderka | 0:16fd37cf4a7c | 105 | |
jkaderka | 0:16fd37cf4a7c | 106 | //Return actual maximum sample rate |
jkaderka | 0:16fd37cf4a7c | 107 | int actual_sample_rate(void); |
jkaderka | 0:16fd37cf4a7c | 108 | |
jkaderka | 0:16fd37cf4a7c | 109 | //Return pin ID of ADC channel |
jkaderka | 0:16fd37cf4a7c | 110 | PinName channel_to_pin(int chan); |
jkaderka | 0:16fd37cf4a7c | 111 | |
jkaderka | 0:16fd37cf4a7c | 112 | //Return pin number of ADC channel |
jkaderka | 0:16fd37cf4a7c | 113 | int channel_to_pin_number(int chan); |
jkaderka | 0:16fd37cf4a7c | 114 | |
jkaderka | 0:16fd37cf4a7c | 115 | |
jkaderka | 0:16fd37cf4a7c | 116 | private: |
jkaderka | 0:16fd37cf4a7c | 117 | int _pin_to_channel(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 118 | uint32_t _data_of_pin(PinName pin); |
jkaderka | 0:16fd37cf4a7c | 119 | |
jkaderka | 0:16fd37cf4a7c | 120 | int _adc_clk_freq; |
jkaderka | 0:16fd37cf4a7c | 121 | void adcisr(void); |
jkaderka | 0:16fd37cf4a7c | 122 | static void _adcisr(void); |
jkaderka | 0:16fd37cf4a7c | 123 | static ADC *instance; |
jkaderka | 0:16fd37cf4a7c | 124 | |
jkaderka | 0:16fd37cf4a7c | 125 | uint32_t _adc_data[8]; |
jkaderka | 0:16fd37cf4a7c | 126 | void(*_adc_isr[8])(uint32_t value); |
jkaderka | 0:16fd37cf4a7c | 127 | void(*_adc_g_isr)(int chan, uint32_t value); |
jkaderka | 0:16fd37cf4a7c | 128 | void(*_adc_m_isr)(void); |
jkaderka | 0:16fd37cf4a7c | 129 | }; |
jkaderka | 0:16fd37cf4a7c | 130 | |
jkaderka | 0:16fd37cf4a7c | 131 | #endif |