I have this Interrupt Service Routine in my project that flips pin 11 in my Arduino UNO, as you can see it uses the typical and slowest method of digitalWrite, I know that there is a faster way to flip the pin, by directly manipulating the pin on its port, I assume it is port B, can someone please advice me with the line of code to replace my code but will perform the digitalWrite much fatser
Well not from that snippet because we have no idea as to what sort of variable you are dealing with. Please post all your code because often the problem is not in the bit you thought it was.
This can look a bit confusing, most of the Arduino documentation pages will tell you PINx is an INPUT register and is READ-ONLY, but if you look at the data sheet for the atmega328, it states that writing a 1 to PINx will toggle the corresponding bit in the OUTPUT register.
@AhmedBahgat
see what the original code of digitalWrite is doing and leave away things you don't need.
The nice thing about the OEM code is, that it doesn't care about which pin is on which port and that makes the code usable on lot of controllers.
my stripped down version:
// I expect that people know what they do with their ports,
// therefore I use a slimmer (and faster) version of digitalWrite
// by noiasca
void digitalWriteFast(uint8_t pin, uint8_t val) {
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out = portOutputRegister(port);
if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}
}
the remaining question is, why the heck do you want a pin toggling in an ISR. "Time saving" for switching on or off a horn sounds so curious for me that I doubt the usage of the ISR isn't the brightest idea anyway.
I couldn't resist and my figures might be wrong but...
digitalWrite will take about 5µs
a single bit port manipulation will take about 0.125µs
The speed of sound is 343 m / s
in 0,125µs the sound of the horn will travel 0,042 mm
in 5µs the sound of the horn will travel 1,715 mm
so moving the horn by less then 2mm towards the hearing position
will gain you a faster hearable noise than messing around with direct port manipulation
The reason I want it to toggle in ISR, I treied to toggle it in my loop using millis() to toggle the car horn on/off every half second, based on a condition of course, and it just does not time right all the times especially if I try to use some external hardware while in the moment the horn is on, this causes delay for the on period and it is really annoying, So I believe the best course of action is to use ISR and it working much better but I still need to speed it as much as I can because my sketch is about 2000 lines
Yes, I certainly did read everything, I am a good reader, but at the same time I only wanted a single line of code to get me going quickly for now and I got it from ChhatGPT
I agree, however I only wanted it for UNO and will not be used on another controller
I actually tried your code and it is working but I honestly needed just a single line of code for now and without adding more libraries as I already added a lot