vccy118
Newbie level 4
I need help on writing a simple C code to make the 8bit LEDs on Spartan 3E Starter Kit board to do several things:
1. LED 2 light up for indefinite duration when SWITCH 1 is being toggled
2. Wait for 3 seconds
3. LED 1 will light up and start blinking at 0.5 sec interval for indefinite duration
4. LED 1 and 2 will stop blinking when SWITCH 1 is being toggled back to initial state
5. LED 2 and 4 light up for indefinite duration and LED 1 will start blinking at 0.5 sec interval for indefinite duration when SWITCH 1 and 2 is being toggled
6. Wait for 3 seconds
7. LED 3 will light up and start blinking at 0.5 sec interval for indefinite duration
8. LED 1, 2, 3 and 4 will stop blinking when SWITCH 1 is being toggled back to initial state
And this is the C code I wrote so far:
... and it doesn't work
When SWITCH 1 is toggled, LED 2 wont light up, but LED 1 will light up and won't blink.
SWITCH 2 is without the delay and blinking codes, so when I toggle SWITCH 2, LED 4 will light up indefinitely, as expected.
Some of the variables were defined ahead of time and not currently used, so please ignore it.
I really hope that I could get some insightful help here! Really out of ideas to implement this...
Thanks!
1. LED 2 light up for indefinite duration when SWITCH 1 is being toggled
2. Wait for 3 seconds
3. LED 1 will light up and start blinking at 0.5 sec interval for indefinite duration
4. LED 1 and 2 will stop blinking when SWITCH 1 is being toggled back to initial state
5. LED 2 and 4 light up for indefinite duration and LED 1 will start blinking at 0.5 sec interval for indefinite duration when SWITCH 1 and 2 is being toggled
6. Wait for 3 seconds
7. LED 3 will light up and start blinking at 0.5 sec interval for indefinite duration
8. LED 1, 2, 3 and 4 will stop blinking when SWITCH 1 is being toggled back to initial state
And this is the C code I wrote so far:
Code:
#include "xgpio.h"
#include "xparameters.h"
#define LED_1 0x01
#define LED_2 0x02
#define LED_3 0x04
#define LED_4 0x08
#define LED_5 0x10
#define LED_6 0x20
#define LED_7 0x40
#define LED_8 0x80
int main(void)
{
XGpio LED;
XGpio SWITCH;
int switch_initial, switch_state = 0;
int switch_update = 0;
int SWITCH_1 = 0x1;
int SWITCH_2 = 0x2;
int SWITCH_1_2 = 0x3;
int TIME_DELAY = 1000000;
int TIME_WAIT = 1000000;
int TIME_INTERVAL = 1000000;
volatile int count=1;
volatile int wait=0;
XGpio_Initialize(&LED, XPAR_LEDS_8BIT_DEVICE_ID);
XGpio_Initialize(&SWITCH, XPAR_DIP_SWITCHES_4BIT_DEVICE_ID);
XGpio_SetDataDirection (&LED, 1, 0x0);
XGpio_SetDataDirection (&SWITCH, 1, 0xffffffff);
switch_initial = XGpio_DiscreteRead (&SWITCH,1);
while (1)
{
switch_state = XGpio_DiscreteRead (&SWITCH,1);
if (switch_state == SWITCH_1 && switch_update == 0)
{
XGpio_DiscreteWrite (&LED, 1, LED_2);
XGpio_DiscreteClear (&LED, 1, LED_1 | LED_8 | LED_3 | LED_4 | LED_5 | LED_6 | LED_7);
switch_update = 0;
for (count; count>0; count++)
{
switch_state = XGpio_DiscreteRead (&SWITCH,1);
if (switch_state != SWITCH_1)
{
break;
}
for (wait; wait<TIME_DELAY; wait++);
XGpio_DiscreteWrite (&LED, 1, LED_1);
for (wait; wait<TIME_DELAY; wait++);
XGpio_DiscreteClear (&LED, 1, LED_1);
}
}
if (switch_state == SWITCH_2 && switch_update == 0)
{
for (wait; wait<TIME_DELAY; wait++);
XGpio_DiscreteWrite (&LED, 1, LED_4);
XGpio_DiscreteClear (&LED, 1, LED_1 | LED_2 | LED_3 | LED_8 | LED_5 | LED_6 | LED_7);
switch_update = 0;
}
if (switch_state == SWITCH_1_2 && switch_update == 0)
{
for (wait; wait<TIME_DELAY; wait++);
XGpio_DiscreteWrite (&LED, 1, LED_2| LED_4);
XGpio_DiscreteClear (&LED, 1, LED_1 | LED_3 | LED_8 | LED_5 | LED_6 | LED_7);
switch_update = 0;
}
if (switch_state == switch_initial && switch_update == 0)
{
XGpio_DiscreteClear (&LED, 1, LED_1 | LED_2 | LED_3 | LED_4 | LED_5 | LED_6 | LED_7 | LED_8);
switch_update = 0;
}
}
}
When SWITCH 1 is toggled, LED 2 wont light up, but LED 1 will light up and won't blink.
SWITCH 2 is without the delay and blinking codes, so when I toggle SWITCH 2, LED 4 will light up indefinitely, as expected.
Some of the variables were defined ahead of time and not currently used, so please ignore it.
I really hope that I could get some insightful help here! Really out of ideas to implement this...
Thanks!