Projectlab Elektronica-ICT KULeuven

Dependencies:   EthernetInterface TMP102 TextLCD mbed-rtos mbed

werking.pdf

Committer:
seppeduwe
Date:
Fri Mar 14 19:41:14 2014 +0000
Revision:
0:ae3af7d18c4a
Child:
1:635e76c52151
Nog voor klasse

Who changed what in which revision?

UserRevisionLine numberNew contents of line
seppeduwe 0:ae3af7d18c4a 1 #include "mbed.h"
seppeduwe 0:ae3af7d18c4a 2 #include "Frame.h"
seppeduwe 0:ae3af7d18c4a 3 #include "lib_crc.h"
seppeduwe 0:ae3af7d18c4a 4 //SOF LEN IDD TMP PWM ID0 ID1 … IDn CRC EOF
seppeduwe 0:ae3af7d18c4a 5 //SOF (16bit): start of frame (0xAA 0xBB)
seppeduwe 0:ae3af7d18c4a 6 //LEN (8bit): lengte van frame in bytes (volledige frame!)
seppeduwe 0:ae3af7d18c4a 7 //IDD (8bit): ID destinationmbed(1-15)
seppeduwe 0:ae3af7d18c4a 8 //TMP (16bit): 0b0000XXXX 0bXXXXXXXX Temperature(0 –4095) (-25°tot 85°)
seppeduwe 0:ae3af7d18c4a 9 //PWM (8bit): PWM dutycycle(0 –255) (0% –100%)
seppeduwe 0:ae3af7d18c4a 10 //TUN (8bit): Select tune & delay (1 –15) zie tabel 1
seppeduwe 0:ae3af7d18c4a 11 //ID0–IDn(n x 8bit): lijst van reeds bezochte mbeds
seppeduwe 0:ae3af7d18c4a 12 //CRC (16bit): CRC16 checksum(x16+ x15+ x2+ 1) checksumop “LEN IDD TMP PWM ID0…IDn”
seppeduwe 0:ae3af7d18c4a 13 //EOF (16bit): end of frame (0xCC 0xDD)
seppeduwe 0:ae3af7d18c4a 14
seppeduwe 0:ae3af7d18c4a 15 //char buf[]={0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0xF2,0xE4,0xCC,0xDD}; //BB 0E A0 0F 32 02 05 08 09 F2 E4 CCDD";
seppeduwe 0:ae3af7d18c4a 16 // SOF SOF LEN IDD TMP TMP PWM TUN ID0 ID1 ID2 CRC CRC EOF EOF
seppeduwe 0:ae3af7d18c4a 17
seppeduwe 0:ae3af7d18c4a 18 void decode(char* frame)
seppeduwe 0:ae3af7d18c4a 19 {
seppeduwe 0:ae3af7d18c4a 20 int length;
seppeduwe 0:ae3af7d18c4a 21 int IdDestinationMbed;
seppeduwe 0:ae3af7d18c4a 22 int temperature;
seppeduwe 0:ae3af7d18c4a 23 int pWMDutycycle;
seppeduwe 0:ae3af7d18c4a 24 int tune;
seppeduwe 0:ae3af7d18c4a 25 char* IDs;
seppeduwe 0:ae3af7d18c4a 26 char* CRC;
seppeduwe 0:ae3af7d18c4a 27 int numberOfFrames;
seppeduwe 0:ae3af7d18c4a 28
seppeduwe 0:ae3af7d18c4a 29 length = (int)frame[2];
seppeduwe 0:ae3af7d18c4a 30 numberOfFrames = length- 12;
seppeduwe 0:ae3af7d18c4a 31 IDs = new char[numberOfFrames];
seppeduwe 0:ae3af7d18c4a 32
seppeduwe 0:ae3af7d18c4a 33 if ( frame[0]==0xAA && frame[1]==0xBB && frame[length-2]==0xCC && frame[length-1]==0xDD) {
seppeduwe 0:ae3af7d18c4a 34
seppeduwe 0:ae3af7d18c4a 35 IdDestinationMbed = (int) frame[3];
seppeduwe 0:ae3af7d18c4a 36 temperature = (int) ((frame[4]<<8) | frame[5]);
seppeduwe 0:ae3af7d18c4a 37 pWMDutycycle = (int)frame[6];
seppeduwe 0:ae3af7d18c4a 38 tune = (int) frame [7];
seppeduwe 0:ae3af7d18c4a 39
seppeduwe 0:ae3af7d18c4a 40 for (int i=0; i<numberOfFrames; i++) {
seppeduwe 0:ae3af7d18c4a 41 IDs[i]=frame[i+numberOfFrames];
seppeduwe 0:ae3af7d18c4a 42 }
seppeduwe 0:ae3af7d18c4a 43 printf("Decode length: %d IdDestinationMbed: %d temperature: %d \n\r",length,IdDestinationMbed,temperature);
seppeduwe 0:ae3af7d18c4a 44 printf("pWMDutycycle: %d tune: %d \n\r",pWMDutycycle,tune);
seppeduwe 0:ae3af7d18c4a 45
seppeduwe 0:ae3af7d18c4a 46 }
seppeduwe 0:ae3af7d18c4a 47 }
seppeduwe 0:ae3af7d18c4a 48
seppeduwe 0:ae3af7d18c4a 49 int encode(
seppeduwe 0:ae3af7d18c4a 50 int IdDestinationMbed,
seppeduwe 0:ae3af7d18c4a 51 int temperature,
seppeduwe 0:ae3af7d18c4a 52 int pWMDutycycle,
seppeduwe 0:ae3af7d18c4a 53 int tune,
seppeduwe 0:ae3af7d18c4a 54 int lengthIDs,
seppeduwe 0:ae3af7d18c4a 55 char* IDs,
seppeduwe 0:ae3af7d18c4a 56 char* encode
seppeduwe 0:ae3af7d18c4a 57 )
seppeduwe 0:ae3af7d18c4a 58 {
seppeduwe 0:ae3af7d18c4a 59 int length = 12+lengthIDs;
seppeduwe 0:ae3af7d18c4a 60 //encode = new char[length];
seppeduwe 0:ae3af7d18c4a 61
seppeduwe 0:ae3af7d18c4a 62 encode[0]=0xAA;
seppeduwe 0:ae3af7d18c4a 63 encode[1]=0xBB;
seppeduwe 0:ae3af7d18c4a 64
seppeduwe 0:ae3af7d18c4a 65 encode[2]= (char) length;
seppeduwe 0:ae3af7d18c4a 66 encode[3]= (char) IdDestinationMbed;
seppeduwe 0:ae3af7d18c4a 67 encode[4]= (char) (temperature>>8);
seppeduwe 0:ae3af7d18c4a 68 encode[5]= (char) temperature;
seppeduwe 0:ae3af7d18c4a 69 encode[6]= (char) pWMDutycycle;
seppeduwe 0:ae3af7d18c4a 70 encode[7]= (char) tune;
seppeduwe 0:ae3af7d18c4a 71
seppeduwe 0:ae3af7d18c4a 72 for (int i=0; i<lengthIDs; i++) {
seppeduwe 0:ae3af7d18c4a 73 encode[8+i]=IDs[i];
seppeduwe 0:ae3af7d18c4a 74 }
seppeduwe 0:ae3af7d18c4a 75
seppeduwe 0:ae3af7d18c4a 76 //copy over static fields
seppeduwe 0:ae3af7d18c4a 77 /* for (int i=2; i<7; i++) {
seppeduwe 0:ae3af7d18c4a 78 CRC[i-2]= encode[i];
seppeduwe 0:ae3af7d18c4a 79 }*/
seppeduwe 0:ae3af7d18c4a 80
seppeduwe 0:ae3af7d18c4a 81
seppeduwe 0:ae3af7d18c4a 82 // Calculate CRC
seppeduwe 0:ae3af7d18c4a 83 int CRCint = MakeCRC(encode[2],encode[3],encode[4],encode[5],encode[6],IDs,lengthIDs);
seppeduwe 0:ae3af7d18c4a 84
seppeduwe 0:ae3af7d18c4a 85 encode[length-4]= ( char)(CRCint>>8);
seppeduwe 0:ae3af7d18c4a 86 encode[length-3]=( char)(CRCint);
seppeduwe 0:ae3af7d18c4a 87
seppeduwe 0:ae3af7d18c4a 88 encode[length-2]=0xCC;
seppeduwe 0:ae3af7d18c4a 89 encode[length-1]=0xDD;
seppeduwe 0:ae3af7d18c4a 90 return length;
seppeduwe 0:ae3af7d18c4a 91 }
seppeduwe 0:ae3af7d18c4a 92
seppeduwe 0:ae3af7d18c4a 93 int MakeCRC(
seppeduwe 0:ae3af7d18c4a 94 char LEN, //lenght of frame
seppeduwe 0:ae3af7d18c4a 95 char IDD, //Destination ID
seppeduwe 0:ae3af7d18c4a 96 char TMP0,//first tmp char
seppeduwe 0:ae3af7d18c4a 97 char TMP1,//second tmp char
seppeduwe 0:ae3af7d18c4a 98 char PWM, //pwm tune
seppeduwe 0:ae3af7d18c4a 99 char* data, //data frame
seppeduwe 0:ae3af7d18c4a 100 int lenghtData //lenght of the data
seppeduwe 0:ae3af7d18c4a 101 )
seppeduwe 0:ae3af7d18c4a 102 {
seppeduwe 0:ae3af7d18c4a 103 //char* crcData = new char[5+lenghtData] ;
seppeduwe 0:ae3af7d18c4a 104 char crcData[5+lenghtData] ;
seppeduwe 0:ae3af7d18c4a 105
seppeduwe 0:ae3af7d18c4a 106 crcData[0] = LEN; //lenght of frame
seppeduwe 0:ae3af7d18c4a 107 crcData[1] = IDD; //Destination ID
seppeduwe 0:ae3af7d18c4a 108 crcData[2] = TMP0;//first tmp char
seppeduwe 0:ae3af7d18c4a 109 crcData[3] = TMP1;//second tmp char
seppeduwe 0:ae3af7d18c4a 110 crcData[4] = PWM; //pwm tune
seppeduwe 0:ae3af7d18c4a 111
seppeduwe 0:ae3af7d18c4a 112 // add the data to the crcData
seppeduwe 0:ae3af7d18c4a 113 for (int i=0; i<lenghtData; i++) {
seppeduwe 0:ae3af7d18c4a 114 crcData[i+5]= data[i];
seppeduwe 0:ae3af7d18c4a 115 }
seppeduwe 0:ae3af7d18c4a 116 return calculate_crc16(crcData, lenghtData+5);
seppeduwe 0:ae3af7d18c4a 117 }