Menu Close

RISC-V C Programming 2 (2)Button Project

1.Schematics Analysis

Direct to Table of Contents:


RISC-V Syllabus


In this case, buttons are considered as inputs. When pressing the push buttons, the PB will connect the left and right side wiring, otherwise, it can be considered as an open circuit. When the PB is pressed, and the connection is on, the VCC voltage will drop through a resistor, and then connects to ground. Thus FPGA wiring will connect to ground directly, thus in low voltage. If the PB is open circuit, there is no way VCC could flow, thus no current flow through the resistors, and the FPGA wiring will have the same voltage as VCC.

button schematics

Figure 1 Button wiring of schematics



2.RISCV_button Project

Same as the previous steps in RISC-V C Programming 2 (1)Segment Display Project, import the project into the Freedom Studio, as shown in Figure 2. The experiment phenomenon is that when pressing the buttons (only three buttons are implemented here: menu, up, and return), the corresponding segment display will change from ‘1’ to ‘0’.

Figure 2 RISCV_button project


Except for the main function of RISCV_button is different than that of RISCV_seg_cnt, the other header files are all the same. Therefore, only main.c file will be listed here. The detailed explanation will be commented in the code block.

  • Main.c
#include <stdio.h>               //include the standard I/O library, mainly used to declare printf function

#include "platform.h"           //user defined function in C library, including GPIO configuration, definition of the memory, 

                                //and related subfunctions and parameters

#define NOP_DELAY 0x00000200          //define a macro, the value is different than that in RISCV_seg_cnt. It can be modified according to

                                      //the needs

int main(void)              //main function for RISCV_button function


    unsigned int  curr_seat = 0x01;          //segment display initial location (the right most segment display)

    volatile unsigned int  i;                //used in the delay loop

    unsigned int  curr_gpio_val;             //read the button input

    (*(volatile unsigned int *)(GPIO_ADDR + SEAT_DIR)) = 0;         //segment location value's direction is output

    (*(volatile unsigned int *)(GPIO_ADDR + SEG_DIR)) = 0;          //segment selection value's direction is output

    (*(volatile unsigned int *)(GPIO_ADDR + BUT_DIR)) = ~0L;        //button direction is input (note there is a negation)

    (*(volatile unsigned int *)(GPIO_ADDR + SEG_VAL)) = ~SEG__;     //initialize the current segment display to be empty (no display)

                                                                    //negation is applied for the segment display wiring on schematics

    printf("\r\nThis is a button test program. \r\n");

    while (1)//main loop


        curr_gpio_val = (*(volatile unsigned int *)(GPIO_ADDR + BUT_VAL));     //read the button input

        (*(volatile unsigned int *)(GPIO_ADDR + SEAT_VAL)) = ~curr_seat;       //set the segment display location

                                                                               //negation is applied for the wiring on schematics

        if(curr_gpio_val & curr_seat)      //decision making, if it is not the corresponding location

                                           //button and the corresponding segment display location are bitwise equal

                                           //button is negation for the wiring on schematics

            (*(volatile unsigned int *)(GPIO_ADDR + SEG_VAL)) = ~SEG_1;       //display 1

        else          //if it is the corresponding location

            (*(volatile unsigned int *)(GPIO_ADDR + SEG_VAL)) = ~SEG_0;        //display 0

        for(i = 0; i < NOP_DELAY; i ++ )       //time delay for the segment display


        curr_seat = curr_seat << 1;            //shift the lit segment display to the left 1 bit

        if(curr_seat == 0x08) curr_seat = 0x01;       //decision making, if the segment display location is exceeds the 3rd

                                                      //3 segment display corresponding 3 push buttons




Posted in Application and Development, Articles, C, Programming Language, RISC-V, RISC-V Textbook, Textbook and Training Project

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!