Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 02:25 02 May 2024 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 : MMBasic with input pins

Author Message
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 09:02pm 16 Sep 2015
Copy link to clipboard 
Print this post

I'm back again. Now with a 2nd problem.

My little Pic32 micromite is doing well. I seem to be having more trouble programming in MMBasic than I would in C.

The issue is that I need to take input pins using SetPin xx, din, pulldown and write a function that takes say 5 pins and turns it into a number. ie

SetPin 15, din, pulldown
SetPin 16, din, pulldown
SetPin 17, din, pulldown
SetPin 18, din, pulldown

SetPin 21, din, pulldown
SetPin 22, din, pulldown
SetPin 23, din, pulldown
SetPin 24, din, pulldown


The pins all collect either a 1 for on or 0 for off. How would I make it so that it makes a number with the binary inputs. Say I have switch 24, 23, and 22 on... and 18 on. So effectively that would be 00010111 ie binary for 23 in decimal. Then set it as a variable. I'm not sure how to convert strings or pins into a number.

Do I just do it manually. Ie pin 24 adds 1, pin 23 adds 2, pin 22 adds 4, pin 21 adds 8 etc...? Or is there a more simple way?

Cheers.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 09:05pm 16 Sep 2015
Copy link to clipboard 
Print this post

Do I just do it manually. Ie pin 24 adds 1, pin 23 adds 2, pin 22 adds 4, pin 21 adds 8 etc...? Or is there a more simple way?


The PORT function will do exactly what you want
 
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 09:15pm 16 Sep 2015
Copy link to clipboard 
Print this post

I'm using the port function already funny enough. But it's for writing dout to a 7seg.

Does it read pins in as well? I'm just reading pins from two banks of 4x switches.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 09:22pm 16 Sep 2015
Copy link to clipboard 
Print this post

Many ways to do it. Using PORT is good if pins are in 'blocks' of sequential pins. If not then another way is (using your pin numbers):

xx=pin(15)*128
xx=xx+pin(16)*64
xx=xx+pin(17)*32
xx=xx+pin(18)*16
xx=xx+pin(21)*8
xx=xx+pin(22)*4
xx=xx+pin(23)*2
xx=xx+pin(24)

WW




For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 09:23pm 16 Sep 2015
Copy link to clipboard 
Print this post

  Quote  Does it read pins in as well? I'm just reading pins from two banks of 4x switches.


These is a PORT command (output) and a PORT function (input). Look in the function section of the manual for details of the input version
 
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 09:32pm 16 Sep 2015
Copy link to clipboard 
Print this post

I did read that part in the manual but didn't quite understand it.


[code]
DIM INTEGER Init_Number = port(24,-4,18,-4)
[/code]

Bunching commands together doesn't always seem to work. Or doesn't it take negitive numbers?

  WhiteWizzard said   Many ways to do it. Using PORT is good if pins are in 'blocks' of sequential pins. If not then another way is (using your pin numbers):

xx=pin(15)*128
xx=xx+pin(16)*64
xx=xx+pin(17)*32
xx=xx+pin(18)*16
xx=xx+pin(21)*8
xx=xx+pin(22)*4
xx=xx+pin(23)*2
xx=xx+pin(24)

WW

That will work, but I thought maybe there was a way to do it on one line of code. :)Edited by B15HOP 2015-09-18
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9066
Posted: 09:42pm 16 Sep 2015
Copy link to clipboard 
Print this post

I just use N=PORT(1,4) or N=PORT(1,4,61,4)(for the 64-pin MM+)

The first example will read pin1,pin2,pin3 and pin4, and save the binary value of the combined pins in variable N, and the 2nd version will read an 8-bit binary number on pins 1-4 and pins 61-64 on the MM+, and save the value in N.

You make reference to -4, which is illegal I think?

EDIT:

SetPin 15, din, pulldown
SetPin 16, din, pulldown
SetPin 17, din, pulldown
SetPin 18, din, pulldown

SetPin 21, din, pulldown
SetPin 22, din, pulldown
SetPin 23, din, pulldown
SetPin 24, din, pulldown


...in your case would become:

N=PORT(15,4,21,4)

Variable 'N' will contain the value of all 8-bits of your two switch banks. Edited by Grogster 2015-09-18
Smoke makes things work. When the smoke gets out, it stops!
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 09:45pm 16 Sep 2015
Copy link to clipboard 
Print this post

  Quote  DIM INTEGER Init_Number = port(24,-4,18,-4)


Try:
DIM INTEGER Init_Number = port(21,4,15,4)
 
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 09:50pm 16 Sep 2015
Copy link to clipboard 
Print this post

  Grogster said   I just use N=PORT(1,4) or N=PORT(1,4,61,4)(for the 64-pin MM+)

The first example will read pin1,pin2,pin3 and pin4, and save the binary value of the combined pins in variable N, and the 2nd version will read an 8-bit binary number on pins 1-4 and pins 61-64 on the MM+, and save the value in N.

You make reference to -4, which is illegal I think?

EDIT:

SetPin 15, din, pulldown
SetPin 16, din, pulldown
SetPin 17, din, pulldown
SetPin 18, din, pulldown

SetPin 21, din, pulldown
SetPin 22, din, pulldown
SetPin 23, din, pulldown
SetPin 24, din, pulldown


...in your case would become:

N=PORT(15,4,21,4)

Variable 'N' will contain the value of all 8-bits of your two switch banks.


  matherp said  
  Quote  DIM INTEGER Init_Number = port(24,-4,18,-4)


Try:
DIM INTEGER Init_Number = port(21,4,15,4)


Yeah I just thought it could read the ports backwards but I guess not. Guess it doesn't matter if I flip the breadboard around to read them upside down. :)
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9066
Posted: 09:53pm 16 Sep 2015
Copy link to clipboard 
Print this post

Ahhhhh - I see what you mean by the -4 now. No, pins must be numbered consecutively to use the PORT function. I will watch this thread, and see if anyone comes up with a brilliant solution - someone usually does.
Smoke makes things work. When the smoke gets out, it stops!
 
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 10:03pm 16 Sep 2015
Copy link to clipboard 
Print this post

  Grogster said   Ahhhhh - I see what you mean by the -4 now. No, pins must be numbered consecutively to use the PORT function. I will watch this thread, and see if anyone comes up with a brilliant solution - someone usually does.


Both techniques definitely work. The function just can't take negitive values so Mr Wizzard's option is the solution for now. But I really do like having just one line of code. :)
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8592
Posted: 10:22pm 16 Sep 2015
Copy link to clipboard 
Print this post

You can go:

DIM INTEGER Init_Number = port(24,1,23,1,22,1,18,1,17,1,16,1,15,1)

or any other order to get what you want

 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2794
Posted: 10:25pm 16 Sep 2015
Copy link to clipboard 
Print this post

  B15HOP said  . . so Mr Wizzard's option is the solution for now. But I really do like having just one line of code. :)

xx=pin(15)*128 +pin(16)*64+pin(17)*32+pin(18)*16+pin(21)*8+pin(22)*4+pin(23)*2+pin(24)
For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
B15HOP
Newbie

Joined: 11/09/2015
Location: Australia
Posts: 15
Posted: 06:23am 03 Oct 2015
Copy link to clipboard 
Print this post

Cheers for your help guys. Appreciate it a lot. :)
 
Print this page


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

© JAQ Software 2024