Go Down

Topic: count and send pulses simultaneously (Read 666 times) previous topic - next topic

JarvisSong

Hi everyone,

I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

Here is the code:

int encoderCount = 0;
boolean sampledPin;
boolean lastVal=0;

void setup() {
  // put your setup code here, to run once:
pinMode(3,OUTPUT);
pinMode(22,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

while(true)
{
sampledPin = !!(PIOB->PIO_PDSR & (1<<26));

if(lastVal ^ sampledPin)
 {
  encoderCount++;
  lastVal = sampledPin;
  }

if (encoderCount == 3000 )
{
  encoderCount = 0;
 
  PIOC->PIO_SODR=1<<28;
  delayMicroseconds (1000) ;
  PIOC->PIO_CODR=1<<28;
  }
}

}

JarvisSong

Hi everyone,

I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?

Here is the code:

int encoderCount = 0;
boolean sampledPin;
boolean lastVal=0;

void setup() {
  // put your setup code here, to run once:
pinMode(3,OUTPUT);
pinMode(22,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

while(true)
{
sampledPin = !!(PIOB->PIO_PDSR & (1<<26));

if(lastVal ^ sampledPin)
 {
  encoderCount++;
  lastVal = sampledPin;
  }

if (encoderCount == 3000 )
{
  encoderCount = 0;
 
  PIOC->PIO_SODR=1<<28;
  delayMicroseconds (1000) ;
  PIOC->PIO_CODR=1<<28;
  }
}

}

vaj4088

If you want to be able to do something while delaying, do not use delay(...) or delayMicroseconds(...).  Learn to use millis() and micros().

At the top of this forum is a tutorial on how to use millis().  The same principles apply for using micros().


vaj4088

I am puzzled.  What does !! accomplish?  Doesn't this just waste time unless it gets optimized out?

ard_newbie


See the example sketch, Blink without delay.

Speklap

On the due, you can attach an interrupt handler to each pin.

Code: [Select]


void ISR1() {

//rising edge
//count up

}

const uint8_t input = 22;
attachInterrupt(input, ISR1, RISING);

Robin2

#6
May 16, 2018, 10:30 am Last Edit: May 16, 2018, 10:30 am by Robin2
I'm using an Arduino Due to count and send out pulses. The sending out pulses are suppose to last for 1000 microseconds. How do I keep counting the incoming pulses at the same time?
This is confusing.

You say you are sending out pulses and then you say you want to count incoming pulses. Which are you doing -  sending or receiving?

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge


Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What is the application?

Are you counting pulses from an external source and want to gate them ON for a particular number of pulses or time.
OR
Are you producing the pulses in the DUE and gating them ON for a particular number of pulses or time.


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?


Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

horace

have a look at the tc_lib library for the Due

Speklap

crosspost: https://forum.arduino.cc/index.php?topic=547718.msg3733727#msg3733727

Coding Badly


@JarvisSong, please do not cross-post.  Threads merged.


JarvisSong


I have got it working.

Thank you all for your helpful advice.

(Sorry I cross-posted, I'm new to the community)

TomGeorge

I have got it working.

Thank you all for your helpful advice.

(Sorry I cross-posted, I'm new to the community)
To thank us, can you tell us what you did and post your code please.
Thanks..Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up