hi guys
i use arduino UNO to generate a pulse train with high frequency, using the instruction delayMicrosecond (1) it reaches 100kHz, but i need more !!
i have seen somewhere that we can do it working directly on the µcontroller or some thing like that (i'm not a programmer
)
can someone help me to start with this ?
You need more but you don't say how much more. Tough to make a recommendation without a specification. Maybe you can clue us in.
I need between 500kHz and 1 MHz (the ideal for my application is 1MHZ ! )
Well why didn't you say so! I just happen to have a sketch that does that.
Yessss, thank you so much
it works exactly as i dreamed
but we need always more 
In fact my old programme (given as follow) was expected to generate a ten pulse train when i press a pushbutton.
const int bouton = 2; //le bouton est connecté à la broche 2 de la carte Adruino
const int led1 = 11; //la LED à la broche 13
int memoire = LOW; // initialiser l'état du bouton
int courant = 0;
void setup()
{
pinMode(led1, OUTPUT); //la led est une sortie
pinMode(bouton, INPUT); //le bouton est une entrée
}
void loop()
{
//etatBouton = digitalRead(bouton); //Rappel : bouton = 2
int courant = digitalRead(bouton);
if(memoire == LOW && courant == HIGH) //test front montant;
{
for(int i=0; i<=10;i++){
digitalWrite(led1, HIGH);
delay (1);
digitalWrite(led1, LOW);
delay (1);
}
}
memoire = courant;
}
i tried to modify your scripte to do the same with 1MHz but it dosen't work ( because i don't control the assembly-language.!!)
You need to put the stuff to start the timer into your sketch, and you didn't ! That ocra stuff is needed. Did you actually look at Jack's program ??
Yes i do
I tested it and it works, it generates continuously the pulse train
in fact, i don't really understand what the "ocra stuff" means
seddik-ferdjallah:
"ocra stuff"
That code actually sets up one of the hardware timers to output a continuous 1MHz square wave. Once it's set up, it just runs, and doesn't consume any CPU resources at all, which is kind of neat.
So, do I understand the real requirement to be that pushing a button will output 10 cycles only at 1MHz?
Jack,
I tried the "copy to clipboard " option in the link and it copied only the file name. Either to Notepad or IDE.
What did I do wrong?
Since this may be OT here can your reply via PM?
Appreciate it, but no hurry.
Vaclav
Ah ok, it is more clear now, Thank you
yes, that's exactly what i need, but there is a small remark
when i make the condition the pushed button, it continues restarting the generating even the first train is not finished , then i think that it will be better if the condition will be a test of rising edge and not the pushed button !
seddik-ferdjallah:
Ah ok, it is more clear now, Thank you
yes, that's exactly what i need, but there is a small remark
when i make the condition the pushed button, it continues restarting the generating even the first train is not finished , then i think that it will be better if the condition will be a test of rising edge and not the pushed button !
Sounds like that would be what is called a retriggerable situation, although 10 cycles of 1MHz is only 10µs, and most pushbuttons are going to require at least 1000 times that for debounce, so I don't see it happening.
Here is another thread that was a very similar situation, although more cycles at a lower frequency. Still, I don't see why the same approach couldn't be used.
http://forum.arduino.cc/index.php?topic=195800.msg1445115#msg1445115
ah okay, I understand the concept now
I saw your second suggetion, but I definitely want a frequency greater than 500kHz, so I will always use your program for 1MHz and add a delay of 1 second after pressing the button (it doesn't matter if the train start 1 second after pressing the button ), you think it works?
seddik-ferdjallah:
You think it works?
Hard to say without seeing the code. Or sometimes actually trying it helps too
I can't do it =( i really can't understand what does these ( TCCR1A , ICR1 ,OCR1A ...) can do :~ ... can you send me a simple document to learn about this, please !
for the 2nd code you gave me, how can i change the PWM frequency ( more than 20kHz) ?
Thanks in advance
You'll want to learn how to use the timers and also interrupts. This guy's tutorials really helped me on the timers. Have a look at "Newbie's Guide to AVR Interrupts" and "Newbie's Guide to AVR Timers".
Ok, thank you for your help
I'll do my best, I hope to understand
otherwise one last question, is it possible to generate a PRBS signal using Arduino?
PRBS? Pseudorandom binary sequence? I don't know why it wouldn't be possible.
Hello Jack
i'm back to you after a hard week with port and interrupts
basing on your sketch (generate 1 MHz pulse train).
I have written the following code which is supposed to generate ten pulses of 1MHz after detecting a rising edge using an interrupt, the algorithme works correctly but it can't maintain the frequency at 1MHz !! it falls to 100kHz.
( without interrupts and instructions it can reach even more than 4MHz for a continous pulse train )
#define ledPin 11
int long N = 0;
//boolean flag = 0;
int long button = 2;
int long newState = 0;
int long oldState = 0;
static boolean ledState=0;
void setup()
{
//Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT); //le bouton est une entré en pin 2
digitalWrite(ledPin, ledState);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
//TCCR1B |= (1 << CS12); // 256 prescaler
TCCR1B |= (1 << CS10); // 1 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
//interrupts(); // enable all interrupts
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
N = N+1;
if (N <= 20)
{
//digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
digitalWrite(ledPin, ledState = !ledState);
}
}
void loop()
{
newState = digitalRead(button);
if(oldState == LOW && newState == HIGH) //test front montant;
{
interrupts();//on autorise les interruptions
}
oldState = newState ;
}
I can't understand what's the wrong here
please help
Hello
I have written the following code which is supposed to generate ten pulses of 1MHz after detecting a rising edge using interrupts,
in the first time without interrupts and other instructions ( just generate the µcontroller clock) it can reach even more than 4MHz for a continous pulse train.
when i use interrupt to generate just 10 pulses after detecting a rising edge the algorithme works correctly but it can't maintain the frequency at 1MHz !! it falls to 100kHz. I can't understand what's the wrong here (i'm using the Arduino UNO 16Mb so even we say that this delay is due to instructions in the loop , but not that much i think !!!
#define ledPin 11
int long N = 0;
//boolean flag = 0;
int long button = 2;
int long newState = 0;
int long oldState = 0;
static boolean ledState=0;
void setup()
{
//Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(button, INPUT); //le bouton est une entré en pin 2
digitalWrite(ledPin, ledState);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
//TCCR1B |= (1 << CS12); // 256 prescaler
TCCR1B |= (1 << CS10); // 1 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
//interrupts(); // enable all interrupts
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
N = N+1;
if (N <= 20)
{
//digitalWrite(ledPin, digitalRead(ledPin) ^ 1); // toggle LED pin
digitalWrite(ledPin, ledState = !ledState);
}
}
void loop()
{
newState = digitalRead(button);
if(oldState == LOW && newState == HIGH) //test front montant;
{
interrupts();//on autorise les interruptions
}
oldState = newState ;
}
Thnak you
If you want to generate continuous pulses why not use the timer hardware?
Interrupt service routines take time to execute.
I estimate your ISR will take around 5.8 mS to execute which means you could execute around 172000 per second. So your observed 100 kHz is not that far out.
There's no way you could achieve 1 MHz because the ISR would have to enter, do its stuff, and leave, all in 16 clock cycles. With many instructions taking 2 clock cycles this isn't going to happen (the ISR has to push stuff onto the stack to preserve what was happening when the interrupt occurred).