// PIC18F14K50 in C - Versione 0.7 - Febbraio 2012 // Copyright (c) 2010-2012 VincenzoV.net // Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported. // Creative Commons | Attribution-Share Alike 3.0 Unported // https://www.vincenzov.net/tutorial/elettronica-di-base/PIC18F14K50 // Using internal ADC - Version 2 #include #pragma config FOSC = IRCCLKOUT // Internal RC oscillator (F/4 to OSC2 - pin 3) #pragma config WDTEN = OFF // Watchdog disabled #pragma config LVP = OFF // Disable Low Voltage Programming #pragma config MCLRE = ON // Master clear enabled void main(void) { unsigned int voltage; OSCCON=0x70; // Select 16 MHz internal clock TRISC = 0x00; // Set PORTC as Output ANSEL = 0; // Set as Digital I/O ANSELH = 0; // Set as Digital I/O TRISBbits.TRISB4 = 1; // Set RB4 (pin 13, aka AN10) to input ANSELHbits.ANS10 = 1; // Set AN10 (pin 13, aka RB4) to analog ADCON0=0b00101001; // Channel AN10 (xx10 10xx), Enable ADC (xxxx xxx1) ADCON1=0b00000000; // VDD and VSS as voltage reference (xxxx 0000) ADCON2=0b00001101; // Left justify result (0xxx xxx), 2 TAD delay (xx00 1xxx), TAD = 1 us (Fck/16 = 1 Mhz) (xxxx x101) for (;;) { ADCON0bits.GO = 1; // Start conversion //PORTCbits.RC4 = 1; // RC4 up at start of conversion - Debug and timing while (ADCON0bits.GO); // Wait conversion done //PORTCbits.RC4 = 0; // RC4 down at end of conversion - Debug and timing voltage = ADRESH; // Read ADC high register and convert to 0-255 integer PORTC = (voltage >> 4) & 0x0F; // Dispaly 4 MSB } }