News:

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

Main Menu

Addressing a Pin

Started by keytapper, Mar 25, 2022, 08:36 AM

Previous topic - Next topic

keytapper

Hello Forum
I'm trying to get a procedure to read a pin, but all it does is reading an unused variable.
Latest upgraded version of 4.0.1.4

My procedure
Device = 16F876
Do
  If wait4btn(Pin_B0,10) >= PRESSED Then
      do_something
  End If
Loop
End
Proc wait4btn(PortPin As Byte, longpress As Byte), Byte
  While PortPin = 0
    Inc Result
    DelayMS 20
    If Result > longpress  Then
      Result = LNGPRESS
      Return
    End If
  Wend
  If Result > 10 Then
    Result = PRESSED
  Else
    Clear Result
  End If
EndProc
The resulting asm shows

F1_000085 equ $ ; in [TEST.BAS] Proc wait4btn(PortPin As byte, longpress As byte), byte
wait4btn
F1_000086 equ $ ; in [TEST.BAS] While PortPin = 0
_lbl__28
    movf wait4btnPortPin,F
    _set_cpage _lbl__29
    btfss STATUS,2
    goto _lbl__29
F1_000087 equ $ ; in [TEST.BAS] Inc Result
    incf wait4btnResult,F
F1_000088 equ $ ; in [TEST.BAS] DelayMs 20
    movlw 20
    _mcall __delay_ms_
Which it means that there isn't the call to convert my Pin_B0 into the expected value.
Probably I don't get the correct way to make it work, but as the manual shows for a subroutine seems that is not the same way.
Probably my manual is not the latest. I couldn't find a version written in that manual.

EDIT
Found the culprit, I forgotten to use the function SetPin
Ignorance comes with a cost

top204

You cannot pass the actual Port.Pin as a Byte parameter, because the parameter will just be a byte variable.

As you have found, the compiler allows the value held in a variable to be accessed as a pin using the LoadPin, SetPin, GetPin, PinHigh, PinLow etc, commands. In the manual you will see that each pin is assigned a value, where PORTA.0 is value 0, PORTA,7 is value7, PORTB.0 is value 8, and so on. And this is how the Pin manipulating functions work, by calculating a mask for the Bit, and getting the address of the Port, then using that address and mask with Ands and Ors when reading and writing etc...

You can actually pass a Port.Pin as the new and unique Pin variable type, and that will solve a lot of your problems. See the manual's "Pin" variable section.

Yasin

Dear @keytapper. You should also check out Les's previous work titled "Static Variables". The "Button_Get" procedure is a excellent code example.



https://protoncompiler.com/index.php/topic,736.msg5257.html#msg5257

keytapper

Quote from: top204 on Mar 25, 2022, 11:48 AMYou cannot pass the actual Port.Pin as a Byte parameter, because the parameter will just be a byte variable.

As you have found ....
Yeah, I overlooked the example, that the defined Pin_XY are calculate with that do_withPin commands. As I got the mistake cleared, the procedure now is working elegantly, Thanks boss.

@Yasin I've missed sometime to follow the forum, I probably left that great snippet off.
Ignorance comes with a cost