This is a simple program that displays a spectrum analyzer on the Nokia LCD from an input through a 3.5mm audio jack. It uses FFT\'s to compute the fourier transform of the incoming audio signal and display the amplitudes across the screen at the different frequencies.

Dependencies:   mbed NokiaLCD

Committer:
gth646f
Date:
Mon Feb 28 03:26:53 2011 +0000
Revision:
0:b6451e68016a

        

Who changed what in which revision?

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