Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 23:44 25 Apr 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 : PORT command/function

     Page 1 of 2    
Author Message
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 06:35am 01 Dec 2022
Copy link to clipboard 
Print this post

Really liking this....Great job, Pete  

Not able to prove it for myself, atm and I'm 99.9% sure that I have this right but if I limit my read to 8 bits, I can do this?:

print #2, chr$(port(gp2,8))+chr$(port(gp10,4));



Craig
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 08:34am 01 Dec 2022
Copy link to clipboard 
Print this post

difficult without some idea of what you are trying to do but as pure MMBasic, a string of 2 characters formed as 8 consecutive bits from GP2 and 4 consecutive bits from GP10... looks OK - again without trying it.

I'd be wary printing characters outside the range 31<=n=>127 (your second PORT statement guarantees this) but it entirely depends on your application.
Edited 2022-12-01 18:42 by CaptainBoing
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3519
Posted: 09:07am 01 Dec 2022
Copy link to clipboard 
Print this post

CaptainBoing,

This would translate to sending 2 groups of 6 bits, mapped to characters 64..127

print #2, chr$(64+port(gp2,6))+chr$(64+port(gp8,6));


This would ensure it is still 2 characters, and does not violate any special characters in the PRINT and serial routines...

Volhout
PicomiteVGA PETSCII ROBOTS
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3655
Posted: 09:13am 01 Dec 2022
Copy link to clipboard 
Print this post

Should work.

If you're sending the chars down a comms line then should be no problems.

John
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 09:33am 01 Dec 2022
Copy link to clipboard 
Print this post

Yeah, I have 12 inputs and I need to efficiently send their states to another MCU where the individual bits are evaluated. No problems re: printing, etc.  
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8578
Posted: 09:49am 01 Dec 2022
Copy link to clipboard 
Print this post

Check BIN2STR$
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 09:50am 01 Dec 2022
Copy link to clipboard 
Print this post

  Tinine said  I have 12 inputs and I need to efficiently send their states to another MCU


good to go I'd say. Nice solution
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8578
Posted: 10:01am 01 Dec 2022
Copy link to clipboard 
Print this post

Check the manual

  Quote  I/O pins used must be numbered consecutively and any I/O
pin that is invalid or not configured as an input will cause an error.
You need to split the function up more to miss the GND pins

e.g. print #2,BIN2STR$(UINT16,PORT(gp2,4,gp6,4,gp10,4));

will send a 16-bit binary value and all the pins will be read simultaneously
Edited 2022-12-01 20:03 by matherp
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 10:33am 01 Dec 2022
Copy link to clipboard 
Print this post

  matherp said  Check the manual

  Quote  I/O pins used must be numbered consecutively and any I/O
pin that is invalid or not configured as an input will cause an error.
You need to split the function up more to miss the GND pins

e.g. print #2,BIN2STR$(UINT16,PORT(gp2,4,gp6,4,gp10,4));

will send a 16-bit binary value and all the pins will be read simultaneously


Whoa! I had no idea that a GND would be evaluated. Glad I asked. Thanks Pete  
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3519
Posted: 12:12pm 01 Dec 2022
Copy link to clipboard 
Print this post

Woaw.... I need to digest this a bit....

#1: the bin2str$ will send character values between 0 and 255. Was it envisioned we should avoid values between 0-32 and 128-255 ? Or is that not necessary ? Can we pass all values through the UART using the print #x and input #x commands ?

#2: @Peter... so you are translating GPxx numbers to pico pin numbers, and from there back to GPxx numbers ???? This is not really clear to me.

I understand from your text that under water (becuase we must skip ground pins) you convert GP2 to pin 4 of the pico, and size 4 (pins 4,5,6,7). But then (deeper under water) you have to translate again to the GPxx to reach the correct IO registers inside the RP2040. Sounds not very logical.

Confusing since Tinine is not using a pico board, but a board that has ground on different pins. And he is/I was/ under the impression that when using GPxx numbers you distance from the board layout of the pico, and use the RP2040 reference.
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8578
Posted: 12:43pm 01 Dec 2022
Copy link to clipboard 
Print this post

BIN2STR$
  Quote  Returns a string containing the binary representation of 'value'.
'type' can be:
INT64 signed 64-bit integer converted to an 8 byte string
UINT64 unsigned 64-bit integer converted to an 8 byte string
INT32 signed 32-bit integer converted to a 4 byte string
UINT32 unsigned 32-bit integer converted to a 4 byte string
INT16 signed 16-bit integer converted to a 2 byte string
UINT16 unsigned 16-bit integer converted to a 2 byte string
INT8 signed 8-bit integer converted to a 1 byte string
UINT8 unsigned 8-bit integer converted to a 1 byte string
SINGLE single precision floating point number converted to a 4 byte
string
DOUBLE double precision floating point number converted to a 8
byte string

  Quote  This function makes it easy to prepare data for efficient binary file I/O or
