In the example code, PORTF is set inside a for loop.
The output makes me think that setting PORTF is asynchronous.
Adding a "delay(1)" after PORTF resolves the asynchronous behavior.
I am using Teensy 2.0. Example code, output, and picture of the setup are below.
What is the best way to guarantee that PORTF is set before using PORTF?
Where can I read more about this?
![port_manipulation.JPG port_manipulation.JPG](https://forum.pjrc.com/data/attachments/1/1082-d9e635f5ec523b0bf4f2e7beb72b5457.jpg)
Thank you.
The output makes me think that setting PORTF is asynchronous.
Adding a "delay(1)" after PORTF resolves the asynchronous behavior.
I am using Teensy 2.0. Example code, output, and picture of the setup are below.
What is the best way to guarantee that PORTF is set before using PORTF?
Where can I read more about this?
Code:
#include <inttypes.h>
#include <Keyboard.h>
void setup()
{
// port B has 8 pins
PORTB = B11111111; // set all port B pins to pullup
// port F has 6 pins
DDRF = B11110011; // set all port F pins to outputs
}
void loop()
{
uint16_t keyCode[4] = { KEY_0, KEY_1, KEY_2, KEY_3 };
const uint8_t FPins[4] = {B00000001, B00000010, B00010000, B00100000};
uint8_t sample;
delay(20);
for (uint8_t pin = 0; pin < 4; pin++)
{
PORTF = ~FPins[pin]; // set active pin to low
//delay(1); // delay resolves asynchronous behavior
sample = ~PINB; // read all pins on port B
if (sample != 0) // if key is pressed
{
Keyboard.press(keyCode[pin]); // print pin number
Keyboard.release(keyCode[pin]);
}
}
}
Code:
Pin number button pressed. Output with delay(1). Output w/o delay(1).
0 0 1
1 1 2
2 2 3
3 3 0 // wrap
![port_manipulation.JPG port_manipulation.JPG](https://forum.pjrc.com/data/attachments/1/1082-d9e635f5ec523b0bf4f2e7beb72b5457.jpg)
Thank you.
Last edited: