void printByte( uint8_t byte ) { /* Converts a byte to a string of decimal text, sends it */ transmitByte( '0' + ( byte / 100 ) ); /* Hundreds */ transmitByte( '0' + ( ( byte / 10) % 10 ) ); /* Tens */ transmitByte( '0' + ( byte % 10 ) ); /* Ones */ }
void printWord(uint16_t word) { transmitByte('0' + (word / 10000)); /* Ten-thousands */ transmitByte('0' + ((word / 1000) % 10)); /* Thousands */ transmitByte('0' + ((word / 100) % 10)); /* Hundreds */ transmitByte('0' + ((word / 10) % 10)); /* Tens */ transmitByte('0' + (word % 10)); /* Ones */ }
void printHexByte(uint8_t byte) { /* Prints a byte as its hexadecimal equivalent */ uint8_t nibble; nibble = (byte & 0b11110000) >> 4; transmitByte(nibbleToHexCharacter(nibble)); nibble = byte & 0b00001111; transmitByte(nibbleToHexCharacter(nibble)); }
//-------------------------------------------------------------- // MID LEVEL MESSAGE FUNCTIONS //-------------------------------------------------------------- // Handle UART communication error void commError(void){ transmitByte(UART_COMM_ERROR); transmitByte(END_MSG); // Flush receive buffer by temporarily disabling RXEN0 UCSR0B &= ~(1 << RXEN0); _delay_us(25); UCSR0B |= (1 << RXEN0); }
//Takes an integer and transmits the characters void printByte(uint8_t byte) { //(modified to only print last 2 digits (tens & ones)). //while (!(UCSR1A & (1 << UDRE1))) {} //Wait until the USART 0 data register is empty (ready to transmit). //transmitByte('0'+ (byte/100)); //Hundreds while (!(UCSR1A & (1 << UDRE1))) {} //Wait until the USART 0 data register is empty (ready to transmit). transmitByte('0'+ ((byte/10) % 10)); //Tens while (!(UCSR1A & (1 << UDRE1))) {} //Wait until the USART 0 data register is empty (ready to transmit). transmitByte('0'+ (byte % 10)); //Ones }
void printBinaryByte(uint8_t byte) { /* Prints out a byte as a series of 1's and 0's */ uint8_t bit; for (bit = 7; bit < 255; bit--) { if (bit_is_set(byte, bit)) transmitByte('1'); else transmitByte('0'); } }
void printFloat(float number) { number = round(number * 100) / 100; /* round off to 2 decimal places */ transmitByte('0' + number / 10); /* tens place */ transmitByte('0' + number - 10 * floor(number / 10)); /* ones */ transmitByte('.'); transmitByte('0' + (number * 10) - floor(number) * 10); /* tenths */ /* hundredths place */ transmitByte('0' + (number * 100) - floor(number * 10) * 10); printString("\r\n"); }
// This function will transmit a full message to RasPi uint8_t transmitMessage(volatile int sendData[4]){ // Initialize loop counter uint8_t i; uint8_t badSendFlag = 0; // Clear global interrupt to stop receive interrupt cli(); // Send AVR request RasPi byte transmitRequest(); // Wait for response from RasPi // **** This has possibility of hanging program -- needs improvement! if (receiveByte() == RASPI_READY){ // Remove EOM from buffer receiveByte(); // Loop to send all of data array for(i=0; i<4; i++){ transmitByte(sendData[i]); } // Transmit end of message byte transmitByte(END_MSG); // Check for good confirm byte if (receiveByte() == RASPI_REC_MSG_CONFIRM){ // Remove EOM from buffer receiveByte(); } else{ // Set bad send flag badSendFlag = 2; } } // If improper response is received, call commError() else{ // Set bad send flag badSendFlag = 1; } // If badSendFlag is set - call commError() if(badSendFlag != 0){ commError(); } // reenable global interrupts sei(); // Return value of badSendFlag return badSendFlag; }
//Takes an integer and prints the binary equivalent. void printBinaryByte(uint8_t byte) { uint8_t bit; for(bit=1; bit<255; bit--) //For full 8-bits, modify to: "for(bit=7; bit<255; bit--)". { if(bit_is_set(byte, bit)) { transmitByte('1'); } else { transmitByte('0'); } } }
void printString(const char myString[]) { uint8_t i = 0; while (myString[i]) { transmitByte(myString[i]); i++; } }
void printString_Progmem(const char *stringP) { char oneLetter; while ((oneLetter = pgm_read_byte(stringP))) { transmitByte(oneLetter); stringP++; } }
int main(void) { DDRD = (1<<1); // habilita output no pino PD1 (TXD) DDRB = (1<<PB0); // LED na porta PB0 DDRB = (1<<PB5); // habilita LED usado no bootloader // (para piscar em status) char serialCharacter; LED_DDR=0xff; initUSART(); PORTB |= (1<<PB0); PORTB &= ~(1<<PB5); printString("Ola Mundo\r\n"); while(1) { serialCharacter = receiveByte(); PORTB ^= _BV(PB0); transmitByte(serialCharacter); //LED_PORT=serialCharacter; PORTB ^= _BV(PB5); } return(0); }
void readString( char myString[], uint8_t maxLength ) { char response; uint8_t i; i = 0; while ( i < ( maxLength - 1 ) ) { /* prevent over-runs */ response = receiveByte(); /* echo */ transmitByte(response); if ( response == '\r' ) { /* enter marks the end */ break; } else { /* add in a letter */ myString[ i ] = response; i++; } } /* terminal NULL character */ myString[ i ] = 0; }
/* Encodes the byte array data to z85 and transmits on serial interface */ uint8_t encode_Z85_and_transmit(uint8_t *data, size_t size) { // Accepts only byte arrays bounded to 4 bytes if (size % 4) return 1; /* size_t encoded_size = size * 5 / 4; */ /* char *encoded = malloc (encoded_size + 1); */ /* uint char_nbr = 0; */ size_t byte_nbr = 0; uint32_t value = 0; while (byte_nbr < size) { // Accumulate value in base 256 (binary) value = value * 256 + data [byte_nbr++]; if (byte_nbr % 4 == 0) { // Output value in base 85 uint32_t divisor = 52200625; // = 85 * 85 * 85 * 85; while (divisor) { /* encoded [char_nbr++] = encoder [value / divisor % 85]; */ transmitByte(encoder [value / divisor % 85]); // write to USART divisor /= 85; } value = 0; } } /* assert (char_nbr == encoded_size); */ /* encoded [char_nbr] = 0x00; */ /* return encoded; */ /* transmitByte(0x00); */ return 0; }
int main(void) { // Single character for serial TX/RX char a; // Enable output on all 8 bits in DDRB (but only PB0 and PB1 are used) DDRB = 0xff; // Enable pin change interrupt for the B pins, but only check PB0 and PB1. sei(); PCICR |= (1 << PCIE0); PCMSK0 |= ((1 << PB0) | (1 << PB1)); init_timer1(); initUSART(); pb[0] = PB0; pb[1] = PB1; while (1) { // for (uint8_t i=0; i<255; i++) // cmd[i] = 0; // char cmd[255]; // readString(cmd, 255); // if (strcmp(cmd, "V0_ON") == 0) // { // printString("\r\nYou wrote: "); // printString(cmd); // printString("\r\n"); // PORTB |= (1 << PB0); // } // if (strcmp(cmd, "V1_ON")) // PORTB |= (1 << PB1); // if (strcmp(cmd, "V0_OFF")) // PORTB &= ~(1 << PB0); // if (strcmp(cmd, "V1_OFF")) // PORTB &= ~(1 << PB1); // if (cmd == "V0_ON") set_bit(PORTB, PB0); // PORTB |= (1 << PB0); a = receiveByte(); transmitByte(a); if (a == '0') PORTB &= ~(1 << PB0); if (a == '1') PORTB |= (1 << PB0); if (a == '2') PORTB &= ~(1 << PB1); if (a == '3') PORTB |= (1 << PB1); // PORTB = a; } return 0; }
//*************************************************************************** //Function: to read multiple blocks from SD card & send every block to UART //Arguments: none //return: unsigned char; will be 0 if no error, // otherwise the response byte will be sent //**************************************************************************** unsigned char SD_readMultipleBlock (unsigned long startBlock, unsigned long totalBlocks) { unsigned char response; unsigned int i, retry=0; retry = 0; response = SD_sendCommand(READ_MULTIPLE_BLOCKS, startBlock <<9); //read a Block command //block address converted to starting address of 512 byte Block if(response != 0x00) { //check for SD status: 0x00 - OK (No flags set) return response; } SD_CS_ASSERT; while( totalBlocks ) { retry = 0; while(SPI_receive() != 0xfe) { //wait for start block token 0xfe (0x11111110) if(retry++ > 0xfffe) { SD_CS_DEASSERT; return 1; } //return if time-out } for(i=0; i<512; i++) { //read 512 bytes buffer[i] = SPI_receive(); } SPI_receive(); //receive incoming CRC (16-bit), CRC is ignored here SPI_receive(); SPI_receive(); //extra 8 cycles TX_NEWLINE; transmitString_F(PSTR(" --------- ")); TX_NEWLINE; for(i=0; i<512; i++) { //send the block to UART if(buffer[i] == '~') { break; } transmitByte ( buffer[i] ); } TX_NEWLINE; transmitString_F(PSTR(" --------- ")); TX_NEWLINE; totalBlocks--; } SD_sendCommand(STOP_TRANSMISSION, 0); //command to stop transmission SD_CS_DEASSERT; SPI_receive(); //extra 8 clock pulses return 0; }
void printString_Progmem(const char *stringP) { char oneLetter; while ((oneLetter = pgm_read_byte(stringP))) { transmitByte(oneLetter); stringP++; _delay_ms(100); /* only b/c it's cute */ } }
void positionCursor(uint8_t row, uint8_t col) { transmitByte(0x1b); // <ESC> printString("["); printByte(row); printString(";"); printByte(col); printString("H"); }
//Transmits a string of characters. void printString(const char string[]) { uint8_t i = 0; //Counter to increment for every character in the string. while ((string[i]) != '\0') //Until null character (end of string). { transmitByte(string[i]); //UCSR0A = USART 0 Control and Status Register A i++; //UDRE0 = USART 0 Data Register Empty Flag } }
int sendTestPacket() { int i; for (i = 0; i < (strlen(TEST_PKT)); i++) { uint8_t current = ((uint8_t) TEST_PKT[i]); transmitByte(current); } return 0; }
//call on avalible data on usart line void UART4_IRQHandler(void) { uint8_t data; GPIOD->ODR ^= DISCOF4_LED_RED; if ( UART4->SR & USART_SR_RXNE ) { UART4->SR &= ~USART_SR_RXNE; data = UART4->DR; transmitByte(data); } }
int main(void) { initUSART(); char oneLetter; uint8_t i; while (1) { for (i = 0; i < sizeof(myVeryLongString); i++) { oneLetter = pgm_read_byte(&(myVeryLongString[i])); transmitByte(oneLetter); _delay_ms(100); /* slow it down to simulate typing effect :) */ } _delay_ms(1000); printWord(&sixteenBits); /* this throws a compiler warning... */ transmitByte('\r'); transmitByte('\n'); printWord(pgm_read_word(&sixteenBits)); } /* End event loop */ return 0; /* This line is never reached */ }
void dumpBuffer(void){ uint8_t i; for (i=0; i<BUFFER_LENGTH; i++){ if (buffer.oldest_index == buffer.newest_index && buffer.newest_index == i){ transmitByte('='); } else if (buffer.oldest_index == i){ transmitByte('['); } else if (buffer.newest_index == i){ transmitByte(']'); } else { transmitByte('.'); } } printString("\n"); for (i=0; i<BUFFER_LENGTH; i++){ transmitByte(buffer.data[i]); } printString("\n"); }
void printVoltage(float voltage) { float number = (voltage/4) * (REF_VCC/1023); transmitByte('0' + ((number/10) - floor(number/10))*10); transmitByte('.'); transmitByte('0' + floor(10*((number-floor(number))))); transmitByte('0' + floor(10*(((number*10)-floor(number*10))))); transmitByte('0' + floor(10*(((number*100)-floor(number*100))))); transmitByte('0' + floor(10*(((number*1000)-floor(number*1000))))); printString("\r\n"); }
float printThermRes(float voltage) { voltage = (voltage/4) * (REF_VCC/1023); float R_T = (REF_VCC/voltage)*(VOLTAGE_DIV_RES-((voltage*VOLTAGE_DIV_RES)/REF_VCC)); float number = R_T; transmitByte('0' + floor(10*((number/100000)-floor(number/100000)))); transmitByte('0' + floor(10*((number/10000)-floor(number/10000)))); transmitByte('0' + floor(10*((number/1000)-floor(number/1000)))); transmitByte('0' + floor(10*((number/100)-floor(number/100)))); transmitByte('0' + ((number/10) - floor(number/10))*10); transmitByte('.'); transmitByte('0' + floor(10*((number-floor(number))))); printString("\r\n"); return R_T; }
uint8_t getNumber(void) { // Gets a numerical 0-255 from the serial port. // Converts from string to number. char hundreds = '0'; char tens = '0'; char ones = '0'; char thisChar = '0'; do { /* shift over */ hundreds = tens; tens = ones; ones = thisChar; thisChar = receiveByte(); /* get a new character */ transmitByte(thisChar); /* echo */ } while (thisChar != '\r'); /* until type return */ return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0'); }
void printFloat(float voltage) { float number = voltage/4; //transmitByte('0' + floor(10*((number/100000)-floor(number/100000)))); transmitByte('0' + floor(10*((number/10000)-floor(number/10000)))); transmitByte('0' + floor(10*((number/1000)-floor(number/1000)))); transmitByte('0' + floor(10*((number/100)-floor(number/100)))); transmitByte('0' + ((number/10) - floor(number/10))*10); transmitByte('.'); transmitByte('0' + floor(10*((number-floor(number))))); printString("\r\n"); }
int main(void) { clock_prescale_set(clock_div_16); uint8_t dummy; initUSART(); initFreerunningADC(); while (1) { //transmitByte(ADCH); dummy = ADCH; dummy = ADCL; transmitByte(dummy); _delay_ms(SAMPLE_DELAY); } return (0); }
int main(void) { char serialCharacter; // -------- Inits --------- // LED_DDR = 0xff; /* set up LEDs for output */ initUSART(); printString("Hello World!\r\n"); /* to test */ // ------ Event loop ------ // while (1) { serialCharacter = receiveByte(); transmitByte(serialCharacter); LED_PORT = serialCharacter; /* display ascii/numeric value of character */ } /* End event loop */ return 0; }
void printInt(int i) { if (i < 0) { printString("-"); i = -i; } if (i == 0) { transmitByte('0'); } else { transmitByte('0' + (i / 10000)); /* Ten-thousands */ transmitByte('0' + ((i / 1000) % 10)); /* Thousands */ transmitByte('0' + ((i / 100) % 10)); /* Hundreds */ transmitByte('0' + ((i / 10) % 10)); /* Tens */ transmitByte('0' + (i % 10)); /* Ones */ } }