Ext interrupt pulse measurement

Post your cool example code here.
dangthaison1992
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Ext interrupt pulse measurement

Postby dangthaison1992 » Sun Nov 27, 2016 9:58 am

I try the code ext interrupt pulse measurement on PA8 at STM32F103C8T6 chip. it's ok

Code: Select all

#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++;
}

User avatar
RogerClark
Posts: 5465
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Ext interrupt pulse measurement

Postby 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
Posts: 974
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Ext interrupt pulse measurement

Postby zmemw16 » Mon Nov 28, 2016 1:38 am

@dangthaison1992
out of curiosity
any particular reason you're using Serial1?

srp

dangthaison1992
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Re: Ext interrupt pulse measurement

Postby 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
Posts: 974
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: Ext interrupt pulse measurement

Postby zmemw16 » Mon Nov 28, 2016 1:59 am

your output text is via Serial1, i just wondered why?
srp

dangthaison1992
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Re: Ext interrupt pulse measurement

Postby 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
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Re: Ext interrupt pulse measurement

Postby 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
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Re: Ext interrupt pulse measurement

Postby 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.

User avatar
RogerClark
Posts: 5465
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Ext interrupt pulse measurement

Postby RogerClark » Mon Nov 28, 2016 2:56 am

Thanks...


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

dangthaison1992
Posts: 6
Joined: Sun Nov 27, 2016 9:28 am

Re: Ext interrupt pulse measurement

Postby 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


Return to “Code snipplets”

Who is online

Users browsing this forum: No registered users and 1 guest