1

I want to receive a float on Atmega32 from matlab. I display the output on an lcd, but i receive 0.000, can any one help me? Please ...

matlab code

s=serial('COM1','BaudRate',9600);
fopen(s)
fwrite(s,170,'uint8');//header frame
fwrite(s,1.2,'float');//the data to be sent

ATmega32 code

union {
  signed long l;
  float f;
} data;

int main(void)
{   
  DDRA=0xff;//for LCD
  DDRB=0xff;//for LCD 
  data.l;
  char value[8];
  usart_init();
  Lcd8_Init();

  while(1)
  { 
    if(uart_receive()==0xAA)
    { 
      //header,0xAA=170
      Lcd8_Set_Cursor(0,0);
      Lcd8_Write_String("Float");
      _delay_ms(100);

      data.l=(long)uart_receive();
      data.l=(long)(data.l||(uart_receive()<<8));
      data.l=(long)(data.l||(uart_receive()<<16));
      data.l=(long)(data.l||(uart_receive()<<24));

      dtostrf(data.f,8,3,value);//convert float to string 

      Lcd8_Clear();
      Lcd8_Set_Cursor(0,0);
      for(int i=0;i<8;i++) Lcd8_Write_Char(value[i]); 
   }

}}
3
  • I would say you wrote the value into the long member of data data.l, but you tried to convert the float member of data data.f to a string. data.f is still 0.0 Commented Dec 24, 2019 at 12:33
  • data.l contains the float value sent from matlab..so how to convert data.l to float? Commented Dec 24, 2019 at 12:36
  • You send a float value from mathlab byte wise to the AVR and store 4 bytes of the float value into a long variable of the AVR. That's complicated, becaus you have to know several things. How is the structure of a float in matlab? Is this the same on a AVR? I would suggest you send a String with the value from mathlab to the AVR and on the AVR convert the string into a float. (I know atof() for this reason. You use an AVR lib I don't know. In matlab you can use the fprintf function to convert the float to a string and send it to the AVR. Commented Dec 24, 2019 at 12:58

0

Your Answer

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.