Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <string.h>
  3. #include <avr/interrupt.h>
  4.  
  5.   #define FOSC 16000000 // Clock speed
  6.   #define BAUD 9600
  7.   #define MYUBRR FOSC/16/BAUD-1
  8.   /* UART Buffer Defines */
  9.   #define UART_RX_BUFFER_SIZE 32 /* 2,4,8,16,32,64,128 or 256 bytes */
  10.   #define UART_TX_BUFFER_SIZE 32
  11.   #define UART_RX_BUFFER_MASK (UART_RX_BUFFER_SIZE - 1)
  12.   #if (UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK)
  13.   #error RX buffer size is not a power of 2
  14.   #endif
  15.   #define UART_TX_BUFFER_MASK (UART_TX_BUFFER_SIZE - 1)
  16.   #if (UART_TX_BUFFER_SIZE & UART_TX_BUFFER_MASK)
  17.   #error TX buffer size is not a power of 2
  18.   #endif
  19.   /* Static Variables */
  20.   static char UART_RxBuf[UART_RX_BUFFER_SIZE];
  21.   static volatile char UART_RxHead;
  22.   static volatile char UART_RxTail;
  23.   static char UART_TxBuf[UART_TX_BUFFER_SIZE];
  24.   static volatile char UART_TxHead;
  25.   static volatile char UART_TxTail;
  26.   /* Prototypes */
  27.   void InitUART(unsigned int ubrr_val);
  28.   char ReceiveByte(void);
  29.   void TransmitByte(char data);
  30.   void ReceiveString(char *str);
  31.   void TransmitString(char *str);
  32.  
  33.   char buffer[30];
  34.   char student_number[] = "12345\r\n";
  35.   char my_name[] = "\nDan\n";
  36.   char not_match[] = "\nPlease try again ... \n";
  37.  
  38.  
  39.   int main(void)
  40.   {
  41.       /* Initialize the UART */
  42.       InitUART(MYUBRR);
  43.       sei();
  44.       while(1){
  45.          
  46.         /* Send and check for match */
  47.           ReceiveString(buffer);
  48.           if (strcmp(student_number, buffer) == 0)
  49.               TransmitString(my_name);
  50.           else
  51.             TransmitString(not_match);
  52.          
  53.         /* Echo the received character */
  54.         //TransmitByte(ReceiveByte());
  55.       }
  56.       return 0;
  57.   }
  58.   /* Initialize UART */
  59.   void InitUART(unsigned int ubrr_val)
  60.   {
  61.       char x;
  62.       /* Set the baud rate */
  63.       UBRR0H = (unsigned char)(ubrr_val>>8);
  64.       UBRR0L = (unsigned char)ubrr_val;
  65.       /* Enable UART receiver and transmitter */
  66.       UCSR0B = ((1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0));
  67.       /* Flush receive buffer */
  68.       x = 0;
  69.       UART_RxTail = x;
  70.       UART_RxHead = x;
  71.       UART_TxTail = x;
  72.       UART_TxHead = x;
  73.   }
  74.   /* Interrupt handlers */
  75.   ISR(USART_RX_vect)
  76.   {
  77.       char data;
  78.       unsigned char tmphead;
  79.       /* Read the received data */
  80.       data = UDR0;
  81.       /* Calculate buffer index */
  82.       tmphead = (UART_RxHead + 1) & UART_RX_BUFFER_MASK;
  83.       /* Store new index */
  84.       UART_RxHead = tmphead;
  85.       if (tmphead == UART_RxTail) {
  86.           /* ERROR! Receive buffer overflow */
  87.       }
  88.       /* Store received data in buffer */
  89.       UART_RxBuf[tmphead] = data;
  90.   }
  91.   ISR(USART_UDRE_vect)
  92.   {
  93.       unsigned char tmptail;
  94.       /* Check if all data is transmitted */
  95.       if (UART_TxHead != UART_TxTail) {
  96.           /* Calculate buffer index */
  97.           tmptail = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK;
  98.           /* Store new index */
  99.           UART_TxTail = tmptail;
  100.           /* Start transmission */
  101.           UDR0 = UART_TxBuf[tmptail];
  102.           } else {
  103.           /* Disable UDRE interrupt */
  104.           UCSR0B &= ~(1<<UDRIE0);
  105.       }
  106.   }
  107.   char ReceiveByte(void)
  108.   {
  109.       unsigned char tmptail;
  110.       /* Wait for incoming data */
  111.       while (UART_RxHead == UART_RxTail);
  112.       /* Calculate buffer index */
  113.       tmptail = (UART_RxTail + 1) & UART_RX_BUFFER_MASK;
  114.       /* Store new index */
  115.       UART_RxTail = tmptail;
  116.       /* Return data */
  117.       return UART_RxBuf[tmptail];
  118.   }
  119.   void TransmitByte(char data)
  120.   {
  121.       unsigned char tmphead;
  122.       /* Calculate buffer index */
  123.       tmphead = (UART_TxHead + 1) & UART_TX_BUFFER_MASK;
  124.       /* Wait for free space in buffer */
  125.       while (tmphead == UART_TxTail);
  126.       /* Store data in buffer */
  127.       UART_TxBuf[tmphead] = data;
  128.       /* Store new index */
  129.       UART_TxHead = tmphead;
  130.       /* Enable UDRE interrupt */
  131.       UCSR0B |= (1<<UDRIE0);
  132.   }
  133.   /*
  134.   * This function gets a string of characters from the USART.
  135.   * The string is placed in the array pointed to by str.
  136.   *
  137.   * - This function uses the function ReceiveByte() to get a byte
  138.   * from the UART.
  139.   * - If the received byte is equal to '\n' (Line Feed),
  140.   * the function returns.
  141.   * - The array is terminated with ´\0´.
  142.   */
  143.   void ReceiveString(char *str)
  144.   {
  145.       uint8_t t = 0;
  146.       while ((str[t] = ReceiveByte()) != '\n')
  147.       {
  148.           t++;
  149.       }
  150.       str[t++] = '\n';
  151.       str[t] = '\0';
  152.   }
  153.   /*
  154.   * Transmits a string of characters to the USART.
  155.   * The string must be terminated with '\0'.
  156.   *
  157.   * - This function uses the function TransmitByte() to
  158.   * transmit a byte via the UART
  159.   * - Bytes are transmitted until the terminator
  160.   * character '\0' is detected. Then the function returns.
  161.   */
  162.   void TransmitString(char *str)
  163.   {
  164.       while(*str)
  165.       {
  166.           TransmitByte(*str++);
  167.       }
  168.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement