With a Arduino Mega, I want to send a "1" to digital output, say D4.
The "D" will be static. But the port number needs to change.
For example I will have a loop where I increase the integer value of PIN,and send 0 or 1 in a digital write.
In the following statement, for a digital output, the variable "PIN" is an integer.
DigitalOutput = STRING ('D' + PIN) FC is ok with the ststement, but it prints "69"
Any help is paareciated.
Creating a string to be used in a digital output statement
-
Billduck1302
- Posts: 35
- http://meble-kuchenne.info.pl
- Joined: Fri Dec 06, 2024 4:41 pm
- Has thanked: 2 times
- Been thanked: 1 time
-
mnfisher
- Valued Contributor
- Posts: 1884
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 887 times
Re: Creating a string to be used in a digital output statement
The easiest way to do this is with a little C.
For example for port d (assuming the pin is set as an output)
PORTD |= (1 << FCL_PIN);
Will turn on 'pin' without affecting other pins in port d
Martin
For example for port d (assuming the pin is set as an output)
PORTD |= (1 << FCL_PIN);
Will turn on 'pin' without affecting other pins in port d
Martin
-
mnfisher
- Valued Contributor
- Posts: 1884
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 887 times
Re: Creating a string to be used in a digital output statement
I had a little play with this. I used an Arduino Uno - however the code will run as is on an Mega.
I created a macro SetPin(port, pin, on) - that takes a port ('B', 'C' etc) - this calculates the address of the port register and writes the required value to set the pin on or off. No check is made that the port (or indeed pin) is valid.
I use pin 11 (B3) - and have 3 possible methods to output in a loop - one can be selected by enabling / disabling the relevant macros.
I compare a Flowcode output statement (note this is hardcoded to a pin), the SetPin macro and inline C code. The port / pin for the latter two are set using (local to main) variables port and pin.
The possible 'fly in the ointment' here is speed - the macro is probably easiest to understand, and most flexible. However it is slowest at ~3.16uS. Calling the macro adds an overhead.
Next - the output block. Here the code is created at compile time (set_pin is a C macro) - and set high is quicker than set low (which requires an extra instruction) - 750/560nS)
Quickest is the inline code (630/430ns) - however note that the FC output also sets the pin as an output - and I have macros SetPinOutput(port, pin) (set an individual pin as output) and SetPortOutput(port) - to set either an individual pin or a whole port as output.
Interestingly - because of the left shift (<<) higher bits are slightly slower (cf pin 0 and pin 7!) - if consistent speed was a requirement - a lookup table of the bit pattern for each pin could be used. I've not tested this - left as an exercise for the reader
Martin
I created a macro SetPin(port, pin, on) - that takes a port ('B', 'C' etc) - this calculates the address of the port register and writes the required value to set the pin on or off. No check is made that the port (or indeed pin) is valid.
I use pin 11 (B3) - and have 3 possible methods to output in a loop - one can be selected by enabling / disabling the relevant macros.
I compare a Flowcode output statement (note this is hardcoded to a pin), the SetPin macro and inline C code. The port / pin for the latter two are set using (local to main) variables port and pin.
The possible 'fly in the ointment' here is speed - the macro is probably easiest to understand, and most flexible. However it is slowest at ~3.16uS. Calling the macro adds an overhead.
Next - the output block. Here the code is created at compile time (set_pin is a C macro) - and set high is quicker than set low (which requires an extra instruction) - 750/560nS)
Quickest is the inline code (630/430ns) - however note that the FC output also sets the pin as an output - and I have macros SetPinOutput(port, pin) (set an individual pin as output) and SetPortOutput(port) - to set either an individual pin or a whole port as output.
Interestingly - because of the left shift (<<) higher bits are slightly slower (cf pin 0 and pin 7!) - if consistent speed was a requirement - a lookup table of the bit pattern for each pin could be used. I've not tested this - left as an exercise for the reader
Martin
- Attachments
-
- pins.fcfx
- (11.31 KiB) Downloaded 10 times