void port_init();
void out_port(unsigned int x);
void delay1();
void delay1() {
volatile long i;
for (i = 0; i < 200000; i++); // Basic delay loop
}
void port_init() {
DDRD = 0xFC; // Set PD2 to PD7 as outputs (0b11111100)
DDRB = 0x3F; // Set PB0 to PB5 as outputs (0b00111111)
DDRC = 0x3F; // Set PC0 to PC5 as outputs (0b00111111)
}
void out_port(unsigned int x) {
// Output lower 6 bits to Port D (PD2 to PD7)
PORTD = (x << 2) & 0xFC; // Shift left to align with PD2 to PD7
Serial.print("HEX Value of D: ");
Serial.println(PORTD, BIN); // Print value in binary format for better visualization
// Output bits 6 to 11 to Port B (PB0 to PB5)
PORTB = (x >> 6) & 0x3F; // Shift right by 6 to align with PB0 to PB5
Serial.print("HEX Value of B: ");
Serial.println(PORTB, BIN); // Print value in binary format for better visualization
// Output bits 12 to 17 to Port C (PC0 to PC5)
PORTC = (x >> 12) & 0x3F; // Shift right by 12 to align with PC0 to PC5
Serial.print("HEX Value of C: ");
Serial.println(PORTC, BIN); // Print value in binary format for better visualization
}
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud
port_init(); // Initialize the ports
}
void loop() {
unsigned char i;
unsigned int y; // Use 'unsigned int' to handle up to 18 bits
// Loop to blink all 18 LEDs and print their corresponding values
for (i = 0; i < 18; i++) {
y = 1 << i;
out_port(y); // Output the value to the ports
delay1(); // Delay for visualization
}
}