#include <Arduino.h>
void delay_1ms() {
// 1 ms delay function using inline assembly
asm volatile (
"ldi r20, 21\n" // Load 16 into r20 (outer loop counter)
"outer_loop:\n"
"ldi r19, 251\n" // Load 256 into r19 (inner loop counter)
"inner_loop:\n"
"dec r19\n" // Decrement r19
"brne inner_loop\n" // If r19 is not zero, branch to inner_loop
"dec r20\n" // Decrement r20
"brne outer_loop\n" // If r20 is not zero, branch to outer_loop
"nop\n" // No operation (to ensure exact delay)
);
}
void delay_ms(uint16_t ms) {
for (uint16_t i = 0; i < ms; i++) {
delay_1ms();
}
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set all PORTB pins as output (if needed)
DDRB = 0xFF;
}
void loop() {
// Record starting time
unsigned long t1 = millis();
PORTB = 0x3f;
PORTD = 0xF0;
// Perform delay for 1000 ms
delay_ms(1000);
// Record ending time
unsigned long t2 = millis();
PORTB = 0x00;
PORTD = 0x00;
// Print the time taken by the task
Serial.print("Time taken by the task: ");
Serial.print(t2 - t1);
Serial.println(" milliseconds");
// Perform any other tasks or operations here
// For example, toggling PORTB as in your original code
// Delay for 1 second (1000 ms) between tasks
delay_ms(1000);
}