News:

Let's find out together what makes a PIC Tick!

Main Menu

PIC18F26K22 PortC Bitwise << >> not working.

Started by JackB, Apr 26, 2026, 12:27 PM

Previous topic - Next topic

JackB

Hello,

here's a simple bitwise function <<,>> it's working fine on PortB, but in PortC I've got different result.

the shift right >> set bit #0 to "1" and bit #1 stay at "0"
the shift left  << set bit #0 to "1" 

any clue what I missed, or is this a bug

Thank's to everyone for helping.

' PORTC Test
Device = 18F26K22             
Declare Xtal = 64
Config_Start
  FOSC = INTIO7               
  PLLCFG = On                 
  MCLRE = INTMCLR
  LVP = OFF                   
Config_End

TRISA = %00000000               
TRISB = %00000000               
TRISC = %00000000               
TRISE = %00000000

ANSELA = 0                       
ANSELB = 0                       
ANSELC = 0                       

OSCCON  = %01110000  '$70       
OSCTUNE = %01000000  '$64       
                         
PORTC =   %00000001
PORTA.0  = 1
DelayMS 20

Do                         
    SerOut PORTA.0,84,[" PORTC   : ",Bin8 PORTC,13,10]
    DelayMS 1000                   
    PORTC =  PORTC << 1
Loop                     







Frizie

#1
Hi Jack, I've just test it on a 18F46K22 (same family, only 40-pins) and both << as well as >> works fine.

PORTC: 10000000
PORTC: 01000000
PORTC: 00100000
PORTC: 00010000
PORTC: 00001000
PORTC: 00000100
PORTC: 00000010
PORTC: 00000001

and

PORTC: 00000001
PORTC: 00000010
PORTC: 00000100
PORTC: 00001000
PORTC: 00010000
PORTC: 00100000
PORTC: 01000000
PORTC: 10000000
Ohm sweet Ohm | www.picbasic.nl

trastikata

Reading and writing the port sequentially might be too fast. Read the port to a temporary variable and pass it back to the port.

streborc

Could this be yet another instance of the read-modify-write gotcha?

https://download.mikroe.com/documents/compilers/mikroc/pic/help/rmw.htm

JackB

Many thanks Frizie, trastikata, streborc for taking time to answer this topic.

Solution was :  read-modify-write from streborc

in fact by using LATC (Latch instead of the PortC directly) it's running just fine while a PIC18F25K22 can address PORTC directly but for the PIC18F26K22 it has to be done using LATC.

same family type PIC:
                Flash  Single
                bytes Words    SRAM    EEPROM
PIC18(L)F25K22  32K    16384    1536    256
PIC18(L)F26K22  64k    32768    3896    1024

'----------------------------------------------
' PORTC Test
Device = 18F26K22             
Declare Xtal = 64
Config_Start
  FOSC = INTIO7               
  PLLCFG = On                 
  MCLRE = INTMCLR
  LVP = OFF                   
Config_End

TRISA = %00000000             
TRISB = %00000000             
TRISC = %00000000             
TRISE = %00000000

ANSELA = 0                     
ANSELB = 0                     
ANSELC = 0                     

OSCCON  = %01110000  '$70     
OSCTUNE = %01000000  '$64     
                         
LATC =  %10000000
'PORTC =  %00000001
PORTA.0  = 1
DelayMS 20

Do                         
'    SerOut PORTA.0,84,[" PORTC  : ",Bin8 PORTC,13,10]
    SerOut PORTA.0,84,[" LATC  : ",Bin8 LATC,13,10]
    DelayMS 1000                   
'    PORTC =  PORTC << 1
    LATC =  LATC >> 1
Loop                     



Frizie

Nice that you found the problem.
I copied your program 100%, just changed DEVICE to 18F46K22, and your program worked fine by me.

Quote from: JackB on Apr 26, 2026, 02:49 PMsame family type PIC:
                Flash  Single
                bytes Words    SRAM    EEPROM
PIC18(L)F25K22  32K    16384    1536    256
PIC18(L)F26K22  64k    32768    3896    1024

The 18F45K22 and 18F46K22 also belong to this family.
In fact, they have the same datasheet.
Ohm sweet Ohm | www.picbasic.nl

JackB

#6
same datasheet, inside gutt slightly different
I'm learning everyday.

About the LATC from the datasheet page 137:

The Data Latch register (LATC) is also memory
mapped. Read-modify-write operations on the LATC
register read and write the latched output value for
PORTC.



CPR

General PIC rule of thumb - Write to a LAT, Read from a PORT.

(edited - oops had that round the wrong way)

diebobo

I found a nice answer a few days ago myself when i was headscratching about this subject.. Check 2nd answer, very clear : https://stackoverflow.com/questions/2623428/difference-between-port-and-latch-on-pic-18f

Stephen Moss

Quote from: JackB on Apr 26, 2026, 12:27 PMthe shift right >> set bit #0 to "1" and bit #1 stay at "0"
Even if there had not been other issues setting bit 0 to 1 for a right shift would not have been a good test as on the first shift the 1 would have been shifted out of the register and you would have had all 0's after that as 0's are shifted in from the left side, you should have been setting bit 7 to 1 for a basic shift right test.

Quote from: CPR on Apr 26, 2026, 06:12 PMGeneral PIC rule of thumb - Write to a LAT, Read from a PORT.
That is good advice, if the device has LAT registers for the Port, many older devices only have PORT registers so your only option is to read and write to the PORT register for those devices.

Bravo

I had a problem with 18F26K22 TrisB = %11111111 . I was trying to use PortB.2 as an interupt input which did not work. But it used to work a year ago. In order to solve the problem, I had use input Portb.0 : input PortB.1  etc.  I assume there was a change in the compiler, or has the command changed?
Retired RF Tech