for preparing numbers for output to sensors and saving to flash memory.
See also the function STR2BIN


As with all the MMBasic family the I/O is based on the physical pin no.s and in the case of the PicoMite this uses the Pico as the reference layout. To make things easier for the user I have allowed the use of GP no.s but these are immediately translated internally to the physical pin no.

Tinine's port function could equally be PORT(4,4,9,4,14,4). PORT(4,6) will give the error "Invalid input pin" as it tries to use GND pin 8 as an input
struct s_PinDef {
int pin;
int GPno;
   char pinname[5];
   uint64_t mode;
   unsigned char ADCpin;
unsigned char slice;
};

const struct s_PinDef PinDef[NBRPINS + 1]={
   { 0, 99, "NULL",  UNUSED  ,99, 99},                                                         // pin 0
   { 1,  0, "GP0",  DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART0TX  | I2C0SDA | PWM0A,99,0},   // pin 1
{ 2,  1, "GP1",  DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM0B ,99,128},        // pin 2
{ 3, 99, "GND",  UNUSED  ,99,99},                                                           // pin 3
{ 4,  2, "GP2",  DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM1A ,99,1},      // pin 4
{ 5,  3, "GP3",  DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM1B ,99,129},     // pin 5
{ 6,  4, "GP4",  DIGITAL_IN | DIGITAL_OUT | SPI0RX| UART1TX  | I2C0SDA | PWM2A ,99,2},   // pin 6
{ 7,  5, "GP5",  DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL | PWM2B ,99,130},     // pin 7
{ 8, 99, "GND",  UNUSED  ,99, 99},                                                          // pin 8
{ 9,  6, "GP6",  DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM3A ,99, 3},   // pin 9
{ 10,  7, "GP7",  DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM3B ,99, 131},    // pin 10
   { 11,  8, "GP8",  DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART1TX  | I2C0SDA | PWM4A ,99, 4}, // pin 11
{ 12,  9, "GP9",  DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL | PWM4B ,99, 132},     // pin 12
{ 13, 99, "GND",  UNUSED  ,99, 99},                                                         // pin 13
{ 14, 10, "GP10",  DIGITAL_IN | DIGITAL_OUT | SPI1SCK | I2C1SDA | PWM5A ,99, 5},   // pin 14
{ 15, 11, "GP11",  DIGITAL_IN | DIGITAL_OUT | SPI1TX | I2C1SCL | PWM5B ,99, 133},       // pin 15
{ 16, 12, "GP12",  DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART0TX | I2C0SDA | PWM6A ,99, 6},   // pin 16
{ 17, 13, "GP13",  DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM6B ,99, 134},     // pin 17
{ 18, 99, "GND", UNUSED  ,99, 99},                                                          // pin 18
{ 19, 14, "GP14",  DIGITAL_IN | DIGITAL_OUT | SPI1SCK | I2C1SDA | PWM7A ,99, 7},     // pin 19
{ 20, 15, "GP15",  DIGITAL_IN | DIGITAL_OUT | SPI1TX | I2C1SCL | PWM7B ,99, 135},   // pin 20

{ 21, 16, "GP16",  DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART0TX | I2C0SDA | PWM0A ,99, 0},   // pin 21
{ 22, 17, "GP17",  DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM0B ,99, 128},     // pin 22
{ 23, 99, "GND",  UNUSED  ,99, 99},                                                         // pin 23
   { 24, 18, "GP18",  DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM1A ,99, 1}, // pin 24
   { 25, 19, "GP19",  DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM1B ,99, 129},   // pin 25
   { 26, 20, "GP20",  DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART1TX| I2C0SDA | PWM2A ,99, 2},    // pin 26
   { 27, 21, "GP21",  DIGITAL_IN | DIGITAL_OUT | UART1RX| I2C0SCL | PWM2B ,99, 130},     // pin 27
{ 28, 99, "GND",  UNUSED  ,99, 99},                                                         // pin 28
{ 29, 22, "GP22",  DIGITAL_IN | DIGITAL_OUT | I2C1SDA| PWM3A ,99, 3},     // pin 29
{ 30, 99, "RUN",  UNUSED  ,99, 99},                                                         // pin 30
   { 31, 26, "GP26",  DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1SCK| I2C1SDA | PWM5A , 0 , 5},// pin 31
   { 32, 27, "GP27",  DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1TX| I2C1SCL | PWM5B , 1, 133},// pin 32
{ 33, 99, "AGND", UNUSED  ,99, 99},                                                         // pin 33
{ 34, 28, "GP28", DIGITAL_IN |DIGITAL_OUT| ANALOG_IN| SPI1RX| UART0TX|I2C0SDA| PWM6A, 2, 6},// pin 34
   { 35, 99, "VREF", UNUSED  ,99, 99},                                                         // pin 35
{ 36, 99, "3V3", UNUSED  ,99, 99},                                                          // pin 36
{ 37, 99, "3V3E", UNUSED  ,99, 99},                                                         // pin 37
{ 38, 99, "GND", UNUSED  ,99, 99},                                                          // pin 38
{ 39, 99, "VSYS", UNUSED  ,99, 99},                                                         // pin 39
{ 40, 99, "VBUS", UNUSED  ,99, 99},                                                         // pin 40
{ 41, 23, "GP23", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL| PWM3B  ,99 , 131},           // pseudo pin 41
{ 42, 24, "GP24", DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART1TX | I2C0SDA| PWM4A  ,99 , 4},   // pseudo pin 42
{ 43, 25, "GP25", DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL| PWM4B  ,99 , 132},          // pseudo pin 43
{ 44, 29, "GP29", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | UART0RX | I2C0SCL | PWM6B, 3, 134},// pseudo pin 44
};

