PORT initialization and ADC error

Status
Not open for further replies.

fasihuddin

Newbie level 6
Joined
Jun 1, 2011
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,372
iam usiing pic2f32ka304 controller and writing the port initialization program as follows



#include"stdio.h"
#include"stdlib.h"
#include"p24F32KA304.h"
#include"libpic30.h"
void init_port(void);
void main()
{
init_port();
}

void init_port(void)
{
TRISA=0x0004; // set RA2 as an i/p and others as o/p
TRISB=0x000C; // set RB2,RB3 as i/p and others as o/p
TRISC=0x0007; // set RC0,RC1,RC2 as i/p and others as o/p
ANSELA=0x0004; // set RA2 as an i/p and others as o/p
ANSELB=0x000C; // set RB2,RB3 as i/p and others as o/p
ANSELC=0x0007; // set RC0,RC1,RC2 as i/p and others as o/p

}



when iam using the watch function and seeing whether the ports r being correctly initializing r not...in the TRISA register iam getting 0x000c instead of 0x0004..but in ANSELA register iam getting correct value(0x0004)....whts the problem....help me



in the following adc program when i simulate the program using mplab sim iam getting the following error

ADC-E0003: No input stimulus file is attached to the ADC
// ADC program starts from here
#include"stdio.h"
#include"stdlib.h"
#include"p24F32KA304.h"
#define FCY 8000000UL
#include"libpic30.h"
#define FNOSC_FRCDIV 0xFFFF
#define green PORTBbits.RB3
#define samples 16
#define div_val 4
unsigned int voltage;
unsigned char readadc(unsigned char adc_channel);
unsigned char getadc(unsigned char adc_channel);
unsigned char sensing_voltage=0x0004;
void init_port(void);
void init_adc(void);
void main()
{
init_port();
init_adc();
while(1)
{
voltage=0;
voltage = getadc(sensing_voltage);
if(voltage > 255)
green=0;
else
green=1;
}
}
void init_port(void)
{
TRISA=0x0004; /* set RA2 as an i/p and others as o/p*/
TRISB=0x000C; /* set RB2,RB3 as i/p and others as o/p*/
TRISC=0x0007; /* set RC0,RC1,RC2 as i/p and others as o/p*/
ANSELA=0x0004; /* set RA2 as an i/p and others as o/p*/
ANSELB=0x000C; /* set RB2,RB3 as i/p and others as o/p*/
ANSELC=0x0007;
//return(0);
}

void init_adc(void)
{
AD1CON1=0x0400; //A/D convertor is operating ...
AD1CON2=0x0000; //Voltage references +ve AVdd .. -ve is AVss
AD1CON3=0x0000;
AD1CON5=0x0000;
AD1CHS=0x0000;
AD1CHITH=0x0000;
AD1CHITL=0x0000;
//return(0);
}

unsigned char readadc(unsigned char adc_channel)
{
volatile unsigned int ADCValue;
IFS0bits.AD1IF = 0;// Clear A/D conversion interrupt
IEC0bits.AD1IE = 1;// Enable A/D conversion interrupt
AD1CON1bits.ADON = 1;
AD1CON1bits.SAMP = 1;// Start sampling the input
__delay_ms(1);// Ensure the correct sampling time has elapsed
AD1CON1bits.SAMP = 0;// start converting
while (!AD1CON1bits.DONE)continue;// conversion done r not
ADCValue = ADC1BUF0;
return(ADCValue);
}

unsigned char getadc(unsigned char adc_channel)
{
int result;
unsigned char k;
result=0;
for(k=0;k<samples;k++)
{
result+=readadc(adc_channel);
__delay_ms(1);
}
result=(result>>div_val);
return result;
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…