// 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 (8 bit) #define VREF 3.20 #include "configurationbits.h" void display(unsigned voltage); void display2(unsigned voltage); void display3(unsigned voltage); void main(void) // Program start here { unsigned char voltage8bits; double voltage; OSCCON = 0x70; // Set internal clock to 16 MHz TRISC = 0x00; // Port C as output TRISBbits.RB5 = 1; // Disable digital output buffer on AN11/RB5 ANSELHbits.ANS11 = 1; // Disable digital input buffer for AN11 ADCON2bits.ADFM = 0; // Left justified ADCON2bits.ADCS = 0b101; // A/D Conversion Clock = FOSC/16 (1 us @ 16 MHz) ADCON2bits.ACQT = 0b001; // A/D Acquisition time set to 2 TAD ADCON1 = 0; // Set VDD and VSS as voltage reference ADCON0bits.CHS = 0b1011; // Analog Channel Select AN11 ADCON0bits.ADON = 1; // ADC is enabled while (1) { ADCON0bits.GO = 1; // Start conversion while (ADCON0bits.nDONE); // Wait End Of Conversion voltage8bits = ADRESH; // Read voltage (8 bit only!) voltage = (double) VREF / 256 * voltage8bits; display3(voltage8bits); } } void display(unsigned voltage) { PORTC = voltage; } void display2(unsigned voltage) { voltage = voltage >> 5; PORTC = 1 << voltage; } void display3(unsigned voltage) { voltage = voltage + 16; voltage = (voltage >> 5); PORTC = ~(0xFF << voltage); }