Searching \ for '[PIC] Reading an input' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: massmind.org/techref/microchip/ios.htm?key=input
Search entire site for: 'Reading an input'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Reading an input'
2008\09\09@013131 by Grant Brown

flavicon
face
Hi List,

Target device 16F84 (soon to be replaced with a 16F88)

I know this will be the simplest thing but I can't seem to get it to work.

I am trying to set up PortA pin 1 as an input then read a push button

So I have Initialized the port with

  PORTA = 0;
  TRISA = 0x3;  // outputs except RA0 and RA1,which is set to input

  PORTB = 0;
  TRISB = 0;     // set as all outputs

And to read the port pin I have a function that I use,

char ReadPort_A_Bit(unsigned int BitReq)
{
return 0;
#asm
BANKSEL PORTA; // select bnak for port A
BTFSS PORTA BitReq; // read the bit value
return 1;
#endasm
}

But it does not seem to be working so could someone point me in the
right direction.

--
Kind Regards
Grant Brown

SiteDoc Pty Ltd
mob: 0412 926 995
http://www.sitedoc.com.au


2008\09\09@014201 by Forrest W Christian

flavicon
face
Grant Brown wrote:
> char ReadPort_A_Bit(unsigned int BitReq)
> {
> return 0;
> #asm
>  BANKSEL PORTA; // select bnak for port A
>  BTFSS PORTA BitReq; // read the bit value
>  return 1;
> #endasm
> }
Doesn't this chunk of C return 0 no matter what?  That is, the #asm is
never executed.

Just curious, which compiler did you end up using?   Try it with say,
MikroC...

-forrest

2008\09\09@014934 by Bob Blick

face
flavicon
face
Hi Grant,

The function will always return 0.

Also, no need to use asm.

You could write it like this:

unsigned char ReadPort_A_Bit(unsigned char BitReq){
 return (BitReq & PORTA);
}

and test the return for nonzero.

Cheerful regards,

Bob


Grant Brown wrote:
{Quote hidden}

2008\09\09@022506 by Grant Brown

flavicon
face
Hi Bob,

Thanks for that,

Just to add to my learning why does it always return 0 ?

I thought the idea with BTFSS was that it skipped the next instruction
only if the bit was not set ie 0

Does return not act as a temporary variable until the function exits ?

Kind regards
Grant Brown

Bob Blick wrote:
{Quote hidden}

--
Kind Regards
Grant Brown

SiteDoc Pty Ltd
mob: 0412 926 995
http://www.sitedoc.com.au
__________________________________________________________________________________________________________
This email message together with any attachments is intended only for the addressee(s) and contains information that may be confidential and/or copyright.  If you are not the intended recipient, please notify the sender by reply email (or telephone SiteDoc Pty Ltd on +61 2 42291185) and immediately delete this email together with any attachments from your computer. If you are the intended recipient, you must not copy, disclose, reproduce or distribute this communication together with any attachments without the authority of SiteDoc Proprietary Limited. No representation is made that this email or any attachments are free of viruses. Virus scanning is recommended and is the responsibility of the recipient. Unless specifically stated by the sender as the views of SiteDoc Proprietary Limited, any views expressed in this message are those of the individual sender, and no responsibility will be borne by SiteDoc Proprietary Limited for its content or outcomes.



2008\09\09@023637 by Forrest W Christian
flavicon
face
Grant Brown wrote:
> Just to add to my learning why does it always return 0 ?
>  
Here's your answer:
> Does return not act as a temporary variable until the function exits ?
>  
No, return immediately exits from the function and returns the value shown.

I take it you haven't written much code in C...   is there another
language you are more familiar with?

-forrest

2008\09\09@025436 by Grant Brown

flavicon
face
Hi Forrest,

Not much in C but 20 odd years in Delphi.

I think I was confusing "return" with "Result" in Delphi which does act
as a temporary  variable until the function exits.

so for the given function below is the way I have re-written it correct
- so in other words do I just

   * declare a local variable
   * assign it a value
   * then return the local variable

