lab2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
csggreen
Date:
Fri Nov 24 04:08:33 2017 +0000
Commit message:
lab2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 6ebe2b838587 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 24 04:08:33 2017 +0000
@@ -0,0 +1,284 @@
+#include "mbed.h"
+
+AnalogIn analog_value(A1);//อานค่าAnalogจากขาA1
+
+DigitalOut gsled0(D2);
+DigitalOut gsled1(D3);
+DigitalOut gsled2(D4);
+DigitalOut gsled3(D5);
+DigitalOut gsled4(D6);
+DigitalOut gsled5(D7);
+DigitalOut gsled6(D8);
+DigitalOut gsled7(D9);
+
+DigitalOut gsled8(D10);
+DigitalOut gsled9(D11);
+DigitalOut gsled10(D12);
+DigitalOut gsled11(D13);
+
+DigitalOut gsled12(D14);
+DigitalOut gsled13(D15);
+
+DigitalIn sw_in(USER_BUTTON);
+
+float formvolume;//รับค่าจากR
+float mVolt;//รับค่าที่แปลงแล้วหน่วยmV
+int Volt;
+int Volt_f;
+int Volt_i ;
+int x=0;
+/*
+void ReadVolt(){
+        formvolume = analog_value.read();
+        mVolt = formvolume * 3300;
+        Volt = mVolt/1000;
+        Volt_i = Volt*1;
+        Volt_f = 0.1 % (Volt - Volt_i);
+        //printf("measure = %f mV + %f V\n", mVolt,Volt);
+    }*/
+    
+void ReadVolt(){
+        formvolume = analog_value.read();
+        mVolt = formvolume * 3300;
+        Volt = mVolt/100;
+        Volt_i = (Volt-(Volt%10))/10;
+        Volt_f = Volt%10;
+    }
+
+void LEDoutput(){
+        if (mVolt>0&&mVolt<412){
+             gsled0 = 1;
+        }
+        if (mVolt>=412&&mVolt<825){
+             gsled0 = 1;
+             gsled1 = 1;   
+        }
+        if (mVolt>=825&&mVolt<1237){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+        }
+        if (mVolt>=1237&&mVolt<1650){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+             gsled3 = 1;
+        }
+        if (mVolt>=1650&&mVolt<2062){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+             gsled3 = 1;
+             gsled4 = 1;
+        }
+        if (mVolt>=2062&&mVolt<2475){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+             gsled3 = 1;
+             gsled4 = 1;
+             gsled5 = 1;
+        }
+        if (mVolt>=2475&&mVolt<2887){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+             gsled3 = 1;
+             gsled4 = 1;
+             gsled5 = 1;
+             gsled6 = 1;
+        }
+        if (mVolt>=2887&&mVolt<3300){
+             gsled0 = 1;
+             gsled1 = 1;
+             gsled2 = 1;
+             gsled3 = 1;
+             gsled4 = 1;
+             gsled5 = 1;
+             gsled6 = 1;
+             gsled7 = 1;
+        }
+        else{
+             gsled0 = 0;
+             gsled1 = 0;
+             gsled2 = 0;
+             gsled3 = 0;
+             gsled4 = 0;
+             gsled5 = 0;
+             gsled6 = 0;
+             gsled7 = 0;
+            }
+    }
+void iNumber0(){//int_Number
+    //gsled0 = 0;
+    //gsled1 = 0;
+    gsled8 = 0;
+    gsled9 = 0;
+    }
+void iNumber1(){
+    //gsled0 = 0;
+    //gsled1 = 0;
+    gsled8 = 0;
+    gsled9 = 1;
+    }
+void iNumber2(){
+    //gsled0 = 0;
+    //gsled1 = 0;
+    gsled8 = 1;
+    gsled9 = 0;
+    }
+void iNumber3(){
+    //gsled0 = 0;
+    //gsled1 = 0;
+    gsled8 = 1;
+    gsled9 = 1;
+    }
+void fNumber0(){//float Number
+    gsled10 = 0;
+    gsled11 = 0;
+    gsled12 = 0;
+    gsled13 = 0;
+    }
+void fNumber1(){//float Number
+    gsled10 = 0;
+    gsled11 = 0;
+    gsled12 = 0;
+    gsled13 = 1;
+    }
+void fNumber2(){//float Number
+    gsled10 = 0;
+    gsled11 = 0;
+    gsled12 = 1;
+    gsled13 = 0;
+    }
+void fNumber3(){//float Number
+    gsled10= 0;
+    gsled11 = 0;
+    gsled12 = 1;
+    gsled13 = 1;
+    }
+void fNumber4(){//float Number
+    gsled10 = 0;
+    gsled11 = 1;
+    gsled12 = 0;
+    gsled13 = 0;
+    }
+void fNumber5(){//float Number
+    gsled10 = 0;
+    gsled11 = 1;
+    gsled12 = 0;
+    gsled13 = 1;
+    }
+void fNumber6(){//float Number
+    gsled10 = 0;
+    gsled11 = 1;
+    gsled12 = 1;
+    gsled13 = 0;
+    }
+void fNumber7(){//float Number
+    gsled10 = 0;
+    gsled11 = 1;
+    gsled12 = 1;
+    gsled13 = 1;
+    }
+void fNumber8(){//float Number
+    gsled10 = 1;
+    gsled11 = 0;
+    gsled12 = 0;
+    gsled13 = 0;
+    }
+void fNumber9(){//float Number
+    gsled10 = 1;
+    gsled11 = 0;
+    gsled12 = 0;
+    gsled13 = 1;
+    }
+
+void floatfunction(){
+    if (Volt_f==0){
+        fNumber0();
+        }
+    else if (Volt_f==1){
+        fNumber1();
+        }
+    else if (Volt_f==2){
+        fNumber2();
+        }
+    else if (Volt_f==3){
+        fNumber3();
+        }
+    else if (Volt_f==4){
+        fNumber4();
+        }
+    else if (Volt_f==5){
+        fNumber5();
+        }
+    else if (Volt_f==6){
+        fNumber6();
+        }
+    else if (Volt_f==7){
+        fNumber7();
+        }
+    else if (Volt_f==8){
+        fNumber8();
+        }
+    else if (Volt_f==9){
+        fNumber9();
+        }
+}
+
+void segmentoutput(){
+    if (Volt_i==0){
+        iNumber0();
+        floatfunction();
+        }
+    else if(Volt_i==1){
+        iNumber1();
+        floatfunction();
+        }
+    else if(Volt_i==2){
+        iNumber2();
+        floatfunction();
+        }
+    else if(Volt_i==3){
+        iNumber3();
+        floatfunction();
+        }
+    }
+    
+int main()
+{
+   while(1) {
+        ReadVolt();
+        if(sw_in==0){
+            while(sw_in==0);
+            x+=1;
+            if(x>=2){
+                x=0;
+            }
+        wait(0.2);
+        }
+        if (x==0){
+        segmentoutput();
+        }
+        if (x==1){
+        LEDoutput();
+        }
+    /*while(1) {
+        ReadVolt();
+        if(sw_in==1){
+            x=1;
+        }
+        else if(sw_in==0){
+            x=0;
+        }
+        if (x==0){
+            segmentoutput();
+            }
+        else if (x==1){
+            LEDoutput();
+            }*/
+    }
+}
+
+
diff -r 000000000000 -r 6ebe2b838587 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Nov 24 04:08:33 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/fb8e0ae1cceb
\ No newline at end of file