go_away

Author Topic: [NEED HELP !!]adc read IR senzor  (Read 1675 times)

0 Members and 1 Guest are viewing this topic.

Offline alex2988Topic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
[NEED HELP !!]adc read IR senzor
« on: May 21, 2010, 10:39:38 AM »
I'm a beginner and i'm trying to read a value with from my IR senzor. I'm useing a PIC 16f877 but sometimes the adc gives me another set of data. I think i dont really got the adc read function to work correctly. Please help me

here is a sample from my code
I'm using microC PRO for PIC to write the code and as a compiler
 

unsigned int Temp_res;

void read_senzor(){

 
   delay_ms(20);
ADCON0=  00011001   
ADCON0 = 00011101;
   while(ADCON0.GO==1);
   temp_res = ADC_Read(3);   // Get 10-bit results of AD conversion
   }


void main() {
   TRISA  = 0x08;              // PORTA is input
   TRISB = 0  ;
   ADCON0 = 10011001;
   ADCON1 = 10000000;
   PIR1=00000000;
   PIE1=01000000;
   INTCON=11000000;

   PORTA=0x00;
   PORTB=0x00;
   PORTE=0x00;
   PORTD=0x00;

   delay_ms(20);
    for(i=0;i<60;i++){
   servo_center();
                      }

 do {
  citire_senzor();
  if(temp_res>200)
  {....}
else{
servo_left;
}

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,938
  • Helpful? 98
Re: [NEED HELP !!]adc read IR senzor
« Reply #1 on: May 21, 2010, 12:35:19 PM »
Try wiring a potentiometer and Voltmeter to the ADC input to verify that your code is working correctly.

Code: [Select]
void read_senzor(){

   delay_ms(20);
ADCON0=  00011001   
ADCON0 = 00011101;
   while(ADCON0.GO==1);
   temp_res = ADC_Read(3);   // Get 10-bit results of AD conversion
   }

Why are you writing to ADCON0 with two different values?

Also, after changing which ADC pin is being read you need the delay to allow the ADC sampling cap to charge.

What does the function ADC_Read() do? Is it working correctly?

Here is the C code I use. It doesn't use any compiler's built-in functions.
ADC_init() set up the ADC with the defines
GetADC(channel) selects the ADC channel and returns the result
Code: [Select]
#define adcon0_mask 0x81 // Fosc/32, ch0, ADON
#define adcon1_init_lr 0x84        // RA0,1,3 = AD input, RA2,5, RE0,1,2 = Digital input
       // right justify AD result

//---------------------------------------------------------------
void ADselect( unsigned char a) {
const char SelectAD[5] = {
0x81,
0x89,
0x91,
0x99,
0xa1
};
ADCON0 = SelectAD[a];
return;
}
//---------------------------------------------------------------
// Get data from AD
// Fosc/32 = 1.536 MHz, Tad = 6.5104 usec
// conversion = 12 Tad = 78.125 usec 
// 2Tad = 13.02 usec
unsigned int GetADC( unsigned char c) {
ADselect( c);
DelayUs ( 10);
GODONE = 1;
AD_wait:
if ( GODONE == 1)
goto AD_wait;
return (ADRESH<<8 & ADRESL);
}

//---------------------------------------------------------------
// 8 bits in ADRESH
void ADC_init(void) {
ADCON0 = adcon0_mask;
ADCON1 = adcon1_init_lr;
}

Offline alex2988Topic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: [NEED HELP !!]adc read IR senzor
« Reply #2 on: May 21, 2010, 12:52:41 PM »
ADCON0=  00011001   -- THE GODONE bit in set to 0
ADCON0 = 00011101;   --the GODONE bit ins set to 1 to start the conversion
Thats what I tried to do.
the READ_ADC() function gives me a value to se if an obstacole is in front of the IR OR NOT....adn it sometimes works and sometomes doaesn't work

thanks a lot for your help and for the code:D

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,938
  • Helpful? 98
Re: [NEED HELP !!]adc read IR senzor
« Reply #3 on: May 21, 2010, 04:12:59 PM »
You don't need to clear the GO/DONE bit. You only set the bit and when the ADC conversion is complete the bit clears. That is why in my code I do:
Code: [Select]
AD_wait:
if ( GODONE == 1)
goto AD_wait;

 


Get Your Ad Here