communication between matlab and microcontroller (problem with the C code)

Discussion in 'Programmer's Corner' started by abd.hed, Apr 12, 2012.

  1. abd.hed

    Thread Starter New Member

    Apr 12, 2012
    2
    0
    Hi there !

    I'm trying to send 4 string signals as one array from Matlab to dspic30F4013. Each signal is 13 bytes. I use MPLAB X IDE. In the Microcontroller code, I reconstruct the 4 signals back from the received array. Then add the first two signals together and the same with the signals 3 and 4 so I just send 2 signals back to the MATLab, again as an array that have "a" after the first signal and the terminator "/r" after the second. This is done, so it will be easy to recognize them by the Matlab code, by reconstructing the bytes of the first signal until "a" is reached and then start building the other signal. However I keep facing problems since I don't receive anything in the matlab. Here is the code and I hope somebody can help me.. thanks



    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <string.h>
    5.  
    6. #include <p30F4013.h>
    7.  
    8. _FOSC (FRC_PLL16 & CSW_FSCM_OFF);
    9.  
    10. #define FOSC 7370000
    11. #define PLL 16
    12. #define FCY ((FOSC * PLL) / 4.0)
    13.  
    14. void uart_init (unsigned short uart_register);
    15. char uart_get_byte (void);
    16. void uart_send_byte (char byte);
    17. void uart_send_string (char *string, int size);
    18. void uart_read_string (char *string, int size, char delimeter);
    19.  
    20. int main(int argc, char** argv)
    21. {
    22. char recieved_bytes[54], sent_bytes[28];
    23. char x1[13], x2[13], x3[13], x4[13];
    24. float value1, result1, value2, result2, value3, value4 ;
    25. int i;
    26.  
    27.  
    28.  
    29. uart_init (47);
    30.  
    31. while (1)
    32. {
    33. /* Read the number as a string from the serial port*/
    34. uart_read_string (recieved_bytes, 54, '\r');
    35.  
    36. for (i=0;i<13;i++)  
    37. {
    38. x1=recieved_bytes;
    39. x2=recieved_bytes[i+13];
    40. x3=recieved_bytes[i+26];
    41. x4=recieved_bytes[i+39];
    42. }
    43.  
    44. /* Cast the recieved bytes into a double percision number */
    45. value1 = atof(x1) ;
    46. value2 = atof(x2) ;
    47. value3 = atof(x3) ;
    48. value4 = atof(x4) ;
    49.  
    50. /* Perform Control Algorithm */
    51. result1 = value1 * value2;
    52. result2 = value3 * value4;
    53.  
    54. sprintf(sent_bytes,"%fa%f", result1,result2);
    55.  
    56.  
    57.  
    58. /* Sent the converted float */
    59. uart_send_string (sent_bytes, 28);
    60.  
    61.  
    62. }
    63.  
    64. return EXIT_SUCCESS;
    65. }
    66.  
    67. void uart_init (unsigned short uart_register)
    68. {
    69. /* Set the baudrate register to correct value */
    70. U1BRG = uart_register;
    71.  
    72. /* Enable UART, TX and RX */
    73. U1MODEbits.UARTEN = 1;
    74. U1STAbits.UTXEN = 1;
    75. }
    76.  
    77. char uart_get_byte (void)
    78. {
    79. char Recieved;
    80.  
    81. while (U1STAbits.URXDA == 0);
    82. Recieved = U1RXREG;
    83.  
    84. return Recieved;
    85. }
    86.  
    87. void uart_send_byte (char byte)
    88. {
    89. while (U1STAbits.UTXBF == 1);
    90. U1TXREG = byte;
    91. }
    92.  
    93. void uart_send_string (char *string, int size)
    94. {
    95. int i;
    96.  
    97. for (i = 0; i < size; i++)
    98. {
    99. if (string == '\0')
    100. break;
    101. uart_send_byte (string);
    102. }
    103. }
    104.  
    105. void uart_read_string (char *string, int size, char delimeter)
    106. {
    107. int i;
    108.  
    109. for (i = 0; i < size; i++)
    110. {
    111. string = uart_get_byte ();
    112.  
    113. if (string == delimeter)
    114. break;
    115. }
    116.  
    117. if (i >= size)
    118. string[size] = '\0';
    119. else
    120. string[i + 1] = '\0';
    121. }
    122.  
    123.  
     
    Last edited by a moderator: Apr 12, 2012
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Have you connected to the setup to a terminal program. Just to see whatever that may show up on the serial port. A common mistake in RS232 setups. Are not using proper RS232 signal level. Do you have a TTL to RS232 level converter in your setup.
     
  3. abd.hed

    Thread Starter New Member

    Apr 12, 2012
    2
    0
    yes I have done that and it worked with 1 signal
     
Loading...