]> rtime.felk.cvut.cz Git - sysless.git/blob - board/arm/lpceurobot/libs/eb_ebb/adc.c
h8eurobot: Enable PXMC
[sysless.git] / board / arm / lpceurobot / libs / eb_ebb / adc.c
1 #include <lpc21xx.h>                          // LPC21XX Peripheral Registers
2 #include <types.h> 
3 #include "adc.h"
4 #include <stdlib.h>
5
6
7 #define ADCCH0 22       ///< ADC0 value for PINSEL
8 #define ADCCH1 24       ///< ADC1 value for PINSEL
9 #define ADCCH2 26       ///< ADC2 value for PINSEL
10 #define ADCCH3 28       ///< ADC3 value for PINSEL
11
12 /**
13  *  ADC ISR routine. This routine reads selected ADC value and
14  *  multiplies it by #ADC_MUL and adds #ADC_OFFSET to calculate the
15  *  volage (3.25mV/div).  After this reading the next ADC channel is
16  *  set up for measuring.
17  */ 
18 void adc_isr(void) __attribute__ ((interrupt));
19 void adc_isr(void) 
20 {
21         unsigned char chan =0;
22         unsigned int val =0;
23
24
25         chan = (char) ((ADDR>>24) & 0x07);
26         val = (((((ADDR >> 6) & 0x3FF) * ADC_MUL + ADC_OFFSET) + adc_val[chan]) >> 1) ;
27
28
29
30         adc_val[chan] = val;
31
32         ADCR &= ~(ADC_CR_START_OFF_m);
33
34
35         switch(chan)
36         {
37                 case 0:
38                         ADCR = ((ADC_CR_ADC1_m) | (ADC_CR_CLKS_11_m) | (ADC_CR_PDN_ON_m) | (ADC_CR_START_NOW_m) | (20*ADC_CR_CLK_DIV_1_m));
39                         break;
40
41                 case 1:
42                         ADCR = ((ADC_CR_ADC2_m) | (ADC_CR_CLKS_11_m) | (ADC_CR_PDN_ON_m) | (ADC_CR_START_NOW_m) | (20*ADC_CR_CLK_DIV_1_m));
43                         break;
44                         
45                 case 2:
46                         ADCR = ((ADC_CR_ADC3_m) | (ADC_CR_CLKS_11_m) | (ADC_CR_PDN_ON_m) | (ADC_CR_START_NOW_m) | (20*ADC_CR_CLK_DIV_1_m));
47                         break;
48                         
49                 case 3:
50                         ADCR = ((ADC_CR_ADC0_m) | (ADC_CR_CLKS_11_m) | (ADC_CR_PDN_ON_m) | (ADC_CR_START_NOW_m) | (20*ADC_CR_CLK_DIV_1_m));
51                         break;                                                                                                                   
52         }
53         
54          VICVectAddr = 0;
55
56 }
57
58 /**
59  *  Inicializes ADC service for all ADC (4) channels and installs ISR routine to VIC.
60  *   Automaticly starts the conversion of first channel on given conversion frequency.
61  */ 
62 void init_adc(unsigned rx_isr_vect)
63 {
64         
65         PINSEL1 |= ((PINSEL_1 << ADCCH0) | (PINSEL_1 << ADCCH1) | (PINSEL_1 << ADCCH2) | (PINSEL_1 << ADCCH3));         
66
67         ((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x32;
68         ((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned) adc_isr;
69         VICIntEnable = 0x40000;
70
71         ADCR = ((ADC_CR_ADC0_m) | (ADC_CR_CLKS_11_m) | (ADC_CR_PDN_ON_m) | (ADC_CR_START_NOW_m) | (ADC_VPB_DIV*ADC_CR_CLK_DIV_1_m));
72 }