Hi guys,
thanks for all the help, but do anyone knows how to use 8 bit ADC in PIC16F77 using XC8, because I'm going to use it to demonstrate this as an example code implementation of an 8-bit ADC and 10-bit ADC of modern PICs in the upcoming video...
I tried but with no luck:
config.h
#include <xc.h>
// include processor files - each processor file is guarded.
// PIC16F77 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = LP // Oscillator Selection bits (LP oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bit (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#define _XTAL_FREQ 32786 // Sets the crystal oscillator frequency
void ADC_Init()
{
ADCON0 = 0b00000001;
/*
*See the the "0b" tells the compiler that this is a binary number,
* The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
* Bit 7-6 are A/D Conversion Clock Select bits, which set the sample clock.
* Bit 5-3 are Analog Channel Select bits, which will select a particular channel out of 8 Analog channel.
* Bit 2 is A/D Conversion Status bit, which initiates A/D conversion and it will be automatically cleared when the conversion completes.
* Bit 1 is not used so set to zero.
* Bit 0 is A/D On bit, which basically turns on/off the ADC
*/
ADCON1 = 0b00000000; //All pins as Analog Input and setting Reference Voltages
/*
*See the the "0b" tells the compiler that this is a binary number,
* The number right after the "0b" is MSB, the last number -right before the ";"- is LSB.
* Bit 7-3 is not used so set to zero.
* Bit 2-0 are A/D Port Configuration Control bits, which set the analog pins and refrence.
*/
}
unsigned int ADC_Read(unsigned int channel)
{
if(channel > 7) //Channel range is 0 ~ 7
{
return 0;
}
ADCON0 &= 0xC5; //Clearing channel selection bits
ADCON0 |= channel<<3; //Setting channel selection bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D conversion
while(GO_nDONE); //Waiting for conversion to complete
return ((ADRES));
}
main.c
#include "config.h"
void main(void) {
TRISAbits.TRISA0=1; // Analog Input pin AN0
TRISAbits.TRISA1=1; // Analog Input pin AN1
TRISBbits.TRISB7=0; // Digital Output pin RB7
ADC_Init(); //Initialize ADC
unsigned int ADC; // value of first analog channel
unsigned int ADC1; // value of second analog channel
ADC1=ADC_Read(1); //Initialize channel (1)
ADC=ADC_Read(0); //Initialize channel (0)
while(1){
if (ADC==ADC1){
PORTBbits.RB7=1; //if both channels are equal turn RB7 to high
}
else{
PORTBbits.RB7=0;//else turn RB7 to low
}
}
}