// PIC18F14K50 in C - Versione 1.0 - Settembre 2014 // Copyright (c) 2014 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/PIC18/adc.htm // ADC (PLIB) #define VREF 3.20 #define USE_OR_MASKS // Use the OR mask setting #include "configurationbits.h" #include "adc.h" void display(unsigned voltage); void main(void) // Program start here { unsigned int voltage10bits; double voltage; OSCCON = 0x70; // Set internal clock to 16 MHz TRISC = 0x00; // Port C as output TRISBbits.RB5 = 1; // Disable digital output buffer for AN11/RB5 pin ANSELHbits.ANS11 = 1; // Disable digital input buffer for AN11 pin OpenADC(ADC_FOSC_16 | ADC_RIGHT_JUST | ADC_2_TAD, ADC_CH11 | ADC_INT_OFF, ADC_REF_VDD_VSS, 0); // Config ADC - See PLIB documentation while (1) { ConvertADC(); // Start of conversion while (BusyADC()); // Wait until end of conversion voltage10bits = ReadADC(); // Read data voltage = (double) VREF / 1024 * voltage10bits; display(voltage10bits); } } void display(unsigned voltage) { voltage = voltage + 64; voltage = (voltage >> 7); PORTC = ~(0xFF << voltage); }