Kaleborg
Member level 4
read_adc dont work
Hello!
I'm trying to build simple solar tracker but i cant figure out why this isnt working.
The problem is that motor keeps rotating in one direction as if pic only read channel 0 of the adc and did not bother read the second channel. Can anybody help me sort it out.
The method for reading ADC is this:
I did check the connections and all seems to be correct.
So i suspect that my problem is software.
I aprecieate all help.
Thanks in advance
Hello!
I'm trying to build simple solar tracker but i cant figure out why this isnt working.
Code:
#include <pic.h>
#include "my include.h"
#include "MyADC.h"
#include "delay.h"
#include "motor_control.h"
//#define _XTAL_FREQ 4000000
__CONFIG( XT & WDTDIS & PWRTDIS & BORDIS & UNPROTECT );
void main(void){
unsigned long left_ldr_val, right_ldr_val, i;
TRISA|=0x0f;
ANSEL0=0x3;
ANSEL1=0;
// ADCON0=0;
CM2CON0=0;
OPA1CON=0;
INIT_MOTO();
MOTO_PWM();
ADC_INIT();
while(TRUE){
left_ldr_val= READ_ADC(0)&0x03FF;
//DelayUs(10);
left_ldr_val=1024-left_ldr_val;
DelayUs(10);
right_ldr_val= READ_ADC(1)&0x03FF;
//DelayUs(10);
right_ldr_val=1024-right_ldr_val;
DelayUs(10);
//for(i=0; i<0x0200; i++)
while(left_ldr_val>right_ldr_val){
GO_LEFT();
}
while(left_ldr_val<right_ldr_val){
GO_RIGHT();
}
DelayUs(10);
}
}
The problem is that motor keeps rotating in one direction as if pic only read channel 0 of the adc and did not bother read the second channel. Can anybody help me sort it out.
The method for reading ADC is this:
Code:
unsigned long READ_ADC(unsigned char channel){
unsigned long ADC_result;
channel&=0x07; //get rid of unwanted bits
ADCON0&=0xC5; //Clear current channel selection
ADCON0|=(channel << 3); //Set new channel
DelayUs(100);
GODONE = 1; //Start the thing
while(GODONE); //Wait till its done
ADC_result=(ADRESH<<8 + ADRESL);
//ADC_result <<8;
//ADC_result=ADRESL;
return ADC_result;
}
So i suspect that my problem is software.
I aprecieate all help.
Thanks in advance