Variable to ports


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Default Variable to ports

    I am sort of new using pic basic pro. I am learning it in school and i am working on a final project.

    I am trying to take a variable and send it to several different parts simultaneously, i do not know if this is possible but my example is


    would it work to:

    Code:
    'Initialize Variables
    
    
    LED1 var byte
    led1.0=porta.1
    led1.1=portb.3
    led1.2=portd.1

    And then later in my code just write LED1=1 to turn on all 3 ports and LED1=0 to turn them off. would this work

    thanks,

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    LED=%11111111 to turn all on
    LED=%00000000 to turn all off (0 would work also)

    Robert
    Last edited by Demon; - 4th April 2013 at 16:47.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    Hi,
    No, you can't really do it like that.
    You can't create an alias to individal pins "spanning" several ports like that - not that I know anyway.

    And, another point:
    When you say LED.1 = PortB.3 you assign the value of PortB.3 to LED.1 - that's definitely not what you want to do. To create aliases you use the VAR statement, just like you do when creating variables.
    Code:
    LED VAR PortB       ' LED is now an alias of PortB
    Pretty useless example but anyway....

    /Henrik.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    Just a thought. Haven't tried it:

    LED1 var byte
    CLEAR
    LET LED1 = (YOUR VARIABLE #)
    GOTO READPORT

    READPORT:
    IF LED1.BIT0 = 1 THEN HIGH PORTA.1
    IF LED1.BIT1 = 1 THEN HIGH PORTB.3
    IF LED1.BIT2 = 1 THEN HIGH PORTD.1

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    Actually, there was a way posted a while ago. I just can't remember what to use as search string to find it.

    Robert


    EDIT: Here, read this:

    http://www.picbasic.co.uk/forum/showthread.php?t=3753
    Last edited by Demon; - 4th April 2013 at 20:34.

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    Hi,
    Yes of course but then you are actually executing code which writes each bit to the individual port pins.
    Each output is written sequentially, they are not all written simultaneously which the OP asked about.

    But there's no way around that as far as I know.

    /Henrik.

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


    Did you find this post helpful? Yes | No

    Default Re: Variable to ports

    here is an "exemplar" code you may glean some use of, it is for a pic16f690 demo board
    Code:
    @ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BOR_OFF & _FCMEN_OFF & _IESO_OFF 
    DEFINE OSC 4 
    I VAR BYTE
    LED  var byte
    TEMP var WORD
    OVERFLOW VAR WORD
    Relays  VAR BYTE : Relays = 0
    
    'DEFINE ADC_BITS 10      ' Set A/D for 8-bit operation
    'DEFINE ADC_CLOCK 3      ' Set A/D clock Fosc/8
    'DEFINE ADC_SAMPLEUS 50  ' Set A/D sampling time @ 50 uS
    
    
    temp = 0
    OVERFLOW = 0 
    serout porta.0,2, [$FE,1,"Loops ",# temp," "]
    ADCON1 = 0
    ANSEL  = 0
    portc = %00000000
    TRISC = %00000000
    ;LOW portB.7
    
    ;MyPort var byte
    main:
    
    'ADCIN 0, TEMP
    
     myPort var bit[8]
     Portc.0 = MyPort[0]  
     Portc.1 = myPort[1]  
     Portc.2 = myPort[2]  
     Portc.3 = myPort[3]  
     Porta.1 = myPort[4]  
     Porta.2 = myPort[5]   
     Porta.3 = myPort[6]   
     PortB.7 = myPort[7]  
    'random temp 
     'myPort = %00000000
     TrisA = %00000000
     TrisB = %00000000
     Trisc = %00000000
    'FOR led = 0 to 4 + 1 
    
    
    pattern var byte
    index var byte
    'Pattern = %11111111
    
    'Pattern = %00001111
    FOR index = 0 to 19
    ;MYpORT = MYpORT + 1
    LOOKUP index,[%00000000,%00000001,%00000010,%00000100,%00001000,%00010000,%00100000,%01000000,%10000000,%00000101,%11111010,%00000101,%11111010,%00001001,%00000110,%11111111,%11111001,%11111111], pattern
        'pattern = (pattern >>1)
       portc = ~~Pattern   'turn portb.0 on or off based on whether the bit is 0 or 1
     ;Portc.0 = MyPort[0]  
     ;Portc.1 = myPort[1]  
     ;Portc.2 = myPort[2]  
     ;Portc.3 = myPort[3]  
     ;Porta.1 = myPort[4]  
     ;Porta.2 = myPort[5]   
     ;Porta.3 = myPort[6]   
     ;PortB.7 = myPort[7]  
        ;2 bitwise NOTS 1st not changes T/F to Pattern and second inverts value
        ;PORTC = Pattern is a T/F statement the ( ! ) changes it to ENGLISH :D
        PAUSE 300
    NEXT index
    
    temp = temp + 1
    
    
    serout porta.0,2, ["Loop ",# temp," "]
    IF TEMP >= 65535 THEN
    OVERFLOW = OVERFLOW + 1
    IF OVERFLOW >=1 THEN SEROUT PORTA.0,2,["OVERFLOW ",#OVERFLOW," "]
    PAUSE 5000
    ENDIF
    'TEMP = TEMP / 5
    'pause temp MAX 2
    'portc = portc << 1  
    
    'TRISC = I
    'next led
    
    
    'FOR led = 0 to 3 + 1
    'pause temp
    'portc = portc >> 1
       
    'next led
    
    goto main
    
    end
    The code listed above has lots of unimportant stuff in it as I use it to test snippits the important part is portc = ~~Pattern 'turn portb.0 on or off based on whether the bit is 0 or 1
    Last edited by Archangel; - 11th April 2013 at 21:41.
    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.

Similar Threads

  1. Adding Ports status to a variable
    By lilimike in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 20th March 2013, 22:05
  2. Need two SDA SCL ports
    By lerameur in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th November 2010, 16:43
  3. activating i/o ports according to variable value
    By Picman in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 29th July 2010, 13:55
  4. accessing ports pins using an index variable
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th March 2008, 20:36
  5. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 00:18

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