通过UDR0上的串口写入发送浮点数据?

时间:2014-03-12 16:53:28

标签: floating-point serial-port arduino

我需要通过UDR0寄存器为我的c#应用程序发送浮点数据。我能够在我的应用程序中读取它,但我无法使用UDR0发送浮点数据,因为UDR0是1字节(8位)寄存器,浮点数是4字节。

#include <avr/interrupt.h> 
#include <avr/io.h> 
#include <util/delay.h>




void setup()
{    

  #define USART_BAUDRATE 9600
  #define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
   UBRR0H = (uint8_t)(UBRR_VALUE>>8);
   UBRR0L = (uint8_t)UBRR_VALUE;
   UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);   // Turn on the transmission, reception, and Receive interrupt 
  UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);   
   interrupts();   
}

int c = 0;
float send_ ;



void loop()
{  

}

ISR(USART_RX_vect)
{ 
  cli(); 
  while(!(UCSR0A&(1<<RXC0))){};
  c= UDR0;  // clear the USART interrupt
  send_ = (c / 4);
  UDR0 = send_;
  sei();
}

1 个答案:

答案 0 :(得分:0)

你应该读取4个字节(如果你发送二进制数据,如果你把它编码成一个nimber变量的字符串),使用一个union(有其他方式,但union是最简单的)将字节转换为float,将它除以4,然后发回4字节。

因为它是以指数格式存储的(参见IEE754),你可以只针对尾数进行存储,但它会非常复杂并且边际增益或没有增益。

相关问题