News:

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

Main Menu

Output pin state equals Input pin state best way?

Started by trastikata, Apr 03, 2021, 08:39 AM

Previous topic - Next topic

trastikata

Hello,

I would like to ask what would be the best way to make an Output pin state equal to Input pin state. I noticed that direct approach doesn't work, like in the code and I do If-Then.

Device = 18F26J50

Declare Xtal = 8

Symbol PIN1 = PORTA.1
Input PIN1
Symbol LED1 = PORTA.2
Output LED1

LOOP1: 
    'This doesn't work
    While 1 = 1
        LED1 = PIN1
    Wend
 
LOOP2:   
    'This works
    While 1 = 1
        If PIN1 = 1 Then High LED1
        If PIN1 = 0 Then Low LED1
    Wend

John Drew

I could be wrong but I thought that what you want is in an upcoming upgrade for the compiler but not yet.
John

TimB


Device = 18F26J50

Declare Xtal = 8

Symbol PIN1 = PORTA.1
Input PIN1
Symbol LED1 = PORTA.2
Output LED1
Dim bfTemp as bit

LOOP1:
   
    While 1 = 1
        bfTemp = PIN1
        LED1 = bfTemp
    Wend



top204

#3
The code:

    While 1 = 1
        LED1 = PIN1
    Wend

Does work with PIN1 and LED1 set as Port pins using Symbol.

The Asm generated by the above code is:
; While 1 = 1
_lbl__2
;  LED1 = PIN1
    btfsc PORTA,1,0
    bsf PORTA,2,0
    btfss PORTA,1,0
    bcf PORTA,2,0
; Wend
    bra _lbl__2

And, as can be seen, PORTA.2 is set or cleared according to the state of PORTA.1.

Because the bits are parts of an SFR, there is an extra bit test mnemonic to make sure the assignment bit's state is always dictated by the source bit. i.e. A bit is never left set or clear until a condition is met.

Below is what the compiler actually sees when it is converting the code to assembler:
    While 1 = 1
        PORTA.2 = PORTA.1
    Wend

It is possible, you may be seeing the Read Modify Write phenomena happening on the device because the code is so tight and fast, and it is using two pins of the same port. So make the LED1 pin LATA.2, instead of PORTA.2. The compiler commands that are dedicated to Port.Pin operations always, internally, use the LAT SFRS instead of the Port SFRs, if the device has them, in order to counter-act this phenomena with PIC microcontrollers. For Example PinSet, PinClear, Low, High etc....

trastikata

Thank you all for the thorough explanation.