Author Topic: reading sensors from a/d ports pic18f4550  (Read 4970 times)

0 Members and 1 Guest are viewing this topic.

Offline junior000Topic starter

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 0
reading sensors from a/d ports pic18f4550
« on: January 15, 2008, 02:10:05 AM »
hi,

     i am using pic18f4550.

     my development board has RA0..RA5 pins for sensors.if i want to read the values from RA0 will the following code be ok.

    TRISA=0b00000001;
   
    if(RA0<some reading) turn left;

    else if(RA0>some reading) turn right;

    else continue;

     
view my tutorials at


www.myfirstbot.blogspot.com

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: reading sensors from a/d ports pic18f4550
« Reply #1 on: January 15, 2008, 05:33:08 AM »
no, a/d doesnt work like that.

first you have to initiate the a/d module, then get this to sample from the port, and then read the bytes out of the a/d registers.

e.g.

Code: [Select]
#include <pic.h>
#include <adc.h>   // ADC library functions

unsigned int result //where the a/d value will end up


void main(){

TRISA = 0b11111111;

OpenADC(ADC_FOSC_2 & ADC_LEFT_JUST & ADC_1ANA_0REF,
ADC_CH0 & ADC_INT_OFF);//setup adc



SetChanADC(ADC_CH0); //set the a/d converter to sample channel 0
                                   //ADC_CH0 is port a0 / ADC_CH1 is port a1 etc....

ConvertADC(); // Start an A/D conversion
while(BusyADC()){ // Wait for the conversion to complete
//maybe some code for while were waiting (could turn an led on maybe)
}

//once the conversion is complete, the a/d reading will be in the registers
//ADRES and ADRESH - I usually copy these to an int

result = ADRES;
result += ADRESH;

// The a/d reading value is now stored in the variable result
//you can now do something like         if(result > 200){do something}


}
« Last Edit: January 15, 2008, 05:35:22 AM by paulstreats »

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: reading sensors from a/d ports pic18f4550
« Reply #2 on: January 15, 2008, 06:25:22 AM »
which programming language?
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: reading sensors from a/d ports pic18f4550
« Reply #3 on: January 15, 2008, 06:41:49 AM »
I presume the same language as junior000 used in his/her question the day before yesterday

microchip c18.

or thats what my code works on

Offline junior000Topic starter

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 0
Re: reading sensors from a/d ports pic18f4550
« Reply #4 on: January 15, 2008, 08:35:14 AM »
hey tnx....

one more question

can't we directly send or get digital signal(now i know that analog signal is not possible) using RB0,RB1..etc

like

  "RB0=1(or0);"  or do we have to use "PORTB=0b00000001;"



 
view my tutorials at


www.myfirstbot.blogspot.com

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: reading sensors from a/d ports pic18f4550
« Reply #5 on: January 15, 2008, 08:41:13 AM »
I have always used PICBASIC and Assembly , so my answer is based on that

you could give portb a value in binary as you did, or in hexadecimal though that requires a "$" sign before it , or in decimal but that requires a "%" before the number

the $ and %  may be different for C , so check it out
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: reading sensors from a/d ports pic18f4550
« Reply #6 on: January 15, 2008, 09:25:01 AM »
Yes you can use digital signal.

for output.

Makesure the trisregister is set for output.

TRISB = ob00000000;

then to make it easier define a name to a pin i.e.

#define LED PortBBits.RB0

LED = 0; //will make port b0 low

LED1 = 1;//will make port b1 high.

----------------------------------------------

it is the same for reading digital signals

TRISB = ob11111111;

#define LED PortBbits.RB0;

if (LED == 1){
//do this if the port is being driven high
}

or

while (LED == 1){
//while port b0 is high run this code(such as until a switch is turned off)

}



Obviously you can set the tris registers to have certain pins input and certain ones output, but i try to use different ports.

you can just use

PortBbits.rb0 = 0 ; 

to set the port low, but i prefer to define them into meaningful names, so if i had an led on that port call it LED in code.

If I have a servo on that port, call it SERVO in code.


You will actually see this in the servo code that i posted in your other thread

Offline junior000Topic starter

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 0
Re: reading sensors from a/d ports pic18f4550
« Reply #7 on: January 15, 2008, 09:47:00 AM »
thanks paul..you have just made my work lot easier now.

view my tutorials at


www.myfirstbot.blogspot.com

Offline junior000Topic starter

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 0
Re: reading sensors from a/d ports pic18f4550
« Reply #8 on: January 15, 2008, 10:01:12 AM »
hey can u explain the following code

  OpenADC(ADC_FOSC_2 & ADC_LEFT_JUST & ADC_1ANA_0REF,
         ADC_CH0 & ADC_INT_OFF);//setup adc

having a bit of problem understanding it.
view my tutorials at


www.myfirstbot.blogspot.com

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,653
  • Helpful? 21
  • narobo.com
Re: reading sensors from a/d ports pic18f4550
« Reply #9 on: January 15, 2008, 10:04:42 AM »
thats setting the registers of the PIC for the ADC
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline junior000Topic starter

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 0
Re: reading sensors from a/d ports pic18f4550
« Reply #10 on: January 15, 2008, 10:43:26 AM »
if i write source code for a different problem then writing the line
 

   OpenADC(ADC_FOSC_2 & ADC_LEFT_JUST & ADC_1ANA_0REF,
         ADC_CH0 & ADC_INT_OFF)

will set up registers for port A as analog ports ????
view my tutorials at


www.myfirstbot.blogspot.com

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: reading sensors from a/d ports pic18f4550
« Reply #11 on: January 15, 2008, 02:26:53 PM »
Yes.


Look at the header file called a2d.h which is installed in the mcc18 folder for other definitions, there are some parts of that line that you can change to set the pins up differently such as just 2 pins for a2d and 6 for digital etc... There is also settings for sample rate and analog reference voltage pins that can be changed.

Initially it will just sample from ra0 but in the example i showed you how to change it to sample from different pins.

In the example that I wrote This line here:

#define LED PortBBits.RB0

Should read

#define LED PortBbits.RB0

I accidentally put a capital B

 


Get Your Ad Here