Aleksandar Lukic 2020/0247

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //bonus pitanje:
00002 //U vecem delu opsega se ne cuje razlika, jedino pred kraj (~99%),
00003 //jacina zvuka se smanjuje do 0.
00004 
00005 //include statements
00006 #include "mbed.h"
00007 
00008 //global variables
00009 PwmOut buzzer(PA_11);
00010 InterruptIn sw1(PC_9);
00011 AnalogIn pot1(PA_0);
00012 AnalogIn pot2(PA_1);
00013 
00014 DigitalOut A(PA_10), B(PA_9), C(PA_8), D(PB_10), E(PB_5), F(PB_4), G(PB_3);
00015 DigitalOut sel1(PB_6), sel2(PC_7);
00016 
00017 char hex_number[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
00018 DigitalOut display[7] = {A, B, C, D, E, F, G};
00019 
00020 char volume = 0;
00021 
00022 //directive
00023 #define PERIOD_US0 5000.f
00024 #define PERIOD_US1 50.f
00025 #define SCALAR 4950.f
00026 
00027 #define REFRESH_RATE_MS 6
00028 
00029 //functions
00030 void ISR_sw1(void){
00031     volume = pot1.read() * 100;
00032 }
00033 
00034 void hexToDisplay(char sel1_val, char sel2_val, char hex){    
00035     sel1.write(sel1_val);
00036     sel2.write(sel2_val);
00037     
00038     for(int i = 6; i >= 0; i--)
00039         display[i] = (1<<i) & hex;
00040 }
00041 
00042 void currNumToDisplay(char num){
00043     char dec = num / 10;
00044     char dig = num % 10;
00045     
00046     if(dec){
00047         hexToDisplay(0, 1, hex_number[dec]);
00048         wait_ms(REFRESH_RATE_MS);
00049     }
00050     hexToDisplay(1, 0, hex_number[dig]);
00051     wait_ms(REFRESH_RATE_MS);
00052     
00053     
00054     hexToDisplay(1, 1, 0);
00055 }
00056 
00057 //main function
00058 int main(){
00059     volume = pot1.read() * 100;
00060     
00061     sw1.fall(&ISR_sw1);
00062     
00063     while(true){
00064             buzzer.period_us(PERIOD_US0 - SCALAR * pot2.read());
00065             buzzer.write(pot1.read());
00066             
00067             currNumToDisplay(volume);
00068             
00069             wait_ms(10);
00070     }
00071 }