...
All the code for the voltage sensor is available in the voltageSensor.c and .h files. The code which controls the ADC module is extremely straight forward. The process mostly involves initializations. Most of the "magic" is handled automatically by the module, therefore minimal logical coding is required. A few of the important lines have been included below:
Code Block | ||
---|---|---|
| ||
AD1CON1bits.FORM = 0; // Data Output Format: Unsigned Int AD1CON1bits.SSRC = 7; // Internal Counter (SAMC) ends sampling and starts conversion AD1CON1bits.AD12B = 1; // 12-bit single channel operation AD1CON1bits.SAMP = 1; AD1CON2bits.SMPI=0; // Interrupt address every sample/conversion AD1CON2bits.CHPS = 0; //We are using channel 0 AD1CON2bits.VCFG = 0; //Voltage Reference is 3.3V and Ground Reference is Ground AD1CON3bits.SAMC=0; // Auto Sample Time = 0\*Tad AD1CON3bits.ADCS=6; // ADC Conversion Clock Tad=Tcy\*(ADCS+1)= (1/40M)\*7 = 175nS AD1CHS0bits.CH0SA = 0xC; //Channel 0 positive input on AN12 (Sample A) AD1CHS0bits.CH0SB = 0xC; //Channel 0 positive input on AN12 (Sample B) IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit IEC0bits.AD1IE = 1; // Enable A/D interrupt AD1CON1bits.ADON = 1; // Turn on the A/D converter |
...