Society of Robots - Robot Forum
Software => Software => Topic started by: junior000 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;
-
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.
#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}
}
-
which programming language?
-
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
-
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;"
-
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
-
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
-
thanks paul..you have just made my work lot easier now.
-
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.
-
thats setting the registers of the PIC for the ADC
-
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 ????
-
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