Stopwatch on Xilinx NEXYS A7 Board using Vitis ISE(C-Code)

OP #7321322
Rate this post
useful
not useful

Can anyone help me to build Stopwatch with Start/stop, reset, pause/resume functionalities?

I have .xsa bitstream from Vivado and want to implement it on 7-segment display of Xilinx NEXYS A7 board using C-Code (Xilinx Vitis ISE).

I'm not good with C and have to consider C in this case. The following is the code but not really working.

#include "xparameters.h" #include "xgpio.h" #include "xtmrctr.h" #include <unistd.h> #include "xstatus.h"

#define LED_AN_ADDR 0x40000000 //base address for digit select signals #define LED_DIS_ADDR 0x40010000 //base address for 7-segment displays #define SW_ADDR 0x40020000 //base address for 16 switches #define TMRCTR_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID //device ID for the on-board timer

#define TIMER_FREQ 100000000 // Timer frequency in Hz #define DISPLAY_PERIOD 100000000/10 // Period for updating the display in clock cycles

XGpio Gpio_LedAn; XGpio Gpio_LedDis; XGpio Gpio_Sw; XTmrCtr Timer;

unsigned int hours = 0; // Time in hours unsigned int minutes = 0; // Time in minutes unsigned int seconds = 0; // Time in seconds int state = 0; // 0 = stopped, 1 = running void delay(unsigned int delay_count) { unsigned int i; for (i = 0; i < delay_count; i++); }

unsigned int get7segData(int num) { static const unsigned int segData[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F}; if(num >= 0 && num <= 999999) { unsigned int result = 0; int i; for (i = 0; i < 6; i++) { result |= segData[num % 10] << (i * 8); num /= 10; } return result; } else { return 0; } }

void update_display() { // turn off all digit select signals XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x00000000);

1
// Display hour tens digit on the leftmost 7-segment display
2
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(hours / 10));
3
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x1); // turn on digit select signal for leftmost display
4
delay(0.05 * 1000000);
5

6
// Display hour ones digit on the second leftmost 7-segment display
7
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(hours % 10));
8
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x2); // turn on digit select signal for second leftmost display
9
delay(0.05 * 1000000);
10

11
// Display minute tens digit on the third leftmost 7-segment display
12
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(minutes / 10));
13
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x4); // turn on digit select signal for third leftmost display
14
delay(0.05 * 1000000);
15

16
// Display minute ones digit on the fourth leftmost 7-segment display
17
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(minutes % 10));
18
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x8); // turn on digit select signal for fourth leftmost display
19
delay(0.05 * 1000000);
20

21
// Display second tens digit on the fifth leftmost 7-segment display
22
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(seconds / 10));
23
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x10); // turn on digit select signal for fifth leftmost display
24
delay(0.05 * 1000000);
25

26
// Display second ones digit on the rightmost 7-segment display
27
XGpio_DiscreteWrite(&Gpio_LedDis, 1, get7segData(seconds % 10));
28
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x20); // turn on digit select signal for rightmost display
29
delay(0.05 * 1000000);
30

31
// turn off all digit select signals
32
XGpio_DiscreteWrite(&Gpio_LedAn, 1, 0x00000000);

}

void timer_interrupt(void *CallbackRef, u8 TmrCtrNumber) { // call the function to display the time on the 7-segment displays update_display(); // increment the time seconds++; if (seconds == 60) { seconds = 0; minutes++; } if (minutes == 60) { minutes = 0; hours++; } if (hours == 100) { hours = 0; } // reset the timer counter value XTmrCtr_Reset(CallbackRef, TmrCtrNumber); // call the function to display the time on the 7-segment displays //Read switches int switches = XGpio_DiscreteRead(&Gpio_Sw, 1);

1
// Check the reset switch
2
if (switches & 0x1) {
3
    // Reset the timer
4
    hours = 0;
5
    minutes = 0;
6
    seconds = 0;
7
}
8

9
// Check the start/stop switch
10
if (switches & 0x2) {
11
    // Toggle the state of the stopwatch
12
    state = !state;
13
}
14

15
// If the stopwatch is running, re-enable the timer interrupt
16
if (state) {
17
XTmrCtr_EnableIntr(&Timer, TmrCtrNumber);
18
}

}

int main() { int status;

1
// Initialize the digital I/O for the 7-segment displays and digit select signals
2
status = XGpio_Initialize(&Gpio_LedDis, LED_DIS_ADDR);
3
if (status != XST_SUCCESS) return XST_FAILURE;
4
status = XGpio_Initialize(&Gpio_LedAn, LED_AN_ADDR);
5
if (status != XST_SUCCESS) return XST_FAILURE;
6

7
// Initialize the digital I/O for the 16-bit switches
8
status = XGpio_Initialize(&Gpio_Sw, SW_ADDR);
9
if (status != XST_SUCCESS) return XST_FAILURE;
10

11
// Initialize the timer
12
status = XTmrCtr_Initialize(&Timer, TMRCTR_DEVICE_ID);
13
if (status != XST_SUCCESS) return XST_FAILURE;
14

15
// Set the timer to generate an interrupt every 1 second
16
XTmrCtr_SetResetValue(&Timer, 0, DISPLAY_PERIOD);
17
XTmrCtr_SetOptions(&Timer, 0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION);
18

19
// Register the timer interrupt handler
20
XTmrCtr_SetHandler(&Timer, timer_interrupt, &Timer);
21

22
// Enable the global interrupt
23
microblaze_enable_interrupts();
24

25
// Start the timer
26
XTmrCtr_Start(&Timer, 0);
27

28
return 0;
29
}
#7321880
Rate this post
useful
not useful

Its not good style to edit a post afterwards and add big amounts of extra information not provided in the first place. It makes other people look like fools. You should have typed that into your new post. Regarding your program:

Checking every second the input pin state will not work. One has to hold Keys for at least one second to have the keypress recognized. Furthermore if key is pressed a bit longer than one second it might "detect" two key presses. You need proper edge detection. (and maybe as well debouncing)

At the end of main() you just return. That exits your application. If you do that, your application is gone. Most likely the platform specific c runtime code will also do a reset of your software in that case. In most cases, embedded applications are not supposed to return from main() at all.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now