Cheack pin state

Status
Not open for further replies.

Prigo87

New member
Hello,

I work with teensy 4.0, and I have a pin (Pin number 1 on the board) which output 50% duty-cycle PWM at 1Mhz. This clock is produced using these 2 rows at the setup:

analogWriteFrequency(1, 1000000);
analogWrite(1, 128);

I want to synchronize other signals to this clock and therefore I need to detect when there is a rising edge on that pin. How can I detect the rising edge? I tried using:

digitalReadFast(1)

but it's not working.
which register should I check for this pin state (let's assume i check the state inside a while loop so I check fast enough so the first time I detect HIGH state is close enough to the rising edge time)?

Thank you,
Amit Prigozin
 
I thought about that too but I am a little short on pins so I prefer to avoid such a solution.

There must be a way to read directly the register itself.
Pin 1 is pad M11 on the chip which define as GPIO_AD_B0_02.
I know that the register GPIOx_DR holds the data for the current state of the pin but I am not sure which GPIO is it. according to the datasheet, it should be GPIO1.IO[2] but I don't see any change in that register at all.

Any help will be very appreciated here.
 
On Teensy 4.0, PWM on pin 1 is controlled by FlexPWM1.0. Details here:

https://www.pjrc.com/teensy/td_pulse.html

Those FlexPWM timers have 6 registers called VAL0 to VAL5, and 3 outputs, called A, B and X. Pin 1 is the X output, which is controlled by the VAL0 register. The VAL1 register is the counter. So if you rapidly read both of those registers, you can compare them to tell whether the timer count (VAL1) was before or after the point were it changes the pin (VAL0).

The exact register names should be FLEXPWM1_SM0VAL0 and FLEXPWM1_SM0VAL1.

And just to reinforce the correct advise already given, each pin has a mux which configures which peripheral gets access to the pin. GPIO is considered a peripheral, just like timers, serial ports and everything else. When the pin mux gives a timer access to the pin, it can't be accessed by GPIO or any of the other peripherals.
 
Status
Not open for further replies.
Back
Top