]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - adc_random.c
Use bit definitions to simplify adc_random()
[rgblamp.git] / adc_random.c
index 05450f4f87f767947c1149db0d807be1ca2466dc..d516d7fbc74b6802b1d4fe4a6f55aa3013c2ec27 100644 (file)
@@ -10,8 +10,8 @@
 
 unsigned char adc_random()
 {
-    unsigned char save_ansela;
-    unsigned char save_trisa;
+    static bit save_ansa2;
+    static bit save_trisa2;
     unsigned char accumulate = 0;
 
     /* Turn on the FVR, configured for 1.024V to the ADC */
@@ -19,10 +19,10 @@ unsigned char adc_random()
     while (!FVRRDY); /* wait for ready signal */
 
     /* Configure RA2 (AN2) for ADC input */
-    save_ansela = ANSELA;
-    ANSELA |= 4;
-    save_trisa = TRISA;
-    TRISA |= 4;
+    save_ansa2 = ANSA2;
+    ANSA2 = 1;
+    save_trisa2 = TRISA2;
+    TRISA2 = 1;
     ADCON1 = 0b11110011; /* Right justified result, Frc clk, FVR/Vss refs */
     ADCON0 = 0b00001001; /* Enable channel AN2, enable ADC */
 
@@ -37,8 +37,8 @@ unsigned char adc_random()
     /* Turn off ADC, FVR, and revert PORTA changes */
     ADON = 0;
     FVREN = 0;
-    TRISA = save_trisa;
-    ANSELA = save_ansela;
+    TRISA2 = save_trisa2;
+    ANSA2 = save_ansa2;
 
     /* Return the result */
     return accumulate;