Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] problem with ADC - simple solar tracker isnt working

Status
Not open for further replies.

Kaleborg

Member level 4
Joined
Nov 2, 2008
Messages
75
Helped
7
Reputation
14
Reaction score
5
Trophy points
1,288
Activity points
1,776
read_adc dont work

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;
}
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
 

opa1con

rewrite the below code

Code:
      while(left_ldr_val>right_ldr_val){ 
          
         GO_LEFT(); 
      } 
      while(left_ldr_val<right_ldr_val){ 
          
       
         GO_RIGHT(); 
      }

to

Code:
      if (left_ldr_val > right_ldr_val) 
           GO_LEFT(); 
      else if (left_ldr_val < right_ldr_val)
           GO_RIGHT();

Hope this helps!!

Nishal
 

    Kaleborg

    Points: 2
    Helpful Answer Positive Rating
Re: problem with ADC

I changed the code as you said and it's working perfectly now!!

I wasn't even loking at that part of code, i was convinced that adc chanells were the problem.

Thanks for help Nishal
 

Re: problem with ADC

Hi I am currently working on a solar tracker too. I would be really appreciate if you could contact me by email or pm. Thx
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top