Software testing lpcanvca.h
[LPC2xxx_and_RobotSpejbl.git] / app / eb_ebb / adc.c
blobfd19078745fbb57db97afe12b9a8eaef9e4049b2
1 #include <lpc21xx.h> // LPC21XX Peripheral Registers
2 #include <types.h>
3 #include "adc.h"
4 #include <stdlib.h>
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
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)
21 unsigned char chan =0;
22 unsigned int val =0;
25 chan = (char) ((ADDR>>24) & 0x07);
26 val = (((((ADDR >> 6) & 0x3FF) * ADC_MUL + ADC_OFFSET) + adc_val[chan]) >> 1) ;
30 adc_val[chan] = val;
32 ADCR &= ~(ADC_CR_START_OFF_m);
35 switch(chan)
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;
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;
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;
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;
54 VICVectAddr = 0;
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)
65 PINSEL1 |= ((PINSEL_1 << ADCCH0) | (PINSEL_1 << ADCCH1) | (PINSEL_1 << ADCCH2) | (PINSEL_1 << ADCCH3));
67 ((uint32_t*)&VICVectCntl0)[rx_isr_vect] = 0x32;
68 ((uint32_t*)&VICVectAddr0)[rx_isr_vect] = (unsigned) adc_isr;
69 VICIntEnable = 0x40000;
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));