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: 1885
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 888 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: 1885
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 888 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 62 times
Re: Creating a string to be used in a digital output statement
Hi Martin,
Thanks for the code. It helps us in sharpening our C skills. Your are really good in C.
A small out of the context question.
I was trying to simulate ( C-Sim ON) Pins.fcfx in Flowcode V11, I am getting an error message. I am sure this code will work on Hardware.
I am attaching the error message screen shot. please suggest if there is anyway I can simulate this C Code.
.
Thanks for the code. It helps us in sharpening our C skills. Your are really good in C.
A small out of the context question.
I was trying to simulate ( C-Sim ON) Pins.fcfx in Flowcode V11, I am getting an error message. I am sure this code will work on Hardware.
I am attaching the error message screen shot. please suggest if there is anyway I can simulate this C Code.
.
- Attachments
-
- Pins fcfx C sim error.jpg (40 KiB) Viewed 79 times
S_V
-
mnfisher
- Valued Contributor
- Posts: 1885
- Joined: Wed Dec 09, 2020 9:37 pm
- Has thanked: 154 times
- Been thanked: 888 times
Re: Creating a string to be used in a digital output statement
Hi,
Yes - sorry it won't simulate. Of it was useful it could be converted into a component, with separate implementation to do the simulation.
It is also specific to AVR MCUs - although it might be possible to do a similar thing on others (it relies on the port register's having a fixed interval between them)
Martin
Yes - sorry it won't simulate. Of it was useful it could be converted into a component, with separate implementation to do the simulation.
It is also specific to AVR MCUs - although it might be possible to do a similar thing on others (it relies on the port register's having a fixed interval between them)
Martin