* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*/
-#include "adc.h"
+
+//#include "adc.h"
#include "adc_int.h"
void intadc_init(void)
{
} else { // Done, Clear interrupt
a = ((ADRESH << 8) + ADRESL) & 0x03ff;
ADCON0bits.GO_DONE = 0;
+ ADCON0bits.ADON = 0;
return a;
}
}
*/
void startadc(unsigned char ch)
{
+ unsigned int ansel_val;
+ unsigned char config1, config2;
#ifdef __SDCC
adc_open(ch, ADC_FOSC_64, 0, ADC_FRM_RJUST | ADC_INT_OFF | ADC_VCFG_VDD_VSS | ADC_NVCFG_VSS | ADC_PVCFG_VDD);
adc_setchannel(ch);
adc_conv();
#else
- OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD, (ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS & (0x0f << 2)) | (ch << 2) , 0x0001 << ch);
+ if(ch < 16) {
+ config1 = 0b10101101; // ADC_RIGHT_JUST & ADC_FOSC_32 & ADC_16_TAD & ADC_INT_OFF,
+ config2 = ch << 3;
+ ansel_val = 0x0001 << ch;
+ } else {
+ config1 = 0b11101111; // ADC_RIGHT_JUST & ADC_FOSC_64 & ADC_20_TAD & ADC_INT_OFF,
+ config2 = 0b00111000;
+ ansel_val = 0x00;
+ }
+ OpenADC(config1, config2, ansel_val);
+// idle_time_ms(2); // 64OSC * 20 = 1280OSC < 1ms
ADCON0bits.GO = 1;
#endif
}
unsigned int a;
PIE1bits.ADIE = 0;
PIR1bits.ADIF = 0;
- while (ADCON0bits.DONE != 0)
- { // converting or not enable.
+ 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;
return a;
}
// raw = (reflevel[0.01V] * b) / 1024 * divide :divide = 4
// return ((b * 4) / reflevel) * (1024/1000)* 120; // Divide = 4
- reflevel = reflevel / 4;
- return ((b * 120) / reflevel) * 4 / 4;// 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.
}
\ No newline at end of file