Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:54 12 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Assigning names to PINS and PORTS

Author Message
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 10:41pm 07 Jan 2019
Copy link to clipboard 
Print this post

How can I address a pin or a port by name?

For the context of this question I am a "migrant" from PICAXE. Migrating from PICAXE BASIC to MMBASIC has been really very easy indeed but one feature that I cannot seem to find a migration route for is the very useful SYMBOL command in PICAXE BASIC. This enables a name to be substituted for a constant, a variable or, most importantly, a PIN.

The syntax is SYMBOL symbolname=value
e.g. SYMBOL Green_LED = C.7 ; this assigns a name Green_LED to OUTPIN C.7.

As variables are handled in a different manner in MMBASIC and can be given a full descriptive name there is no problem with missing the SYMBOL command. The same does not, however, appear to apply with naming pins and ports. Hopefully, I am missing something here and MMBASIC does have a simpler way of addressing PORTs and PINS. Is there any way to assign an easy name to i/o pins in the manner of PICAXE BASIC?

i.e. if the SYMBOL command in PICAXE BASIC applied in MMBASIC then I would write;

SYMBOL Sensor_pins = PORT(1,5,8,4,41,4)
SYMBOL Red_LED = PIN(36)
If Sensor_pins = 0 then....etc.
Red_LED=1 etc.


With the larger 100/144 pin chips it is a nightmare trying to remember which pin is connected to what. Naming the pins and the ports would be of enormous help in control applications. Is there a way to do this that I have not cottoned on to or am I asking for a new command?
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9754
Posted: 11:25pm 07 Jan 2019
Copy link to clipboard 
Print this post

Hello.
Welcome. I too am a migrant from PICAXE.
I still use PICAXE for the simple stuff though.

Use CONST - short for Constant.

Example:


CONST Green_LED=36 'Assign the number 36 to a constant variable name

SetPin Green_LED,DOUT 'Use that constant to setup the pin

Pin(Green_LED)=1 'Use that constant name to control the pin


Basically, use as many CONST commands as you need, to define names for any pin, then reference THOSE in your code, so it makes it easy to remember what the line of code is doing.


EDIT: Here is an example from a a current code I am working on:

  Quote  '---------- PIN ASSIGNMENTS -------------
Const SENSOR1=02,SENSOR2=03,SENSOR3=04,SENSOR4=05,PLATE=35 'Sensors on the sub-board
Const LOCAL1=37,LOCAL2=32 'Local sensors on the main board
Const HEATER=15,PIEZO=13,FAN=11,BATTERY=10,SMC=26,HIVE=12 'Everything else
'---------- IR REMOTE CONTROL KEY CODES ------------
Const POWER=2,TVPWR=170,SET=154,TVIN=26,VOLDN=234,VOLUP=106
Const SETUP=194,APP=240,VOLDNYEL=8,VOLUPBLU=24
Const HOME=136,GOBACK=152,MENU=50,MOUSE=0
Const UPA=104,DOWNA=88,LEFTA=138,RIGHTA=10
Const ONE=114,TWO=176,THREE=48,FOUR=82,FIVE=144
Const SIX=16,SEVEN=98,EIGHT=160,NINE=32,ZERO=128
Const MUTE=130,DEL=66,OK=200
'---------- SETUP SYSTEM -----------------
SetPin HEATER,DOUT 'Heating pad on/off or PWM 2A control pin
SetPin FAN,DOUT 'Circulating fan on/off or PWM 2B control pin
SetPin BATTERY,AIN 'Battery voltage input via potential divider
SetPin PIEZO,DOUT 'Piezo beeper driver

Edited by Grogster 2019-01-09
Smoke makes things work. When the smoke gets out, it stops!
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 11:48pm 07 Jan 2019
Copy link to clipboard 
Print this post

Thank you for that; your explanation is clear and the example code is most helpful. It goes a good way to resolving my problem. I had tried using CONST but I had not caught on to the "secondary substitutions" that you propose above.

My main problem lies with naming PORTS. The key sequence for extended ports can be quite cumbersome and I was hoping to be able to substitute a simple name for the port. When I tried using CONST for the PORT I found that the value of the port pins at the time of the declaration became a defined numerical constant rather than the reference to the port by name.

Any ideas on using a named approach for PORTs?
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9754
Posted: 11:56pm 07 Jan 2019
Copy link to clipboard 
Print this post

Off the top of my head, I would think of a one-dimensional numeric array, and pass that as part of the PORT command: PORT (LPT())

The () just tells MMBASIC to pass the entire array in one go, rather then any specific element(s).

I have not tried it, but something along the lines of:


DIM AS INTEGER LPT(6)

LPT=(1,5,8,4,41,4)

DTA=PORT(LPT())


I have NOT tried this, so it would have to be tested to see if it worked. Other members will probably come up with other ideas for the PORT command, if my one does not work.
Smoke makes things work. When the smoke gets out, it stops!
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 12:51pm 08 Jan 2019
Copy link to clipboard 
Print this post

  Grogster said  
I have NOT tried this, so it would have to be tested to see if it worked. Other members will probably come up with other ideas for the PORT command, if my one does not work.


