adamdunkels | 573bc29 | 2003-10-01 08:04:03 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * Functions for reading the sensors of the sensor board |
| 4 | * \author Based on the work of anonymous students at FU Berlin |
| 5 | * |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <io.h> |
| 10 | #include <signal.h> |
| 11 | |
| 12 | #define SENSOR_VIB (0x10) // so never change these!!! |
| 13 | |
| 14 | volatile unsigned int sensors_mic, /**< On-board microphone. */ |
| 15 | sensors_extern, /**< External sensor. */ |
| 16 | sensors_battery, /**< Battery sensor. */ |
| 17 | sensors_temp, /**< Temperature sensor. */ |
| 18 | sensors_vib; /**< Vibration sensor. */ |
| 19 | |
| 20 | /*-----------------------------------------------------------------------------------*/ |
| 21 | /** |
| 22 | * \internal |
| 23 | * Interrupt handler which reads the values of the A/D converters. |
| 24 | * |
| 25 | */ |
| 26 | /*-----------------------------------------------------------------------------------*/ |
| 27 | interrupt (ADC_VECTOR) |
| 28 | sensors_ad_irq(void) |
| 29 | { |
| 30 | unsigned int temp; |
| 31 | char vk; |
| 32 | |
| 33 | sensors_mic = ADC12MEM0; |
| 34 | sensors_extern = ADC12MEM1; |
| 35 | sensors_battery = ADC12MEM2; |
| 36 | temp = ADC12MEM3; |
| 37 | |
| 38 | vk = (temp >> 8); |
| 39 | if(vk & 0x80) { |
| 40 | vk = ~vk; |
| 41 | } |
| 42 | sensors_temp = vk; |
| 43 | } |
| 44 | /*-----------------------------------------------------------------------------------*/ |
| 45 | /** |
| 46 | * \internal |
| 47 | * Interrupt handler for vibration sensor. |
| 48 | * |
| 49 | */ |
| 50 | /*-----------------------------------------------------------------------------------*/ |
| 51 | interrupt(PORT1_VECTOR) |
| 52 | p1_irq (void) |
| 53 | { |
| 54 | if(P1IFG & SENSOR_VIB) { |
| 55 | ++sensors_vib; |
| 56 | P1IFG &= ~(SENSOR_VIB); |
| 57 | } |
| 58 | } |
| 59 | /*-----------------------------------------------------------------------------------*/ |
| 60 | /** |
| 61 | * Initialize the sensors. |
| 62 | */ |
| 63 | /*-----------------------------------------------------------------------------------*/ |
| 64 | void |
| 65 | sensors_init(void) |
| 66 | { |
| 67 | |
| 68 | /* Set up the ADC. */ |
| 69 | |
| 70 | ADC12CTL0 = ADC12ON + REFON + REF2_5V + SHT0_6 + SHT1_6 + MSC; |
| 71 | ADC12CTL1 = SHP + CONSEQ_3 + CSTARTADD_0; |
| 72 | |
| 73 | ADC12MCTL0 = INCH_3 + SREF_0; |
| 74 | ADC12MCTL1 = INCH_4 + SREF_0; |
| 75 | ADC12MCTL2 = INCH_0 + SREF_0; |
| 76 | ADC12MCTL3 = INCH_5 + SREF_0 + EOS; |
| 77 | |
| 78 | ADC12IE = 0x0008; |
| 79 | |
| 80 | /* Delay */ |
| 81 | {unsigned int i,j; for(i = 0x200; i > 0; --i) {j = j*j;}} |
| 82 | |
| 83 | ADC12CTL0 |= ENC; |
| 84 | ADC12CTL0 |= ADC12SC; |
| 85 | |
| 86 | |
| 87 | P1IE |= SENSOR_VIB; |
| 88 | } |
| 89 | /*-----------------------------------------------------------------------------------*/ |