Hi,
can the arduino mega output on one of two pins based on if the answer is positive or negative? The input pins are connected to an ISR so I think digital or analogWrite would be of no use since they are too slow. Thanks!
Hi,
can the arduino mega output on one of two pins based on if the answer is positive or negative? The input pins are connected to an ISR so I think digital or analogWrite would be of no use since they are too slow. Thanks!
Did you check out Arduino Reference - Arduino Reference ?
What is too slow?
.
it seems the timer functions are too slow
devasri:
it seems the timer functions are too slow
Post your code and tell us the performance you require expressed in microseconds. "Too slow" is meaningless.
Try the digitalWriteFast() library.
...R
this is my code that I need two outputs for, I'm not sure how fast it should be if I am operating at 40k Hz
if (OCR2A<0)
{
// write to pin 10;
}
else if(OCR2A>0)
{
// write to pin 11;
}
What if OCR2A==0?
Does "write to pin 10" also turn off pin 11 if it was previously on?
We need to see more of the code. That code executes in nanoseconds so it is not the source of your problem.
Note that 40kHz is kind of fast for a 16000kHz processor. That does not give you a lot of time to do calculations which may take a few thousand clock cycles. It depends on exactly what you are doing.
40kHz is one every 25 µsecs - which is fast by Arduino standards. digitalWrite() takes about 8 µsecs IIRC but digitalWriteFast() is nearly as quick as port manipulation - just a few instructions so maybe 0.25 µsecs
It would be easy to write a short test program to measure how long the instructions take. Just take the time for 10,000 or 20,000 using micros().
...R
this is the code
if its 0 I will output to pin 11 as well
#include <avr/io.h>
#include <avr/interrupt.h>
void setup() {
//Serial.begin(9600);
pinMode(2, INPUT); //set pins 2 and 3 as inputs and 10 and 11 as output
pinMode(3, INPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
cli(); // disable all interrupts
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); //set up timer2 to generate a PWM
TCCR2B = _BV(CS20); //set prescalar value to 1
TCCR1A = 0;
TCCR1B = _BV(CS20);
TCCR1B |= (1 << CS12); //256 values
TCNT2 = 0;
sei(); //enable all interrupts
attachInterrupt(0, t1_ISR, RISING); //enable interrupt service routines on pins 2 and 3
attachInterrupt(1, t2_ISR, RISING);
// Serial.print(OCR2A);
}
uint8_t t1, t2;
void loop() { //compare time that one signal goes high to time the other signal goes high
if(t2>t1){
OCR2A = (t2-t1); //output PWM based on phase difference on pin 11
}
if(t2<t1){
OCR2A = (t1-t2); //output PWM on pin 11
}
}
if (OCR2A<0)
{
pin 10;
}
else if(OCR2A>=0)
{
pin 11;
}
void t1_ISR() { //interrupt if signal on pin 2 goes high and store counter
t1 = TCNT2; //value to variable t1
}
void t2_ISR() { //interrupt if signal on pin 3 goes high and store counter
t2 = TCNT2; //value to variable t2
}
And what is wrong with analogWrite()?
wouldn't analogWrite be too slow to operate at .25 us? or 40 kHz
Normally there's no reason to change the standard frequency that analogWrite() uses. It is something that is done infrequently, mostly because the standard frequency is within the range of hearing and you want to stop the motor making the whining noise.
Look up the online resources on changing the analogWrite() frequency.
However, in this application, I fail to see why that frequency is important. This website may help: www.xyproblem.info
Do you really want to treat the register OCRA2 as a signed 8 bit integer?
if (OCR2A<0)
devasri:
wouldn't analogWrite be too slow to operate at .25 us? or 40 kHz
Maximum PWM frequency being 4 MHz (albeit at just 2-bit resolution) doing a 40 kHz PWM should be no problem. More tricks here.
If the output should be digital will analogwrite work? Can you link me to where does it say it has max frequency of 4 MHz? I couldn’t find that number when I looked
analogWrite() produces PWM (look up the manual).
Link is already in my post #14.
It does produce pwm but it doesn’t say anything about 4 MHz, or I don’t see it
My first link was specifically about the maximum possible PWM rate. The second a bit more general about PWM.
Ohh, now I see. I’m sorry, my link didn’t work