Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Example program for 7segment display with keypad.
- Vypisuje na LCD kod stisknute klavesy.
- Na 7segmentovem displeji zobrazuje cisla od 0 do 9 a postupne prepina
- vsechny displeje.
- */
- #include "MKL25Z4.h"
- #include "drv_systick.h"
- #include "drv_lcd.h"
- #include "7segkeypad.h"
- #include <stdio.h>
- #include <stdbool.h>
- void orderOfCounter();
- void printToDisplay(int num, int num2, int operation);
- void printToLCD(int num1, int num2, int operation);
- int * SmurfMyNumber(int number);
- int outputToSegmentsNum[4] = { 0 };
- int outputToSegmentsNum2[4] = { 0 };
- int outputToSegmentsResult[4] = { 0 };
- int storeForSegments[3];
- int main(void) {
- SEGKEYPAD_InitializeDisplay();
- SEGKEYPAD_InitializeKeyboard();
- SYSTICK_initialize();
- LCD_initialize();
- LCD_clear();
- int number = 0;
- int key = -1;
- int base_val = 10;
- int num[2] = {0};
- int operation = 0;
- int operaceBool = 0;
- int equalsHit = 0;
- int result;
- int calculated = 0;
- char* c;
- int counter1 = 0;
- int counter2 = 0;
- for (;;) {
- // vypis stisknutou klavesu
- while (key == -1){
- key = SEGKEYPAD_GetKey();
- if(calculated == 0) {
- int * numberToSegment1 = SmurfMyNumber(num[0]);
- int pom = 3;
- for(int Show1 = 4;Show1>0;Show1--)
- {
- SEGKEYPAD_SelectDisplay(Show1);
- SEGKEYPAD_ShowNumber(numberToSegment1[pom--]);
- SYSTICK_delay_ms(2);
- }
- int * numberToSegment2 = SmurfMyNumber(num[1]);
- int pom2 = 3;
- for(int Show2 = 8;Show2>4;Show2--)
- {
- SEGKEYPAD_SelectDisplay(Show2);
- SEGKEYPAD_ShowNumber(numberToSegment2[pom2--]);
- SYSTICK_delay_ms(2);
- }
- }else{
- int * numberToSegment3 = SmurfMyNumber(storeForSegments[2]);
- int pom4 = 7;
- for(int Show4 = 8;Show4>4;Show4--)
- {
- SEGKEYPAD_SelectDisplay(Show4);
- SEGKEYPAD_ShowNumber(numberToSegment3[pom4--]);
- SYSTICK_delay_ms(2);
- }
- int pom3 = 3;
- for(int Show3 = 4;Show3>0;Show3--)
- {
- SEGKEYPAD_SelectDisplay(Show3);
- SEGKEYPAD_ShowNumber(numberToSegment3[pom3--]);
- SYSTICK_delay_ms(2);
- }
- }
- }
- if (key == 0) {
- LCD_clear();
- calculated = 0;
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 7;
- counter1++;
- }
- }
- if (operaceBool){calculated = 0;
- if(counter2 < 4){
- num[1] = 10 * num[1] + 7;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 1) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 4;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 4;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 2) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 1;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 1;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 3) {calculated = 0;
- LCD_clear();
- if (num[0] != 0 || num[1] != 0) {
- if (!operaceBool){
- num[0] = num[0] / 10;
- counter1--;
- }
- if (operaceBool){
- num[1] = num[1] / 10;
- counter2--;
- }}
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 4) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 8;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 8;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 5) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 5;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 5;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 6) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 2;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 2;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 7) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 0;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 0;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 8) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 9;
- counter1++;
- }
- }
- if (operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 9;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 9) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 6;
- counter1++;
- }
- }
- if(operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 6;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 10) {calculated = 0;
- LCD_clear();
- if (!operaceBool){
- if(counter1 < 4){
- num[0] = 10 * num[0] + 3;
- counter1++;
- }
- }
- if(operaceBool){
- if(counter2 < 4){
- num[1] = 10 * num[1] + 3;
- counter2++;
- }
- }
- printToDisplay(num[0], num[1], operation);
- printToLCD(num[0], num[1], operation);
- key = -1;
- }
- if (key == 11) {calculated = 0;
- LCD_clear();
- if (operation == 0)
- result = num[0] + num[1];
- else if (operation == 1)
- result = num[0] - num[1];
- else if (operation == 2)
- result = num[0] * num[1];
- else if (operation == 3) {
- if (num[0] != 0 || num[1] != 0) {
- result = num[0] / num[1];
- }
- }
- calculated = 1;
- printToDisplay(num[0], num[1], operation);
- LCD_set_cursor(2, 1);
- //printToLCD(num[0], num[1], operation);
- char Barbaronan[100];
- sprintf(Barbaronan," = %d ",result);
- LCD_puts(Barbaronan);
- storeForSegments[0] = num[0];
- storeForSegments[1] = num[1];
- storeForSegments[2] = result;
- // printf(" = %d\n", result);
- num[0] = num[1] = result = operaceBool = 0;
- counter1 = counter2 = 0;
- key = -1;
- }
- if (key == 12) {
- operation = 0;
- operaceBool = 1;
- key = -1;
- }
- if (key == 13) {
- operation = 1;
- operaceBool = 1;
- key = -1;
- //-
- }
- //multiply
- if (key == 14) {
- operation = 2;
- operaceBool = 1;
- key = -1;
- }
- // /
- if (key == 15) {
- operation = 3;
- operaceBool = 1;
- key = -1;
- }
- SYSTICK_delay_ms(500);
- }
- /* int inc = 0;
- int disp = 3;
- for (int i = counter; i > 0; i--) {
- SEGKEYPAD_SelectDisplay(disp);
- SEGKEYPAD_ShowNumber(prom1[i]);
- inc++;
- disp--;
- }*/
- // vypis cislo na displeji
- //SEGKEYPAD_ShowNumber(number);
- // posun na dalsi cislo a dalsi display
- /*
- number++;
- if (number > 9) {
- number = 0;
- disp++;
- if (disp > 8)
- disp = 1;
- SEGKEYPAD_SelectDisplay(disp);
- }
- */
- return 0;
- }
- void printToDisplay(int num, int num2, int operation) {
- char c;
- if (operation == 0)
- c = '+';
- else if (operation == 1)
- c = '-';
- else if (operation == 2)
- c = '*';
- else if (operation == 3)
- c = '/';
- printf("cislo1 cislo2 operace => %d %c %d \n", num, c, num2);
- return;
- }
- void printToLCD(int num1, int num2, int operation) {
- char c[100];
- char o;
- if (operation == 0)
- o = '+';
- else if (operation == 1)
- o = '-';
- else if (operation == 2)
- o = '*';
- else if (operation == 3)
- o = '/';
- sprintf(c, "%d %c %d", num1, o, num2);
- LCD_puts(c);
- return;
- }
- int * SmurfMyNumber(int number){
- int base_val = 10, digit, i = 0, n = 0;
- int t, numstring[32];
- while (number) {
- digit = number % base_val;
- number /= base_val;
- if (digit < 10) {
- numstring[n++] = digit;
- } else {
- numstring[n++] = digit;
- }
- }
- for (int i = 0; i < n; i++) {
- t = numstring[i];
- numstring[i] = numstring[n - i - 1];
- numstring[n - i - 1] = t;
- }
- return numstring;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // EOF
- ////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement