ArakelTheDragon
- Joined Nov 18, 2016
- 1,362
The correct datasheet for reference.
All the analog modules have to be disabled, that means not only "PORTA", but also the comparators, clock pins(oscilator pins) if you use the internal oscillator, voltage reference and all other if present on your PIC.
An example for PIC "16F506". Do remember that for different "PICs" the registers may have different names. "XC8" exposes the registers as variables, but also allows us to use them as "bit field unions". Meaning you can choose to set the register as" REGISTER = 0x00" or you can choose to set an individual bit as "REGISTER.bit0 = 0".
All the analog modules have to be disabled, that means not only "PORTA", but also the comparators, clock pins(oscilator pins) if you use the internal oscillator, voltage reference and all other if present on your PIC.
An example for PIC "16F506". Do remember that for different "PICs" the registers may have different names. "XC8" exposes the registers as variables, but also allows us to use them as "bit field unions". Meaning you can choose to set the register as" REGISTER = 0x00" or you can choose to set an individual bit as "REGISTER.bit0 = 0".
Code:
*************************************************************************
* *
* Architecture: Baseline PIC *
* Processor: 16F506 *
* Compiler: MPLAB XC8 v1.01 (Free mode) *
* *
*************************************************************************
* *
* Files required: none *
* *
*************************************************************************
* *
* Description: Lesson 7, example 1 *
* *
* Demonstrates basic use of ADC *
* *
* Continuously samples analog input, copying value to 4 x LEDs *
* *
*************************************************************************
* *
* Pin assignments: *
* AN0 = voltage to be measured (e.g. pot output) *
* RC0-3 = output LEDs (RC3 is MSB) *
* *
************************************************************************/
#include <xc.h>
/***** CONFIGURATION *****/
// ext reset, no code protect, no watchdog, 4 MHz int clock
__CONFIG(MCLRE_ON & CP_OFF & WDT_OFF & IOSCFS_OFF & OSC_IntRC_RB4EN);
// Pin assignments
#define LEDS PORTC // output LEDs on RC0-RC3
/***** MAIN PROGRAM *****/
void main()
{
//*** Initialisation
// configure ports
TRISC = 0b110000; // configure RC0-RC3 as outputs
CM2CON0 = 0; // disable comparator 2 -> RC0, RC1 digital
VRCON = 0; // disable CVref -> RC2 usable
// configure ADC
ADCON0bits.ADCS = 0b11; // clock = INTOSC/4
ADCON0bits.ANS = 0b10; // AN0, AN2 analog
ADCON0bits.CHS = 0b00; // select channel AN0
ADCON0bits.ADON = 1; // turn ADC on
// -> AN0 ready for sampling
//*** Main loop
for (;;)
{
// sample analog input
ADCON0bits.GO = 1; // start conversion
while (ADCON0bits.nDONE) // wait until done
;
// display result on 4 x LEDs
LEDS = ADRES >> 4; // copy high nybble of result to LEDs
}
}