AD2ch Module library for MCU Gear with LPC1114. It contains wiring data.
AD2chModuleLPC1114.cpp@0:0050ff39b5f7, 2014-05-06 (annotated)
- Committer:
- Info
- Date:
- Tue May 06 07:14:31 2014 +0000
- Revision:
- 0:0050ff39b5f7
AD2ch Module library for MCU Gear with LPC1114.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Info | 0:0050ff39b5f7 | 1 | |
Info | 0:0050ff39b5f7 | 2 | #include "mbed.h" |
Info | 0:0050ff39b5f7 | 3 | #include "MCUGearALPC1114.h" |
Info | 0:0050ff39b5f7 | 4 | #include "MCUGearBaseALPC1114.h" |
Info | 0:0050ff39b5f7 | 5 | #include "AD2chModuleLPC1114.h" |
Info | 0:0050ff39b5f7 | 6 | |
Info | 0:0050ff39b5f7 | 7 | AD2chModule::AD2chModule(SPI _spi, PinName _cs, MCUGear *mcugear) |
Info | 0:0050ff39b5f7 | 8 | :spi(_spi),cs(_cs) { |
Info | 0:0050ff39b5f7 | 9 | |
Info | 0:0050ff39b5f7 | 10 | //miso,mosi,sck,cs |
Info | 0:0050ff39b5f7 | 11 | mcugear->setWireA(IOMISO, IO_REG_IN_DIR, 0); |
Info | 0:0050ff39b5f7 | 12 | mcugear->setWireA(IOMOSI, IO_REG_OUT_DIR, 1); |
Info | 0:0050ff39b5f7 | 13 | mcugear->setWireA(IOSCK, IO_REG_OUT_DIR,2); |
Info | 0:0050ff39b5f7 | 14 | mcugear->setWireA(IOCS, IO_REG_OUT_DIR,3); |
Info | 0:0050ff39b5f7 | 15 | } |
Info | 0:0050ff39b5f7 | 16 | |
Info | 0:0050ff39b5f7 | 17 | |
Info | 0:0050ff39b5f7 | 18 | void AD2chModule::initAD2ch(MCUGear *mcugear){ |
Info | 0:0050ff39b5f7 | 19 | |
Info | 0:0050ff39b5f7 | 20 | cs = 1; |
Info | 0:0050ff39b5f7 | 21 | |
Info | 0:0050ff39b5f7 | 22 | //12bit SPI DA module---------------- |
Info | 0:0050ff39b5f7 | 23 | spi.frequency(2000000); //1MHz |
Info | 0:0050ff39b5f7 | 24 | } |
Info | 0:0050ff39b5f7 | 25 | |
Info | 0:0050ff39b5f7 | 26 | int AD2chModule::readADm(MCUGear *mcugear, char ch){ |
Info | 0:0050ff39b5f7 | 27 | |
Info | 0:0050ff39b5f7 | 28 | char sendData; |
Info | 0:0050ff39b5f7 | 29 | int whoami[3]; |
Info | 0:0050ff39b5f7 | 30 | |
Info | 0:0050ff39b5f7 | 31 | // spi.frequency(2000000); //1MHz |
Info | 0:0050ff39b5f7 | 32 | if(ch == 0){ |
Info | 0:0050ff39b5f7 | 33 | sendData = 0x80; |
Info | 0:0050ff39b5f7 | 34 | }else if(ch == 1){ |
Info | 0:0050ff39b5f7 | 35 | sendData = 0xC0; |
Info | 0:0050ff39b5f7 | 36 | }else{ |
Info | 0:0050ff39b5f7 | 37 | return -1; |
Info | 0:0050ff39b5f7 | 38 | } |
Info | 0:0050ff39b5f7 | 39 | |
Info | 0:0050ff39b5f7 | 40 | //12bit AD Mobdule---------------- |
Info | 0:0050ff39b5f7 | 41 | //cs = 1; //reset CS pin |
Info | 0:0050ff39b5f7 | 42 | // mcugear->connectModule(); //connect--- |
Info | 0:0050ff39b5f7 | 43 | |
Info | 0:0050ff39b5f7 | 44 | cs = 0; |
Info | 0:0050ff39b5f7 | 45 | //FRDM KL25Z is 8-bit format. |
Info | 0:0050ff39b5f7 | 46 | whoami[2] = spi.write(0x01);//0000 0001 |
Info | 0:0050ff39b5f7 | 47 | whoami[2] = spi.write(sendData); |
Info | 0:0050ff39b5f7 | 48 | whoami[0] = whoami[2]<<8; |
Info | 0:0050ff39b5f7 | 49 | whoami[0] = whoami[0] + spi.write(0x00) - 0xE000;//0000 0000 |
Info | 0:0050ff39b5f7 | 50 | // whoami[2] = 0;//clear buffer |
Info | 0:0050ff39b5f7 | 51 | cs = 1; |
Info | 0:0050ff39b5f7 | 52 | // mcugear->disconnectModule(); //disconnect--- |
Info | 0:0050ff39b5f7 | 53 | return whoami[0]; |
Info | 0:0050ff39b5f7 | 54 | |
Info | 0:0050ff39b5f7 | 55 | } |
Info | 0:0050ff39b5f7 | 56 | |
Info | 0:0050ff39b5f7 | 57 | int AD2chModule::readADa(MCUGear *mcugear, char ch){ |
Info | 0:0050ff39b5f7 | 58 | |
Info | 0:0050ff39b5f7 | 59 | char sendData; |
Info | 0:0050ff39b5f7 | 60 | int whoami[3]; |
Info | 0:0050ff39b5f7 | 61 | |
Info | 0:0050ff39b5f7 | 62 | spi.frequency(2000000); //1MHz |
Info | 0:0050ff39b5f7 | 63 | if(ch == 0){ |
Info | 0:0050ff39b5f7 | 64 | sendData = 0x80; |
Info | 0:0050ff39b5f7 | 65 | }else if(ch == 1){ |
Info | 0:0050ff39b5f7 | 66 | sendData = 0xC0; |
Info | 0:0050ff39b5f7 | 67 | }else{ |
Info | 0:0050ff39b5f7 | 68 | return -1; |
Info | 0:0050ff39b5f7 | 69 | } |
Info | 0:0050ff39b5f7 | 70 | |
Info | 0:0050ff39b5f7 | 71 | //12bit AD Mobdule---------------- |
Info | 0:0050ff39b5f7 | 72 | cs = 1; //reset CS pin |
Info | 0:0050ff39b5f7 | 73 | mcugear->connectModuleA(); //connect--- |
Info | 0:0050ff39b5f7 | 74 | |
Info | 0:0050ff39b5f7 | 75 | cs = 0; |
Info | 0:0050ff39b5f7 | 76 | //FRDM KL25Z is 8-bit format. |
Info | 0:0050ff39b5f7 | 77 | whoami[2] = spi.write(0x01);//0000 0001 |
Info | 0:0050ff39b5f7 | 78 | whoami[2] = spi.write(sendData); |
Info | 0:0050ff39b5f7 | 79 | whoami[0] = whoami[2]<<8; |
Info | 0:0050ff39b5f7 | 80 | whoami[0] = whoami[0] + spi.write(0x00) - 0xE000;//0000 0000 |
Info | 0:0050ff39b5f7 | 81 | // whoami[2] = 0;//clear buffer |
Info | 0:0050ff39b5f7 | 82 | cs = 1; |
Info | 0:0050ff39b5f7 | 83 | mcugear->disconnectModule(); //disconnect--- |
Info | 0:0050ff39b5f7 | 84 | return whoami[0]; |
Info | 0:0050ff39b5f7 | 85 | |
Info | 0:0050ff39b5f7 | 86 | } |