cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Visitor
Visitor
573 Views
Registered: ‎06-30-2017

Frequency of 1 Hz for LEDS

Hello guys, Need help over here . So I want to make LEDs blink with frequency of 1 Hz when i push button. I cant figure out how to make it frequency on 1 hz

HERE IS THE CODE

#include <stdio.h>
#include "platform.h"
#include "xparameters.h"
#include "xgpio.h"

void print(char *str);

int main()
{
	init_platform();
	int switchesValue;
	int btnValue;
	XGpio switches;
	XGpio_Initialize(&switches, XPAR_DIP_SWITCHES_8BITS_DEVICE_ID);
	XGpio_SetDataDirection(&switches, 1, 0xffffffff);

	XGpio leds;
	XGpio_Initialize(&leds, XPAR_LEDICE_DEVICE_ID);
	XGpio_SetDataDirection(&leds, 1, 0x00000000);

	XGpio buttons;
	XGpio_Initialize(&buttons, XPAR_BTN_DEVICE_ID);
	XGpio_SetDataDirection(&buttons, 1, 0xffffffff);

	while(1){
		btnValue=XGpio_DiscreteRead(&buttons, 1);
		xil_printf("Stanje %d\r\n",btnValue);
		int i;
		if(btnValue == 4){
				while(btnValue == 4){

					for(i=0;i<666666;i++){
						XGpio_DiscreteWrite(&leds, 1, 0b10101010);
					}
					for(i=0;i<666666;i++){
					XGpio_DiscreteWrite(&leds, 1, 0b00000000);
								}
					btnValue=XGpio_DiscreteRead(&buttons, 1);
				}
				}
	}
	return 0;
}
0 Kudos
2 Replies
Highlighted
Mentor
Mentor
547 Views
Registered: ‎02-24-2014

Re: Frequency of 1 Hz for LEDS

In general, it's extremely difficult to perform timing tasks in software without the usage of a timer interrupt.  If you implement a timer that generates an interrupt every 500 ms, it will be easy to have the interrupt toggle the LED to give a 1 Hz flashing rate.   Otherwise, it's far easier to write a little hardware code to make a hardware circuit to give a 1 Hz toggle.

Don't forget to close a thread when possible by accepting a post as a solution.
0 Kudos
Highlighted
Moderator
Moderator
511 Views
Registered: ‎07-31-2012

Re: Frequency of 1 Hz for LEDS

Hi @yelyah,

 

A hint if the board oscillator runs at frequency 50Mhz then as T=1/F, you should make my input frequency 50 millions times lesser to led blink every 1 second for the frequency at 1 Hz.

 

Regards

Praveen


-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
0 Kudos