#include <Arduino.h>
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Serial.begin(9600);
// Set all PORTB pins as output
DDRB = 0xFF;
// Set all PORTD pins as input with pull-up resistors
DDRD = 0x00;
PORTD = 0x0F;
}
void loop() {
// Read the input from PORTD
uint8_t portd_value = PIND;
// Shift right by 2 bits
portd_value = portd_value >> 2;
asm volatile (
"ldi r21, 0x00\n" // Load immediate value 0x00 into register R21
"lds r20, portd_value\n" // Load immediate value 0x45 into register R20
"ldi r16, 0x01\n"
"cp r20, r21"
"brne next1\n"
"jmp over" // Branch to 'next' if the carry flag is clear
"next1: add r21, 1"
"lsl r16\n"
"cp r20, r21"
"brne next2\n" // Branch to 'next' if the carry flag is clear
"jmp over"
"next2: add r21, 1"
"lsl r16\n"
"cp r20, r21"
"breq over\n" // Branch to 'next' if the carry flag is clear
"lsl r16\n"
"over: lsr r16"
"sts %0, r16\n" // Store the value of R20 into the variable x // Store the value of R21 into the variable y
: "=m" (portd_value) // Output operands: x and y are memory locations to be written to
: // No input operands
: "r20", "r21", "r16" // Clobbered registers: R20, R21, R16 will be used and modified
);
// Print the value to the serial monitor
Serial.println(portd_value);
// Output the value to PORTB
PORTB = portd_value;
// Wait for a second before the next loop iteration
delay(10);
}