eg,
char ReadPort_A_Bit(unsigned int BitReq)

{
char temp = 0;
#asm
BANKSEL PORTA; // select bnak for port A
BTFSS PORTA BitReq; // read the bit value
Temp = 1;
#endasm
Return temp
}



Kind regards
Grant Brown

Forrest W Christian wrote:
{Quote hidden}

--
Kind Regards
Grant Brown

SiteDoc Pty Ltd
mob: 0412 926 995
http://www.sitedoc.com.au
__________________________________________________________________________________________________________
This email message together with any attachments is intended only for the addressee(s) and contains information that may be confidential and/or copyright.  If you are not the intended recipient, please notify the sender by reply email (or telephone SiteDoc Pty Ltd on +61 2 42291185) and immediately delete this email together with any attachments from your computer. If you are the intended recipient, you must not copy, disclose, reproduce or distribute this communication together with any attachments without the authority of SiteDoc Proprietary Limited. No representation is made that this email or any attachments are free of viruses. Virus scanning is recommended and is the responsibility of the recipient. Unless specifically stated by the sender as the views of SiteDoc Proprietary Limited, any views expressed in this message are those of the individual sender, and no responsibility will be borne by SiteDoc Proprietary Limited for its content or outcomes.

2008\09\09@025955 by Jan-Erik Soderholm

face picon face
Forrest W Christian wrote:
> Grant Brown wrote:
>> Just to add to my learning why does it always return 0 ?
>>  
> Here's your answer:
>> Does return not act as a temporary variable until the function exits ?
>>  
> No, return immediately exits from the function and returns the value shown.
>
> I take it you haven't written much code in C...

Why not simply start with a few simple ASM codes ?

2008\09\09@035037 by Forrest W Christian

flavicon
face
Grant Brown wrote:
> Not much in C but 20 odd years in Delphi.
>  
You may want to look at mikropascal then...
http://www.mikroe.com/en/compilers/mikropascal/pic/ .   I'm not trying
to discourage you from learning C... but learning C on a PIC should be
an interesting experience....

One thing you need to keep in mind is that a PIC is EXTREMELY resource
limited, and there are things you would normally do which will fail
miserably on a PIC, just because of lack of code and or data space -
even though the compiler is perfectly happy to let you try.
> I think I was confusing "return" with "Result" in Delphi which does act
> as a temporary  variable until the function exits.
>  
Yep...   That's why I caught the not a lot of C....   Anyone who's
written much C would have caught this mistake :)  ...  But again, I've
put a few semicolons in the wrong spot and couldn't figure out why it
was always executing a block (or not).
> so for the given function below is the way I have re-written it correct
> - so in other words do I just
>
>     * declare a local variable
>     * assign it a value
>     * then return the local variable
>  
For programming efficiency, I'd recommend using an early return instead
of a scratch variable.  There are two reasons:  1) the variable consumes
memory and 2) the return helps short circuit code which may not need to
need to be run.   As I don't generally mix assembler with C, I'm not the
best one to fix your particular code.   In fact, the compilers I use all
have functions and/or defs built in to do this, so you can just go
directly after the bit value without resorting to writing your own
function.  That is, in say MikroC you say something like:

  PORTA=0x00;
  TRISA=0x00;

  if (PORTA.F1)
  {
      //do stuff here
   }

Each C compiler has a little different way of doing this (like CCS C has
functions, etc), but the idea is the same.

Back on the return thing, what I am talking about would be something like:

char yourfunction(char arg)
{
  if (PORTA.F1)  return 1;
  return 0;
}


2008\09\09@041734 by Vitaliy

flavicon
face
Grant Brown wrote:
> Not much in C but 20 odd years in Delphi.

Surely, you must mean 'Pascal'. Delphi is only 13 years old:

http://en.wikipedia.org/wiki/CodeGear_Delphi#History

I'm also a hard-core Delphi fan -- alas, it may be fair to say, a "former
fan"...

Vitaliy

2008\09\09@070608 by Grant Brown

