News:

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

Main Menu

How can I solve this?

Started by Mapo, Dec 16, 2025, 09:41 PM

Previous topic - Next topic

Mapo

Hi everyone,
I have two buttons and would like to change the position of the controls.
In simple terms:
Dim Button1 as byte = 0
Dim Button2 as byte = 1
If portb.button2 = 0 Then
....
The compiler writes this error:
*** Invalid Cast for this type of variable! ***

How can I fix this?


RGV250

Hi,
Dim Button1 as bit = 0
Dim Button2 as bit = 1
If button2 = 0 Then

Bob

RGV250

Hi,
Or did you want to do something like this?
 Symbol Button2 = PortB.1
 if button2 = 1 then

Bob

Mapo

Example: key 1 moves forward and key 2 moves backward.
Now, using a switch, I'd like the ability to swap key 1 with key 2.
Of course, I'm not limited to just two keys.

RGV250

Hi,
If I understand correctly

if Switch = left
Button 1 = forwards
Button 2 = backwards

if Switch = Right
Button 1 = backwards
Button 2 = forwards

If this is the case I think it would be best to have a couple of subroutines or procedures, one for left and one for right which returns a temporary variable for the direction.

Regards,
Bob

trastikata

Dim Button1 As Pin
Dim Button2 As Pin
Symbol Switch = PORTC.0
 
Main:
    If Switch = 0 Then
        Button1 = PORTB.0
        Button2 = PORTB.1 
    Else
        Button1 = PORTB.1
        Button2 = PORTB.0
    EndIf
End


Make sure you have the correct port direction set i.e. I/O

Stephen Moss

Quote from: Mapo on Dec 16, 2025, 11:23 PMExample: key 1 moves forward and key 2 moves backward.
Now, using a switch, I'd like the ability to swap key 1 with key 2.
Of course, I'm not limited to just two keys.
I am sure you have a reason for wanting to do that, if it is an internal switch for factory customisation so it is a set once and leave then it would be fine. However, if it is an external switch I am not sure that is a particularly practical solution I think it would get annoying in use as you press the button for one direction but it goes the other way due to the switch position being changed.

Quote from: Mapo on Dec 16, 2025, 11:23 PMOf course, I'm not limited to just two keys.
If it was just the two then as you are using switch anyway I would have suggested using a DTDT switch to swap them instead of remapping them in code, but that may not work so well if you are swapping more than two inputs.

Mapo

#7
thanks all for reply,
trastikata you understand well what I mean, but it doesn't work
all keys are not responding
I made this modification on a working firmware with keys

trastikata

Hello Mapo,

without seeing the full code, can't say much as for why it is not working.

Mapo

this is test
                Xtal = 64

                Declare Dead_Code_Remove = On
                Declare Optimiser_Level = 3


' SETTAGGI CLOCK
                OSCTUNE = %11011111 ' attiva PLL 4x

' SETTAGGI PORTE
                TRISA = %11011111
                TRISB = %11111111
                TRISC = %11101011
                TRISE = %11111111

                INTCON2.7 = 0                                      'PORTB_Pullups
                WPUB = %11111110



' SETTAGGI SERIALE
                Declare Hserial_Baud = 57600                        ' Set baud rate to 57600
                Declare Hserial_RCSTA = %10010000                  ' Enable serial port and continuous receive
                Declare Hserial_TXSTA = %00100100                  ' Enable transmit and asynchronous mode
                Declare Hserial_Clear = On                          ' Enable Error clearing on received characters



                Symbol Tasto1 = PORTB.6
'                Symbol Tasto2 = PORTB.5
'                Symbol Tasto3 = PORTB.4


'                Dim Tasto1 As Pin
                Dim Tasto2 As Pin
                Dim Tasto3 As Pin


Start:
'                Tasto1 = PORTB.6
                Tasto2 = PORTB.5
                Tasto3 = PORTB.4

                IF Tasto1 = 0 then
                      HRSOUT 13,"TASTO 1",13
                EndIf
                IF Tasto2 = 0 then
                      HRSOUT 13,"TASTO 2",13
                EndIf
                IF Tasto3 = 0 then
                      HRSOUT 13,"TASTO 3",13
                EndIf

                GOTO START

if I use "Symbol" the key is ok
I think the "Pin" command is for Output only



trastikata


Mapo


top204

Not an anomaly.

