]>
oss.titaniummirror.com Git - rgblamp.git/blob - adc_random.c
4 * Generate a random 8-bit value by reading an unused (floating) IO pin.
5 * Pin 1, RA2 (AN2) is free in the current design.
11 unsigned char adc_random()
13 static bit save_ansa2
;
14 static bit save_trisa2
;
15 unsigned char accumulate
= 0;
17 /* Turn on the FVR, configured for 1.024V to the ADC */
19 while (!FVRRDY
); /* wait for ready signal */
21 /* Configure RA2 (AN2) for ADC input */
26 ADCON1
= 0b11110011; /* Right justified result, Frc clk, FVR/Vss refs */
27 ADCON0
= 0b00001001; /* Enable channel AN2, enable ADC */
29 /* Sample the ADC several times, accumulating the LSB of the result */
30 for (unsigned i
= 0; i
< 128; i
++) {
31 tmr_uwait(10); /* Sampling time */
32 ADGO
= 1; /* Start the conversion */
33 while (ADGO
); /* wait for completion */
37 /* Turn off ADC, FVR, and revert PORTA changes */
43 /* Return the result */