detail : http://www.mcugear.com/

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* MCU Gear Library, only for testing MCUGear without any circuit you connected.
00002  * Copyright (c) 2013, NestEgg Inc., http://www.mcugear.com/
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "mbed.h"
00024 #include "TextLCD.h"
00025 
00026 #include "MCUGearBase.h"
00027 #include "MCUGear.h"
00028 #include "common.h"
00029 
00030 Serial pc(USBTX, USBRX); // tx, rx Set BaudRate = 115200 in main()
00031 
00032  //Make instance. Set I2C pins, Module address and Connector numnber of Modules.
00033 
00034 #ifdef LPC1768_mbed
00035 //caution: Do not use I2C(p28 & p27). It is reserved in this system with high speed 1.5MHz I2C.
00036 //If you want to use I2C, you should change I2C clock.
00037 // or use multi-function module.
00038 
00039 //Address setting:you can see the detail on common.h file.
00040 //address format : (Type of device)_AD2pin_AD1pin_AD0pin
00041 //N(PCA9674) A(PCA9674A) (VDD = +3V3  VSS = GND)
00042 
00043 //Initialize modules-----------------------------------------------------------------------
00044 MCUGear AD12M(p28, p27, N_SCL_SCL_VSS,4);       //AD(MCP3202) 12bit Module : Set number of module pin 
00045 MCUGear AD8ch12M(p28, p27, N_SDA_SDA_SDA,4);    //AD(MCP3208) 12bit 8ch Module
00046 MCUGear DA12M(p28, p27, N_SCL_SCL_SCL,4);       //DA(MCP4901) 12bit Module
00047 MCUGear LCDM(p28, p27, N_VDD_VSS_VSS,6);        //LCD Module
00048 MCUGear SHM(p28, p27, N_VDD_VSS_VDD,8);         //Signal Hold Module
00049 MCUGear Uni2M(p28, p27, N_VDD_VDD_VDD,8);       //2 Connector Universal Module
00050 MCUGear MFM(p28, p27, N_VSS_VSS_VSS,4);         //Multifunction Module
00051 //-----------------------------------------------------------------------------------------
00052 
00053 //Sample setting-----------------------------------------------------------------------
00054 //TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD16x2); // rs, e, d4-d7
00055 SPI spi(p5, p6, p7); // mosi, miso, sclk
00056 DigitalOut cs(p8); //for SPI communications
00057 DigitalOut Dout(p15);//for 12bit DA module
00058 BusOut SignalHold (p16,p21,p22,p23,p24,p25,p26);
00059 
00060 DigitalOut Dout1(p16);
00061 DigitalOut Dout2(p21);
00062 DigitalOut Dout3(p22);
00063 DigitalOut Dout4(p23);
00064 DigitalOut Dout5(p24);
00065 DigitalIn Din1(p9);
00066 DigitalIn Din2(p10);
00067 
00068 //-------------------------------------------------------------------------------------
00069 #endif
00070 
00071 
00072 #ifdef FS_KL25Z
00073 //Initialize modules-----------------------------------------------------------------------
00074 MCUGear AD12M(PTE0, PTE1, N_SCL_SCL_VSS,4);       //AD(MCP3202) 12bit Module : Set number of module pin 
00075 MCUGear AD8ch12M(PTE0, PTE1, N_SDA_SDA_SDA,4);    //AD(MCP3208) 12bit 8ch Module
00076 MCUGear DA12M(PTE0, PTE1, N_SCL_SCL_SCL,4);       //DA(MCP4901) 12bit Module
00077 MCUGear LCDM(PTE0, PTE1, N_VDD_VSS_VSS,6);        //LCD Module
00078 MCUGear SHM(PTE0, PTE1, N_VDD_VSS_VDD,8);         //Signal Hold Module
00079 MCUGear Uni2M(PTE0, PTE1, N_VDD_VDD_VDD,8);       //2 Connector Universal Module
00080 MCUGear MFM(PTE0, PTE1, N_VSS_VSS_VSS,4);         //Multifunction Module
00081 //-----------------------------------------------------------------------------------------
00082 
00083 //Sample setting-----------------------------------------------------------------------
00084 //TextLCD lcd(PTA1, PTA2, PTD4, PTA12, PTA4, PTA5, TextLCD::LCD16x2); // rs, e, d4-d7
00085 SPI spi(PTD2,PTD3,PTD1); // mosi, miso, sclk
00086 DigitalOut cs(PTD0); //for SPI communications
00087 DigitalOut Dout(PTA13);//for 12bit DA module
00088 BusOut SignalHold (PTB0, PTB1, PTD4, PTA12, PTA4, PTA5, PTC8);
00089 
00090 DigitalOut Dout1(PTB0);
00091 DigitalOut Dout2(PTB1);
00092 DigitalOut Dout3(PTD4);
00093 DigitalOut Dout4(PTA12);
00094 DigitalOut Dout5(PTA4);
00095 DigitalIn Din1(PTB0);
00096 DigitalIn Din2(PTB1);
00097 #endif
00098 
00099 //sample functions------------------------------------------------------------
00100 void IOSimpleSPI(MCUGear *mcugear);             //IO setting for Simple SPI module
00101 void IO12bitDA(MCUGear *mcugear);               //IO setting for DA 12bit module
00102 void IOLCD(MCUGear *mcugear);                   //IO setting for LCD module
00103 void IOSHM(MCUGear *mcugear);                   //IO setting for Signal Hold module
00104 void IOUni2M(MCUGear *mcugear);                 //IO setting for 2 Connector Universal module 
00105 
00106 int read12bitAD(MCUGear *mcugear, char ch);     //simple ADC 
00107 void write12bitDA(MCUGear *mcugear, int data);  //simple DA
00108 int read12bit8chAD(MCUGear *mcugear, char ch);  //simple ADC 8ch
00109 
00110 #ifdef AD_MODE //for Multifunction Module
00111 int read10bitAD(MCUGear *mcugear, int ch);
00112 #endif
00113 
00114 #ifdef PWM_MODE //for Multifunction Module
00115 void initPWM(MCUGear *mcugear, unsigned int Divider, unsigned int friquency, unsigned int duty0, unsigned int duty1, unsigned int duty2);
00116 void PWMfriq(MCUGear *mcugear, unsigned int friquency);
00117 void PWMDuty(MCUGear *mcugear, int ch, unsigned int Duty);
00118 void StopPWM(MCUGear *mcugear);
00119 void StartPWM(MCUGear *mcugear);
00120 #endif
00121 //----------------------------------------------------------------------------
00122 
00123 
00124 //***************************************************************************************
00125 int main() {
00126 
00127     int SW1,SW2;
00128 
00129 //    pc.printf("hello world!!!");
00130     
00131     initBase(); //initialize Baseboard
00132     
00133 
00134    //Set IO --------------------------------------------------------------
00135 
00136     IOSimpleSPI(&AD12M);
00137     IOSimpleSPI(&AD8ch12M);
00138     IO12bitDA(&DA12M);
00139     IOLCD(&LCDM);
00140     IOSHM(&SHM);
00141     IOUni2M(&Uni2M);
00142     IOSimpleSPI(&MFM);    //IO setting for Multifunction module
00143 
00144    //---------------------------------------------------------------------
00145    wait(1);
00146    
00147     LCDM.connectModule();
00148  #ifdef LPC1768_mbed
00149     TextLCD lcd(p21, p22, p23, p24, p25, p26, TextLCD::LCD16x2); // rs, e, d4-d7
00150  #endif
00151  
00152  #ifdef FS_KL25Z
00153     TextLCD lcd(PTB0, PTB1, PTD4, PTA12, PTA4, PTA5, TextLCD::LCD16x2); // rs, e, d4-d7
00154  #endif
00155  
00156     lcd.cls();
00157     lcd.printf("Hello World!\n");
00158     wait(1);
00159     LCDM.disconnectModule();
00160 
00161 
00162 
00163     int data1[2];
00164     int data2[8];
00165     int data3[3];
00166     uint8_t busData = 0;
00167 
00168 #ifdef PWM_MODE
00169 
00170     uint32_t PWM_data = 0x7FF;
00171     uint32_t PWM_data_old = 0;
00172     
00173     MFM.connectModule();   //connect---
00174     initPWM(&MFM, 24, 40950, 10, 10, 10);// 0-2msec 12bit 20msec for RC servosetting 
00175     //initPWM(&MFM, 120, 4095, 20, 50, 80);//Simple 10msec PWM
00176     //initPWM(&MFM, 48, 26, 70, 70, 70);//38kHz 26usec for IR LED
00177     MFM.disconnectModule();   //disconnect---
00178 
00179 #endif    
00180     
00181     while(1) {
00182        //12bit AD Mobdule-------------------------------------------------
00183 
00184        cs = 1; //reset CS pin
00185        AD12M.connectModule();   //connect---
00186        data1[0] = read12bitAD(&AD12M, 0);
00187        data1[1] = read12bitAD(&AD12M, 1);
00188        AD12M.disconnectModule();   //disconnect---
00189        
00190        //12bit 8ch AD Module
00191        
00192        cs = 1; //reset CS pin
00193        AD8ch12M.connectModule();   //connect---
00194        data2[0] = read12bit8chAD(&AD8ch12M, 0);    //0ch
00195        data2[1] = read12bit8chAD(&AD8ch12M, 1);    //1ch
00196        data2[2] = read12bit8chAD(&AD8ch12M, 2);    //2ch
00197        data2[3] = read12bit8chAD(&AD8ch12M, 3);    //3ch
00198        data2[4] = read12bit8chAD(&AD8ch12M, 4);    //4ch
00199        data2[5] = read12bit8chAD(&AD8ch12M, 5);    //5ch
00200        data2[6] = read12bit8chAD(&AD8ch12M, 6);    //6ch
00201        data2[7] = read12bit8chAD(&AD8ch12M, 7);    //7ch
00202        AD8ch12M.disconnectModule();   //disconnect---
00203        
00204 #ifdef AD_MODE //for Multifunction Module
00205        cs = 1; //reset CS pin
00206        MFM.connectModule();   //connect---
00207        data3[0] = read10bitAD(&MFM, 1); //1ch
00208        data3[1] = read10bitAD(&MFM, 2); //2ch
00209        data3[2] = read10bitAD(&MFM, 3); //3ch
00210        MFM.disconnectModule();   //disconnect---
00211 #endif
00212 
00213 #ifdef PWM_MODE //for Multifunction Module
00214 
00215         cs = 1; //reset CS pin
00216         MFM.connectModule();   //connect---
00217         
00218         //RC Servo and Simple PWM
00219 /*        PWMDuty(&MFM, 1, 0x7FF);    //duty 50%
00220         PWMDuty(&MFM, 2, 0x7FF);
00221         PWMDuty(&MFM, 3, 0x7FF);
00222 */
00223  
00224         if(PWM_data > 0xFFF){
00225             PWM_data = 0x7FF;
00226         }
00227         
00228         //RC Servo and Simple PWM
00229         PWMDuty(&MFM, 1, 0x7FF);    //duty 50%
00230         
00231         if(PWM_data_old != PWM_data){
00232             PWMDuty(&MFM, 2, PWM_data);
00233             PWM_data_old = PWM_data;
00234             
00235         }
00236         
00237         //PWMDuty(&MFM, 3, PWM_data);
00238         PWMDuty(&MFM, 3, data1[0]+0x7FF);
00239         //mwait01(10000);   //If the system is heavy or not, you should adjust the value.
00240 
00241         PWM_data = PWM_data + 0x0FF;
00242 
00243         //Sample for IR LED 
00244 /*
00245         StopPWM(&MFM);
00246         wait_us(10);
00247         StartPWM(&MFM);
00248         wait_us(10);        
00249         StopPWM(&MFM);
00250         wait_us(10);
00251         StartPWM(&MFM);
00252         wait_us(10);        
00253         StopPWM(&MFM);
00254 */
00255         MFM.disconnectModule();   //disconnect---
00256 
00257 #endif        
00258        
00259 
00260        //LCD Module------------------------------------------------------
00261        LCDM.connectModule();   //connect---
00262        lcd.printf(" ");    //set IO as a neutral position
00263        lcd.cls();   //clear LCD
00264        lcd.printf("A%d B%d\nC%d D%d",data1[0],data1[1],data2[0],data2[6]);   //write LCD data
00265        wait_ms(10);    //delay for view the LCD
00266        LCDM.disconnectModule();   //disconnect---
00267 
00268        //12bit 2ch AD
00269        //pc.printf("0ch%d, 1ch%d,  ",data1[0],data1[1]);
00270        //12bit 8ch AD
00271        pc.printf("0ch%d, 1ch%d, 2ch%d, 3ch%d, 4ch%d, 5ch%d, 6ch%d, 7ch%d,  ",data2[0],data2[1],data2[2],data2[3],data2[4],data2[5],data2[6],data2[7]);
00272 #ifdef AD_MODE //for Multifunction Module
00273        pc.printf("0ch%d, 1ch%d, 2ch%d",data3[0],data3[1],data3[2]);
00274 #endif
00275        pc.printf("\r\n");
00276 
00277 
00278        //12bit SPI DA module---------------------------------------------
00279        cs = 1; //reset CS pin
00280        Dout=1;    //reset ratch signal
00281        DA12M.connectModule();   //connect---
00282        write12bitDA(&DA12M, data1[0]);
00283        DA12M.disconnectModule();   //disconnect---
00284 
00285 
00286        //Signal Hold module (TC74VHCT540AF)------------------------------
00287        Dout = 0;    //ratch standby
00288        SHM.connectModule();   //connect---
00289        
00290        SignalHold = busData;   //set 7bit bus out
00291        Dout = 1;    //ratch
00292        Dout = 0;    //ratch standby
00293        SHM.disconnectModule();   //disconnect---
00294        
00295        //2 connector universal module ------------------------------
00296        
00297        Dout = 1;
00298        Dout1 = 1;
00299        Dout2 = 1;
00300        Dout3 = 1;
00301        Dout4 = 1;
00302        Dout5 = 1;
00303        
00304        Uni2M.connectModule();   //connect---
00305        
00306        SW1 = Din1.read();
00307        SW2 = Din2.read();
00308        
00309        if(SW1 == 1){
00310             Dout = 1;
00311             Dout2 = 1;
00312             Dout3 = 1;
00313        }else{
00314             Dout = 0;
00315             Dout2 = 0;
00316             Dout3 = 0;
00317        }
00318        if(SW2 == 1){
00319             Dout1 = 1;
00320             Dout4 = 1;
00321             Dout5 = 1;
00322        }else{
00323             Dout1 = 0;
00324             Dout4 = 0;
00325             Dout5 = 0;
00326        }
00327        
00328        Uni2M.disconnectModule();   //disconnect---
00329 
00330        /*++busData;
00331        if(busData >= 0x80)
00332             busData = 0x00;
00333        */
00334        
00335        if(busData == 0x00)
00336             busData = 0x7F;
00337        
00338        --busData;
00339     }    
00340 }
00341 //***************************************************************************************
00342 
00343 
00344 //Smple functions
00345 //----------------------------------------------------------------------------------
00346 //Simple read 12bit 2ch AD module
00347 //----------------------------------------------------------------------------------
00348 int read12bitAD(MCUGear *mcugear, char ch){
00349 
00350     char sendData;
00351     int whoami[3];
00352     
00353 #ifdef LPC1768_mbed
00354     spi.frequency(1000000);//1MHz
00355     if(ch == 0){
00356         sendData = 0x06;
00357     }else if(ch == 1){
00358         sendData = 0x07;
00359     }else{
00360         return -1;
00361     }
00362     //12bit AD Mobdule----------------
00363 //    cs = 1; //reset CS pin
00364 //    mcugear->connectModule();   //connect---
00365     spi.format(14,0);    //Change SPI format mbed only
00366     cs = 0;
00367     spi.write(sendData);
00368     wait_us(1);
00369     whoami[0] = spi.write(0x2000)-0x2000;   //get data from AD device
00370     cs = 1;
00371 //    mcugear->disconnectModule();   //disconnect---
00372     return whoami[0];
00373 #endif
00374 
00375 #ifdef FS_KL25Z
00376     spi.frequency(2000000); //1MHz for KL25Z
00377     if(ch == 0){
00378         sendData = 0x80;
00379     }else if(ch == 1){
00380         sendData = 0xC0;
00381     }else{
00382         return -1;
00383     }
00384     
00385     //12bit AD Mobdule----------------
00386  //   cs = 1; //reset CS pin
00387  //   mcugear->connectModule();   //connect---
00388     
00389     cs = 0; 
00390     //FRDM KL25Z is 8-bit format.
00391     whoami[2] = spi.write(0x01);//0000 0001
00392     whoami[2] = spi.write(sendData);
00393     whoami[0] = whoami[2]<<8;
00394     whoami[0] = whoami[0] + spi.write(0x00) - 0xE000;//0000 0000
00395 //    whoami[2] = 0;//clear buffer
00396     cs = 1;
00397  //   mcugear->disconnectModule();   //disconnect---
00398     return whoami[0];
00399 #endif
00400 }
00401 
00402 //----------------------------------------------------------------------------------
00403 //IO setting for LCD module
00404 //----------------------------------------------------------------------------------
00405 void IOLCD(MCUGear *mcugear){
00406     uint8_t fio[12];
00407     mcugear->detect_module(fio);    // detect LCDM
00408 
00409 #ifdef LPC1768_mbed
00410     mcugear->savePinSetting(0, IO_MBED_P21, IO_REG_OUT_DIR, fio[0]);
00411     mcugear->savePinSetting(1, IO_MBED_P22, IO_REG_OUT_DIR, fio[1]);
00412     mcugear->savePinSetting(2, IO_MBED_P23, IO_REG_OUT_DIR, fio[2]);
00413     mcugear->savePinSetting(3, IO_MBED_P24, IO_REG_OUT_DIR, fio[3]);
00414     mcugear->savePinSetting(4, IO_MBED_P25, IO_REG_OUT_DIR, fio[4]);
00415     mcugear->savePinSetting(5, IO_MBED_P26, IO_REG_OUT_DIR, fio[5]);
00416 #endif
00417 
00418 #ifdef FS_KL25Z
00419     mcugear->savePinSetting(0, IO_MBED_PTB0, IO_REG_OUT_DIR, fio[0]);
00420     mcugear->savePinSetting(1, IO_MBED_PTB1, IO_REG_OUT_DIR, fio[1]);
00421     mcugear->savePinSetting(2, IO_MBED_PTD4, IO_REG_OUT_DIR, fio[2]);
00422     mcugear->savePinSetting(3, IO_MBED_PTA12, IO_REG_OUT_DIR, fio[3]);
00423     mcugear->savePinSetting(4, IO_MBED_PTA4, IO_REG_OUT_DIR, fio[4]);
00424     mcugear->savePinSetting(5, IO_MBED_PTA5, IO_REG_OUT_DIR, fio[5]);
00425 #endif
00426 
00427 #ifdef BOOST_MODE
00428     mcugear->makeCircuit();
00429 #endif
00430 }
00431 
00432 
00433 //----------------------------------------------------------------------------------
00434 //IO setting for 12bit DA module
00435 //----------------------------------------------------------------------------------
00436 void IO12bitDA(MCUGear *mcugear){
00437     uint8_t fio[12];
00438    //---------------------
00439     mcugear->detect_module(fio);
00440     //Dout(p15),mosi(p5),sck(p7),cs(p8)
00441 #ifdef LPC1768_mbed
00442     mcugear->savePinSetting(0, IO_MBED_P15, IO_REG_OUT_DIR, fio[0]);
00443     mcugear->savePinSetting(1, IO_MBED_P5, IO_REG_OUT_DIR, fio[1]);
00444     mcugear->savePinSetting(2, IO_MBED_P7, IO_REG_OUT_DIR, fio[2]);
00445     mcugear->savePinSetting(3, IO_MBED_P8, IO_REG_OUT_DIR, fio[3]);
00446 #endif
00447 
00448 #ifdef FS_KL25Z
00449     //Dout(PTA13),mosi(PTD2),sck(PTD1),cs(PTD0)
00450     mcugear->savePinSetting(0, IO_MBED_PTA13, IO_REG_OUT_DIR, fio[0]);
00451     mcugear->savePinSetting(1, IO_MBED_PTD2, IO_REG_OUT_DIR, fio[1]);
00452     mcugear->savePinSetting(2, IO_MBED_PTD1, IO_REG_OUT_DIR, fio[2]);
00453     mcugear->savePinSetting(3, IO_MBED_PTD0, IO_REG_OUT_DIR, fio[3]);
00454 #endif
00455 
00456 #ifdef BOOST_MODE
00457     mcugear->makeCircuit();
00458 #endif
00459 }
00460 
00461 //----------------------------------------------------------------------------------
00462 //Simple write for 12bit DA module
00463 //----------------------------------------------------------------------------------
00464 void write12bitDA(MCUGear *mcugear, int data){
00465 
00466        //12bit SPI DA module----------------
00467        
00468 #ifdef LPC1768_mbed
00469 //       spi.frequency(1000000); //1MHz
00470        spi.frequency(20000000); //20MHz
00471        spi.format(16,0);        //Change SPI format
00472 //       mcugear->connectModule();   //connect---
00473        cs = 0;
00474        spi.write((0x3000)+(data)); //write 0011 1000 0000 0000
00475 #endif
00476 
00477        //12bit SPI DA module----------------
00478 #ifdef FS_KL25Z
00479        //spi.frequency(2000000); //1MHz for KL25Z
00480        spi.frequency(20000000); //10MHz
00481        //connection
00482  //      mcugear->connectModule();   //connect---
00483        cs = 0;
00484        spi.write((0x30)+((data>>8)&0x0F));//write 0011 0000
00485        spi.write(data&0xFF);//write 0011 1000 0000 0000
00486 #endif
00487 
00488        wait_us(1);  //over 15nsec
00489        cs = 1;      //CS high
00490        wait_us(1);  //over 40nsec
00491        Dout=0;      //ratch for DA out enable
00492        wait_us(1);  //over 100nsec
00493        Dout=1;      //reset ratch signal
00494 //       mcugear->disconnectModule();   //disconnect---
00495 //       wait_us(1);  //wait after disconnect
00496 }
00497 
00498 //----------------------------------------------------------------------------------
00499 //Simple write for 12bit 8ch AD module
00500 //----------------------------------------------------------------------------------
00501 int read12bit8chAD(MCUGear *mcugear, char ch){
00502 
00503     char sendData;
00504     int whoami[3];
00505     
00506 #ifdef LPC1768_mbed
00507 //    if((ch >= 0 )&&(ch < 8)){
00508     if(ch < 8){
00509         sendData = 0x18+ch; //0001 1000
00510     }else{
00511         return -1;
00512     }
00513     
00514     //12bit AD Mobdule----------------
00515     spi.frequency(1000000); //1MHz
00516  //   cs = 1; //reset CS pin
00517  //   mcugear->connectModule();   //connect---
00518     spi.format(14,0);    //Change SPI format mbed only
00519     cs = 0;
00520     spi.write(sendData);
00521     whoami[0] = spi.write(0x0000)-0x2000;   //get data from AD device
00522     cs = 1;
00523 //    mcugear->disconnectModule();   //disconnect---
00524     return whoami[0];
00525 #endif
00526 
00527 #ifdef FS_KL25Z
00528     char sendData2;
00529 //0000 0000 0110 00 00 [0000 0000 0000]
00530 //    if((ch >= 0 )&&(ch < 8)){
00531     if(ch < 8){
00532         sendData = 0x18+ch; //0001 1000
00533         sendData2 = sendData >> 2;
00534         sendData = sendData << 6;
00535         
00536     }else{
00537         return -1;
00538     }
00539     
00540     //12bit AD Mobdule----------------
00541     spi.frequency(2000000); //1MHz for KL25Z
00542 //    cs = 1; //reset CS pin
00543 //    mcugear->connectModule();   //connect---
00544     cs = 0;
00545     spi.write(sendData2);
00546     whoami[2] = spi.write(sendData);
00547     whoami[0] = whoami[2]<<8;
00548     whoami[0] = whoami[0] + spi.write(0x00)-0xE000;   //get data from AD device
00549     cs = 1;
00550 //    mcugear->disconnectModule();   //disconnect---
00551     return whoami[0];
00552 
00553 
00554 #endif
00555 }
00556 
00557 
00558 //----------------------------------------------------------------------------------
00559 //Simple universal module
00560 //----------------------------------------------------------------------------------
00561 void IOSHM(MCUGear *mcugear){
00562 
00563     uint8_t fio[12];
00564     // detect SHM
00565     mcugear->detect_module(fio);
00566     //CLK for ratch(1), DigialOut(2-8)
00567 
00568 #ifdef LPC1768_mbed
00569     mcugear->savePinSetting(0, IO_MBED_P15, IO_REG_OUT_DIR, fio[0]);
00570     mcugear->savePinSetting(1, IO_MBED_P16, IO_REG_OUT_DIR, fio[1]);
00571     mcugear->savePinSetting(2, IO_MBED_P21, IO_REG_OUT_DIR, fio[2]);
00572     mcugear->savePinSetting(3, IO_MBED_P22, IO_REG_OUT_DIR, fio[3]);
00573     mcugear->savePinSetting(4, IO_MBED_P23, IO_REG_OUT_DIR, fio[4]);
00574     mcugear->savePinSetting(5, IO_MBED_P24, IO_REG_OUT_DIR, fio[5]);
00575     mcugear->savePinSetting(6, IO_MBED_P25, IO_REG_OUT_DIR, fio[6]);
00576     mcugear->savePinSetting(7, IO_MBED_P26, IO_REG_OUT_DIR, fio[7]);
00577 #endif
00578 
00579 #ifdef FS_KL25Z
00580     //PTA1, PTA2, PTD4, PTA12, PTA4, PTA5, PTC8
00581     mcugear->savePinSetting(0, IO_MBED_PTA13, IO_REG_OUT_DIR, fio[0]);
00582     mcugear->savePinSetting(1, IO_MBED_PTA1, IO_REG_OUT_DIR, fio[1]);
00583     mcugear->savePinSetting(2, IO_MBED_PTA2, IO_REG_OUT_DIR, fio[2]);
00584     mcugear->savePinSetting(3, IO_MBED_PTD4, IO_REG_OUT_DIR, fio[3]);
00585     mcugear->savePinSetting(4, IO_MBED_PTA12, IO_REG_OUT_DIR, fio[4]);
00586     mcugear->savePinSetting(5, IO_MBED_PTA4, IO_REG_OUT_DIR, fio[5]);
00587     mcugear->savePinSetting(6, IO_MBED_PTA5, IO_REG_OUT_DIR, fio[6]);
00588     mcugear->savePinSetting(7, IO_MBED_PTC8, IO_REG_OUT_DIR, fio[7]);
00589 #endif
00590  
00591 #ifdef BOOST_MODE
00592     mcugear->makeCircuit();
00593 #endif
00594 
00595 }
00596 
00597 //----------------------------------------------------------------------------------
00598 //Simple universal module
00599 //----------------------------------------------------------------------------------
00600 void IOUni2M(MCUGear *mcugear){
00601 
00602     uint8_t fio[12];
00603     // detect SHM
00604     mcugear->detect_module(fio);
00605     //CLK for ratch(1), DigialOut(2-8)
00606 
00607 #ifdef LPC1768_mbed
00608     mcugear->savePinSetting(0, IO_MBED_P9, IO_REG_IN_DIR, fio[0]);  //input
00609     mcugear->savePinSetting(1, IO_MBED_P10, IO_REG_IN_DIR, fio[1]); //input
00610     mcugear->savePinSetting(2, IO_MBED_P15, IO_REG_OUT_DIR, fio[2]);
00611     mcugear->savePinSetting(3, IO_MBED_P16, IO_REG_OUT_DIR, fio[3]);
00612     mcugear->savePinSetting(4, IO_MBED_P21, IO_REG_OUT_DIR, fio[4]);
00613     mcugear->savePinSetting(5, IO_MBED_P22, IO_REG_OUT_DIR, fio[5]);
00614     mcugear->savePinSetting(6, IO_MBED_P23, IO_REG_OUT_DIR, fio[6]);
00615     mcugear->savePinSetting(7, IO_MBED_P24, IO_REG_OUT_DIR, fio[7]);
00616  
00617 #endif
00618 
00619 #ifdef FS_KL25Z
00620     //PTA1, PTA2, PTD4, PTA12, PTA4, PTA5, PTC8
00621     mcugear->savePinSetting(0, IO_MBED_PTB0, IO_REG_IN_DIR, fio[0]);
00622     mcugear->savePinSetting(1, IO_MBED_PTB1, IO_REG_IN_DIR, fio[1]);
00623     mcugear->savePinSetting(2, IO_MBED_PTA13, IO_REG_OUT_DIR, fio[2]);
00624     mcugear->savePinSetting(3, IO_MBED_PTA1, IO_REG_OUT_DIR, fio[3]);
00625     mcugear->savePinSetting(4, IO_MBED_PTA2, IO_REG_OUT_DIR, fio[4]);
00626     mcugear->savePinSetting(5, IO_MBED_PTD4, IO_REG_OUT_DIR, fio[5]);
00627     mcugear->savePinSetting(6, IO_MBED_PTA12, IO_REG_OUT_DIR, fio[6]);
00628     mcugear->savePinSetting(7, IO_MBED_PTA4, IO_REG_OUT_DIR, fio[7]);
00629  
00630 #endif
00631  
00632 #ifdef BOOST_MODE
00633     mcugear->makeCircuit();
00634 #endif
00635 
00636 }
00637 
00638 
00639 
00640 //----------------------------------------------------------------------------------
00641 //Read from Multifunction module for 10bit AD mode
00642 //----------------------------------------------------------------------------------
00643 int read10bitAD(MCUGear *mcugear, int ch){
00644     //Multi-function module
00645     int setCh = 0;
00646     int getData;
00647     
00648  //   mcugear->connectModule();
00649     
00650     switch(ch){
00651         case 1:
00652             setCh = 3;
00653             break;
00654         case 2:
00655             setCh = 1;
00656             break;
00657         case 3:
00658             setCh = 2;
00659             break;
00660         
00661         default:
00662             return -1;
00663     }
00664     
00665 #ifdef LPC1768_mbed
00666     spi.frequency(1000000); //1MHz
00667     spi.format(16,0);
00668     cs = 0;
00669     spi.write(setCh);
00670     cs = 1;
00671     wait_us(1);
00672     cs = 0;
00673     getData = spi.write(0);
00674     cs = 1;
00675     wait_us(1);
00676 #endif
00677         
00678 #ifdef FS_KL25Z
00679     spi.frequency(2000000); //1MHz for KL25Z
00680     cs = 0;
00681     spi.write(0x00);
00682     spi.write(setCh);
00683     cs = 1;
00684     wait_us(1);
00685     cs = 0;
00686     getData = spi.write(0);
00687     getData = (getData<<8)+spi.write(0);
00688     cs = 1;
00689     wait_us(1);
00690 #endif
00691 
00692 
00693  //   mcugear->disconnectModule();
00694  //   wait_ms(1);
00695     return getData;
00696 }
00697 
00698 #ifdef PWM_MODE
00699 //----------------------------------------------------------------------------------
00700 // Initialize PWM
00701 //----------------------------------------------------------------------------------
00702 void initPWM(MCUGear *mcugear, unsigned int Divider, unsigned int friquency, unsigned int duty0, unsigned int duty1, unsigned int duty2){
00703 
00704  //   mcugear->connectModule();
00705 
00706 #ifdef LPC1768_mbed
00707     spi.frequency(1000000); //1MHz
00708     spi.format(16,0);
00709     cs = 0;
00710     //spi.write(4800);//Divider
00711     spi.write(Divider);//Divider
00712     cs = 1;
00713     wait_us(1);
00714 
00715     cs = 0;
00716     //spi.write(100);//friquency
00717     spi.write(friquency);//friquency
00718     cs = 1;
00719     wait_us(1);
00720 
00721     cs = 0;
00722     spi.write(duty0);//Duty0
00723     cs = 1;
00724     wait_us(1);
00725         
00726     cs = 0;
00727     spi.write(duty1);//Duty1
00728     cs = 1;
00729     wait_us(1);
00730         
00731     cs = 0;
00732     spi.write(duty2);//Duty2
00733     cs = 1;
00734     wait_us(1);        
00735 #endif
00736 
00737 
00738 #ifdef FS_KL25Z
00739     spi.frequency(2000000); //1MHz for KL25Z
00740     cs = 0;
00741     spi.write(Divider >> 8);//Divider
00742     spi.write(0x00FF & Divider);//Divider
00743     cs = 1;
00744     wait_us(1);
00745 
00746     cs = 0;
00747     spi.write(friquency >> 8);//friquency
00748     spi.write(0x00FF & friquency);//friquency
00749     cs = 1;
00750     wait_us(1);
00751 
00752     cs = 0;
00753     spi.write(duty0 >> 8);//Duty0
00754     spi.write(0x00FF & duty0);//Duty0
00755     cs = 1;
00756     wait_us(1);
00757         
00758     cs = 0;
00759     spi.write(duty1 >> 8);//Duty1
00760     spi.write(0x00FF & duty1);//Duty1
00761     cs = 1;
00762     wait_us(1);
00763         
00764     cs = 0;
00765     spi.write(duty2 >> 8);//Duty2
00766     spi.write(0x00FF & duty2);//Duty2
00767     cs = 1;
00768     wait_us(1);        
00769 #endif
00770 
00771 //    mcugear->disconnectModule();
00772 }
00773 
00774 //----------------------------------------------------------------------------------
00775 // changing PWM friquency
00776 //----------------------------------------------------------------------------------
00777 void PWMfriq(MCUGear *mcugear, unsigned int friquency){
00778 
00779  //   mcugear->connectModule();
00780 #ifdef LPC1768_mbed
00781     spi.frequency(1000000); //1MHz
00782     spi.format(16,0);
00783     cs = 0;
00784     spi.write(friquency);//friquency
00785     cs = 1;
00786     wait_us(1);
00787 #endif
00788 
00789 #ifdef FS_KL25Z
00790     spi.frequency(2000000); //1MHz for KL25Z
00791     cs = 0;
00792     spi.write(friquency >> 8);//Divider
00793     spi.write(0x00FF & friquency);//Divider
00794     cs = 1;
00795     wait_us(1);
00796 #endif
00797 
00798  //   mcugear->disconnectModule();
00799 }
00800 
00801 void PWMDuty(MCUGear *mcugear, int ch, unsigned int Duty){
00802 
00803     //data format
00804     //15-12:setting(Header)
00805     //  0 :stop PWM out
00806     //  1 :start PWM out
00807     //  2 :Frequency setting
00808     //  3 :Duty0 setting
00809     //  4 :Duty1 setting
00810     //  5 :Duty2 setting
00811     //  other :stop PWM out
00812     //11-0:Data
00813     
00814  //   mcugear->connectModule();
00815     
00816 #ifdef LPC1768_mbed
00817     spi.frequency(1000000); //1MHz
00818     spi.format(16,0);
00819     cs = 0;
00820     switch (ch){
00821         case 1:
00822             spi.write(0x3000 + Duty);   //Duty0
00823         case 2:
00824             spi.write(0x4000 + Duty);   //Duty0
00825         case 3:
00826             spi.write(0x5000 + Duty);   //Duty0
00827         default:
00828             break;
00829     }
00830     
00831     cs = 1;    
00832 #endif
00833 
00834 #ifdef FS_KL25Z
00835     spi.frequency(2000000); //1MHz for KL25Z
00836     cs = 0;
00837     switch (ch){
00838         case 1:
00839             spi.write((0x3000 + Duty) >> 8);//Divider
00840             spi.write(0x00FF & (0x3000 + Duty));//Divider
00841         case 2:
00842             spi.write((0x4000 + Duty) >> 8);//Divider
00843             spi.write(0x00FF & (0x4000 + Duty));//Divider
00844         case 3:
00845             spi.write((0x5000 + Duty) >> 8);//Divider
00846             spi.write(0x00FF & (0x5000 + Duty));//Divider
00847         default:
00848             break;
00849     }
00850     
00851     cs = 1;    
00852 #endif
00853 
00854  //   mcugear->disconnectModule();
00855 
00856 }
00857 
00858 
00859 //----------------------------------------------------------------------------------
00860 // Stop PWM
00861 //----------------------------------------------------------------------------------
00862 void StopPWM(MCUGear *mcugear){
00863 
00864 //    mcugear->connectModule();
00865     
00866 #ifdef LPC1768_mbed
00867     spi.frequency(1000000); //1MHz
00868     spi.format(16,0);
00869     cs = 0;
00870     spi.write(0x1000);//stop
00871     cs = 1;
00872 #endif
00873 
00874 #ifdef FS_KL25Z
00875     spi.frequency(2000000); //1MHz for KL25Z
00876     cs = 0;
00877     spi.write(0x10);//Duty1
00878     spi.write(0x00);//Duty1
00879     cs = 1;
00880 #endif
00881 
00882 //    mcugear->disconnectModule();
00883 }
00884 
00885 //----------------------------------------------------------------------------------
00886 // Start PWM
00887 //----------------------------------------------------------------------------------
00888 void StartPWM(MCUGear *mcugear){
00889 
00890  //   mcugear->connectModule();
00891     
00892 #ifdef LPC1768_mbed
00893     spi.frequency(1000000); //1MHz
00894     spi.format(16,0);
00895     cs = 0;
00896     spi.write(0x6000);//start
00897     cs = 1;
00898 #endif
00899 
00900 #ifdef FS_KL25Z
00901     spi.frequency(2000000); //1MHz for KL25Z
00902     cs = 0;
00903     spi.write(0x60);//start
00904     spi.write(0x00);//start
00905     cs = 1;
00906 #endif
00907 
00908 //    mcugear->disconnectModule();
00909 }
00910 
00911 
00912 #endif
00913 
00914 //----------------------------------------------------------------------------------
00915 //IO setting for Multifunction module
00916 //----------------------------------------------------------------------------------
00917 void IOSimpleSPI(MCUGear *mcugear){
00918     uint8_t fio[12];
00919     mcugear->detect_module(fio);
00920     
00921 #ifdef LPC1768_mbed
00922     //miso(p6),mosi(p5),sck(p7),cs(p8)
00923     mcugear->savePinSetting(0, IO_MBED_P6, IO_REG_IN_DIR, fio[0]);
00924     mcugear->savePinSetting(1, IO_MBED_P5, IO_REG_OUT_DIR, fio[1]);
00925     mcugear->savePinSetting(2, IO_MBED_P7, IO_REG_OUT_DIR, fio[2]);
00926     mcugear->savePinSetting(3, IO_MBED_P8, IO_REG_OUT_DIR, fio[3]);
00927 #endif
00928 
00929 #ifdef FS_KL25Z
00930     //miso(PTD3),mosi(PTD2),sck(PTD1),cs(PTD0)
00931     mcugear->savePinSetting(0, IO_MBED_PTD3, IO_REG_IN_DIR, fio[0]);
00932     mcugear->savePinSetting(1, IO_MBED_PTD2, IO_REG_OUT_DIR, fio[1]);
00933     mcugear->savePinSetting(2, IO_MBED_PTD1, IO_REG_OUT_DIR, fio[2]);
00934     mcugear->savePinSetting(3, IO_MBED_PTD0, IO_REG_OUT_DIR, fio[3]);
00935 #endif
00936 
00937 #ifdef BOOST_MODE
00938     mcugear->makeCircuit();
00939 #endif
00940 }
00941 
00942 //end of sample functions----------------------------------------------------
00943 
00944