Equating Ports to Data Bits


Closed Thread
Results 1 to 7 of 7
  1. #1
    Osiris's Avatar
    Osiris Guest

    Question Equating Ports to Data Bits

    Hi There,

    I'm to this forum so I'd first like to say hi to all you people out there. My question seems rather trivial but I can't seem to figure it out. Basically I have a 16 bit bus which is scattered through PortA, B, and C on an 16F877A. I would like to define the 16 bit data and have each bit of the data automatically associated with the PortX.Y like so

    BUS_DATA var word
    BUS_DATA.0 = PortA.0
    ..etc

    The problem is that I can't get it to work and had to do the following routine:

    If BUS_DATA=1 then
    PortA.0 = 1
    Else
    PortA.0 = 0
    Endif
    ...etc

    This is rather lengthy and eats lots of space. I've done it before were I define a byte like MY_INFO var PortE.0 but that's an entity on it's own. I can't seem to associate data bits with port bits.

    Can anybody help me out?

    Many thanks,
    Osiris

  2. #2
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Cool Two ways as far as I know

    Hi there,

    If you have a variable say "PORTDATA" then it could be like this
    Code:
    PORTA.1 = PORTDATA.0
    
    "" "" ""
    
    PORTB.1 = PORTDATA.15
    and so on. Or you can create bit alliases like
    Code:
    PORTA.1 VAR PORTDATA.7
    and so on.
    Regards

    Sougata

  3. #3
    Osiris's Avatar
    Osiris Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Sougata,

    Thanks for the tips. I tried them today and they do not seem to work for me. I know they should because it seems logical that they should work but somehow it's not working.

    The first idea: PortA.1 = Data.0 compiled without error, the second one gave me lots of errors "redefinition of VAR".

    It's strange but there must be a simple way to do this.

    Many thanks,
    Osiris

  4. #4
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Question Can you post your entire code.

    Hi,

    I would like to see the entire code and find out whats going on. Sometimes "typos" are hard to find.
    Regards

    Sougata

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Re the second example...

    A Variable is a Variable, and a Port is a Port. You CAN'T define one as another which is why the second example is illegal. You CAN make a Port equal the state of a Variable, or you can make a Variable equal the state of a Port.

    Going back to your problem... if you have arranged your 16-bit BUS illogically and without thought scattering it randomly across many Port Pins, then you can only gather it together with (worst case) sixteen individual statements like so...

    Myword.0=PortA.3
    Myword.1=PortB.7
    Myword.2=PortB.3
    Myword.3=PortB.5
    Myword.4=PortA.0
    Myword.5=PortC.4

    and so on...

    The best scenario was you had your 16-bit bus arriving sensibly at two PIC Ports and you caould have gathered it like so...

    Myword.Lowbyte=PortB
    Myword.Highbyte=PortC

    You can of course redefine the name 'Myword', and you can redefine the Port names, but you CANNOT automatically associate a physical lump of Hardware (ie a Port pin) to a virtual object (ie a RAM variable).

  6. #6
    Osiris's Avatar
    Osiris Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie,

    Thanks for the explanation. Indeed I had no choice but to scatter my bus around because some of my port bits are for dedicated hardware and since the pins have special features (like hardware interrupt, HUART, HPWM, etc) I had no choice but to scatter them about.

    My worry is that I did try the following:

    Myword Var word

    Myword.0=PortA.3
    Myword.1=PortB.7
    ...
    Myword.15=PortC.4


    But when I write the following statement it does not transfer the configuration to the port:

    newdata var word

    Newdata = 1234
    Myword = Newdata

    I would assume it would transfer the bit pattering from "newdata" to "myword" which would then output the bits on the associated ports.

    It seems too simple of a problem and at the back of my head I have a feeling that I'm missing something very fundamental which does not allow the program to work.

    Many thanks,
    Osiris

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    In your first example...

    Myword Var word

    Myword.0=PortA.3
    Myword.1=PortB.7
    ...
    Myword.15=PortC.4

    ...you transfer the contents of the Port Pins into your variable.

    Newdata = 1234
    Myword = Newdata

    Now the above two lines stuffs Myword with the contents of Newdata ie 1234.

    If now you want that to appear on the Port Pins, you have to do the reverse of what you originally did... ie...

    PortA.3=Myword.0
    PortB.7=Myword.1
    ...
    PortC.4=Myword.15

    Remember Myword is a RAM variable, not a Port alias.

Similar Threads

  1. Using Nokia LCD
    By BobP in forum mel PIC BASIC Pro
    Replies: 300
    Last Post: - 3rd May 2018, 04:47
  2. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  3. LCD + bar graph
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 5th October 2005, 14:50
  4. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31
  5. Sinus calculating !
    By Don Mario in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 28th November 2004, 23:56

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts