Created
January 20, 2020 17:43
-
-
Save carlosdelfino/3b2fc37567fee4e657e51ef6380440df to your computer and use it in GitHub Desktop.
codificação de escrita na serial nativa para AVR,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <avr/io.h> | |
union floatBytes { | |
double f; | |
unsigned long b; | |
}; | |
void USART1_Init(unsigned int baud) { //Initialize USART hardware & settings for Serial Radio | |
uint16_t baud_setting; | |
// assign the baud_setting, a.k.a. ubrr (USART Baud Rate Register) | |
if (((F_CPU == 16000000UL) && (baud == 57600)) || (baud_setting >4095)) | |
{ | |
UCSR0A = 0; | |
baud_setting = (F_CPU / 8 / baud - 1) / 2; | |
}else{ | |
UCSR0A = 1 << U2X0; | |
baud_setting; = (F_CPU / 4 / baud - 1) / 2; | |
} | |
UBRR0H = baud_setting >> 8; | |
UBRR0L = baud_setting; | |
UCSR0B = (1 << TXEN0); //enable transmitter only | |
//set the data bits, parity, and stop bits | |
//UCSR0C |= 0x80; // select UCSRC register (shared with UBRRH) | |
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01); // Set frame format: 8data, 1 stop bit | |
} | |
/* | |
* Transmit a byte of data over USART to the Serial Radio | |
*/ | |
void USART1_Trans(unsigned char data) { | |
while (!(UCSR0A & (1 << UDRE0))); //wait for transmition to complete | |
UDR0 = data; | |
} | |
long long doubleUp(union floatBytes float32) { | |
unsigned char sign = (float32.b >> 31) & 0x01; | |
unsigned long fraction = float32.b & 0x007FFFFF; | |
unsigned long exponent = (float32.b >> 23) & 0xFF; | |
if ((float32.b & 0x7FFFFFFF) == 0) { //special case for +/- 0 | |
return (((long long)sign) << 63); | |
} | |
if (exponent == 0xFF) { //special case for +/- infinity, NAN | |
return (((long long)sign) << 63) | 0x7FF0000000000000 | (((long long)fraction) << 29); | |
} | |
exponent = exponent + 1023 - 127; | |
return (((long long)sign) << 63) | (((long long)exponent) << 52) | (((long long)fraction) << 29); | |
} | |
void setup() { | |
USART1_Init(57600); | |
double float32 = -7.51e-32; | |
long long float64 = doubleUp((union floatBytes)float32); | |
//long long float64 = 11111111111; | |
USART1_Trans(float64 >> 56); | |
USART1_Trans(float64 >> 48); | |
USART1_Trans(float64 >> 40); | |
USART1_Trans(float64 >> 32); | |
USART1_Trans(float64 >> 24); | |
USART1_Trans(float64 >> 16); | |
USART1_Trans(float64 >> 8); | |
USART1_Trans(float64); | |
while (1); | |
return 0; | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment