malcolm lear / Mbed 2 deprecated PWRMONV01

Dependencies:   USBDevice mbed

Fork of THzBiasControl08 by malcolm lear

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBSerial.h"
00003 
00004 AnalogIn   ain(p20);      // read load voltage
00005 AnalogOut  aout(p18);     // set load current
00006 USBSerial serial;         // USB serial communication
00007 
00008 float io = 0;             // load current 0 to 1023
00009 int vi;                   // load voltage
00010 int t, i, e, len;
00011 char CmdIn[128];
00012 char CmdIL[] = "IL=";     // set load current
00013 char CmdVL[] = "VL";      // return load voltage
00014 char CmdID[] = "ID";      // Identification
00015 
00016 void SetCurrent(float d) {
00017     d = d - (d / 10) - 10; // Bodge it !!
00018     d = d / 1024;         //
00019     aout = d;
00020 }
00021 
00022 int main() {
00023     SetCurrent(io);
00024     while(1)
00025     {
00026         e = 1;            // set error
00027         serial.scanf("%s", CmdIn);
00028         if (strcmp(CmdIn, CmdVL) == 0) {
00029             vi = ain * 1024;
00030             serial.printf("%d\n", vi);           
00031             e = 0;
00032         }
00033         if (strncmp(CmdIn, CmdIL, 3) == 0) {
00034             io = 0;
00035             len = strlen(CmdIn);
00036             for(i=3; i<len; i++){
00037                 t = CmdIn[i] - '0';
00038                 if ((t >= 0) && (t <= 9)) { 
00039                     io = io * 10 + t; 
00040                 }
00041                 else {
00042                     io = -1;   // on input error make v out of range
00043                     break;
00044                 }
00045             }
00046             if ((io >= 0) && (io <= 1023)) {
00047                 SetCurrent(io);
00048                 e = 0;  
00049             }
00050         }
00051         if (strcmp(CmdIn, CmdID) == 0) {
00052             serial.printf( "Load Monitor V1.0\n");
00053             e = 0;
00054         }
00055         if (e == 1) {
00056             serial.printf( "Command Error ");
00057             serial.printf("%s\n", CmdIn);
00058         }
00059         else {
00060             serial.printf( "Command Executed\n");  
00061         }
00062         wait(1);
00063     }
00064 }