Re: Question about Bluepill arduino using OnePulse mode
Posted: Mon Jan 02, 2023 1:01 pm
Yes I understand and corrected itozcar wrote: Mon Jan 02, 2023 10:58 am I can easily get your previous code to fail - as in stop responding altogether, by just presenting it with more pulses than it expects.
I cannot get it to stop responding if I add this to the handler_channel_1() routine:
I'm not sure exactly what you have done now, but adding stray arrays is not the answer. If you do that, don't be surprised if it starts to fail again as soon as you add something else to it.Code: Select all
void handler_channel_1(void) { //This function is called when channel 1 is captured. if ( index1 > 49 ) return; // prevent overrun of data array if (0b1 & GPIOA_BASE->IDR >> 0) { //If the receiver channel 1 input pulse on A0 is high. ...
I can see that at some time you tried to print the index1 value in the interrupt routine, but that statement has been commented out. So, maybe you found out for yourself that using Serialx.print() is not such a good idea in an interrupt routine, but it seems that you were somehow suspicious of index1.
If you really want to work out what is happening, change the interrupt routine so that it does not just bail out completely when the index gets too great. Instead, when the index is greater than the array size, stop storing the values into the array, but still increment index1. Then print out index1 in loop(). I'll bet you will find that without that change you were running beyond the end of the array (in other words, you will sometimes see values of index1 > 49).
Code: Select all
void handler_channel_1(void) { //This function is called when channel 1 is captured.
if (0b1 & GPIOA_BASE->IDR >> 0) { //If the receiver channel 1 input pulse on A0 is high.
channel_1_start = TIMER2_BASE->CCR1; //Record the start time of the pulse.
TIMER2_BASE->CCER |= TIMER_CCER_CC1P; //Change the input capture mode to the falling edge of the pulse.
}
else { //If the receiver channel 1 input pulse on A0 is low.
channel_1 = TIMER2_BASE->CCR1 - channel_1_start; //Calculate the total pulse time.
if (channel_1 < 0)channel_1 += 0xFFFF; //If the timer has rolled over a correction is needed.
TIMER2_BASE->CCER &= ~TIMER_CCER_CC1P; //Change the input capture mode to the rising edge of the pulse.
DHT11_data[index1++]=channel_1;
if(index1==41)
Timer2.detachInterrupt(TIMER_CH1);
}
}