cyrusthevirus
New Member
Here is the new code:
main.c
adc.c
adc.h
main.c
Code:
//***************************************************/
// Include Header File
//***************************************************/
#include "p24FJ128GA110.h"
#include "adc.h"
//***************************************************/
// Define Constants Variables
//***************************************************/
//***************************************************/
int main (void)
{
initADC(0xFFEF); //make rb2 AN4 an analog input
TRISB = 0xff00; //Make all B outputs
int a, j;
T1CON = 0x8030; // TMR1 on, prescale 1:256 Tclk/2
while(1)
{
a = 0;
for ( j=16; j >0; j--)
{
TMR1 = 0;
while ( TMR1 < 3900); // 3900 x 256 x Tcy ~= 1sec
a += readADC(4);
}
a >>= 4;
if(a > 255)
{
PORTB = 0b10000000; //turn on LED on pin B7
}
}
};
//end of code
adc.c
Code:
#include <p24FJ128GA110.h>
#include "adc.h"
int readADC( int sensor)
{
AD1CHS = sensor; // select analog input channel
AD1CON1bits.SAMP = 1; // start sampling, automatic conversion will follow
while (!AD1CON1bits.DONE); // wait to complete the conversion
return ADC1BUF0; // read the conversion result
} // readADC
void initADC( int amask)
{
AD1PCFG = amask; // select analog input pins
AD1CON1 = 0x00E0; // auto convert after end of sampling
AD1CSSL = 0; // no scanning required
AD1CON3 = 0x1F02; // max sample time = 31Tad, Tad = 2 x Tcy = 125ns >75ns
AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC
adc.h
Code:
#ifndef __ADCDRV_H__
#define __ADCDRV_H__
int readADC( int sensor);
void initADC( int amask);
#endif