View Full Version : digital input question...
Kevin Karan
03-23-2004, 07:50 PM
Alright, feeling stupid. I know that the digial inputs give wierd junk since they are char variables. So what bit do I read and how. Thanks
Ryan M.
03-23-2004, 08:21 PM
? ;)
If you just want to tell if it is HIGH or LOW, you can do a check if it is 1 or 0. If for some reason you have to actually access the bits, experiment. I'm not sure if the chip is big- or little-endie. :)
Kevin Karan
03-23-2004, 08:25 PM
The digital inputs are given as a char, but only one of the bits are actualy accurate, but I dont know what one. I know there is a post on this forum about it, but I cannot find it. AND, i cant remember how to get the value of a specific bit under c, is it var:1 or is it var&1
KenWittlief
03-23-2004, 09:27 PM
the digital inputs on the RC are already defined in the code for you - they can only have a value of 1 or 0
all you need to do is look at the right variable
rc_dig_in01 is digital input one on the RC
so you can do stuff like
if (rc_dig_in01 ==1) variableXYZ=123;
if (rc_dig_in14 ==0) variableQRS=127;
what else do you want to do with them?
Ryan M.
03-24-2004, 05:57 AM
the digital inputs on the RC are already defined in the code for you - they can only have a value of 1 or 0
all you need to do is look at the right variable
rc_dig_in01 is digital input one on the RC
so you can do stuff like
if (rc_dig_in01 ==1) variableXYZ=123;
if (rc_dig_in14 ==0) variableQRS=127;
what else do you want to do with them?Looking back at my old post, I meant that, but I see how you didn't get it. You can't access a certain bit in a variable in an actual function. The first syntax you stecified can be used in a struct like this:
stuct bitVariable
{
int bit0:1;
int bit1:1;
int bit2:1;
int bit3:1;
// ...
};
Basically, tht gives each of the variables inside it one bit of the int. (at least, I think I got that right... :)) Like Ken said though, you can just read the variable directly.
10intheCrunch
03-24-2004, 10:38 AM
I'm pretty sure you can, if they are set up in a struct together. Say:
if(SomeStructbits.bit0) blah;
If you only want to see the first bit of any variable, though, use modulus.
if(someVar % 2) morestufftodo;
If you aren't familiar with modulus, it gives the remainder of dividing by the number you mod by, ie modding by 2 gives you one or zero depending on if the number is even or odd (or rather, if the first bit is 1 or 0).
Good luck.
Astronouth7303
03-24-2004, 10:50 AM
If you want the 2 least significant bits, wouldn't it make more sense to do:
if(someVar & 3) evenmorestufftodo;?
Joe Ross
04-08-2004, 02:35 PM
If you want the 2 least significant bits, wouldn't it make more sense to do:
if(someVar & 3) evenmorestufftodo;?
That is what I would do, becasue the modulus operator is often slow (of course depending on the microprocessor). I haven't checked on the PIC, but if you ever want to move your code to a different platform, it's best to write for the lowest common denominator.
Sorry for bringing this thread up from the dead.
Kevin Karan
04-11-2004, 10:42 PM
sorry for not being at all active on my threads lately... at buckeye I came up with something that worked well, though I cant remember it...
Astronouth7303
04-12-2004, 07:34 AM
I HATE that!
vBulletin® v3.6.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.