You are on page 1of 2

/*

* File: adc.c
* Author: SATELLITE
*
* Created on 15 de enero de 2018, 10:45
*/

#define Tad 0.250 //us


#define FCY 7370000UL/2
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <p33FJ16GP304.h>
#include <libpic30.h>
void Init_HW(void);
void Convert_Analog(void);
int Read_Analog(uint16_t* ADC_Values);

void Init_HW(void)
{
//All PortC as Output
TRISC = 0x000;
PORTC = 0x000;
//PortA.RA10 as input
TRISA = 0b10000000000;
//Initialization of the ADC Module
/* 1. Select analog inputs CH0, CH1, CH2, CH3 */
AD1CON1bits.AD12B = 0;//10?bit, 4?channel ADC operation
AD1PCFGL = 0b1111111000000;//Analog inputs enabled for AN0 to AN5
AD1CSSL = 0b0000000111100;
AD1CON2bits.BUFM = 0; //Always starts filling buffer from the beginning
AD1CON1bits.SIMSAM = 1; //Samples CH0, CH1, CH2, CH3 simultaneously
AD1CON1bits.ASAM = 0; //Sampling begins when SAMP bit is set
AD1CHS123bits.CH123NB = 0b00; //CH1, CH2, CH3 negative input is VREF?
AD1CHS123bits.CH123SB = 1; //CH1 positive input is AN3, CH2 positive input
//is AN4, CH3 positive input is AN5
AD1CHS123bits.CH123NA = 0b00;//CH1, CH2, CH3 negative input is VREF?
AD1CHS123bits.CH123SA = 1; //CH1 positive input is AN3, CH2 positive input
//is AN4, CH3 positive input is AN5
AD1CHS0bits.CH0NB = 0; //Channel 0 negative input is VREF?
AD1CHS0bits.CH0SB = 0b00010;//Channel 0 positive input is AN2
AD1CHS0bits.CH0NA = 0;//Channel 0 negative input is VREF?
AD1CHS0bits.CH0SA = 0b00010;//Channel 0 positive input is AN2
/* 2. Select voltage reference source */
AD1CON2bits.VCFG = 0b011;//ADREF+ is External Vref+ and ADREF? is External Vref?
/* 3. Select the analog conversion clock */
AD1CON3bits.ADRC = 1;//ADC internal RC clock 4MHz
AD1CON3bits.ADCS = 0x00;//Tad = 1/Fadc = 1/4MHz = 250 ns
/* 4. Determine how many S&H channels will be used simultaneously*/
AD1CON2bits.CHPS = 0b11;//S&H channels for Converting CH0, CH1, CH2 and CH3
/* 5. Select the appropriate sample/conversion sequence */
AD1CON1bits.SSRC = 0x000;//Clearing sample bit ends sampling and starts conv
AD1CON3bits.SAMC = 0x00001; //Sampling time is 1 TAD
/* 6. Select the way conversion results are presented in the buffer */
AD1CON1bits.FORM = 0x00;//Format of 10?bits Data is Integer
//(DOUT = 0000 00dd dddd dddd)
/* 7. Turn on the ADC module��*/
AD1CON1bits.ADON = 1;//Turn on the ADC Module��������
return;
}
void Convert_Analog(void)
{
AD1CON1bits.SAMP = 1;//ADC sample?and?hold amplifiers are sampling
__delay_us(4*Tad);
AD1CON1bits.SAMP = 0;//ADC sample?and?hold amplifiers are sampling
while (AD1CON1bits.DONE == 0);
return;
}
int Read_Analog(uint16_t* ADC_Values)
{
ADC_Values[0] = ADC1BUF0;
ADC_Values[1] = ADC1BUF1;
ADC_Values[2] = ADC1BUF2;
ADC_Values[3] = ADC1BUF3;
return 4;
}

int main(int argc, char** argv) {


uint16_t ADC_Values[4];
char i = 0;
Init_HW();
while (1)
{
if (PORTAbits.RA10 == 0)
{
i = 0;
while (i<4)
{
Convert_Analog();
i++;
}
Read_Analog(ADC_Values);
PORTC = (ADC_Values[0] * 3.3 * 100.0)/1024.0;
__delay_ms(2000);
PORTC = (ADC_Values[1] * 3.3 * 100.0)/1024.0;
__delay_ms(2000);
PORTC = (ADC_Values[2] * 3.3 * 100.0)/1024.0;
__delay_ms(2000);
PORTC = (ADC_Values[3] * 3.3 * 100.0)/1024.0;
__delay_ms(2000);
}
else
{
PORTC = 0x2FF;
}
}
return (EXIT_SUCCESS);
}

You might also like