Ext interrupt pulse measurement

dangthaison1992
Sun Nov 27, 2016 9:58 am
I try the code ext interrupt pulse measurement on PA8 at STM32F103C8T6 chip. it’s ok
#include "HardwareTimer.h"
volatile int state = LOW;
volatile int HighToLowCount = 0;
volatile int LowToHighCount = 0;
volatile int val;
volatile int CurrentCount;
volatile int Period;
volatile int OldCount;
volatile int PulseWidth;
volatile int OverflowCount;
volatile int timeroverflow;

HardwareTimer timer(1);

void setup()
{
Serial1.begin(9600);
pinMode(PA8, INPUT);
attachInterrupt(0, exti_handler, CHANGE);

CurrentCount = 0;
OldCount = 0;
Period = 0;
PulseWidth = 0;
OverflowCount = 0;
timeroverflow = 0;

// Setup Counting Timers
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
timer.pause();
timer.setCount(0);
timer.setPrescaleFactor(72); //run at 72 MHz/72 = 1MHz count every 1us
timer.setOverflow(65535);
timer.setCompare1(1);
timer.attachCompare1Interrupt(timer_handler);
timer.refresh();
timer.resume();
}

void loop()
{
Serial1.print("LowToHighCount: ");
Serial1.print(LowToHighCount);
Serial1.print(" HighToLowCount: ");
Serial1.print(HighToLowCount);
Serial1.print(" Period: ");
Serial1.print(Period);
Serial1.print(" PulseWidth: ");
Serial1.print(PulseWidth);
Serial1.print(" Overflow: ");
Serial1.println(OverflowCount);
delay(1000);
}

void exti_handler()
{
timer.pause();
CurrentCount = timer.getCount();
val = digitalRead(0);

if (val == HIGH)
{
LowToHighCount++;
Period = CurrentCount;
timer.setCount(0);
timer.refresh();
}
else { // state must be LOW
HighToLowCount++;
PulseWidth = CurrentCount;
}

timer.resume();
}

void timer_handler()
{
PulseWidth = 0;
OverflowCount = 0;
timeroverflow++;
}


RogerClark
Sun Nov 27, 2016 9:09 pm
Thanks for sharing.

Looking at the code, I think it measures pulse width, of both the high and low parts of a waveform?
With the value in microseconds

Can I add this code to the examples in the repo?


zmemw16
Mon Nov 28, 2016 1:38 am
@dangthaison1992
out of curiosity
any particular reason you’re using Serial1?

srp


dangthaison1992
Mon Nov 28, 2016 1:53 am
I used analogWrite(5, value); on arduino mega2560 for PWM and I measure the pulse pin PA8 on Generic STM32F103c8t6. it low noise more than use pulseIn(PA8, High, 5000);

Used ext interrupt pulse measurement
Image

Used pulseIn(PA8, High, 5000);
Image


zmemw16
Mon Nov 28, 2016 1:59 am
your output text is via Serial1, i just wondered why?
srp

dangthaison1992
Mon Nov 28, 2016 2:04 am
@dangthaison1992
out of curiosity
any particular reason you’re using Serial1?

I used usart1 PA9 TX -> pin0, PA10 RX ->pin1 of board arduino uno


dangthaison1992
Mon Nov 28, 2016 2:15 am
your output text is via Serial1, i just wondered why?
srp

i designed board. I used usart1 view data

Image
Image


dangthaison1992
Mon Nov 28, 2016 2:18 am
Thanks for sharing.

Looking at the code, I think it measures pulse width, of both the high and low parts of a waveform?
With the value in microseconds

Can I add this code to the examples in the repo?

yes, you can add this code to the example in the repo.


RogerClark
Mon Nov 28, 2016 2:56 am
Thanks…

BTW. What does the board do ?
(Some sort of power controller / motor controller ?)


dangthaison1992
Mon Nov 28, 2016 3:17 am
Thanks…

BTW. What does the board do ?
(Some sort of power controller / motor controller ?)

The board is PID motor controller. I working the project pid driver motor use Arduino_STM32

Image


RogerClark
Mon Nov 28, 2016 3:22 am
Wow.

Looks like its heavy duty


miki
Tue Dec 13, 2016 9:34 am
I made a similar code for a PPM signal decoder with 0.5us resolution .It works like a charm.

zmemw16
Tue Dec 13, 2016 1:16 pm
i do like those power lines :D

istr a discussion and quite a debate somewhere about the effectiveness and the amount of solder actually required, where oh where ?

this google search >effect adding solder to power traces tracks pcb< returns a few.

stephen


Leave a Reply

Your email address will not be published. Required fields are marked *