Edited 2022-12-01 22:49 by matherp
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 12:49pm 01 Dec 2022
Copy link to clipboard 
Print this post

@Volhout

This is how I send a 32bit value:


posn = -1356781234
db1 = posn and 255
db2 = posn>>8 and 255
db3 = posn>>16 and 255
db4 = posn>>24 and 255

cksum =&h7+89+ db1+ db2+ db3+ db4 and 255
PRINT#2,chr$(&hAA)+chr$(&h7)+chr$(89)+chr$(db1)+chr$(db2)+chr$(db3)+chr$(db4)+chr$(cksum);



@matherp: So I can streamline this by using INT32? Yay, I love this stuff  

Edit: I meant BIN2STR$(int32.....

Craig
Edited 2022-12-01 23:06 by Tinine
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 03:21pm 01 Dec 2022
Copy link to clipboard 
Print this post

Yup...Very cool indeed  




strt=timer
for i = 1 to 1000
 a$ = bin2str$(int32,&h7fffffff)
next
fini=timer


print fini-strt

for i = 1 to len(a$)

 print asc(mid$(a$,i))

next


 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 03:52pm 01 Dec 2022
Copy link to clipboard 
Print this post

Ayup Craig

as an aside, in your example above as you appear to be measuring how long things take and leaning towards quicker code...

your last loop above, the string slice can be made about 16% faster (if it matters) tweaking it as follows:


for i=1 to len(a$)
 Print Peek(VAR a$,i)
Next


it also begins a journey into the guts of MMBasic variables...

this isn't so important on really fast mites but I saw it and, well, no reason, jus' sayin'

enjoy
Edited 2022-12-02 02:09 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 04:42pm 01 Dec 2022
Copy link to clipboard 
Print this post

Hey Cap'n

Oh I'm all about this kinda thing (need to get a life  ). Is this tip on FOTS?
I go to ridiculous lengths for efficiency...even though I'm running only @126MHz.

Cheers  
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 06:19pm 01 Dec 2022
Copy link to clipboard 
Print this post

it is. scroll right to the bottom

http://www.fruitoftheshed.com/MMBasic.Some-Hints-Tips-for-writing-efficient-code.ashx?HL=peek,var

I do loads of stuff like this to squeeze every ounce of power from the mites I use (170s mostly). Stuff like this got the Winbond flash memory code really quick and got a de/encryption algorithm usable
Edited 2022-12-02 04:21 by CaptainBoing
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 06:50pm 01 Dec 2022
Copy link to clipboard 
Print this post

Oh shoot!


I grabbed a bunch of those Winbond modules after seeing your code but when they arrived, I had forgotten why I ordered them  
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1985
Posted: 08:25pm 01 Dec 2022
Copy link to clipboard 
Print this post

I love them. I even came up with an arrangement to mount them on a full-size SD card shaped pcb so they could be swapped out. probably a bad idea as people might think they are compatible... never did anything with it so no harm done  
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 11:28am 02 Dec 2022
Copy link to clipboard 
Print this post

Hey Pete, they both run without complaining:


SetPin gp2, DIN
SetPin gp3, DIN
SetPin gp4, DIN
SetPin gp5, DIN
SetPin gp6, DIN
SetPin gp7, DIN
SetPin gp8, DIN
SetPin gp9, DIN
SetPin gp10, DIN
SetPin gp11, DIN
SetPin gp12, DIN
SetPin gp13, DIN

a$ = BIN2STR$(UINT16,PORT(gp2,8,gp10,4))
a$ = BIN2STR$(UINT16,PORT(gp2,4,gp6,4,gp10,4))








Edit: Not crossing any GNDs, right?
Edited 2022-12-02 21:32 by Tinine
 
Tinine
Guru

Joined: 30/03/2016
Location: United Kingdom
Posts: 1646
Posted: 11:45am 02 Dec 2022
Copy link to clipboard 
Print this post

Huh? even this runs:


a$ = BIN2STR$(UINT16,PORT(gp2,12))
 
     Page 1 of 2    
Print this page
© JAQ Software 2024