• 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

    towlerg-21522

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    towlerg Yesterday, 15:45 Go to last post
    towlerg-21522

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    towlerg Yesterday, 19:19 Go to last post
    kuhrig-602

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    kuhrig Today, 10:33 Go to last post
    palamont-28794

    MID$ Problem with a PIC18F25K22

    Thread Starter: palamont

    Hi everybody, I encounter a strange problem : I have a string of 230 characters. This string is divided into 10 segments of 23 characters, with a...

    palamont Today, 10:08 Go to last post