void setup() {
// Set up the direction register and output register pointers
volatile char *dir, *dirk, *ink, *outf;
dir = (volatile char *) 0x30; // Data direction register for output (7-segment)
dirk = (volatile char *) 0x107; // Data direction register for switch
ink = (volatile char *) 0x106; // Input register (switch input)
outf = (volatile char *) 0x31; // Output register for 7-segment display
*dir = 0xFF; // Set the 7-segment display port as output
*dirk = 0x00; // Set the switch port as input
}
void loop() {
volatile char *ink = (volatile char *) 0x106; // Input register (switch input)
volatile char *outf = (volatile char *) 0x31; // Output register (7-segment display)
// Array holding the 7-segment values for numbers 0 to 9
char segmentMap[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
int i = 0; // Counter to track current number displayed
char prevSwitchState = 0x00; // To store previous state of the switch
while(1) {
char switchState = *ink; // Read the switch input
// Check if the switch is pressed (switchState == 0x01) and it was not previously pressed
if (switchState == 0x01 && prevSwitchState == 0x00) {
*outf = segmentMap[i]; // Output the corresponding 7-segment code
i = (i + 1) % 10; // Increment to the next digit, and wrap around after 9
prevSwitchState = 0x01; // Set previous state to pressed
} else if (switchState == 0x00) {
prevSwitchState = 0x00; // Set previous state to not pressed when switch is released
}
// Simple delay for debouncing (you may adjust or use a proper debouncing technique)
for (long n = 0; n < 100000; n++);
}
}