Searching \ for '[PIC] Merging pins into a "virtual" port/register' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/ios.htm?key=port
Search entire site for: 'Merging pins into a "virtual" port/register'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] Merging pins into a "virtual" port/register '
2006\05\28@044530 by moja nona

picon face
Hello.

Situation:    Driving a LCD. 3 pins are say on PORTA, 4 pins are on PORTB. I am using CCS for programing. Is there a way to group the pins of both the ports into a single register? The code would be easyer/prettyer.

Regards!

2006\05\28@103618 by Johan Baarman

picon face
Hi.
You might want to use this modified lcd driver found on ccs userforum. I
havent tested it myself, but it seems to be what you are looking for.

http://www.ccsinfo.com/forum/viewtopic.php?t=24661

/Johan

moja nona skrev:
> Hello.
>
> Situation:    Driving a LCD. 3 pins are say on PORTA, 4 pins are on PORTB. I am using CCS for programing. Is there a way to group the pins of both the ports into a single register? The code would be easyer/prettyer.
>
> Regards!
>  

2006\05\28@125725 by moja nona

picon face
Hello!

Thank you, I already considered modifying the 420LCD file, but I finaly
decided to try and write the routine myself. For practice. It has been 7
years since I last saw C and I think I need more practice. I find the struct
{} and union{} confusing - can't make it work?! I know that one of those is
what I am looking for.

Regards!




----- Original Message -----
From: <spam_OUTpiclist-requestTakeThisOuTspammit.edu>
To: <.....piclistKILLspamspam@spam@mit.edu>
Sent: Sunday, May 28, 2006 6:02 PM
Subject: Piclist Digest, Vol 22, Issue 33


{Quote hidden}

> {Original Message removed}

2006\05\31@001938 by Jesse Lackey

flavicon
face
Hi - unfortunately I don't think this is possible in CCS, and probably
not in other PIC C compilers, but that's just a guess.

What you're trying to do is something like this:

typedef union {
  byte Byte;
  struct {
word  SCME        :1;    /* Self-clock mode enable */
word  PCE         :1;    /* COP Enable during Pseudo Stop Bit */
word  PRE         :1;    /* RTI Enable during Pseudo Stop Bit */
word              :1;
word  ACQ         :1;    /* Acquisition */
word  AUTO        :1;    /* Automatic Bandwidth Control */
word  PLLON       :1;    /* Phase Lck Loop On */
word  CME         :1;    /* Crystal Monitor Enable */
  } Bits;
} PLLCTLSTR;

(this is from a header file for the freescale MC9S12NE64)

so that in code you can say:
PLLCTLSTR foobar;        (with some compiler-dependent pragma to set foobar to
the correct special chip register)

then:
foobar= 0xFF;
or
foobar.PRE= 1;
etc.

but for splitting across two 8-bit registers, the union{} would have to
cover both, and any assignment directly (like foobar= 0x0123) would set
all the pins on both ports.

Even if this is possible in other compilers, they are going to generate
a number of masks and shifts and so forth, so it won't be smaller/faster
than how you'd do it "by hand".

So I think you're going to have to write a function to do writes to your
LCD.  See bit_set() and bit_clear() CCS functions to efficiently
set/clear a single bit.  Alternatively something like:
PORTA= (PORTA & 0x7) | ((LCDdata & 0b0111000) >> 5);
would set the lowest 3 bits of PORTA to bits 6,5,4 of LCDdata.

Also see #inline, you may want your function inlined for speed.

This low-level bit-bangyness takes up a remarkable fraction of the code
for many of my projects.  It isn't always possible to arrange the
hardware pin layouts to make the software simpler, as I guess you've
discovered as well.  :)

J




moja nona wrote:

> Hello.
>
> Situation:    Driving a LCD. 3 pins are say on PORTA, 4 pins are on
> PORTB. I am using CCS for programing. Is there a way to group the
> pins of both the ports into a single register? The code would be
> easyer/prettyer.
>
> Regards!

2006\05\31@010718 by Xiaofan Chen

face picon face
On 5/31/06, Jesse Lackey <EraseMEjsl-mlspamEraseMEcelestialaudio.com> wrote:
{Quote hidden}

I do not know about CCS but it is quite common to use Union for
these kind of things with Hi-Tech PICC and MPLAB C18.

For example, the following is from Hi-Tech FAQ.
http://www.htsoft.com/support/faqs.php

**********************************************
How to map bits onto a RAM variable?

Q: I want to be able to access single bits in a byte, but if I try to define
  a bit variable using the absolute variable construct, e.g.

static bit bitvar @ ((unsigned)&bytevar)*8+0;

  I get a compiler error. How can I do this?

A: The short answer is "you can't do this". The absolute variable
construct using
@ requires an address known at compile time.

The long (and more useful) answer will depend on what you're actually trying
to do. You may find all you need is some simple macros like:

#define  testbit(var, bit)   ((var) & (1 <<(bit)))
#define  setbit(var, bit)    ((var) |= (1 << (bit)))
#define  clrbit(var, bit)    ((var) &= ~(1 << (bit)))

or you may like to define a union, e.g.

union both {
      unsigned char byte;
      struct {
          unsigned     bit0:1;  // etc.
      } bits;
}     var;

Now you can refer to var.byte or var.bits.bit0 for example.

Taking this a bit further, you can use unions to map bit "variables"
onto an existing byte, which is getting close to your original
question.

First up, define a structure type with bits in it:

typedef struct {
       unsigned        b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
}       bitv;


Now given a char variable, e.g.

char    myvar;

define a "variable" to be a single bit in that char like this:

#define mybit   (((bitv *)&myvar)->b0)

Now you can refer to mybit and it will access bit 0 of myvar. You can
use this just like you would use a bit variable defined any other way.
The code generated will be just as good as any other way. Example of
use:

        if(mybit)
              something();
        mybit = 1;


To streamline the process a little, you can define a helper macro like
this:

#define _paste(a,b)     a##b
#define bitof(var,num)  (((bitv *)&(var))->_paste(b,num))

Now defining a bit "variable" is done like this:

#define x4      bitof(myvar, 4)

So now x4 represents bit 4 of myvar.
**********************************************


'[PIC] Merging pins into a "virtual" port/register '
2006\06\02@074459 by Jesse Lackey
flavicon
face
Sure, all of the below is true, and is the long form of what I said. The
question was how to do this when the "char myvar" below is 8 bits across
two RAM locations, and you want to have syntax like "myvar= 57".  I'm
pretty sure it isn't possible in ANSI C anyway.
J


Xiaofan Chen wrote:

{Quote hidden}

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