go away spammer

Author Topic: ADC PIC16F684  (Read 4481 times)

0 Members and 1 Guest are viewing this topic.

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
    • Salmigondis Tech
ADC PIC16F684
« on: January 29, 2009, 12:51:53 PM »
Hello!

I'm back again! :P This time, though, my problem lies with ADC. I know how to initiate it, and how to set it up in a program, but I have no clue how to use it in an IF statement. I'm using PIC C Lite along with MPLAB IDE.

Here's the code:


Code: [Select]
#include <pic.h>


__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
  & UNPROTECT & BORDIS & IESODIS & FCMDIS);


main()
{

    PORTA = 0;
    CMCON0 = 7;                 //  Turn off Comparators
    ANSEL = 0b0000011;                  //  Defining Analog inputs (RA0 and RA1)
    ADCON0 = 0b00000001;        //  Turn on the ADC
    ADCON1 = 0b00010000;        //  Select the Clock as Fosc/8 (4MHz)
TRISA = 0b001011;

         while(1)
             {
              if statement goes here...

Could someone explain to me what I am to write in the IF statement?

Thanks!
Canabots
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

paulstreats

  • Guest
Re: ADC PIC16F684
« Reply #1 on: January 29, 2009, 01:00:29 PM »
An IF statement is used to compare 2 values. Depending on that outcome, it will either run the code in the statement or not running it.

So... Without knowing what values you want to compare and without knowing what you want to happen as a result of that comparison we cant really tell you what to put in it.


(are you just trying to get the results from the analog/digital converter? if so then you dont need an if statement)

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
    • Salmigondis Tech
Re: ADC PIC16F684
« Reply #2 on: January 29, 2009, 01:09:59 PM »
This robot is going to be a photovore. And the microcontroller is 8-bit.

So I suppose that I'm comparing the value of each input (which is variable, since they're photoresistors) to each other. After that, I would want  to make the microcontroller output to a motor controller.

Is this at all helpful?  :-[

Canabots
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

paulstreats

  • Guest
Re: ADC PIC16F684
« Reply #3 on: January 29, 2009, 01:41:30 PM »
I can give you a general idea.

Use the code that you posted above. You will also need to declare some variables there.

So you will need to declare :

threshhold //you will want to preset a value for this. The value depends on how sensitive to change the robot is.
leftreading
rightreading

then before the if statement you need to read the left sensor and store the value in the leftreading variable and the same with the right.

then the if statements.

if(leftreading+threshhold >= rightreading){
//place code here for when the left sensor has more light than the right sensor
//tell your motor controller to turn left
}

if(rightreading+threshold >= leftreading){
//place code here for when the right sensor has more light than the left sensor
//tell your motor controller to turn right
}

if(rightreading-leftreading <= threshhold) OR (leftreading-rightreading <=threshhold){
//place code here for when the 2 sensors are registering the same amounmt of light (within the threshold)
//tell your motor controller to go straight
}

I hope this helps a bit.. Without knowing specifics about the motor controller or where its connected That part has to be left to you... but at least you can see the general structure

Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
    • Salmigondis Tech
Re: ADC PIC16F684
« Reply #4 on: January 29, 2009, 02:26:04 PM »
I can give you a general idea.

Use the code that you posted above. You will also need to declare some variables there.

So you will need to declare :

threshhold //you will want to preset a value for this. The value depends on how sensitive to change the robot is.
leftreading
rightreading

then before the if statement you need to read the left sensor and store the value in the leftreading variable and the same with the right.

then the if statements.



A few questions about this:
1. Is it the larger the threshhold value is, the more sensitive?
2. How does the program know which variable is for which sensor?
3. Could you show me an example of the general code for reading the sensor before the IF statement?

Thanks alot!



« Last Edit: January 29, 2009, 03:12:27 PM by Canabots »
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

paulstreats

  • Guest
Re: ADC PIC16F684
« Reply #5 on: January 29, 2009, 06:44:32 PM »
Okay so first off we need to set up the variables like this..

Code: [Select]
//set up the program as you did above.

unsigned int leftReading;
unsigned int rightReading;
unsigned int threshhold = 150; //start with this value and adjust as necessarry

while (1){      //then begin the infinite while loop here

then we need to tell the variables what the readings are. The mcu only has 1 a/d converter (not 1 for each a/d pin) so before we get it to read we need to tell it what pin to read...

Code: [Select]
//assume the left sensor is plugged into ad0

ADCON0 = 0b00000001;
//                 x_______ 0 = left justified / 1 = right justified             
//                 _x______ 0 = voltage reference from vdd / 1 voltage reference from aref pin
//                 __x_____ not configured (always set to 0)
//                 ___xxx__ Adress of pins to read (000 = an0 / 001 = an1)
//                 ______x_ GO/Done Bit
//                 _______x adc enable bit 1 = on / 0 = off (consumes no power in off state)
//the commenting above is useful to use. Once its put into code you dont have to keep referencing the datasheet.
// the above part selects the AD0 pin and makes sure the a/d module is turned on.
//the ADCON1 register just sets the speed of aquisition... set it up like you did originally and change it.
//The aquisition speed can affect the reading.

//Now we need to tell the a/d module to take a reading.

      ADIE = 0;   //prevent a global interrupt on aquisition
      ADIF = 0;   //reset the a/d interrupt flag
      ADRESL = 0; //reset the a/d low bits register
      ADRESH = 0; //reset the a/d high bits register
      ADGO = 1; //tell the a/d to take a reading

// The reading doesnt happen instantly, it takes a little bit of time so we need to put a small while loop in...

while(!ADIF){           //while the interrupt flag is cleared
// do nothing
}
// yay, the interrupt flag has been set to 1, the reading is complete!!!
//now we need to put the reading into our variable. The reading is stored in 2 seperate registers.
//its a 10 bit module and the registers are only 8 bits wide so....

leftReading = ADRESL;    //ADRESL is where the lower 8 bits are
leftReading += (ADRESH << 8); //ADRESH is where the higher 2 bits are so we needed to shift them 8 bits left

//so now we have the left reading value. The right reading below is the same without commenting.

ADCON0 = 0b00000101;      //notice the value here has changed so we are now setting ad0
ADIE = 0; 
ADIF = 0;
ADRESL = 0;
ADRESH = 0;
ADGO = 1;
while(!ADIF){ 
}

rightReading = ADRESL;
rightReading += (ADRESH << 8);

//Now you can go ahead with the if statements.....




Offline CanabotsTopic starter

  • Contest Winner
  • Robot Overlord
  • ****
  • Posts: 245
  • Helpful? 6
  • It's not a bug, it's a feature!
    • Salmigondis Tech
Re: ADC PIC16F684
« Reply #6 on: January 30, 2009, 07:12:42 AM »
YAY!!!! it worked!!!!! :D

I only had to change ADGO to GODONE in order for it to start ADC.

Thank you so much paulstreats! I don't think I've ever learned so much about programming in such a short time  ;D

Thanks again!
My robotics, electronics, software, or other stuff blog:
www.saltech.wordpress.com

 


Get Your Ad Here

data_list