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