flavicon
face
Hi Vitaliy,

Actually - Turbo Pascal, then D4 then D5 and now BDS2006

Love Delphi, just detest Borland or Code Gear as they call themselves
these days.

Delphi rocks big time 8-)    but alas its is time to learn C. Have been
putting it of for years

Kind Regards
Grant Brown

Vitaliy wrote:
{Quote hidden}

--
Kind Regards
Grant Brown

SiteDoc Pty Ltd
mob: 0412 926 995
http://www.sitedoc.com.au
__________________________________________________________________________________________________________
This email message together with any attachments is intended only for the addressee(s) and contains information that may be confidential and/or copyright.  If you are not the intended recipient, please notify the sender by reply email (or telephone SiteDoc Pty Ltd on +61 2 42291185) and immediately delete this email together with any attachments from your computer. If you are the intended recipient, you must not copy, disclose, reproduce or distribute this communication together with any attachments without the authority of SiteDoc Proprietary Limited. No representation is made that this email or any attachments are free of viruses. Virus scanning is recommended and is the responsibility of the recipient. Unless specifically stated by the sender as the views of SiteDoc Proprietary Limited, any views expressed in this message are those of the individual sender, and no responsibility will be borne by SiteDoc Proprietary Limited for its content or outcomes.

2008\09\09@071615 by Grant Brown

flavicon
face
Hi Forrest,

That was one of the best explanations I think if have read in a long
time, wish all post where as good as that - including mine.

char yourfunction(char arg)
{
  if (PORTA.F1)  return 1;
  return 0;
}

That was what I was trying to achieve but I had the "Return 0" in the
wrong place. Now that I understand that return is really an exit command
it makes sense.

Learning C on a PIC may be somewhat hard but I intend to conquer it and
I will - one just has to be very stubborn and not let it bet ya.

The PIC that my actual project will be completed with is a PIC32 which
seems to be a little bit more user friendly.  But I need to get the
basics down pat first.

Many thanks for your assistance, very much appreciated.

Kind regards
Grant Brown

Forrest W Christian wrote:
{Quote hidden}

--
Kind Regards
Grant Brown

SiteDoc Pty Ltd
mob: 0412 926 995
http://www.sitedoc.com.au
__________________________________________________________________________________________________________
This email message together with any attachments is intended only for the addressee(s) and contains information that may be confidential and/or copyright.  If you are not the intended recipient, please notify the sender by reply email (or telephone SiteDoc Pty Ltd on +61 2 42291185) and immediately delete this email together with any attachments from your computer. If you are the intended recipient, you must not copy, disclose, reproduce or distribute this communication together with any attachments without the authority of SiteDoc Proprietary Limited. No representation is made that this email or any attachments are free of viruses. Virus scanning is recommended and is the responsibility of the recipient. Unless specifically stated by the sender as the views of SiteDoc Proprietary Limited, any views expressed in this message are those of the individual sender, and no responsibility will be borne by SiteDoc Proprietary Limited for its content or outcomes.

2008\09\09@071926 by Michael Rigby-Jones

picon face


> -----Original Message-----
> From: spam_OUTpiclist-bouncesTakeThisOuTspammit.edu [.....piclist-bouncesKILLspamspam@spam@mit.edu] On
Behalf
{Quote hidden}

I'm wondering if you are used to Visual Basic as you can use the
function name to hold a temporary value like this until you exit the
function.

Return in C is equivalent to either return (if no value to return to
caller) or retlw (to return a value to the caller) in PIC assembler.

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

2008\09\09@081317 by olin piclist

face picon face
Grant Brown wrote:
> char ReadPort_A_Bit(unsigned int BitReq)
> {
> return 0;
> #asm
>  BANKSEL PORTA; // select bnak for port A
>  BTFSS PORTA BitReq; // read the bit value
>  return 1;
> #endasm
> }
>
> But it does not seem to be working so could someone point me in the
> right direction.

This is exactly the kind of trouble you will continue to get yourself into
until you really understand the PIC.  You are having a problem because you
are using C before you are ready to do so.

