I am not sure if I am correct with my thinking, but I have no experience with Arduino so far, and I would like to generate a TTL signal using onean Arduino.
As far as I understand, a TTL signal is just a predefined controlled signal that repeats at a certain interval (e.g. 2 microsecμs).
An Arduino circuit arguably has a very fast clock (e.g. 16 MHz), so what I thought is I can basically just draw a square signal for whatever desirable interval (important to be down to the microsecond 10^-6).
So I used the following code, just choosing onea pin and changing its signal state between 0 and 1 and then using Serial.println to draw the signal out. Everything is inside a while loop that times everything to be generated exactly within 1 second.
#include <arduino-timer.h> // Variables unsigned long v = -1; int freq = 100.0; // Hz unsigned long d = floor(1.0/freq*1000000.0) ; // us Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution void setup(){ unsigned long repeatEvery = 5000000; // Microseconds Serial.begin(9600); pinMode(3, OUTPUT); timer.every(repeatEvery, func1); // XXXXX microsec } void func1(){ unsigned long uSec = 1000000; // Microseconds (= 1 sec) PORTD = B00000000; v = digitalRead(3); unsigned long counter = 0, i = 0; auto currentTime = micros(); auto prevTime = currentTime; d = 0; // no delay to check the speed of the execution do{ PORTD = B00001000; v = digitalRead(3); // Serial.println(v); PORTD = B00001000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // first frame PORTD = B00000000; v = digitalRead(3); // Serial.println(v); PORTD = B00000000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // second frame currentTime = micros(); i = (unsigned long)(currentTime - prevTime); counter++; }while(i < uSec); Serial.print(">>>Counter:"); Serial.println(counter); Serial.print(">>>delay (ms):"); Serial.println(d); } void loop(){ timer.tick(); } #include <arduino-timer.h> // Variables unsigned long v = -1; int freq = 100.0; // Hz unsigned long d = floor(1.0/freq*1000000.0) ; // us Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution void setup() { unsigned long repeatEvery = 5000000; // Microseconds Serial.begin(9600); pinMode(3, OUTPUT); timer.every(repeatEvery, func1); // XXXXX microsec } void func1() { unsigned long uSec = 1000000; // Microseconds (= 1 sec) PORTD = B00000000; v = digitalRead(3); unsigned long counter = 0, i = 0; auto currentTime = micros(); auto prevTime = currentTime; d = 0; // no delay to check the speed of the execution do { PORTD = B00001000; v = digitalRead(3); // Serial.println(v); PORTD = B00001000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // first frame PORTD = B00000000; v = digitalRead(3); // Serial.println(v); PORTD = B00000000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // second frame currentTime = micros(); i = (unsigned long)(currentTime - prevTime); counter++; } while(i < uSec); Serial.print(">>>Counter:"); Serial.println(counter); Serial.print(">>>delay (ms):"); Serial.println(d); } void loop() { timer.tick(); } The code above will give output as counter = 47620 // how many times the instructions were repeated and if the Serial.println lines were uncommented so to draw the signal then the execution time gets so slowslows down to counter = 86
I am not sure, if the whole train of thought is wrong and I should do something else or use a different circuit or not. Any
Any hint or help would be highly appreciated.
PS: my Arduino by the way is a UNIROIUNIROI that is supposed to be the same as Arduino R3an Arduino R3.
