Skip to main content

Comparison Arduino, VHDP and VHDL

Sequential Example

#define LED 13

void setup() {
pinMode(LED, OUTPUT); //Declare output
}

void loop() { //Procedural programming
digitalWrite(LED, HIGH); //LED on
delay(1000); //Wait
digitalWrite(LED, LOW); //LED off
delay(1000); //Wait
}

Parallel Calculation Example (+ VARIABLE vs SIGNAL)

Not possible with Arduino

Can't be compared because Arduino doesn't support parallel programming. You can't have multiple parallel processes for different tasks and calculations are much slower because one operation can take more than one clock cycle. If you have multiple operations together (like "(Xi / 10) + 5"), every operation increases the time after calculation is finished.

Main (
Xi : IN INTEGER range 0 to 255; --Declare inputs
Yi : IN INTEGER range 0 to 255;
Xo : OUT INTEGER range 0 to 255; --Declare output
Yo : OUT INTEGER range 0 to 255;
){
Process() { --For parallel and procedural programming
Xo <= (Xi / 10) + 5; --Everything calculated in one clock cycle
Yo <= (Yi / 5) + 10; --Second calculation is finished together with first

SIGNAL i1 : INTEGER := 0; --Create integer signal (takes value one cycle after assignment)
VARIABLE i2 : INTEGER := 0; --Create integer variable (takes value immediately, but can only be used in this process)
i1 <= i1 + 1; --Add both + 1
i2 := i2 + 1;

If(i1 > 0){ --The signal is still 0, so this is false
i1 <= i1 - 1;
}
If(i2 > 0){ --The variable is 1 already
i2 := i2 - 1;
}
}
}

Parallel Process Example

This is a simple code for an ultrasonic-sensor

Main (
US_Trigger : OUT STD_LOGIC := '0';
US_Echo : IN STD_LOGIC := '0';
){
Process Trigger_Process () {
--Creates an impuls every ~100ms to trigger the distance measurement
Thread {
US_Trigger <= '1';
Wait(10us);
US_Trigger <= '0';
Wait(100ms);
}
}

Process Echo_Process () {
Thread {
--Waits for a new echo impuls to calculate the distance
While(US_Echo = '1'){}
While(US_Echo = '0'){}

--Counts the microseconds while the sound travels to the object and back
--58 microseconds = 1cm
For(VARIABLE d : INTEGER := 0; US_Echo = '1'; d := d + 1) {
Wait(58us);
}
--d is now the distance to the object in cm
}
}
}

Parallel Component Example

In the libraries you can find Components that form an interface for different hardware. You can find controller for ultrasonic-sensors or for UART data (like Serial in Arduino). Every NewComponent runs in parallel, so you can add as many UART ports as you need.

Main (
US_Trigger : OUT STD_LOGIC_VECTOR(7 downto 0) := '0'; --8 ultrasonic sensors
US_Echo : IN STD_LOGIC_VECTOR(7 downto 0) := '0';

LED : OUT STD_LOGIC; --1 LED to show distance with brightness
){
TYPE Distance_type IS ARRAY (0 to 7) OF NATURAL range 0 to 255;
SIGNAL Distance : Distance_type; --Distances of the 8 sensors

Generate (for i in 0 to 7) { --Generate an controller for every ultrasonic-sensor
NewComponent Ultrasonic_Controller (
Update_Frequency => 15, --Checks 15 times in a second
Trigger => US_Trigger(i),
Echo => US_Echo(i),
Dist => Distance(i),
);
}

--Convert number from 0 to 255 to a 8-bit bit vector
PWM_Generator_Duty <= STD_LOGIC_VECTOR(TO_UNSIGNED(Distance(0), PWM_Generator_Duty'LENGTH));

SIGNAL PWM_Generator_Duty : STD_LOGIC_VECTOR (7 DOWNTO 0);
NewComponent PWM_Generator ( --Outputs distance of first sensor
Duty => PWM_Generator_Duty,
PWM_Out(0) => LED,
);
}

String Example

void setup() {
Serial.begin(9600); //Start UART with Baudrate = 9600
}

void loop() {
Serial.println("Hello World"); //Send "Hello World" string
delay(2000); //Wait
}

Loop Example

void loop() {
//Only has sequential loops
for (int i = 0; i < 10; i ++){ //Turn LEDs 0-9 on
digitalWrite(i, HIGH);
}
delay(1000); //Wait
int i = 0;
while (i < 10){ //Turn LEDs 0-9 off
digitalWrite(i, LOW);
i ++;
}
delay(1000); //Wait
}

Loop Comparison

The parallel For and While can be also be used in a Thread.

Main (
LEDs : OUT STD_LOGIC_VECTOR(0 to 9);
){
Process() {
Thread {
--Turn on LEDs 0-9 sequentially in 12 clock cycles (10 + 1 for start and end)
For(VARIABLE i : INTEGER := 0; i < 10; i := i + 1) {
LEDs(i) <= '1';
}
Wait(1000ms); --Wait
--Turn on LEDs 0-9 parallely in 1 clock cycle
ParFor(i IN 0 to 9) {
LEDs(i) <= '1';
}
Wait(1000ms); --Wait
}
}
}