As was said before by several people: Learn the PIC with assembler first.

No, I'm not going to provide new advice since you are ignoring previous
advice.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2008\09\09@085843 by Harold Hallikainen

face picon face

> Grant Brown wrote:
>> char ReadPort_A_Bit(unsigned int BitReq)
>> {
>> return 0;
>> #asm
>>  BANKSEL PORTA; // select bnak for port A
>>  BTFSS PORTA BitReq; // read the bit value
>>  return 1;
>> #endasm
>> }

The second argument to BTFSS is a constant (it's encoded as part of the op
code), so you can't use an argument into the C function. If you're using
Microchip C, I'd probably not do a function call at all, but just do
something like this in my calling code:

if (PORTAbits.RA2){
...
}

Harold


--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

2008\09\09@090119 by cdb

flavicon
face
There is a book by Nigel Gardener of Bluebird Electronics (possibly
available still from Farnell) entitled 'An introduction to programming
the Microchip Pic in C " (which ought to get a prize purely for the
length of the title), whcih you might find useful as it has example
assembler produced by (I think the CCS compiler) along with the 'C'
code that provided it.

An alternative guide (FREE) to 'C' and Piccing can be downloaded from
http://www.fored.co.uk, thoughI have to say, it isn't as clear as it could
be.  There is also example code that can be purchased (again for CCS I
think) from http://www.phanderson.com  - this will be examples from his
Morgon State University engineering courses.

Oh yes the Nigel Gardener book - ISBN 1-899013-04-0

Colin
--
cdb, colinspamKILLspambtech-online.co.uk on 9/09/2008

Web presence: http://www.btech-online.co.uk  

Hosted by:  http://www.1and1.co.uk/?k_id=7988359






2008\09\09@091045 by Xiaofan Chen

face picon face
On Tue, Sep 9, 2008 at 7:15 PM, Grant Brown <.....grantKILLspamspam.....sitedoc.com.au> wrote:
> Learning C on a PIC may be somewhat hard but I intend to conquer it and
> I will - one just has to be very stubborn and not let it bet ya.
>
> The PIC that my actual project will be completed with is a PIC32 which
> seems to be a little bit more user friendly.  But I need to get the
> basics down pat first.

If you are going to use PIC32, maybe you can forget about the mid-range
PIC. They are so different.

Since you are an IT professional and proficient in Delphi, learning C
should not be a problem for you. However, you may want to learn
some basics of Electronics and basic Microcontroller. And you can
start right away from PIC32 if you get the demo board. If you want
to use DIP package, maybe you can start with PIC24 even though it
is also quite different from PIC32. At least the  C compiler used for PIC24
and PIC32 have much in common (both are based on GCC).

Anyway, to start with any PICs, you need to understand the
datasheets. To understand the datasheet, you need to know
a bit of assembly. And you need the hardware as well.

Xiaofan

2008\09\09@091940 by John Ferrell

face picon face
If the objective is to learn C that is fine.
An old Pascal fan like me is better off with miKroPascal where
"If PortA.0 then Drive_Forward;"
works well. You might be surprised how much you can do with the demo copy.

John Ferrell    W8CCW

"All that is necessary for the triumph of evil is for good men to do
nothing." -- Edmund Burke
http://DixieNC.US

{Original Message removed}

2008\09\09@182344 by Stephen D. Barnes

flavicon
face
Olin Lathrop wrote:
> Grant Brown wrote:
>  
>> <snip>
>>
>> But it does not seem to be working so could someone point me in the
>> right direction.
>>    
>
> This is exactly the kind of trouble you will continue to get yourself into
> until you really understand the PIC.  You are having a problem because you
> are using C before you are ready to do so.
>
> <snip>
>  
I second Olin's advice as I am learning C on the PIC's as we speak. I
learned to use assembly on each PIC that I have used in a project. That
includes understanding how peripherals function as well as how to
configure them. This approach has really helped get over some hurdles in
C because I can look at the assembly language result of compiling C
source and understand what is happening when my C code fails.
Last week at the library I found what I feel is a good book on C for the
absolute beginner (me!).
"C In Plain English" by Brian Overland ISBN 1-55828-430-3
This book has explained many things I could not wrap my head around.
Just a suggestion that may help

