• PicŪ Basic


  • Re: Remapping port pins and 16 bit port

    Combining 2 8-bit variables into a 16-bit variable is possible, and because ports are, essentially, SFR variables, the same principle applies. Below is a simple demo showing how to combine PORTB and PORTC into a 16-bit pseudo port. Remember, Input, Output, Low, and High only work on 8-bit ports, so I have created simple defines to operate them.

    To Set or Clear bits of the 16-bit port, you can use the Set, Clear, SetBit, or Clearbit commands, and GetBit etc to read them. Or if it is a constant bit value, use Port16.Bit

    Hopefully, you will get the idea of it from the example below:

    Also, remember, you can do the same with TRISB and TRISC, and maybe call the defines Tris16.

    The beauty of using the defines is that they do not make code overheads, so accessing Port16 is the same as accessing PORTB or PORTC.

    Code:
        Device = 18F25K20
        Declare Xtal= 64 
        
        Dim bIndex As Byte  ' Create a variable for the demo  
    '
    ' Create a 16-bit port from PORTB and PORTC
    '    
        Dim U_Port16 As PORTB           ' Create the underlying low port
        Dim U_Port16H As PORTC          ' Create the underlying high port       
        Dim Port16 As U_Port16.Word     ' Combine them into a single 16-bit variable
    '
    ' Create some defines to seperate each 8-bit port
    '
    $define Low_Port16 PORTB
    $define High_Port16 PORTC
    
    '-----------------------------------------------------
    ' Set the 16-bit port to input
    '
    $define Input16()    '
        Input Low_Port16 '
        Input High_Port16
        
    '-----------------------------------------------------
    ' Set the 16-bit port to output
    '
    $define Output16()    '
        Output Low_Port16 '
        Output High_Port16
          
    '-----------------------------------------------------
    ' Set the 16-bit port to output low
    '
    $define Low16()    '
        Low Low_Port16 '
        Low High_Port16
           
    '-----------------------------------------------------
    ' Set the 16-bit port to output high
    '
    $define High16()    '
        High Low_Port16 '
        High High_Port16 
        
    '-----------------------------------------------------    
    ' A simple demo of the above
    '
    Main:
        Low16()                     ' Make the 16-bit port output low
    '
    ' Set the pins high, sequentially
    '    
        For bIndex = 0 To  15 
            SetBit Port16, bIndex  ' Make each pin high  
            DelayMS  100 
        Next   
    '
    ' Set the pins low, sequentially
    '    
        For bIndex =  0  To  15 
            ClearBit Port16, bIndex  ' Make each pin low  
            DelayMS  100 
        Next 
    '
    ' Set the pins using a constant bit value
    '
          Set Port16.0
        Set Port16.1
        Set Port16.2
        Set Port16.3
        Set Port16.4
    '
    ' Alter pins of the port
    '
        Port16 = % 0000111100001111
    This article was originally published in forum thread: Remapping port pins and 16 bit port started by RGV250 View original post
  • Recent Activity

    Tim-107

    PCB Stencils

    Thread Starter: SimonJA

    I've made a lot of PCB's but never used stencils so thought I would give them a try. I usually use a syringe to dispense the solder paste but it's...

    Tim Yesterday, 19:51 Go to last post
    John Drew-26

    Hx711 calibration

    Thread Starter: amod

    Iam using load cell amplifier HX711 module.I have got raw values from module but iam not able to calibrate with load cell.I want make a universal...

    John Drew Yesterday, 23:40 Go to last post
    geoff_c-79

    Modification of program code by data in external memory

    Thread Starter: geoff_c

    For the past several years farmers have been using a product I designed for a customer to measure moisture in grain. Within the code there are...

    geoff_c Today, 15:51 Go to last post