lab2_4

Dependencies:   mbed 2_4_PWM_Ouput

Dependents:   2_4_PWM_Ouput

Committer:
shih775007
Date:
Wed Mar 01 10:09:34 2017 +0000
Revision:
1:4340a1ec8b5c
Parent:
0:543a9d21e043
quiz1_2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shih775007 0:543a9d21e043 1 #include "mbed.h"
shih775007 0:543a9d21e043 2 PwmOut PWM1(D6);
shih775007 1:4340a1ec8b5c 3 Serial pc( USBTX, USBRX );
shih775007 1:4340a1ec8b5c 4 AnalogIn Ain(A0);
shih775007 1:4340a1ec8b5c 5 float ADCdata;
shih775007 0:543a9d21e043 6 int main() {
shih775007 1:4340a1ec8b5c 7 int on = 0;
shih775007 1:4340a1ec8b5c 8 int off = 0;
shih775007 1:4340a1ec8b5c 9 float Cycle = 0.0;
shih775007 1:4340a1ec8b5c 10 PWM1.period(0.5);
shih775007 0:543a9d21e043 11 PWM1 = 0.5;
shih775007 1:4340a1ec8b5c 12 while(1){
shih775007 1:4340a1ec8b5c 13 ADCdata = Ain;
shih775007 1:4340a1ec8b5c 14 if(ADCdata > 0.5){
shih775007 1:4340a1ec8b5c 15 on = on + 1;
shih775007 1:4340a1ec8b5c 16 }
shih775007 1:4340a1ec8b5c 17 else{
shih775007 1:4340a1ec8b5c 18 off = off + 1.0;
shih775007 1:4340a1ec8b5c 19 }
shih775007 1:4340a1ec8b5c 20 Cycle = float(on)/(float(off)+float(on));
shih775007 1:4340a1ec8b5c 21 pc.printf("%f\r\n", Cycle);
shih775007 1:4340a1ec8b5c 22 //pc.printf("%1.3f\r\n", off);
shih775007 1:4340a1ec8b5c 23 wait(0.1);
shih775007 1:4340a1ec8b5c 24 }
shih775007 0:543a9d21e043 25 }