adc_setchannel(ch);
adc_conv();
#else
- if(ch < 16) {
- config1 = 0b10101101; // ADC_RIGHT_JUST & ADC_FOSC_32 & ADC_16_TAD & ADC_INT_OFF,
- config2 = ch << 3;
+ PIE1bits.ADIE = 0;
+ PIR1bits.ADIF = 0;
+ if(ch < _REF_ADC) {
+ ADCON0 = 0;
+ ADCON2 = 0b00000110; // 64OSC, Right
+ ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
+ ADCON2 |= 0b10111000; // TAQ=20TAD, Right
+ ADCON0 = ch << 2;
+
ansel_val = 0x0001 << ch;
+// ansel_val = 0x0080;
} else {
- config1 = 0b11101111; // ADC_RIGHT_JUST & ADC_FOSC_64 & ADC_20_TAD & ADC_INT_OFF,
- config2 = 0b00111000;
- ansel_val = 0x00;
+ ADCON0 = 0;
+ ADCON2 = 0b00000110; // 64OSC, Right
+ ADCON1 = 0b00000000; // VREF+=VDD,VREF-=VSS
+ ADCON2 |= 0b10111000; // TAQ=20TAD, Right
+ ADCON0 = 0x0f << 2;
+ ansel_val = 0x0000;
}
- OpenADC(config1, config2, ansel_val);
-// idle_time_ms(2); // 64OSC * 20 = 1280OSC < 1ms
- ADCON0bits.GO = 1;
+ ANSELH = ansel_val >> 8;
+ ANSEL = ansel_val & 0xff;
+ ADCON0bits.ADON = 1;
+ idle(160); // 64*20 = 1280clock = 160us
+ ADCON0bits.GO_DONE = 1;
#endif
}
}
#endif
-unsigned char polladc2(void)
+unsigned int polladc2(void)
{
unsigned int a;
- PIE1bits.ADIE = 0;
- PIR1bits.ADIF = 0;
- do { // converting or not enable.
- idle_time_ms(1);
- } while (ADCON0bits.DONE != 0);
-
- a = ((ADRESH << 8) + ADRESL) & 0x03ff;
- ADCON0bits.GO_DONE = 0;
- ADCON0bits.ADON = 0;
+// PIE1bits.ADIE = 0;
+// PIR1bits.ADIF = 0;
+ do {
+ idle(50); // wait about 50us
+ a = polladc();
+ } while((a & 0x8000) != 0);
return a;
}
{
// raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
-// return ((b * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
- return ((b * 120) / reflevel) * 4;// Divide = 4, but input impedance of PIC is lower ;-) ,
- // about 0.8 (x 4) mesure value, multiply 32/10.
+ return ((b * 120) / reflevel) * 4;
}
\ No newline at end of file
#endif
extern void intadc_init(void);
extern unsigned int polladc(void);
-extern unsigned char polladc2(void);
+extern unsigned int polladc2(void);
extern void startadc(unsigned char ch);
#ifdef __SDCC
extern void stopadc(void);
cnrlevel = akc6955_get_cnr();
// Battery
// batlevel_6955 = akc6955_get_battery();
- //ADC:Battery - AN7, 28pins PIC should be changed AN number.
- startadc(_BATT_ADC);
- adc = polladc2();
+
//ADC:reference
CVRCON2bits.FVREN = 1;
- while(CVRCON2bits.FVRST == 0) idle_time_ms(1);
+ while(CVRCON2bits.FVRST == 0) idle(100);
+ // Workaround of Errata:
+ // Section 5 of http://ww1.microchip.com/downloads/en/DeviceDoc/80000425K.pdf .
+ // Sample twice on first.
startadc(_REF_ADC);
ref = polladc2();
+ startadc(_REF_ADC);
+ ref = polladc2();
+ TRISEbits.TRISE2 = 1;
+ startadc(_BATT_ADC);
+ adc = polladc2();
+ startadc(_BATT_ADC);
+ adc = polladc2();
CVRCON2bits.FVREN = 0;
- //ADC:reference
battlevel = adc_rawtobatt(adc, ref);
-// battlevel = ref;
+// battlevel = adc * 32 * 4; // 322 = 1000/1024 * 330
+// battlevel = (adc * 4 * 32) / 100;
}
+