Makes 100 samples on maximum sample rate and transmits it over UART

Dependencies:   mbed

Committer:
gno
Date:
Wed Jun 09 12:54:21 2010 +0000
Revision:
0:55ea5a2921b2

        

Who changed what in which revision?

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