Jump to content
Sign in to follow this  
don_erickson

How To Read Portb Pin?

Recommended Posts

When I was using C2C-plus compiler, I could read the value of an input on port b in an if statement such as

 

if (input_pin_port_b(CHG) == 0){ etc

 

but Boostc won't accept this.

I had been trying

 

if (portb, CHG == 0) { etc

 

but Pavel has pointed out that this doesn't do anything, even though the Boostc compiler doesn't object.

 

There must be a way to read the value of a pin of an input port?

The examples that I have found use the version that the C2C-plus compiler uses.

 

Thanks

Don Erickson

Share this post


Link to post
Share on other sites

Don,

 

One of the nicest ways that this can be done in BoostC is:

 

#include <system.h>

 

bit rb0 @ PORTB.0;

 

 

void main()

{

if( rb0 == 1 )

......

}

 

:D

 

Regards

Dave

Share this post


Link to post
Share on other sites
but Pavel has pointed out that this doesn't do anything, even though the Boostc compiler doesn't object.

 

That's because there are no errors in this expression from language point of view :D

 

 

There must be a way to read the value of a pin of an input port?

The examples that I have found use the version that the C2C-plus compiler uses.

 

 

I see at tleast two options. Yu can use bitwise operations. For example to check if bit 2 is set use something like:

 

if( portb & 4 ) ...

 

Or declare a bit variable mapped on a port pin:

 

 

volatile pinB1@0x6.1; //declare bit variable mapped to pin 1 of port B
if( pinB1 ) ...

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Don,

 

Unlike Pavel I forgot an import type specifier - volatile.

 

Without it the compiler will optmise the code sequence and give unexpected results.

 

This is mainly true when the specified bit is used as an output.

 

So best to always define all I/O output bits as volatile.

Internal bits can safely be defined as straight bit.

 

 

Regards

Dave

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...