easytronic
/
Nucleo_read_analog_value
Read an analog value using ADC.
Diff: main.cpp
- Revision:
- 4:cad5a10b7007
- Parent:
- 3:999285691154
--- a/main.cpp Mon Jan 18 05:14:42 2016 +0000 +++ b/main.cpp Mon Mar 07 05:28:28 2016 +0000 @@ -1,28 +1,193 @@ +#define NSAMPLES 2500 +#define NSAMPLING 50 +#define V_max 3.3 + #include "mbed.h" #include "TextLCD.h" -TextLCD lcd(p9,p10); -AnalogIn analog_value(p20); -Serial pc ( USBTX , USBRX ) ; -DigitalOut led(LED1); + +TextLCD lcd(I2C_SDA,I2C_SCL); +//Serial pc ( USBTX , USBRX ) ; +//DigitalOut led(LED1); + +DigitalIn sw_start(D10,PullUp); +DigitalIn sw_stop(D11,PullUp); +DigitalOut motor(D9); +DigitalOut solenoid(D7); +DigitalIn lim_1(D2,PullUp); +DigitalIn lim_2(D3,PullUp); +DigitalIn lim_3(D4,PullUp); +AnalogIn ain_1(A0); + + +double vref_1 = 0.0; +//double v_read=0; +double v_max; + +int readc=100; +char sequence=0; + +void motorcon(); +void press_separuh(); +void press_penuh(); +void current_disp(); + int main() { - float meas; - - printf("\nAnalogIn example\n"); + + solenoid=1; + //acquiring v_ref + for (int i=0; i<NSAMPLES; i++) { + vref_1 += ain_1; + } + vref_1 /= NSAMPLES; + lcd.printf("Vref 1: %.4f\n", vref_1); + //lcd.printf("AnalogIn example"); + // lcd.locate(0,1); + // lcd.printf("TIN CRUSHER"); + wait(3); while(1) { - meas = analog_value; // Converts and read the analog input value (value from 0.0 to 1.0) - meas = meas * 3.3; // Change the value to be in the 0 to 3300 range - printf("measure = %.3f V\n", meas); - lcd.locate(4,2); - lcd.printf("measure = %.3f V", meas); - if (meas > 2) { // If the value is greater than 2V then switch the LED on - led = 1; - } - else { - led = 0; - } - wait(0.5); // 200 ms + //meas = analog_value; // Converts and read the analog input value (value from 0.0 to 1.0) + // value = analog_value* 3300; // Change the value to be in the 0 to 3300 range + // printf("measure = %.3f V\n", meas); + // lcd.locate(0,0); + // lcd.printf("measure = %d mV \n", value); + // lcd.printf("diff = %d mV \n", value-start_value); + // if((value >0 && value>oldvalue )|| (value<0 && value<oldvalue )) + // lcd.printf("peak = %d mV ",value-start_value); + // oldvalue=value; + // lcd.locate(0,2); + // motorcon(); + // lcd.printf("value %d",value); + current_disp(); + v_max=0; //clear v_max + motorcon(); + /// wait(0.5); // 200 ms + + } +} + +void motorcon() +{ char i=0; + if(sw_start==0) + { while(sw_start==0) + { wait(0.1); + i++; ///tekan kurang dari satu sec gerak separuh + } + if(i>5) + press_penuh(); + else + press_separuh(); + + //while(sw_start==0); /// selagi tekan duk sini + } + else if(sw_stop==1) + { motor=0; + while(sw_stop==1); /// selagi tekan duk sini + } } + + +void press_separuh() +{ while(1) + { motor=1; + lcd.cls(); + lcd.printf("press separuh\n"); + lcd.printf("init pos\n"); + + while(lim_1==1) + { current_disp(); + if(sw_stop==1) + break; + } + + solenoid=0; + lcd.printf("press down\n"); + while(lim_2==1) + { current_disp(); + if(sw_stop==1) + break; + } + + solenoid=1; + lcd.printf("up position\n"); + while(lim_1==1) + { current_disp(); + if(sw_stop==1) + break; + } + + lcd.cls(); + motor=0; + break; + } +} + + + + +void press_penuh() +{ while(1) + { motor=1; + lcd.cls(); + lcd.printf("press penuh\n"); + lcd.printf("init pos\n"); + + while(lim_1==1) + { current_disp(); + if(sw_stop==1) + break; + } + + solenoid=0; + lcd.printf("press down\n"); + while(lim_3==1) + { current_disp(); + if(sw_stop==1) + break; + } + + solenoid=1; + lcd.printf("up position\n"); + while(lim_1==1) + { current_disp(); + if(sw_stop==1) + break; + } + + lcd.cls(); + motor=0; + break; + } +} + +void current_disp() +{ // for (int i=0; i<NSAMPLING; i++) { + //acquisition + double sum_1 = 0.0; + //long v_max; + + for (int samples = 0; samples<NSAMPLES; samples++) { + sum_1 += ain_1; + } + sum_1 /= NSAMPLES; + sum_1 = (sum_1-vref_1)*V_max/0.185; + if(sum_1>0 && sum_1>v_max ) + v_max=sum_1; + //vread + lcd.locate(1,2); + lcd.printf("c.max"); + lcd.locate(8,2); + lcd.printf("%.4f A ", v_max);//max + lcd.locate(1,3); + lcd.printf("cbace"); + lcd.locate(8,3); + lcd.printf("%.4f A ", sum_1);//current reading + + + + // } + +}