airspeedSensor.c
Go to the documentation of this file.
1 /*
2  * File: airspeedSensor.c
3  * Author: Chris
4  *
5  * Created on June 15, 2013, 3:40 PM
6  */
7 #include "main.h"
8 #include "../Common/Common.h"
9 #include "airspeedSensor.h"
10 
11 
15 
16 void __attribute__((interrupt, no_auto_psv)) _ADC1Interrupt(void)
17 {
18 
19  currentAirspeedSignal = ADC1BUF0;
20 
21  IFS0bits.AD1IF = 0; // Clear the ADC1 Interrupt Flag
22 
23 }
24 
25 
27  //RA11/AN11 is the pin to get the airspeed information
28  TRISBbits.TRISB11 = 1;
30 
31 }
32 
33 
37  int i = 0;
38  float avgAirspeed = 0;
39  for (i = 0; i < AIRSPEED_HISTORY; i++){
40  avgAirspeed += (float)airspeedHistory[i];
41  }
42  avgAirspeed /= AIRSPEED_HISTORY;
43  return (0.30*(avgAirspeed-1417)); //sensor signal calibration
44 }
45 
47  AD1CON1bits.FORM = 0; // Data Output Format: Unsigned Int
48  AD1CON1bits.SSRC = 7; // Internal Counter (SAMC) ends sampling and starts convertion
49  AD1CON1bits.ASAM = 1; // Sampling begins when SAMP bit is set (for now)
50  AD1CON1bits.SIMSAM = 0; // Sequencial Sampling/conversion
51  AD1CON1bits.AD12B = 1; // 12-bit 2/4-channel operation
52  AD1CON1bits.ADDMABM = 0;
53  AD1CON1bits.SAMP = 1;
54 
55  AD1CON2bits.SMPI = 0; // Interrupt address every sample/conversion
56  AD1CON2bits.BUFM = 0;
57  AD1CON2bits.CHPS = 0; //Converts channel 0
58  AD1CON2bits.VCFG = 0; //Voltage Reference is 3.3V and Ground Reference is Ground
59 
60  AD1CON3bits.ADRC=0; // ADC Clock is derived from Systems Clock
61  AD1CON3bits.SAMC=0; // Auto Sample Time = 0*Tad
62  AD1CON3bits.ADCS=6; // ADC Conversion Clock Tad=Tcy*(ADCS+1)= (1/40M)*7 = 175nS
63 
64  AD1CHS0bits.CH0SA = 11; //Channel 0 positive input on AN11 (Sample A)
65  AD1CHS0bits.CH0SB = 11; //Channel 0 positive input on AN11 (Sample B)
66 
67  AD1PCFGL = 0;
68  // TODO why is this an error? AD2PCFGH = 0;
69  AD1PCFGLbits.PCFG0 = 0; //Port pin set to analog mode (voltage sampling)
70  AD1PCFGLbits.PCFG1 = 0; //Port pin set to analog mode (voltage sampling)
71  AD1PCFGLbits.PCFG2 = 0; //Port pin set to analog mode (voltage sampling)
72  AD1PCFGLbits.PCFG3 = 0; //Port pin set to analog mode (voltage sampling)
73  AD1PCFGLbits.PCFG4 = 0; //Port pin set to analog mode (voltage sampling)AD2PCFGLbits.PCFG12 = 0; //Port pin set to analog mode (voltage sampling)
74  AD1PCFGLbits.PCFG5 = 0; //Port pin set to analog mode (voltage sampling)
75  AD1PCFGLbits.PCFG6 = 0; //Port pin set to analog mode (voltage sampling)
76  AD1PCFGLbits.PCFG7 = 0; //Port pin set to analog mode (voltage sampling)
77  AD1PCFGLbits.PCFG8 = 0; //Port pin set to analog mode (voltage sampling)
78  AD1PCFGLbits.PCFG9 = 0; //Port pin set to analog mode (voltage sampling)AD2PCFGLbits.PCFG12 = 0; //Port pin set to analog mode (voltage sampling)
79  AD1PCFGLbits.PCFG10 = 0; //Port pin set to analog mode (voltage sampling)
80  AD1PCFGLbits.PCFG11 = 0; //Port pin set to analog mode (voltage sampling)
81  AD1PCFGLbits.PCFG12 = 0; //Port pin set to analog mode (voltage sampling)
82 
83 
84 
85 
86  IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
87  IEC0bits.AD1IE = 1; // Enable A/D interrupt
88  AD1CON1bits.ADON = 1; // Turn on the A/D converter
89 
90 }
unsigned int i
void initAirspeedSensor()
int airspeedHistory[AIRSPEED_HISTORY]
int historyCounter
int currentAirspeedSignal
void __attribute__((interrupt, no_auto_psv))
UART2 interrupt function routine for when data comes in from the XBEE.
#define AIRSPEED_HISTORY
void initAirspeedADC()
float getCurrentAirspeed()