The Pin type variable is a carrier of a Port . Pin, so it needs one of the compiler's commands to operate a Port . Pin directly. i.e. PinHigh, PinLow, PinInput, PinSet, PinGet etc...

As a test, I have just created the simple demo code listing below, that reverses the pin designations, based upon the state of another pin:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a simple use of the Pin variable type.
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial_Baud = 9600
    Declare HRSOut_Pin = PORTC.6
'
' Create global constants and aliases here
'
    Symbol cButton1_Pin = PORTB.6                   ' Designate Button1 Port and Pin
    Symbol cButton2_Pin = PORTB.5                   ' Designate Button2 Port and Pin
    Symbol cButton3_Pin = PORTB.4                   ' Designate Button3 Port and Pin

$define Reverse_Pin PORTB.0                         ' Designate the reverse switch Port and Pin (active low)
'
' Create global variable here
'
    Dim pnButton1 As Pin                            ' Holds the Port and Pin used for Button 1
    Dim pnButton2 As Pin                            ' Holds the Port and Pin used for Button 2
    Dim pnButton3 As Pin                            ' Holds the Port and Pin used for Button 3
    Dim tRevState As Bit = 0                        ' Holds 1 if the reverse switch is turned on

'-------------------------------------------------------------------------------
' The main program starts here
' Transmit what button is being pressed, and reverse the buttons when the Reverse_Pin switch is enabled
'
Main:
    Setup()                                         ' Setup the program and any peripherals
    HRsout1Ln "Press a Button"
    Do                                              ' Create a loop
        If Reverse_Pin = 0 Then                     ' Is the 'reverse pins' switch on?
            pnButton3 = cButton1_Pin                ' \
            pnButton2 = cButton2_Pin                ' | Yes. So re-arrange the pin arrangements
            pnButton1 = cButton3_Pin                ' /
            tRevState = 1                           ' Signal that the 'reverse pins' switch is on
        Else                                        ' Otherwise... The 'reverse pins' switch is off
            pnButton1 = cButton1_Pin                ' \
            pnButton2 = cButton2_Pin                ' | Setup the pin arrangements
            pnButton3 = cButton3_Pin                ' /
            tRevState = 0                           ' Signal that the' reverse pins' switch is off
        EndIf

        If PinGet(pnButton1) = 0 Then               ' Is button 1 pressed?
            HRSout "Button 1 Pressed"               ' Yes. So Transmit text to a serial terminal
            Display_RevState()                      ' Transmit the 'reverse pins' state
        EndIf
        If PinGet(pnButton2) = 0 Then               ' Is button 2 pressed?
            HRSout "Button 2 Pressed"               ' Yes. So Transmit text to a serial terminal
            Display_RevState()                      ' Transmit the 'reverse pins' state
        EndIf
        If PinGet(pnButton3) = 0 Then               ' Is button 3 pressed?
            HRSout "Button 3 Pressed"               ' Yes. So Transmit text to a serial terminal
            Display_RevState()                      ' Transmit the 'reverse pins' state
        EndIf
        DelayMs 250                                 ' A small delay between button tests
    Loop                                            ' Do it forever

'-------------------------------------------------------------------------------
' Transmit the state of the reverse switch
' Input     : tRevState holds the state of the reverse switch
' Output    : None
' Notes     : None
'
Proc Display_RevState()
    If tRevState = 1 Then
        HRsout1Ln " : Reversed"
    Else
        HRsout1Ln " : None-Reversed"
    EndIf
EndProc

'-------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    PinMode pnButton1, Input_Pullup                 ' Make the button 1 pin an input, with the internal pull-up resistor enabled
    PinMode pnButton2, Input_Pullup                 ' Make the button 2 pin an input, with the internal pull-up resistor enabled
    PinMode pnButton3, Input_Pullup                 ' Make the button 3 pin an input, with the internal pull-up resistor enabled
    PinMode Reverse_Pin, Input_Pullup               ' Make the reverse pin an input, with the internal pull-up resistor enabled
EndProc


And this is where the proteus simulator can come in handy, with simple simulations, and the screenshot below shows the code listing above working. It serially transmits what button has been pressed, then reverses pins 1 and 3, when the reverse switch is turned on:

Buttons_Screenshot.jpg

The above code listing and the proteus file have been attached to the post.

Regards
Les

Mapo

Thanks a lot, Les, for the explanation.
I need to use the PinGet command.

Thanks all for support