Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Trying to make LED to blink rapidly on Microblaze(C languag)

Status
Not open for further replies.

vccy118

Newbie level 4
Joined
Feb 11, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,335
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:

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;
  	}  	
  }


}
... 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!
 

Re: Trying to make LED to blink rapidly on Microblaze(C lang

I didn't look through you code but I'll try to suggest you how to learn to do it.
When you use the wizard to generate the platform you also get a test a application which tests your peripherals. One of them is the LEDs GPIO. Have a look at the generated code and this will help you a lot. You can also study the GPIO API (right click on peripheral and look for View Driver Sourcecode (if I correctly remember). Then you can use the debugger to step though you code an see what happens. Hopefully this will help you learn how to work with Microblaze.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top