My first post and asking for help already. I'm new to all this arduino and Blue Pill stuff. Had to learn a lot to solve a problem.
What I'm trying to do is count the number of pulses every second for 5ms. I'm using counters for time interrupts and for counting the number of pulses.
I have checked with my oscilloscope that the board gets the pulses at the proper pin. When copilling I get no errors, but the serial shows 67 pulses counted while there should be more than 200 (I'm feeding it a 44100 Hz signal). Moreover it "counts" those 67 pulses with no signal attached. Every now and then it shows 85 counted pulses. I could use some help.
Cheers, Domagoj
Here''s the code:
Code: Select all
/*
Pulses counter
*/
#define COUNT_RATE 1000000 // in microseconds; gives 1Hz toggles
#define COUNT_TIME 5000 // in microseconds; gives 5ms count time
uint16_t timer3CH1CounterValue;
void setup()
{
Serial.begin(115200);
// setup PA6 (Timer3 channel 1) as input (capture input mode)
pinMode(PA6, INPUT);
// setup timer 2 interrupt after COUNT_RATE microseconds
Timer2.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
Timer2.setPeriod(COUNT_RATE); // in microseconds
Timer2.setCompare(TIMER_CH1, 1); // Overflow
Timer2.attachInterrupt(TIMER_CH1, count_pulses);
}
void loop()
{
}
void count_pulses(void) // configure timers, starts counting
{
// stop the timers before configuring them
Timer3.pause();
Timer4.pause();
// setup timer 3 channels 1 and 2 capture on rising edge
Timer3.setMode(TIMER_CH1, TIMER_INPUT_CAPTURE);
Timer3.refresh(); // reset the counter to 0, update configuration
Timer3.getCompare(TIMER_CH1); // clear capture flag NEEDED?
// setup timer 4 interrupt after COUNT_TIME microseconds
Timer4.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);
Timer4.setPeriod(COUNT_TIME); // in microseconds
Timer4.setCompare(TIMER_CH1, 1); // Overflow
Timer4.attachInterrupt(TIMER_CH1, stop_count);
Timer4.refresh(); // reset the counter to 0, update configuration
// start the timers 3 and 4
Timer3.resume();
Timer4.resume();
}
void stop_count(void) // stops counting, displays number of counts
{
// get the number of counts
timer3CH1CounterValue = Timer3.getCount();
// stop the timers
Timer3.pause();
Timer4.pause();
// display the number of counts
Serial.print("Count 1: ");
Serial.println(timer3CH1CounterValue);
}
[code]