![]() |
Forum Index : Microcontroller and PC projects : Converting Pico GP numbers to Pin numbers
Author | Message | ||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2458 |
Observations: Many people prefer:- a) GP numbers over pin numbers b) To make everything adjustable with variables at the start of the program. Problem: Pin assignments won't use string variables Solution: A SUB for converting GP numbers to RPPico module pin numbers. You supply a string "GPx" and it returns the pin no. in the second variable of your choice. eg > GP2Pin "GP7", MyPin% : ? MyPin% 10 > Dim Integer x, y For x=0 To 28 GP2Pin "GP"+Str$(x), y If y <> 0 Then : Print "GP";x, "Pin";y : EndIf Next Sub GP2Pin GP$, PinN 'GP must be upper case If Left$(GP$,2) = "GP" Then PinN = Val(Right$(GP$,Len(GP$)-2)) PinN = PinN + 1 + Int((PinN+2) / 4) If PinN > 29 Then : PinN = 0 : EndIf If GP$ ="GP26" Then : PinN = 31 : EndIf If GP$ ="GP27" Then : PinN = 32 : EndIf If GP$ ="GP28" Then : PinN = 34 : EndIf Else PinN = Val(GP$) EndIf End Sub A sane person would have used a lookup table, but there is a pattern to most of them. |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10090 |
Useful idea thanks ![]() Edited 2022-09-24 19:55 by matherp |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2458 |
Peter thanks for building that function in. Much more flexible than the Sub. |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2421 |
Why not? CONST GP10 = pin14 CONST GP14 = pin19 etc. |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7585 |
In your example pin14 and pin15 have to be numbers or numeric variables. You can use GPI0 and GPI4 as CONST names though (but not GP0 or GP4). There isn't a way to write a program using PCB pin numbers that will run on variations of RP2040 boards. The RP2040 only knows about GP numbers (it doesn't know what PCB it will be mounted on) so it makes sense to use them wherever possible. If you can't use them for some reason then make sure those locations are *very* well commented as you'll have to change the program if you want to run it on a different RP2040 board. Knowing what we know now, that the RP2040 has appeared on quite a few different variations of boards, it might have been wiser not to allow PCB pin numbers at all. :) Edited 2022-09-25 02:15 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
stanleyella![]() Guru ![]() Joined: 25/06/2022 Location: United KingdomPosts: 2421 |
would CONST pin14 = gp10 work? I see problems if different rp2040 boards. It's same with arduino, portb.0 or digital_8. Uno and nano boards different pins. |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10090 |
No, that would set the variable pin14 to the value 0 assuming that the variable gp10 had not been itself set to something else. If you had set OPTION EXPLICIT then it would simply error Edited 2022-09-25 02:53 by matherp |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2458 |
There is no need for this now, however the SUB is easier to use when turned into a Function. > ? gp2pin("gp19") 25 > Dim Integer x, y For x=0 To 28 gp$ = "GP"+Str$(x) y = GP2Pin(gp$) If y > 0 Then : Print GP$, "Pin";y : EndIf Next Function GP2Pin(GP$) If (Left$(GP$,2) = "GP") Or (Left$(GP$,2) = "gp") Then PinN% = Val(Right$(GP$,Len(GP$)-2)) PinN% = PinN% + 1 + Int((PinN%+2) / 4) If PinN% > 29 Then : PinN% = 0 : EndIf If GP$ ="GP26" Then : PinN% = 31 : EndIf If GP$ ="GP27" Then : PinN% = 32 : EndIf If GP$ ="GP28" Then : PinN% = 34 : EndIf Else PinN% = Val(GP$) EndIf GP2Pin = PinN% End Function |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 4920 |
During the development of picomite there was a inconsistency between pin numbers used for MMBAsic and pin numbers (GP numbers) used in the PIO. MMBasic used numerics that where related to the connector location of the pin. PIO uses numbers that are chip specific (the port/pin numbers). So in the earlier MMBasic versions you could end up with following inconsistency Setpin 31,PIO1 PIO init machine 1,0,1e5,Pio(pinctrl 1,,,27,) Where 27 was GP27, and 31 was GP27. That's when it was proposed to standardize on GPx numbers. It also makes it easier to port to other platforms that use the same chip. PicomiteVGA PETSCII ROBOTS |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |