daily pastebin goal
1%
SHARE
TWEET

Arduino Serial Input/Output Test.c

Ham62 Feb 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr\io.h>
  2. #include <util\delay.h>
  3. #include <stdio.h>
  4.  
  5. #include "moreTypes.h"
  6. #define true -1
  7. #define false 0
  8.  
  9. #define F_CPU 16000000UL // Frequency of processor
  10. #define BAUD  9600       // Baud rate of serial connections
  11.  
  12. void initUART() {
  13.     #include <util\setbaud.h>
  14.     UBRR0H = UBRRH_VALUE;
  15.     UBRR0L = UBRRL_VALUE;
  16.  
  17.     #if USE_2X
  18.         UCSR0A |=  _BV(U2X0);
  19.     #else
  20.         UCSR0A &= ~_BV(U2X0);
  21.     #endif
  22.  
  23.     UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); // Use 8-bit data
  24.     UCSR0B = _BV(RXEN0) | _BV(TXEN0);   // Enable RX/TX
  25. }
  26.  
  27. // Read a byte from the UART
  28. ubyte getByte(int iWaitData) {
  29.     // If wait flag set, loop until data is ready on RX
  30.     if (iWaitData)
  31.        loop_until_bit_is_set(UCSR0A, RXC0);
  32.  
  33.     // Return byte from serial UART
  34.     return UDR0;
  35. }
  36.  
  37. int readString(char* str, int iMaxLength) {
  38.     ubyte tmpByte;
  39.     int iDataRead = 0;
  40.    
  41.     while (1) {
  42.         tmpByte = getByte(true);
  43.        
  44.         switch(tmpByte) {
  45.             // If we read a newline the string parsing is done
  46.             case '\r': case '\n':
  47.                 str[iDataRead] = '\0'; // Set last char to null-terminator
  48.                 return iDataRead;      // Return length of string read
  49.            
  50.             case 0x7F: // Backspace
  51.                 str[iDataRead] = '\0'
  52.                 if (iDataRead > 0)
  53.                     iDataRead--;
  54.                 break;
  55.  
  56.             case else:
  57.                 str[iDataRead++] = tmpByte;
  58.                 if (iDataRead >= iMaxLength)
  59.                     return iDataRead;
  60.         }
  61.  
  62.     }
  63. }
  64.  
  65. // Send a byte to the UART
  66. void sendByte(ubyte data) {
  67.     loop_until_bit_is_set(UCSR0A, UDRE0); // Wait until data register empty
  68.     UDR0 = data;
  69. }
  70.  
  71. // Send raw data of specified size over serial
  72. void sendData(ubyte* data, int dataLen) {
  73.     for (int i = 0; i < dataLen; i++)
  74.         sendByte(data[i]);
  75. }
  76.  
  77. // Send a null-terminated string over serial
  78. void sendString(char* str) {
  79.     int i = 0;
  80.     while (str[i] != 0) {
  81.         sendByte(str[i++]);
  82.     }
  83. }
  84.  
  85. char* sBuff = "Read byte: 0xFF\r\n\0";
  86. void main() {
  87.     initUART();
  88.     while (1) {
  89.         sprintf(sBuff, "Read byte: 0x%02X\r\n\0", getByte(true));
  90.         sendString(sBuff);
  91.     }
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top