View previous topic :: View next topic |
Author |
Message |
Switchblademaster Guest
|
Question on inputs |
Posted: Tue Jul 14, 2009 7:31 pm |
|
|
Ok so I have now started to explore the world of pic microcontrollers, pic18f4520 to be exact. I am currently working on a robotics project for school (which is due in a month btw) and having some trouble with inputs.
Currently I have 6 inputs: L1, L2, CL, CR, R2, R1.
And I want to compare them. Based on a their input states (high or low) the pic will execute different routines. This is what I have so far, for eg.
Code: |
#include <18F4520.h>
#define L1 PIN_A0
#define L2 PIN_A1
int value;
void main()
{
while(true)
{
value = input(PIN_A0); //if A0 is high
if (input(PIN_A0))
{ set_pwm1_duty(50);}
else if (!input(PIN_A0)); //if A0 is low
{set_pwm1_duty(255);}
}
} |
Question: Can I use L1 instead of PIN_A0 above????
What I want to do is to compare the states of PIN_A0 and PIN_A1. So that if PIN_A0 is high and PIN_A1 is low set_pwm1_duty(x).
Also I would like to know how do I store the state of PIN_A0 and PIN_A1.
So I could do this instead:
Let's say :
Code: | Left = input(PIN_A0)
Right = input(PIN_A1) |
So I can now compare Left and Right. Such as, if Left = 0 and right = 1 do something.
How do I store the state of an input and use this for comparison?
How do I test an input to determine weather it is high or low?
I know what !input does but what does it actually mean??
Any help would be appreciated. Strange enough I spent hours on the net looking for this but no luck.........hopefully you guys would help. |
|
![](templates/subSilver/images/spacer.gif) |
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Tue Jul 14, 2009 11:09 pm |
|
|
Quote: | Can I use L1 instead of PIN_A0 above???? | Yes, this is exactly what a #define statement does - it 'defines' another name for an identifier.
Quote: | Also I would like to know how do I store the state of PIN_A0 and PIN_A1. | Declare 'Left' and 'Right' as int1. This is a more efficient form of storage for single bits.
Quote: | How do I store the state of an input and use this for comparison? | You could use Code: | If (Left)
{
.....do something....
} | or, Code: | If (Right)
{
.....do something....
} | or maybe Code: | If (!(Left)&&(Right))
{
.....do something....
} |
Search for the phrase 'Operators' in either the CCS help, or on Google.
Quote: | How do I test an input to determine weather it is high or low? |
Code: | If (input(pin_a0))
{
.....do something.... //this executes if pin_a0 is high
}
else
{
.....do something..... //this executes if pin_a0 is low
} |
Quote: | I know what !input does but what does it actually mean?? | Look at the .lst file generated for your code. You'll get a better understanding of your code. This line first inputs the state of pin_a0, and then inverts that state.
Rohit |
|
![](templates/subSilver/images/spacer.gif) |
Switchblademaster Guest
|
|
Posted: Wed Jul 15, 2009 12:11 am |
|
|
Thanks alot Rohit......... I will use this knowledge in my simulation and see how it works.
Oh one more thing.............
I know (input(pin_a0)) is testing to see if input is high.
But how do you test if input is low??
For example
Code: | left = input(PIN_A0); |
Lets say that left is low(0).........and I want to do this:
Code: | if(left=0) // meaning if PIN_A0 is low.
{
do something
} |
Is that left=0 correct. By using left = input(PIN_A0) will it store 0 in left??
Thanks alot............. |
|
![](templates/subSilver/images/spacer.gif) |
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Wed Jul 15, 2009 1:42 am |
|
|
Quote: | I know (input(pin_a0)) is testing to see if input is high. | This statement doesn't only check if the pin is high, it stores the actual state of the pin. So the statement: Code: | left = input(pin_a0) | reads the state of A0. Assume 'left' is declared as an int1. If A0 is connected to Vdd, 'left' will be 1; conversely, if A0 is grounded, 'left' will be 0. I had mentioned this in my earlier reply:
Quote: | How do I test an input to determine weather it is high or low? |
Code: | If (input(pin_a0))
{
.....do something.... //this executes if pin_a0 is high
}
else
{
.....do something..... //this executes if pin_a0 is low
} |
Also, the statementis the wrong one to use in an 'If' condition. 'left=0' an assignment statement. The value 0 is assigned to left. The correct statement to use is. Note the double '='.
Rohit |
|
![](templates/subSilver/images/spacer.gif) |
mutthunaveen
Joined: 08 Apr 2009 Posts: 100 Location: Chennai, India
|
|
Posted: Wed Jul 15, 2009 2:34 am |
|
|
<< write the headers >>
Code: |
#define L1 pin_a1
#define L2 pin_a2
...........................so on // for L1 and C1 defining
int right, right, front, back;
void pinread(){
front=input(pin_b1);
back=input(pin_b2);
right=input(pin_b3);
............................
}
void main(){
set_tris_a(0x00); port A for out
set_tris_b(0xFF); port B for in
while(1){
pinread();
if (front==1){.........................}
// if u want u can use delays
if (back==1){..........................}
if ((front&&back)==1){.......................}
}
} |
I think I have coded your structure except your confidential stuff........ ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Wed Jul 15, 2009 6:39 am |
|
|
You can also use the negation operator (!) to invert a state, check for a low condition/test for inverse state.
examples:
left=!input(pin_a0)
<or>
if (!input(pin_a0)).... _________________ Google and Forum Search are some of your best tools!!!! |
|
![](templates/subSilver/images/spacer.gif) |
mkuang
Joined: 14 Dec 2007 Posts: 257
|
Re: Question on inputs |
Posted: Wed Jul 15, 2009 8:48 am |
|
|
Switchblademaster wrote: | Ok so I have now started to explore the Code: |
#include <18F4520.h>
#define L1 PIN_A0
#define L2 PIN_A1
int value;
void main()
{
while(true)
{
value = input(PIN_A0); //if A0 is high
if (input(PIN_A0))
{ set_pwm1_duty(50);}
else if (!input(PIN_A0)); //if A0 is low
{set_pwm1_duty(255);}
}
} |
|
No fuses,oscillator settings, or setting up timer2 ? I suggest you fix that or there is no chance your pwm is going to work. |
|
![](templates/subSilver/images/spacer.gif) |
bkamen
Joined: 07 Jan 2004 Posts: 1611 Location: Central Illinois, USA
|
|
Posted: Wed Jul 15, 2009 9:12 am |
|
|
If I may offer another alternative...
Your current path uses:
If (this)
do that;
You can also (I like doing it this way) use structs overlaid on a port like this:
Code: |
struct port_b_pins {
unsigned int dir:4; // the bits I might want to watch
unsigned int unused:4; // don't bother with last 4 bits
} portb;
#locate portb = getenv("SFR:PORTB")
|
make sure to use a set_tris_b(0xFF) and possibly port_b_pullups(true); to make sure B4:7 don't float.
now, you can do stuff like:
Code: |
switch (portb.dir) {
case 0x1: // Left (I'm making this up)
do stuff;
break;
case 0x2: // Right
do stuff;
break;
case 0x3: // Left & Right
do stuff;
break;
}
|
I just thought I'd add it to your list of possibilities.
-Ben
p.s. Like the previous author mentioned... ALWAYs make sure to set your fuses and a #use delay (clock = ) statement!! _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
![](templates/subSilver/images/spacer.gif) |
switchblademaster Guest
|
|
Posted: Wed Jul 15, 2009 9:53 am |
|
|
Thanks guys..........I am going to try this in my code and see how it works. Oh concerning the fuse setup I have all that already but decided to leave it out to just focus on the main problem. Checking and comparing to see an input is low. THANKS FOR ALL THE HELP!!!!!!!!! |
|
![](templates/subSilver/images/spacer.gif) |
|