Labrary from Simon Blandford

Dependents:   DiscoTech ADC_Test filter_implement IR_temperature ... more

Committer:
flash_ahaa
Date:
Mon Sep 23 11:18:37 2013 +0000
Revision:
0:b05e4f5e2c8c
Library from Simon Blandford

Who changed what in which revision?

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