Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 04:45 15 May 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 : Setting multiple pins simultaneously

     Page 1 of 2    
Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 12:26pm 12 May 2025
Copy link to clipboard 
Print this post

Good afternoon from a wet Wales.

Let's say I have 6 LEDs on on GP2 - GP7.

I can set them for outout with SETPIN GP2, DOUT etc and control them with PIN(GP2) = 1 etc.

I am randomly lighting one of the 6 (other five off).  Currently doing this with a clunky Select Case:

pseudoish code:

Select Case LEDon
 CASE 1
   PIN(GP2) = 1
   PIN(GP3) = 0
   PIN(GP4) = 0
   PIN(GP5) = 0
   PIN(GP6) = 0
   PIN(GP7) = 0
 CASE 2 - 'etc etc



Questions:

(1) Is there a way to set all pins low in one simple command? ie PIN(ALL) = 0
(I could abstract this to a SUB, setting each pin individually, so asking in case I've missed something) -- that way I only need to set the pin that is on.

(2) Ideally what I want to do is:
PIN (INT(RND * (m - n + 1)) + n) = 1
 where n=2, m=6 so that it picks a random pin from 2 to 6.  Is there an easy way to achieve this?  I could construct the "GP2" part as a string and use "EVAL" - but that feels clunky.  Again, asking in case I've missed the obvious.

Cheers all.
Entropy is not what it used to be
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4867
Posted: 12:46pm 12 May 2025
Copy link to clipboard 
Print this post

Hi Nimue,

Look at the PORT command. You can write a value to multiple pins at once, as if it where a parallel port.

Volhout
Edited 2025-05-12 22:48 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 12:51pm 12 May 2025
Copy link to clipboard 
Print this post

  Volhout said  Hi Nimue,

Look at the PORT command. You can write a value to multiple pins at once, as if it where a parallel port.

Volhout


Perfect - that will sort it.  Thankyou.

One of the downsides of not printing the manual is I loathe browsing PDFs like a paper manual.  Time to print one again I think.

Cheers
Entropy is not what it used to be
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4867
Posted: 12:55pm 12 May 2025
Copy link to clipboard 
Print this post

@Nimue,

Wait a bit... 6.00.02 to be released soon, and then you can print the latest manual.
I am also waiting for it. For me 6.00.02 will be the version I settle on (for RP2040 VGA / pico RP2040 that will most likely not change much anymore, since it took so much squeezing already, it will kill Peter if we ask for more...).

Volhout
Edited 2025-05-12 22:56 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 01:03pm 12 May 2025
Copy link to clipboard 
Print this post

  Volhout said  
Wait a bit... 6.00.02 to be released soon, and then you can print the latest manual.
Volhout




It amazes me how much is now crammed in - for my mostly trivial use cases, I almost want "less" so I can see the wood for the trees and get back to the "BASIC" in MMBasic.   But of course, it just means I don't use those new spicy features, so not in any way a criticism of all the work going on here.

My use case for what it's worth is to create a "demo" mode for a 1978 SIMON game - so that it can be left on randomly cycling through the colours.  As is, the SIMON uses both a 9V and x2 D cells.  Retrofitting a 'Mite and 4 LEDs (+USB power bank) is far more suitable for use as an ornament.

For what its worth, will post it went up and running.
Entropy is not what it used to be
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2143
Posted: 01:07pm 12 May 2025
Copy link to clipboard 
Print this post

  Nimue said  Good afternoon from a wet Wales


kind of goes without saying   you'll get some sun soon - we haven't had a drop of rain to be worth mentioning here in central England in about 6 weeks... but we have had that pesky north wind which really takes the edge off the sun  

So... As @Volhout has already pointed out, the PORT command is your friend here. It allows you to address a whole load of pins in one go - sounds exactly what you want. Set your GPIO pins so they act as the bits of the port (doesn't have to be 8 bits) and then you just write a value to the port and the binary represenation would light up the LEDs of your choice

Assuming your LEDs are active on logic 1...

If you want single LEDs to light, choose a number between 0 and 5 (to keep the LED numbers nice and "computery), then PORT 2^number for just a single LED. When you want them all off, just write 0 to the PORT


For n=1 to 100'sparkle the LEDs
 PORT(GP2,6) = 2^Int(Rnd*6)
 Pause 100
Next

PORT(GP2,6) = 0' all LEDs off


hth
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2431
Posted: 01:15pm 12 May 2025
Copy link to clipboard 
Print this post

Beat me to it, though slightly different method.

PORT(GP2,6) = 1 << CInt(Rnd * 5)
Edited 2025-05-12 23:24 by phil99
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 01:15pm 12 May 2025
Copy link to clipboard 
Print this post

  CaptainBoing said  
  Nimue said  Good afternoon from a wet Wales


kind of goes without saying   you'll get some sun soon - we haven't had a drop of rain to be worth mentioning here in central England in about 6 weeks... but we have had that pesky north wind which really takes the edge off the sun  

hth


Cloudy, wet, a cool 19'C.  Mind you Newport is always miserable  

  Quote  2^Int(Rnd*6)


Oh my, forgetting my binary from back in the day bit banging the parallel port on AcronElectrons/Plus 1 -- memory is failing me.   Perfect, nice.  Off to tinker.
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 01:17pm 12 May 2025
Copy link to clipboard 
Print this post

  phil99 said  Beat me to it, though slightly different method.

PORT(GP2,6) = 1 << CInt(Rnd * 6)


Yikes, from banging to bit shifting in one simple post -- like it.  Again, off to play.
Entropy is not what it used to be
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 1160
Posted: 01:20pm 12 May 2025
Copy link to clipboard 
Print this post

  Nimue said  It amazes me how much is now crammed in


It's mind boggling  

I'm not aware of any MCU development that comes close. Just when you think there is something to be desired...there it is  

I miss so many developments myself but I will never complain  
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2431
Posted: 01:30pm 12 May 2025
Copy link to clipboard 
Print this post

I made a mistake. 6 pins is 0 to 5 so:-
PORT(GP2,6) = 1 << CInt(Rnd * 5)
or
PORT(GP2,6) = 1 << Rnd * 5

As MMBasic automatically converts the float to an integer in this situation.
Edited 2025-05-12 23:36 by phil99
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 04:36pm 12 May 2025
Copy link to clipboard 
Print this post

Mmmmm curious behaviour (most likely something I am doing)

Version 6.0001 -- plain old Mite via serial terminal.  Fresh flash.

Testing with x4 leds, GP2-GP5

PORT(gp2,4) = &B1111  indeed turns on all 4 LEDS
PORT(gp2,4) = &B0000  does not turn the leds off

Reissuing PORT(gp2,4) = &B1111 turns off the LEDs.

It looks like setting a bit to "0" is not turning off, but setting to "1" toggles the state between 0 and 1 back to 0.


Print "Starting..."
Print "ALL on"
Port(gp2,4) = &B1111
Pause 1000
Print "All off"
Port(gp2,4) = &B0000  'Looks like it does nothing - ie LEDS still lit
Pause 1000
Print "Trying &B1111"
Port(gp2,4)=&B1111    'indeed turns off LEDS
Pause 1000


Therefore once the bit pattern is not all 1111's or all 0000's issuing PORT(GP2,4) = 1 << Rnd * 3  as per this thread only turns off leds once they recive a "1" ie turn off when already on.

This is most likely something I am doing...... any pointers...

Cheers
Edited 2025-05-13 02:42 by Nimue
Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7539
Posted: 04:41pm 12 May 2025
Copy link to clipboard 
Print this post

That looks wrong...  :(
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 04:43pm 12 May 2025
Copy link to clipboard 
Print this post

  Mixtel90 said  That looks wrong...  :(


Yea - addressing them with PIN() - works fine.

The port() command seems to only flip the bits with a 1 - the 0 seems to not change the state.

The code is simple, so not sure I'm missing anything here....
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10078
Posted: 04:59pm 12 May 2025
Copy link to clipboard 
Print this post

There was a bug in 6.00.01, it's mentioned in the bug list on Geoff's site. Upgrade to the latest 6.00.02RC23
Edited 2025-05-13 03:00 by matherp
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7539
Posted: 05:08pm 12 May 2025
Copy link to clipboard 
Print this post

I take it that you configured the pins as DOUT first?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3319
Posted: 05:21pm 12 May 2025
Copy link to clipboard 
Print this post

Works for me with PicoMite MMBasic RP2040 Edition V6.00.02RC20
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 06:08pm 12 May 2025
Copy link to clipboard 
Print this post

  matherp said  There was a bug in 6.00.01, it's mentioned in the bug list on Geoff's site. Upgrade to the latest 6.00.02RC23


100% nailed it - upgraded, now works as expected.

Thanks all for the steer.
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 387
Posted: 06:10pm 12 May 2025
Copy link to clipboard 
Print this post

  Mixtel90 said  I take it that you configured the pins as DOUT first?


Yes!!

Is there a way to configure a range of pins at once or individual SetPin?

Regardless -- all shipshape now.

Thanks


Entropy is not what it used to be
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7539
Posted: 06:13pm 12 May 2025
Copy link to clipboard 
Print this post

No official way to SETPIN a range, no.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
     Page 1 of 2    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025