|
Forum Index : Microcontroller and PC projects : Assigning names to PINS and PORTS
| Author | Message | ||||
| circuit Senior Member Joined: 10/01/2016 Location: United KingdomPosts: 290 |
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 ZealandPosts: 9754 |
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: Smoke makes things work. When the smoke gets out, it stops! |
||||
| circuit Senior Member Joined: 10/01/2016 Location: United KingdomPosts: 290 |
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 ZealandPosts: 9754 |
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 KingdomPosts: 290 |
I tried this out; regretfully it does not work. |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
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 KingdomPosts: 290 |
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... 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? |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4133 |
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: AustraliaPosts: 3308 |
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: Geoff Geoff Graham - http://geoffg.net |
||||
| circuit Senior Member Joined: 10/01/2016 Location: United KingdomPosts: 290 |
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 KingdomPosts: 290 |
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? |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4133 |
You can write code looking like that now. E.g. use FUNCTIONs and SUBs. John |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 10572 |
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 ZealandPosts: 2464 |
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. |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
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) |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |