EDF - OnePIC MCU  Beta
8bit/OnePIC_pot.c
Go to the documentation of this file.
00001 
00009 #include "OnePIC_pot.h"
00010 #include <htc.h>
00011 #include "bsp.h"
00012 #include "TimeDelay.h"
00013 #include "int.h"
00023 static int comp(const void *e1, const void *e2)
00024 {
00025     const int * a1 = e1;
00026     const int * a2 = e2;
00027     if (*a1 < *a2)
00028         return -1;
00029     else if (*a1 == *a2)
00030         return 0;
00031     else
00032         return 1;
00033 }
00034 
00039 void EDF_potInit(void)
00040 {
00041     ADC_CONTROL_REGISTER0 = 0b00110100; //AN13 = POT :: ANXX = MIC
00042     ADC_CONTROL_REGISTER1 = 0b11010000; //Right justified using Vdd/Vss as reference voltages
00043     ADC_ON;
00044     DelayMs(5); //init delay 
00045 }  
00046 
00047 
00053 uint16_t EDF_potRead(void)
00054 {
00055     volatile uint16_t pot_voltage;
00056 
00057     EDF_INT_Disable(); //Turn off interrupts
00058     EDF_potInit(); //need to init again because of mTouch
00059     GO = 1; //start the ADC
00060     while (ADC_WAITING) {
00061         Nop();
00062     } //Wait for conversion to complete
00063     pot_voltage = ADC_RESULT_HIGH; //grab the top 2 MSBs
00064     pot_voltage = pot_voltage << 8; //make room for lower 8 bits
00065     pot_voltage |= ADC_RESULT_LOW;
00066     EDF_INT_Enable(); //entable
00067 
00068 
00069     return pot_voltage;
00070 }