CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question on inputs

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Switchblademaster
Guest







Question on inputs
PostPosted: Tue Jul 14, 2009 7:31 pm     Reply with quote

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.
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 14, 2009 11:09 pm     Reply with quote

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
Code:
!input(pin_a0)
first inputs the state of pin_a0, and then inverts that state.

Rohit
Switchblademaster
Guest







PostPosted: Wed Jul 15, 2009 12:11 am     Reply with quote

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.............
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 15, 2009 1:42 am     Reply with quote

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 statement
Code:
if(left=0)
is 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
Code:
if(left==0)
. Note the double '='.

Rohit
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 2:34 am     Reply with quote

<< 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
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 6:39 am     Reply with quote

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!!!!
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

Re: Question on inputs
PostPosted: Wed Jul 15, 2009 8:48 am     Reply with quote

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.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Jul 15, 2009 9:12 am     Reply with quote

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
switchblademaster
Guest







PostPosted: Wed Jul 15, 2009 9:53 am     Reply with quote

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!!!!!!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group