ozcar wrote: Tue Dec 27, 2022 11:31 am
Show us what you tried, and in what way did it not work?
I am using Timer2 channel1 (PA0) to drive the DHT11 sensor, saving CPU resources for computationally intensive work. DHT11 needs 18ms low-level trigger, I need to use the OnePulse mode of the timer to generate a pulse first, and then quickly turn the timer into capture mode to capture the return pulse of DHT. I have completed the second half of the work of capturing DHT11 signals, but the previous OnePulse is not working properly. Below is my activation code
```
void Request() /* Microcontroller send request */
{
pinMode(DHT11, PWM);
// digitalWrite(DHT11, LOW);
// delay(18); /* wait for 18ms */
uint16_t pulseDelay = 20000;
uint16_t pulseWidth = 19000;
Timer2.pause(); // stop the timers before configuring them
timer_oc_set_mode(TIMER2, 1, TIMER_OC_MODE_PWM_1, TIMER_OC_PE);
Timer2.setPrescaleFactor(72); // 1 microsecond resolution
Timer2.setOverflow(pulseWidth + pulseDelay-1);
Timer2.setCompare(TIMER_CH1, pulseDelay);
// counter setup in one pulse mode, as slave triggered by External input for Timer 2
TIMER2_BASE->CR1 = ( TIMER_CR1_OPM ); // one pulse mode
TIMER2_BASE->SMCR = ( TIMER_SMCR_TS_ETRF | TIMER_SMCR_SMS_TRIGGER );
TIMER2_BASE->CCER = TIMER_CCER_CC1E ; // enable channels 1 and 2
Timer2.attachInterrupt(TIMER_CH1,handler_channel_1);
Timer2.refresh(); // start timer 2
Timer2.resume(); // let timer 2 run
// digitalWrite(DHT11,HIGH); /* set to high pin */
}
void handler_channel_1(void)
{
// turn the timer into capture mode
}
```