Loading...
 

12bit AD in PicBasicPro

PIC ADC

I have used the new 18F8723 with 12 bit A/D. 

To get the the full resolution, you have to use DEFINE

"DEFINE ADC_BITS 10"

Because PBP only "knows about" 8 and 10 bit converters. 
If the DEFINE is missing or not interpreted properly in the
compiler, it will default to 8 bits.

If you use the line above at the top of your program, 
and have a 12-bit converter in your chip, the result will
indeed have a full 12 bits of resolution.

Charles Linquist


long map(long x, long in_min, long in_max, long out_min, long out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}



PWMDuty = 100 + (ADCInValue ** 39322) ' Same as 100 + ADCInValue * 0.600006 but likely faster than 100+ADCInValue*6/10

Think of the ** operator as multiplying by units of 1/65536. 0.6*65536=39322.

  '  m = 0.1219
  '  b = 24.9
  '  y = (ADC * 0.1219) - 24.9
  '  ADC = 1023
  '  y = 99.8
    M   CON 1219     'm = 0.1219 * 10,000
    B   CON 249      'b = 24.9 * 10
    ADC VAR WORD     'INPUT FROM SENSOR
    Y   VAR WORD     'PSI
    Z   VAR WORD     'DUMMY VAR
    Z1  VAR WORD     'DUMMY VAR
    
    START:   
    ADC = 1023  'FULL SCALE HARD CODED, NO SENSOR
    Z = ADC * M
    Z1 = DIV32 1000
    Y = Z1 - B
    LCDOUT $FE,1,"PSI= ",DEC Y/10,".",DEC2 Y//100
    PAUSE 250
    GOTO START



 
29	mainloop:
30	   ADCIN 3, adval               ' Read channel 3 to adval (0-1023)
31	   adval = (adval */ 500)>>2    ' Equates to: (adval * 500)/1024
32