Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
communicate.h@13:57d8e360e9aa, 2015-11-03 (annotated)
- Committer:
- DeguNaoto
- Date:
- Tue Nov 03 00:45:17 2015 +0000
- Revision:
- 13:57d8e360e9aa
- Parent:
- 11:565fca1ead5b
- Child:
- 14:943e663694c3
????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| DeguNaoto | 0:b613dc16f27d | 1 | /***RS485 Communication.***/ |
| DeguNaoto | 0:b613dc16f27d | 2 | #define RS485_TX p13 |
| DeguNaoto | 0:b613dc16f27d | 3 | #define RS485_RX p14 |
| DeguNaoto | 0:b613dc16f27d | 4 | Serial RS485(RS485_TX, RS485_RX); |
| DeguNaoto | 11:565fca1ead5b | 5 | #define EnablePin p12 |
| DeguNaoto | 0:b613dc16f27d | 6 | DigitalOut Enable(EnablePin); |
| DeguNaoto | 5:0e6dd1ff3ff2 | 7 | #define myaddress 8 |
| DeguNaoto | 5:0e6dd1ff3ff2 | 8 | |
| DeguNaoto | 0:b613dc16f27d | 9 | inline void initializeRS485(){ |
| DeguNaoto | 0:b613dc16f27d | 10 | RS485.format(8,Serial::Even); |
| DeguNaoto | 0:b613dc16f27d | 11 | } |
| DeguNaoto | 0:b613dc16f27d | 12 | |
| DeguNaoto | 0:b613dc16f27d | 13 | inline void sendData(int address, unsigned int data8) { |
| DeguNaoto | 0:b613dc16f27d | 14 | unsigned int data; |
| DeguNaoto | 0:b613dc16f27d | 15 | if (data8 <= 15) { |
| DeguNaoto | 0:b613dc16f27d | 16 | data = 128 | (address << 4) | (data8 & 15); |
| DeguNaoto | 0:b613dc16f27d | 17 | RS485.putc(data); |
| DeguNaoto | 0:b613dc16f27d | 18 | } |
| DeguNaoto | 0:b613dc16f27d | 19 | else if (data8 <= 255 && data8 >= 16) { |
| DeguNaoto | 0:b613dc16f27d | 20 | data = (address << 4) | (data8 >> 4); |
| DeguNaoto | 0:b613dc16f27d | 21 | RS485.putc(data); |
| DeguNaoto | 0:b613dc16f27d | 22 | wait(0.000001); |
| DeguNaoto | 0:b613dc16f27d | 23 | data = 128 | (address << 4) | (data8 & 15); |
| DeguNaoto | 0:b613dc16f27d | 24 | RS485.putc(data); |
| DeguNaoto | 0:b613dc16f27d | 25 | } |
| DeguNaoto | 0:b613dc16f27d | 26 | } |
| DeguNaoto | 0:b613dc16f27d | 27 | |
| DeguNaoto | 11:565fca1ead5b | 28 | /***mbed Serial***/ |
| DeguNaoto | 11:565fca1ead5b | 29 | #define MBED_TX p28 |
| DeguNaoto | 11:565fca1ead5b | 30 | #define MBED_RX p27 |
| DeguNaoto | 11:565fca1ead5b | 31 | Serial MbedSerial(MBED_TX,MBED_RX); |
| DeguNaoto | 13:57d8e360e9aa | 32 | unsigned int a2, b, X, Y; |
| DeguNaoto | 0:b613dc16f27d | 33 | |
| DeguNaoto | 13:57d8e360e9aa | 34 | void getMbed(){ |
| DeguNaoto | 13:57d8e360e9aa | 35 | unsigned int info = MbedSerial.getc(); |
| DeguNaoto | 13:57d8e360e9aa | 36 | if (info & 128){ //B1~B4,B5~B11 |
| DeguNaoto | 13:57d8e360e9aa | 37 | for(int s=1,i=1;i<=7;i++){ |
| DeguNaoto | 13:57d8e360e9aa | 38 | if(info & s){ |
| DeguNaoto | 13:57d8e360e9aa | 39 | b = i; |
| DeguNaoto | 13:57d8e360e9aa | 40 | break; |
| DeguNaoto | 13:57d8e360e9aa | 41 | } |
| DeguNaoto | 13:57d8e360e9aa | 42 | if(i==7) b = 0; |
| DeguNaoto | 13:57d8e360e9aa | 43 | s <<= 1; |
| DeguNaoto | 13:57d8e360e9aa | 44 | } |
| DeguNaoto | 0:b613dc16f27d | 45 | } |
| DeguNaoto | 13:57d8e360e9aa | 46 | else{ //A2,X,Y |
| DeguNaoto | 13:57d8e360e9aa | 47 | Y = info & 7; |
| DeguNaoto | 13:57d8e360e9aa | 48 | info >>= 3; |
| DeguNaoto | 13:57d8e360e9aa | 49 | X = info & 7; |
| DeguNaoto | 13:57d8e360e9aa | 50 | info >>= 3; |
| DeguNaoto | 13:57d8e360e9aa | 51 | a2 = info & 1; |
| DeguNaoto | 0:b613dc16f27d | 52 | } |
| DeguNaoto | 0:b613dc16f27d | 53 | } |
| DeguNaoto | 0:b613dc16f27d | 54 | |
| DeguNaoto | 13:57d8e360e9aa | 55 | inline void initializeMbedSerial(){ |
| DeguNaoto | 13:57d8e360e9aa | 56 | MbedSerial.baud(9600); |
| DeguNaoto | 13:57d8e360e9aa | 57 | MbedSerial.format(8,Serial::Even); |
| DeguNaoto | 13:57d8e360e9aa | 58 | MbedSerial.attach(getMbed,Serial::RxIrq); |
| DeguNaoto | 13:57d8e360e9aa | 59 | } |