Home  |  Contents 
Microcontroller and PC projects
  Forum Index : Microcontroller and PC projects         Section
Subject Topic: MMBasic with input pins Post ReplyPost New Topic
Page of 2 Next >>
Author
Message << Prev Topic | Next Topic >>
B15HOP
Newbie
Newbie


Joined: 11 September 2015
Location: Australia
Online Status: Offline
Posts: 15
Posted: 17 September 2015 at 7:02am | IP Logged Quote B15HOP

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.


Back to Top View B15HOP's Profile Search for other posts by B15HOP
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 1510
Posted: 17 September 2015 at 7:05am | IP Logged Quote matherp

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
Back to Top View matherp's Profile Search for other posts by matherp
 
B15HOP
Newbie
Newbie


Joined: 11 September 2015
Location: Australia
Online Status: Offline
Posts: 15
Posted: 17 September 2015 at 7:15am | IP Logged Quote B15HOP

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.
Back to Top View B15HOP's Profile Search for other posts by B15HOP
 
WhiteWizzard
Guru
Guru


Joined: 05 April 2013
Location: United Kingdom
Online Status: Offline
Posts: 2245
Posted: 17 September 2015 at 7:22am | IP Logged Quote WhiteWizzard

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.org
Back to Top View WhiteWizzard's Profile Search for other posts by WhiteWizzard Visit WhiteWizzard's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 1510
Posted: 17 September 2015 at 7:23am | IP Logged Quote matherp

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
Back to Top View matherp's Profile Search for other posts by matherp
 
B15HOP
Newbie
Newbie


Joined: 11 September 2015
Location: Australia
Online Status: Offline
Posts: 15
Posted: 17 September 2015 at 7:32am | IP Logged Quote B15HOP

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



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


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

WhiteWizzard wrote:
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 on 17 September 2015 at 7:39am
Back to Top View B15HOP's Profile Search for other posts by B15HOP
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 4491
Posted: 17 September 2015 at 7:42am | IP Logged Quote Grogster

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 on 17 September 2015 at 7:44am


__________________
Smoke makes things work. When the smoke gets out, it stops!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 1510
Posted: 17 September 2015 at 7:45am | IP Logged Quote matherp

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


Try:
DIM INTEGER Init_Number = port(21,4,15,4)
Back to Top View matherp's Profile Search for other posts by matherp
 
B15HOP
Newbie
Newbie


Joined: 11 September 2015
Location: Australia
Online Status: Offline
Posts: 15
Posted: 17 September 2015 at 7:50am | IP Logged Quote B15HOP

Grogster wrote:
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 wrote:
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. :)
Back to Top View B15HOP's Profile Search for other posts by B15HOP
 
Grogster
Guru
Guru
Avatar

Joined: 31 December 2012
Location: New Zealand
Online Status: Offline
Posts: 4491
Posted: 17 September 2015 at 7:53am | IP Logged Quote Grogster

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!
Back to Top View Grogster's Profile Search for other posts by Grogster Visit Grogster's Homepage
 
B15HOP
Newbie
Newbie


Joined: 11 September 2015
Location: Australia
Online Status: Offline
Posts: 15
Posted: 17 September 2015 at 8:03am | IP Logged Quote B15HOP

Grogster wrote:
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. :)
Back to Top View B15HOP's Profile Search for other posts by B15HOP
 
matherp
Guru
Guru


Joined: 11 December 2012
Location: United Kingdom
Online Status: Offline
Posts: 1510
Posted: 17 September 2015 at 8:22am | IP Logged Quote matherp

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

Back to Top View matherp's Profile Search for other posts by matherp
 


Page of 2 Next >>
In the news...
 
Post ReplyPost New Topic
Printable version Printable version
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum

Powered by Web Wiz Forums version 7.8
Copyright ©2001-2004 Web Wiz Guide

This page was generated in 0.0781 seconds.
Privacy Policy     Process times : 0, 0.02, 0, 0.06