void setup()
{
//put your setup code here, to run once:
volatile long i=0;
volatile char x;
volatile char *dir_f;
volatile char *dir_k;
volatile char *out_f;
volatile char *in_k;
// memory -mapped I/O pregister addresses
dir_f = 0x30; //Direction register for Port F (LEDs)
dir_k= 0x107; // Direction register for Port K (Switch)
out_f = 0x31; // Output register for Port F (LEDs)
in_k= 0x106; // Input register for Port K (Switch)
// Set LED pins (A0, A1, A2) as outputs and Switch pin (A8) as input
*dir_f = 0x07; // Setting Port F pins 0, 1, 2 as outputs (0b00000111)
*dir_k= 0x00; // setting Port K pin A8 as input
// all LEDs are initially OFF
*out_f = 0x00;
i = 0; // Initialize press count
while(1)
{
x=*in_k; // Read button state
if((x & 0x01)==0x01) // Check if button is pressed
{
i++; // Increment the press count
if (i>4)
{
i = 1; // Reset the cycle after the 4th press
}
delay();
}
// Update LEDs based on press count
switch (i)
{
case 1: // 1st press - Turn ON LED1 (A0)
*out_f = 0x01; // 0b00000001
break;
case 2: // 2nd press - Turn ON LED2 (A1)
*out_f = 0x02; // 0b00000010
break;
case 3: // 3rd press - Turn ON LED3 (A2)
*out_f = 0x04; // 0b00000100
break;
case 4: // 4th press - Turn ON all LEDs (A0, A1, A2)
*out_f = 0x07; // 0b00000111
break;
default: // Ensure all LEDs are OFF if no valid state
*out_f = 0x00;
delay();
break;
}
}
}
// void loop(){
// // put your main code here, to run repeatedly:
// }
void delay(){
volatile long j;
for(j=0;j<100000;j++);
}