Question
Anyone who is talented in C language, please read head, especially if you are familiar with C and hardware! Basically I have a FPGA board
Anyone who is talented in C language, please read head, especially if you are familiar with C and hardware!
Basically I have a FPGA board with a 16-key keypad that hooks up via PMOD, and 9 switches, and some LEDS.This is not important!!!!!I have functions below that handle these components and are operational. I am trying to make a calculator.
(Once I get the goals below, I will connect it to a seven segment display, but that is another task, and I have functions for it. I need help getting my switches to select tasks, and I need to capture my keypresses as operands.)
I need help creating C functions that take their working inputs and act accordingly, and I am stuck!!!
This is how I want my program to work:
1. Select an operation with the switches(I have a function for selecting the operations at the top of main, but is not used, yet. but my program does grab the switches correctly and output them to my LEDS)
2. Display operation on LEDs(the code below currently passes my keypad presses and turned on switches to my LEDS successfully! this took me a long time to figure out)
3. Pause for some brief amount of time(This is where I need the most help, I need to throttle my ints: keypress and switch_value)
4. Select an operand with the keypad(I need to throttle the keypress so that I can store 1 keypress as an input, operand1)
5. Display operand on LEDs(output_to_LEDs() is working code !!!)
6. Pause for some brief amount of time(not sure how to accomplish this.....more functions? usleep() doesn't seem like it will cut it
7. Repeat steps 4 6 once more for a binary operation(need to store one more keypress value as operand2)
8. Send the keypress as operand1 and operand2, and the LED_switch_value to the getOperation() function, that has yet to be dialed in.
Main starts below:
----------------------------------------------------------------------------------------------------------------------------
/* * main.c * * Created on: Feb 9, 2022 * */
#include \"xil_types.h\" #include \"GPIO.h\" // to include usleep() function #include \"unistd.h\" #include \"stdio.h\" #include \"stdlib.h\" #include \"math.h\"
#define LEDS 0x41210000 // Base address of LED IP Rev. D 2020.2 XSA #define SWITCHES 0x41220000 // Base address of switches #define SSEG 0x43C10000 // Base address of 7 segment display DONT worry about this, I will work on this part of the project after I get the goals above completed
//this function is not yet called in the main, but needs to be connected to LED_switch_value, based off the switch values, it will select an operation.
int getOperation(unsigned int task, int op1, int op2, int *stored) { int solution=0;
switch(task) { case 0b0000: solution=op1+op2; //ADD break; case 0b0001: solution=op1-op2; //SUB break; case 0b0010: solution=op2-op1; //RSB break; case 0b0011: solution=op1*op2; //MUL break; case 0b0100: solution=op1*op2 + *(stored); //MLA break; case 0b0101: solution=op1+op2; //TEQ break; case 0b0110: solution=op1;> break; case 0b0111: solution=op1>>op2; //LSR break; case 0b1000: solution=op1&op2; //AND break; case 0b1001: solution=op1|op2; //ORR break; case 0b1010: solution=op1^op2; //EOR break; case 0b1011: solution=op1 & ~op2; //BIC break; case 0b1100: solution= ~op1; //MVN break; case 0b1101: counter=0; while (value !=0) { value = value >> 1; counter++; } clz=32-counter; //CLZ break; case 0b1110: *stored=solution; //STR break; case 0b1111: solution=stored; //LDR break; } }
unsigned int get_switch_states(void);
//function for outputting values to LEDS for switches and keypresses void output_to_LEDs(unsigned int LED_value) { LED_value &= 0b1111111111; *((uint32_t *)LEDS) = LED_value; }
// function for getting states of switches unsigned int get_switch_states(void) { unsigned int switch_states = 0; switch_states = *((uint32_t *)SWITCHES); switch_states &= 0b1111111111;
return switch_states; }
// the main
int main (void) {
unsigned int LED_switch_value = 0; unsigned int keypress = 0; unsigned int readROWS = 0;
// row = 0 means PMODB top row, row = 1 means PMODB bottom row // inout = 1 means that row is output, inout = 0 means that row is input // for reference void setupPMODB(unsigned int inout, unsigned int row); setupPMODB(0, 0); // input; top row setupPMODB(1, 1); // output; bottom row
while(1) {
// Step 1, get switch values LED_switch_value = get_switch_states(); // Step 2, write switch values //output_to_LEDs(LED_switch_value);
//for reference unsigned int readPMODB(unsigned int row); //for reference void writePMODB(unsigned int value, unsigned int row); (0,0) = input, top row, (1,1)= output, bottom row
writePMODB(0b0111, 1); usleep(200); readROWS = readPMODB(0); switch (readROWS) { case 0b0111: keypress = 0x1; break; case 0b1011: keypress = 0x2; break; case 0b1101: keypress = 0x3; break; case 0b1110: keypress = 0xA; break; }
writePMODB(0b1011, 1); usleep(200); readROWS = readPMODB(0); switch (readROWS) { case 0b0111: keypress = 0x4; break; case 0b1011: keypress = 0x5; break; case 0b1101: keypress = 0x6; break; case 0b1110: keypress = 0xB; break; }
writePMODB(0b1101, 1); usleep(200); readROWS = readPMODB(0); switch (readROWS) { case 0b0111: keypress = 0x7; break; case 0b1011: keypress = 0x8; break; case 0b1101: keypress = 0x9; break; case 0b1110: keypress = 0xC; break; }
writePMODB(0b1110, 1); usleep(200); readROWS = readPMODB(0); switch (readROWS) { case 0b0111: keypress = 0x0; break; case 0b1011: keypress = 0xF; break; case 0b1101: keypress = 0xE; break; case 0b1110: keypress = 0xD; break; } output_to_LEDs(keypress + LED_switch_value); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started