You are on page 1of 2

#include

#include
#include
#include
#include
#include
#include
#include
#include
#include

<stdio.h>
<stdlib.h>
"system.h"
<xc.h>
<libpic30.h>
"lcd.h"
"keypad.h"
"glcd.h"
"GraphicalLCD.h"
"Picture.h"

void InitADC(void);
int ReadADC(char Analogchannel);
int main(){
rewrite_register();
InitADC();
TRISB=0;
while(1)
{
LATB=ReadADC(0);
}
return 0;
}
void InitADC(void)
{
AD1PCFGL &=~(1<<0);
////
//ConfigPinsAsAnalog();
AD1CON1 = 0;
AD1CON1bits.AD12B = 1;
//
AD1CON1bits.FORM = 0;
//
AD1CON1bits.SSRC = 0;
//
sampling and starts converting.
AD1CON2bits.CSCNA = 0;
//
AD1CON2bits.VCFG = 0;
//
AD1CON3 = 0x1F02;
//
s >75ns
IFS0bits.AD1IF = 0;
//
IEC0bits.AD1IE = 0;
//
AD1CON1bits.ADON = 1;
//
}

PIN0 in Analoge mode


// Clear it
12-bit, 1-channel ADC operation
Integer (DOUT = 0000 dddd dddd dddd)
SSRC bit = 111 implies internal counter ends
NO Scan required
AVss and AVdd are used as Vref+/max sample time = 31Tad, Tad = 2 x Tcy = 125n
Clear the A/D interrupt flag bit
Do Not Enable A/D interrupt
turn on the ADC

int ReadADC(char Analogchannel){


AD1CHS0bits.CH0SA = Analogchannel; // Connect ANX to CH0 input
asm("nop");
// Some delay
asm("nop");
asm("nop");
AD1CON1bits.SAMP = 1;
// start sampling, automatic con
version will follow
__delay_ms(5);
// Some delay
AD1CON1bits.SAMP = 0;
// start sampling, automatic con
version will follow
while (!AD1CON1bits.DONE);
// wait to complete the conversi
on
return ADC1BUF0;
// read the conversion result

You might also like