Sample program for LPC1768. This works with LPC1114 DisatnceSensorUnitProgram.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <cmath>
00003 
00004 DigitalOut myled(LED1);
00005 Serial pc(USBTX, USBRX);
00006 Serial com_lpc1114(p13, p14);
00007 
00008 LocalFileSystem local("local");  // define Mount Point(which becomes Direcory Path)
00009 
00010 void echoback()
00011 {
00012     char str[6] = "HELLO";
00013 
00014     com_lpc1114.putc(0x11); //Functional
00015     com_lpc1114.putc(6);    //size of echo
00016     pc.printf("echoback: ");
00017     for(char ch = 0; ch < 6; ch++) {
00018         com_lpc1114.putc(str[ch]);
00019         pc.printf("%c",com_lpc1114.getc());
00020     }
00021     pc.putc('\n');
00022 
00023 }
00024 
00025 int main()
00026 {
00027     FILE* fp;
00028     char cfp;
00029     unsigned short dtc;
00030     char ctmp;
00031     unsigned short dtmp;
00032 
00033     pc.printf("\nPROGRAM BEGIN\n");
00034 
00035     pc.printf("\nselect ch.0\n");
00036     com_lpc1114.putc(0);
00037 
00038     cfp = pc.getc();
00039     if(cfp == 'l') {
00040         fp = fopen("/local/SENSOR00.SAV", "rb");
00041         if (fp == NULL) pc.printf("\nfopen failured\n");
00042 
00043         pc.printf("\nload save data to lpc1114\n");
00044         com_lpc1114.putc('l');
00045         pc.printf("ACK recieved :");
00046         //for(int i = 0; i < 9 * 256 + 3 + 6 * 5; i++) {
00047         int i = 0;
00048 
00049         while(1) {
00050             fread(&cfp, sizeof(char), 1, fp);
00051             com_lpc1114.putc(cfp);
00052             if(i % 3 == 2) {
00053                 cfp = com_lpc1114.getc();
00054                 if (cfp == 0x06) {
00055                     for(double j = 0.0; j < log10(static_cast<double>(i / 3 + 1)); j += 1.0)
00056                         pc.putc(0x08);
00057                     pc.printf("%d", i / 3 + 1);
00058                     //pc.getc();
00059                 }//
00060                 else if (cfp == 0x04) {
00061                     //cfp = com_lpc1114.getc();
00062                     //if (cfp == 0x04) {
00063                     pc.printf("\nSUCCESSED");
00064                     break;
00065                     //}
00066                 }
00067             }
00068             i++;
00069         }
00070         fclose(fp);
00071     }
00072 
00073     else if (cfp == 'c') {
00074         fp = fopen("/local/SENSOR00.SAV", "wb");
00075         if (fp == NULL) pc.printf("\nfopen failured\n");
00076 
00077         com_lpc1114.putc('c');
00078         pc.printf("sampling:0");
00079         for(int i = 0; i < 5; i++){
00080             pc.putc(0x08);
00081             pc.printf("%d", com_lpc1114.getc());
00082         }
00083         pc.putc('\n');
00084         pc.printf("\nsave data from lpc1114\n");
00085         while(1) {
00086             ctmp = com_lpc1114.getc();
00087             if(ctmp == 0x04)break;
00088             dtmp = (static_cast<unsigned short>(ctmp) << 8);
00089             ctmp = com_lpc1114.getc();
00090             dtmp = dtmp | ctmp;
00091             ctmp = com_lpc1114.getc();
00092             com_lpc1114.putc(0x06);
00093             fwrite(&dtmp, sizeof(unsigned short), 1, fp);
00094             fwrite(&ctmp, sizeof(char), 1, fp);
00095         }
00096         pc.printf("\nSUCCESSED");
00097 
00098         fclose(fp);
00099     }
00100 
00101 
00102 
00103     pc.printf("\nLOOP\n");
00104 
00105     echoback();
00106 
00107     printf("dtc: 000");
00108     while(1) {
00109         myled = 1;
00110         wait(0.2);
00111         com_lpc1114.putc(0x12);//Functional
00112         dtc = com_lpc1114.getc();
00113         dtc = ((dtc << 8) | com_lpc1114.getc());
00114         for(int j = 0; j < 3; j++)
00115             pc.putc(0x08);
00116         printf("%3d", dtc);
00117         myled = 0;
00118         wait(0.2);
00119         if(pc.readable()) {
00120             pc.getc();
00121             pc.putc('\n');
00122             break;
00123         }
00124     }
00125 
00126 }