Read PWM Signal GND-controlled

Status
Not open for further replies.

thomas7

Member
Hello,

i have question regarding PWM reading. I know, there are so many examples and documentations in the web but i have something special.
Ok, for me it sounds special :)

I have a PWM Signal which is controlled in 200KHz but by ground.

How should i do that when i want to read the pwm value which will be set to gnd?

I have a teensy 3.2 and connect the GND to Pin 2. (Or do i need a resistor to prevent a short circuit when PIN is HIGH and will be connected to GND by PWM?)
Do i have to set the Pin 2 to HIGH(INPUT_PULLIP) and read the time of the falling edge?

Is that the correct way?

Code:
volatile int pwm_value = 0;
volatile int prev_time = 0;
 
void setup() {
  Serial.begin(115200);
  
  pinMode(2,INPUT_PULLUP);
  
  attachInterrupt(2, falling, FALLING);
}
 
void loop() { }
 
void rising() {
  attachInterrupt(2, rising, RISING);
  prev_time = micros();
}
 
void falling() {
  attachInterrupt(0, falling, FALLING);
  pwm_value = micros()-prev_time;
  Serial.println(pwm_value);
}


I did not complete the wiring for testing, and i still need to understand a lot of the PWM but if i know the frequency of 200KHz, how do i output the value from 0% to 100%?
Add a timer, count the values in one second and map it?
Then i just know the value every second, is there a way to have it faster?

Thanks a lot, for the most here the questions may sound stupid but for me PWM is new.

Regards
Thomas
 
I have no idea what you mean by "but by ground".

If this is a really special type of signal, maybe you could be more specific with a diagram or oscilloscope waveform?
 
I can only guess that Thomas means an inverted PWM signal, which is active low (ground). In that case, the duty cycle would simply be calculated as 100% * t_low / (t_low + t_high) instead of the "normal" 100% * t_high / (t_low + t_high).
 
Status
Not open for further replies.
Back
Top