I tried this out; regretfully it does not work.
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3308
Posted: 02:35pm 08 Jan 2019
Copy link to clipboard 
Print this post

This is one of the problems with switching languages, you miss some unique features of the old but you have not discovered the unique features of the new.

In MMBasic it would be best to use functions and subroutines:
Function Sensor_pins() : Sensor_pins = PORT(1,5,8,4,41,4) : End Function
Sub Red_LED v : PIN(36) = v : End Sub

If Sensor_pins() = 0 then....
Red_LED 1

Not as good but close.

As pointed out by others, this would be better:
Const Red_LED = 36

Pin(Red_LED) = 1


Geoff

Geoff Graham - http://geoffg.net
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 11:06am 31 Jan 2019
Copy link to clipboard 
Print this post

I have just enjoyed an excellent programming session with two boards; one fitted with a 28 pin MM2 and the other with a 44 pin MM2. The two boards have to communicate with each other over a custom link. Both boards have Microbridges fitted and both were connected to my computer with USB leads. Each board had its own iteration of MM Edit running simultaneously on-screen.

Being able to program/download/edit/run on both boards from the same console was simply excellent and made both authoring and de-bugging an absolute breeze.

This was also an opportunity to learn and explore Geoff's proposed workaround for naming ports (above). It does indeed work as expected but I very much agree...

  Geoffg said   Not as good but close.

Geoff


Microcontrollers are primarily used as embedded controllers and the PORT command is extremely useful and effective for both input and output but would be so considerably enhanced if a label could be applied to each PORT when it is declared or defined.

Therefore, Geoff, this is a request to enhance the PORT command with the addition of being able to apply a label to it when first declared. Further, being able to define a label to a pin when it is configured would be so much simpler and more elegant than having to set up constants and functions.

I am thinking along the lines of:
PORT(start,nbr[,start,nbr]...)=value, LABEL
SETPIN pin,cfg,target [,option], LABEL



I am sure that this would be so immensely welcomed by those using multiple PORTs and PINs in complex control programs. Just looking at Grogster's program above emphasises this to me - how much easier it would be if he could just have appended a label to each SETPIN. It would certainly make my programs much more elegant in form, easier to author and edit.

Any chance at all of seeing this in a future version, Geoff?
Edited by circuit 2019-02-01
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4133
Posted: 12:55pm 31 Jan 2019
Copy link to clipboard 
Print this post

That looks weird syntax. So weird that I have to ask what is it supposed to do?

It looks very unlike what anyone would expect so please explain.

John
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3308
Posted: 02:08pm 31 Jan 2019
Copy link to clipboard 
Print this post

The bottom line is that it is impossible to add all the neat features of all the languages out there. Especially as the flash memory in the Micromite is already full with only a few K bytes left for bug fixes and really important things.

As I previously said, MMBasic has lots of features that the PICAXE does not have and after you have used it for a while hopefully you will appreciate it more.

Some time ago in another post I wrote the following and it is just as relevant today:

  Quote  I love these sorts of discussions because every so often up pops an idea that would really add value to MMBasic. However, it needs to add genuine value and these are some of the criteria that I use when deciding on that:
- Will many (ie, 20% or more) users find it useful and, more importantly, actually use it?
- Does the syntax follow the "style" of the BASIC language?
- Can the same outcome be achieved using ordinary BASIC code?
- Will it use up much flash?
- Will it be easy or hard to implement?
- Will it impact the performance of the Micromite for the "ordinary" user?

A lot of ideas are created by people to fix a specific problem in one or two projects and I am very wary of these. It is extremely hard to remove a feature once it has been added to a computer language - this is because there might always be someone out there that has a program using that feature. Unless great care is taken, MMBasic could easily be cluttered with badly thought out "features" that cannot be removed. Just look at the Windows API for a good example.

So, thanks for the suggestions but please do not be disappointed if yours does not make it into MMBasic. It is a difficult balancing act producing a clean and productive computer environment and not every good idea can make it in there (a lot of mine have not made it).

Geoff
Geoff Graham - http://geoffg.net
 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 02:42pm 31 Jan 2019
Copy link to clipboard 
Print this post

  JohnS said   That looks weird syntax. So weird that I have to ask what is it supposed to do?

It looks very unlike what anyone would expect so please explain.

John


What it is supposed to do;

Assuming that one is using the microcontroller to control multiple external devices and to read ports and pins from sensors, this enables the NAME of the external device or sensor to be substituted in place of the port formula or the pin number within the program. Geoff's proposed use of functions, constants and subroutines facilitates this successfully but leads to somewhat bulky program and sometimes several lines of code just to assign a name - see Grogster's approach for pins above by way of example. What I am proposing is to be able to tack a name onto a pin or port when it is first configured to achieve the same in a much simpler manner.

Some possible examples;
SETPIN 15,DIN,,LIMIT_SWITCH
SETPIN 16,DOUT,,MOTOR
SETPIN 17,DOUT,,RED_LED
allowing:
DO WHILE LIMIT_SWITCH =0
MOTOR=1
LOOP
MOTOR=0
RED_LED=1

