Advertisement
honey_the_codewitch

Fast GPIO esp32

Dec 17th, 2021
1,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. ///////////////////////////
  2. // SETTING A SINGLE PIN  //
  3. ///////////////////////////
  4. // if "pin" is a compile time constant the if statement will be compiled out.
  5. if(pin>31) {
  6.     // set this pin by writing the bit for the pin
  7.     // to the high bank
  8.     GPIO.out1_w1ts.val = (1 << ((pin - 32)&31));
  9. } else if(pin>-1) {
  10.     // set the pin in the low bank
  11.     GPIO.out_w1ts = (1 << (pin &31));
  12. }
  13.  
  14. ///////////////////////////
  15. // CLEARING A SINGLE PIN //
  16. ///////////////////////////
  17. // if "pin" is a compile time constant the if statement will be compiled out.
  18. if(pin>31) {
  19.     // clear this pin by writing the bit for the pin
  20.     // to the high bank
  21.     GPIO.out1_w1tc.val = (1 << ((pin - 32)&31));
  22. } else if(pin>-1) {
  23.     // clear the pin in the low bank
  24.     GPIO.out_w1tc = (1 << (pin &31));
  25. }
  26. /////////////////////////
  27. // SET/CLEAR MANY PINS //
  28. /////////////////////////
  29.  
  30. To do this you simply combine the flags you want to set or clear into one 32 bit value
  31. GPIO.out_w1ts = (1<<pin_d0)|(1<<pin_d1)||(1<<pin_d6); // set pin_d0, pin_d1, and pin_d6
  32. // above assumes those pins are less than GPIO 32
  33. // And the same with clearing them
  34.  
  35. /////////////////////////
  36. // NOTES               //
  37. /////////////////////////
  38.  
  39. // This can be too fast sometimes if you use it to control timing sensitive components.
  40. // I workaround that by setting/clearing multiple times instead of just once
  41. //
  42. // Even for a single pin this is much faster than digitalWrite for some reason
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement