Dependencies:   mbed

Committer:
Kaikestu
Date:
Wed Dec 16 12:04:23 2009 +0000
Revision:
0:d322a5241da8

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kaikestu 0:d322a5241da8 1 // ADC_setup.cpp
Kaikestu 0:d322a5241da8 2
Kaikestu 0:d322a5241da8 3 #include "header.h"
Kaikestu 0:d322a5241da8 4 /*****************************************************
Kaikestu 0:d322a5241da8 5 variables and defines
Kaikestu 0:d322a5241da8 6 ****************************************************/
Kaikestu 0:d322a5241da8 7
Kaikestu 0:d322a5241da8 8 int ADC_channel=1; // ADC channel 1
Kaikestu 0:d322a5241da8 9
Kaikestu 0:d322a5241da8 10 /*****************************************************
Kaikestu 0:d322a5241da8 11 subroutines
Kaikestu 0:d322a5241da8 12 *****************************************************/
Kaikestu 0:d322a5241da8 13 void ADC_init(){
Kaikestu 0:d322a5241da8 14
Kaikestu 0:d322a5241da8 15 PINSEL1=0x00010000; //set AD0.1 pin to 0000 0000 0000 0001 0000 0000 0000 0000 = ADC
Kaikestu 0:d322a5241da8 16 PCONP |= (1 << 12); // enable ADC clock
Kaikestu 0:d322a5241da8 17 AD0CR = (1 << ADC_channel) // channel 1
Kaikestu 0:d322a5241da8 18 | (4 << 8) // This gives us 3MHz
Kaikestu 0:d322a5241da8 19 | (0 << 16) // BURST = 0, software control
Kaikestu 0:d322a5241da8 20 | (0 << 17) // CLKS = 0 (11-bits)
Kaikestu 0:d322a5241da8 21 | (1 << 21) // PDN = 1
Kaikestu 0:d322a5241da8 22 | (0 << 22) // TEST1:0 = 00
Kaikestu 0:d322a5241da8 23 | (1 << 24) // START = 1 start A/D conversion
Kaikestu 0:d322a5241da8 24 | (0 << 27); // EDGE = 0 (CAP/MAT singal falling,trigger A/D conversion)
Kaikestu 0:d322a5241da8 25 }
Kaikestu 0:d322a5241da8 26 float ADC_read() {
Kaikestu 0:d322a5241da8 27
Kaikestu 0:d322a5241da8 28 int data;
Kaikestu 0:d322a5241da8 29
Kaikestu 0:d322a5241da8 30 // start A/D conversion on channel
Kaikestu 0:d322a5241da8 31 AD0CR &= (AD0CR & 0xFFFFFF00);
Kaikestu 0:d322a5241da8 32 AD0CR |= (1 << ADC_channel) | (1 << 24);
Kaikestu 0:d322a5241da8 33
Kaikestu 0:d322a5241da8 34 delay_l();
Kaikestu 0:d322a5241da8 35
Kaikestu 0:d322a5241da8 36 // wait for it to finish by polling the ADC DONE bit
Kaikestu 0:d322a5241da8 37 while((AD0GDR & 0x80000000) == 0)
Kaikestu 0:d322a5241da8 38 {
Kaikestu 0:d322a5241da8 39 }
Kaikestu 0:d322a5241da8 40
Kaikestu 0:d322a5241da8 41 // get the data and stop the adc
Kaikestu 0:d322a5241da8 42 data = AD0GDR;
Kaikestu 0:d322a5241da8 43 AD0CR &= 0xF8FFFFFF;
Kaikestu 0:d322a5241da8 44
Kaikestu 0:d322a5241da8 45 // shift data and mask to give 8-bit ADC value
Kaikestu 0:d322a5241da8 46 data=(data>>8)&0x00000FF;
Kaikestu 0:d322a5241da8 47 return data;
Kaikestu 0:d322a5241da8 48 }