If we add in using constants ON=1 OFF=0 then this can give really easy-to-follow (and therefore debug) code such as;

MAIN_MOTOR ON
RED_LED OFF

When it comes to defining multiple PORTS, using a simple and indicative NAME/LABEL for the PORT gives great clarity to the subsequent program sequence. All that I am proposing is to avoid multiple lines of code involving constants, functions and subroutines by attaching a NAME/LABEL to the PIN or PORT at the time that it is configured.


 
circuit
Senior Member

Joined: 10/01/2016
Location: United Kingdom
Posts: 290
Posted: 02:46pm 31 Jan 2019
Copy link to clipboard 
Print this post

  Geoffg said   The bottom line is that it is impossible to add all the neat features of all the languages out there. Especially as the flash memory in the Micromite is already full with only a few K bytes left for bug fixes and really important things.

As I previously said, MMBasic has lots of features that the PICAXE does not have and after you have used it for a while hopefully you will appreciate it more.

Geoff


Geoff, I never expected a reply at this time of the day/night? Yes, I fully understand what you are saying. In fact in the preface to my post I was lauding the features of MMBasic that enabled me to programme two boards at the same time whilst testing out the comms running between them - fantastic! I certainly appreciate the vast chasm of versatility between your language and PICAXE! I have most certainly upgraded in that sense.

I will adjust to the somewhat more convoluted way of substituting names as you previously suggested. As for the value to others, surely the prime aim of an embedded controller is to talk to other devices external to it and I think my proposal would make this easier for everyone.

Perhaps room could be found for this in MicroMite Extreme...especially when dealing with so many more pins and potential PORTs on the 144 pin chips....Peter?
Edited by circuit 2019-02-02
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4133
Posted: 03:13pm 31 Jan 2019
Copy link to clipboard 
Print this post

  circuit said   What it is supposed to do;

snip

allowing:
DO WHILE LIMIT_SWITCH =0
MOTOR=1
LOOP
MOTOR=0
RED_LED=1

If we add in using constants ON=1 OFF=0 then this can give really easy-to-follow (and therefore debug) code such as;

MAIN_MOTOR ON
RED_LED OFF

You can write code looking like that now. E.g. use FUNCTIONs and SUBs.

John
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10572
Posted: 04:05pm 31 Jan 2019
Copy link to clipboard 
Print this post

  Quote  Perhaps room could be found for this in MicroMite Extreme...Peter?


I don't see any advantage over CONST for single pins, in fact it can be worse when you want to reuse the pin in different modes (DIN, DOUT)

I can see that it would be useful to define the pins that make up a "port" and re-use that definition in multiple port commands. However, I suspect the port command is itself in the category of <20% users and probably <5% so I'm afraid I'm with Geoff on this one
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2464
Posted: 04:14pm 31 Jan 2019
Copy link to clipboard 
Print this post

how far would allowing passing an array to PORT() and SETPIN go towards making things simpler and/or easier to understand?

for example:

DIM myport(4)=(2,6,9,2,0) '0 = end of parameters
SETPIN myport, DOUT
[...]
PORT(myport)=45


one could also specify that if passing an array, then the elements represent a simple list of pin numbers, rather than pin, count, pin, count...

in a sense this is similar to the proposal to allow passing an array to MIN and MAX, with perhaps some shared code within the firmware.


cheers,
rob :-)

ADDENDUM: snap! i was writing my post as peter wrote his.
Edited by robert.rozee 2019-02-02
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2171
Posted: 05:13pm 31 Jan 2019
Copy link to clipboard 
Print this post

  circuit said  
Some possible examples;
SETPIN 15,DIN,,LIMIT_SWITCH
SETPIN 16,DOUT,,MOTOR
SETPIN 17,DOUT,,RED_LED



Hate it.

I really don't like the arbitrary way to assign a Constant - by which I mean your example syntax may or may not do so.

The following (recommended?) way is far more 'BASIC' where things are in absolute terms and the application is how you use it rather than being "surprised" that something program-wide was defined by a statement that deals with a different concept. It breaks the logical compartmentalisation of statements. A bit like buying petrol from a greengrocer. Let the memory management statements do what they do and let the I/O statements do what they do etc... I also see a fundamental problem with this for when a pin is re-assigned (I do this when using the PORT statements because I like a standard byte-wide port for some things and am constantly switching from Read to Write and back)... do I re-use the old name, discard it, inherit it??? This is a nightmare for any changes in the firmware for no benefit that I can see (your wish has a trivial work-around)... and why mess with stable code that has been working for years?


Const LIMIT_SWITCH=15
Const MOTOR=16
Const RED_LED=17
...

SETPIN LIMIT_SWITCH,DIN
SETPIN MOTOR,DOUT,
SETPIN RED_LED,DOUT


This does precisely what you want, I think perhaps, being newly arrived on the PICaxe bus, you are still in the exploration stage for MMBasic and this way is a bit new/different for you... it is far more compatible with the general approach of all the BASICs I have ever used - even given that most don't have commands for I/O like MMBasic does (closest you had was port level, byte-wide Inp() and Out)



Edited by CaptainBoing 2019-02-02
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025