Aleksandar Lukic 2020/0247

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //librrary import section
00002 #include "mbed.h"
00003 
00004 //macro definition section
00005 #define REFRESH_RATE_MS 20
00006 #define BTN_DELAY_MS 40
00007 
00008 //global variable section
00009 DigitalIn btn_up(PC_8);
00010 DigitalIn btn_down(PC_9);
00011 
00012 DigitalOut A(PA_10);
00013 DigitalOut B(PA_9);
00014 DigitalOut C(PA_8);
00015 DigitalOut D(PB_10);
00016 DigitalOut E(PB_5);
00017 DigitalOut F(PB_4);
00018 DigitalOut G(PB_3);
00019 
00020 DigitalOut sel_1(PB_6);
00021 DigitalOut sel_2(PC_7);
00022 
00023 //arrays
00024 char hex_number[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
00025 DigitalOut display[7] = {A, B, C, D, E, F, G};
00026 
00027 //function declaration section
00028 void hexToDisplay2(char hex){
00029     sel_1.write(1);
00030     sel_2.write(0);
00031     
00032     for(int i = 6; i >= 0; i--)
00033         display[i] = (1<<i) & hex;
00034 }
00035 
00036 void hexToDisplay1(char hex){    
00037     sel_1.write(0);
00038     sel_2.write(1);
00039     
00040     for(int i = 6; i >= 0; i--)
00041         display[i] = (1<<i) & hex;
00042 }
00043 
00044 void clearDisplay(){
00045     sel_1.write(1);
00046     sel_2.write(1);    
00047 }
00048 
00049 void currNumToDisplay(char num){
00050     char dec = num / 10;
00051     char dig = num % 10;
00052     
00053     if(dec){
00054         hexToDisplay1(hex_number[dec]);
00055         wait_ms(REFRESH_RATE_MS);
00056         
00057         hexToDisplay2(hex_number[dig]);
00058         wait_ms(REFRESH_RATE_MS);
00059     } else {
00060         hexToDisplay2(hex_number[dig]);
00061         wait_ms(REFRESH_RATE_MS);
00062     }
00063     
00064     clearDisplay();
00065 }
00066 
00067 //main function
00068 int main(){
00069     char current_number = 0;
00070 
00071     while(true){
00072         currNumToDisplay(current_number);
00073         
00074         if(!btn_up){
00075             if(current_number == 0) current_number = 15;
00076             else    current_number = (current_number - 1) % 16;    
00077         }
00078         
00079         
00080         if(!btn_down) current_number = (current_number + 1) % 16;
00081         //wait_ms(BTN_DELAY_MS);
00082     }
00083 }
00084 
00085 //function definition section