PIC184550 2 A/D conversion and PWM error

Thread Starter

limjunda

Joined Aug 11, 2010
3
hi i am new to this forum and i am having trouble trying to get my microcontroller to read my analog signal from AN1 after finishing reading from AN0.
i am using PIC184550.
Rich (BB code):
	T0CON=0b00000111;		//Off Timer0, 16-bits mode, prescaler to 256

	T2CON=0b00000111;		//Timer2 is On, Prescaler is 16

	CCP1CON=0b00001100;		//Turn on PWM on CCP1, output at RC2
	CCP2CON=0b00001100;        //Turn on PWM on CCP2
	PR2 = 149;				//Load period of PWM 0.2msec for 5KHz


	TRISA=0xFF;
	TRISCbits.TRISC1 = 0; 	// RC1 as output pin
 	PORTCbits.RC1    = 0;   // RC1 is connected to Relay

	TRISCbits.TRISC2 = 0; 	// RC2 as output pin
 	PORTCbits.RC2    = 0;   // RC2 is connected to Motor

	ADCON0 = 0b00000001;  
	ADCON1 = 0b00001101;
	ADCON2 = 0b00000111;	
while(1)
{
	ADCON0 = 0b00000001;                //select channel 0 AN0
	ADCON2 = 0b00000111;

		ADCON0bits.GO = 1;		// START CONVERSION NOW
	        
		while(ADCON0bits.GO == 1); 	// Waiting for DONE 
		{}	
		result1=ADRESH;
if(result1>0b10001001)
{
		
			CCP1CON=0b00001100;
			CCPR1L = 0b11100000;		
			CCP1CONbits.DC1B1=1;		
			CCP1CONbits.DC1B0=0;
			Delay1sec();
}
else
{
		
			CCP1CON=0b00000000;
			CCPR1L = 0b00000000;		
			CCP1CONbits.DC1B1=1;		
			CCP1CONbits.DC1B0=0;
			Delay1sec();
}

ADCON0 = 0b00010001;                       //select channel 1 AN1
ADCON2 = 0b00100111;

		ADCON0bits.GO = 1;		// START CONVERSION NOW
	        
		while(ADCON0bits.GO == 1); 	// Waiting for DONE 
{}
PORTD=ADRESH;
result=ADRESH;
if(result>0b10001001)
{
			CCP2CON=0b00001100;
			CCPR2L = 0b11100000;		
			CCP2CONbits.DC2B1=1;		
			CCP2CONbits.DC2B0=0;
			Delay1sec();
}
else
{
			
			CCP2CON=0b00000000;
			CCPR2L = 0b00000000;		
			CCP2CONbits.DC2B1=0;		
			CCP2CONbits.DC2B0=0;
			Delay1sec();
}

}
 

lowy

Joined Aug 11, 2010
1
Why are you not using the C18-functions? I have found a tutorial explaining how you can read analog sensors using the 18F4550. The tutorial uses C and you can find it on http://www.dwengo.org/tutorials/light-measurement
When you have read the tutorial, you can extend it easily to multiple sensors by just selecting the correct channel using changing SetChanADC(ADC_CH0) to an other channel for instance SetChanADC(ADC_CH1). Make sure that your sensors are installed correctly. If you need more than 5 AD-channels you will need to set some configuration-bits differently using the OpenADC() function (but that is explained in that tutorial).
 

Thread Starter

limjunda

Joined Aug 11, 2010
3
hi erm i am sorry cause using function from the adc.h is out of my knowledge currently as i am only a polytechnic student and that adc.h is not within the topic i studied for the module.
Therefore i am constraint to using the longer method shown in our textbook.
 

Thread Starter

limjunda

Joined Aug 11, 2010
3
i have also tried using the function by editing a sample code i found on the net.
but it is still unable to get reading for my 2nd analog signal input which is the AN1 from PORTA.

my programme is able to work totally fine when there is only 1 motor and 1 a/d input.
 
Top