--
Regards,
Stephen D. Barnes

2008\09\10@102219 by William Chops Westfield

face picon face

On Sep 9, 2008, at 6:10 AM, Xiaofan Chen wrote:

>> Learning C on a PIC may be somewhat hard but I intend to conquer it  
>> and
>> I will - one just has to be very stubborn and not let it bet ya.
>
> to start with any PICs, you need to understand the
> datasheets. To understand the datasheet, you need to know
> a bit of assembly. And you need the hardware as well.

Hmm.  On the CPUs where it's not intended for you to ever program in  
anything but C, the datasheets talk C, and the manufacturer probably  
supplies a STANDARD C library for manipulating the hardware.  And the  
datasheets pretty much SUCK for describing how the HW works at the bit  
level.

I first ran into this on the Luminary Micro Cortex M3 CPUs; they  
provide a very complete C library for doing things, and thats  
apparently how they expected you to do things, because the datasheet  
sure didn't describe anything well enough to write your own assembly  
from scratch.  I found it very frustrating, actually...

So the problem with programming a PIC in C is that there isn't any  
STANDARD C library for manipulating the hardware (and it doesn't help  
that there's SO many different HW variants.)  Each compiler has some  
libraries, but they're at different levels of functionality...

BillW

2008\09\10@191723 by Xiaofan Chen

face picon face
On Wed, Sep 10, 2008 at 10:21 PM, William Chops Westfield
<EraseMEwestfwspam_OUTspamTakeThisOuTmac.com> wrote:

> I first ran into this on the Luminary Micro Cortex M3 CPUs; they
> provide a very complete C library for doing things, and thats
> apparently how they expected you to do things, because the datasheet
> sure didn't describe anything well enough to write your own assembly
> from scratch.  I found it very frustrating, actually...

Hmm, I believe for ARM based MCUs, they expect you to program
in C. However it is still possible to program in ASM. They expect
you to read the Core manuals from ARM then, so the datasheet does
not touch too much on the core.

> So the problem with programming a PIC in C is that there isn't any
> STANDARD C library for manipulating the hardware (and it doesn't help
> that there's SO many different HW variants.)  Each compiler has some
> libraries, but they're at different levels of functionality...
>

It is said Microchip provides "demo" quality peripheral library for MPLAB
C18. Once upon a time they even intend to discontinue the library by
saying that "From the release of MPLAB C18 v2.42 onward, no support has
been added to the peripheral libraries". Apparently a lot of people's
complains make them to continue it but the quality is still questionable.

Xiaofan

2008\09\10@191748 by Xiaofan Chen

face picon face
On Thu, Sep 11, 2008 at 7:17 AM, Xiaofan Chen <xiaofancspamspam_OUTgmail.com> wrote:
>> So the problem with programming a PIC in C is that there isn't any
>> STANDARD C library for manipulating the hardware (and it doesn't help
>> that there's SO many different HW variants.)  Each compiler has some
>> libraries, but they're at different levels of functionality...
>>
>
> It is said Microchip provides "demo" quality peripheral library for MPLAB
> C18. Once upon a time they even intend to discontinue the library by
> saying that "From the release of MPLAB C18 v2.42 onward, no support has
> been added to the peripheral libraries". Apparently a lot of people's
> complains make them to continue it but the quality is still questionable.
>

The reference is here: http://forum.microchip.com/tm.aspx?m=282340

Xiaofan

2008\09\11@064818 by Dario Greggio

face picon face
Xiaofan Chen wrote:
Apparently a lot of people's
> complains make them to continue it but the quality is still questionable.

who, me?? :-))

--
Ciao, Dario -- ADPM Synthesis sas -- http://www.adpm.tk

More... (looser matching)
- Last day of these posts
- In 2008 , 2009 only
- Today
- New search...