1

****i am using ATmega32 to send float and want to receive it on matlab,i send 4.85 from ATmega32 but on matlab i receive 4.260233679216239e-31...can any one help me..thanks in advance** **

 //Atmel studio code++++code for sending float from Atmega32
 union abc{ float fo; unsigned long lo; };
 union abc data; 
int main(void)
 { usart_init();
 data.fo=4.85;
 while(1) {
for(int i=0; i<=24; i+=8) 
{
 uart_send(data.lo>>i); }}}
 //Matlab code for receiving float 
s=serial('COM1','BaudRate',9600); 
fopen(s); 
out=fread(s,1,'single'); 
 fclose(s);
asked Dec 23, 2019 at 9:16

2 Answers 2

2

i found the solution for this problem, i set a header before sending the data from ATmega32,and on Matlab i check the frame ,if it received correctly,i receive the data

//ATmega32 code
 float x =215.5;
 unsigned char *ptrx;
 int main(void)
 { usart_init();
 //data.fo=12.85;
 ptrx=( unsigned char *) &x;
 while(1)
 {
 uart_send(0xAA);//equals to 170 
 for(int i=0;i<4;i++){
 uart_send(*(ptrx+i));}
 x++;}}
 //on Matlab
 s=serial('COM1','BaudRate',9600);
 fopen(s);
 i=1;
 while i<1000
 header =fread(s,1,'uint8');
 if header==170//equal to 0xAA
 out=fread(s,1,'float');
 data(i)=out;
 i=i+1;
 end
 end
 fclose(s);
answered Dec 24, 2019 at 9:09
0

The binary representation of 4.260233679216239e-31 is 0x0d0a40a3. The first two bytes (in big endian format) are 0x0d and 0x0a, which happen to be the ASCII characters CR and LF. These characters, in tandem, are often used as line terminators. My guess is that uart_send() is not doing what you think...

I suggest using Serial.write() instead. This way you won't have unwanted line terminators. And it will also make your problem on-topic with respect to this site.

Also, do not send the bytes repeatedly, send them only once, on demand. If you send them continuously, Matlab may catch the serial stream in the middle of a number, and assemble the bytes in the wrong order.

answered Dec 23, 2019 at 13:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.