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

Thread Starter

abd.hed

Joined Apr 12, 2012
2
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



Rich (BB code):
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#include <p30F4013.h> 

_FOSC (FRC_PLL16 & CSW_FSCM_OFF); 

#define FOSC 7370000 
#define PLL 16 
#define FCY ((FOSC * PLL) / 4.0) 

void uart_init (unsigned short uart_register); 
char uart_get_byte (void); 
void uart_send_byte (char byte); 
void uart_send_string (char *string, int size); 
void uart_read_string (char *string, int size, char delimeter); 

int main(int argc, char** argv) 
{ 
char recieved_bytes[54], sent_bytes[28]; 
char x1[13], x2[13], x3[13], x4[13]; 
float value1, result1, value2, result2, value3, value4 ; 
int i; 



uart_init (47); 

while (1) 
{ 
/* Read the number as a string from the serial port*/ 
uart_read_string (recieved_bytes, 54, '\r'); 

for (i=0;i<13;i++)  
{ 
x1=recieved_bytes; 
x2=recieved_bytes[i+13]; 
x3=recieved_bytes[i+26]; 
x4=recieved_bytes[i+39]; 
} 

/* Cast the recieved bytes into a double percision number */ 
value1 = atof(x1) ; 
value2 = atof(x2) ; 
value3 = atof(x3) ; 
value4 = atof(x4) ; 

/* Perform Control Algorithm */ 
result1 = value1 * value2; 
result2 = value3 * value4; 

sprintf(sent_bytes,"%fa%f", result1,result2); 



/* Sent the converted float */ 
uart_send_string (sent_bytes, 28); 


} 

return EXIT_SUCCESS; 
} 

void uart_init (unsigned short uart_register) 
{ 
/* Set the baudrate register to correct value */ 
U1BRG = uart_register; 

/* Enable UART, TX and RX */ 
U1MODEbits.UARTEN = 1; 
U1STAbits.UTXEN = 1; 
} 

char uart_get_byte (void) 
{ 
char Recieved; 

while (U1STAbits.URXDA == 0); 
Recieved = U1RXREG; 

return Recieved; 
} 

void uart_send_byte (char byte) 
{ 
while (U1STAbits.UTXBF == 1); 
U1TXREG = byte; 
} 

void uart_send_string (char *string, int size) 
{ 
int i; 

for (i = 0; i < size; i++) 
{ 
if (string == '\0') 
break; 
uart_send_byte (string); 
} 
} 

void uart_read_string (char *string, int size, char delimeter) 
{ 
int i; 

for (i = 0; i < size; i++) 
{ 
string = uart_get_byte (); 

if (string == delimeter) 
break; 
} 

if (i >= size) 
string[size] = '\0'; 
else 
string[i + 1] = '\0'; 
}
 
Last edited by a moderator:

t06afre

Joined May 11, 2009
5,934
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.
 
Top