Pins, ports and variables


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Pins, ports and variables

    Let me know if it works . . .
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default Re: Pins, ports and variables

    Hi,
    It doesn't work like that - any of it I'm afraid.

    First Malcolms example,
    When you declare an alias to a variable or register, like dht_data1 var PORTA.0 the dht_data1 part of it is just a name for you and me to remember. For the compiler it gets converted to an adress in memory which in this case will be the exact same adress as PortA.0 has.

    This means that you can not do dht_data(y) because there is no variable called dht_data, the variable is called dht_data1.

    So, HIGH dht_data1 works because it's exactly equivalent to HIGH PortA.0 - no difference.

    But HIGH dht_data(y) doesn't work, (and doesn't compile) because there is no variable called dht_data. If you did dht_data var PortA.0 then doing HIGH dht_data(y) would set PortA.0+y....I think....which possibly could work in this particular case since the bits are consecutive (in a row) on PortA.


    When you declare an array, which is what you're doing with dht_data VAR bit[4] the compiler reserves memory, in RAM, for 4 consecutive bits.

    You can then, if you want, assign an alias (which is just a different name) TO these individual bits like Bit_0_of_dht_data_bit_array VAR dht_data[0]. This means that reading/writing Bit_0_of_dht_data_bit_array accesses the same memory location as reading/Writing dht_data[0]. The key here is that it's the very same bit in RAM that is accessed in both cases. You can not have an array of bits in ram and then try to alias any of those bit to another bit (like a port pin) because then it isn't pointing at the same location any more.

    In Archangels example strange things will happen because HIGH/LOW is designed to work on PORT registers only and relies on a fixed "offset" between the PORT register and the TRIS register (since HIGH/LOW clears the TRIS bit). Using HIGH/LOW on a variable in RAM will likely corrupt other parts of the memory since it will then (when trying to clear the TRIS bit) clear another bit - which you don't want.

    Malcolm, you're still on PBP2.6 aren't you? That's fine but if download the PBP3 manual there's an updated section (7.6) that explains how arrays and offsets work.

    /Henrik.

Similar Threads

  1. Replies: 2
    Last Post: - 12th November 2014, 08:57
  2. Multiple Variables - Pins
    By GatorGuy in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 1st February 2010, 02:08
  3. Variables and port pins
    By PeterReed in forum mel PIC BASIC
    Replies: 2
    Last Post: - 22nd September 2009, 15:01
  4. accessing ports pins using an index variable
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th March 2008, 21:36
  5. PIC - 8 Pins - 6 Output Pins ?
    By DanPBP in forum Off Topic
    Replies: 0
    Last Post: - 22nd October 2007, 01:23

Members who have read this thread : 0

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