Advertisement
Guest User

UART_Routines.c

a guest
Nov 5th, 2010
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. //**************************************************************
  2. //******** FUNCTIONS FOR SERIAL COMMUNICATION USING UART *******
  3. //**************************************************************
  4. //Controller: ATmega168 (Clock: 8 Mhz-internal)
  5. //Compiler: AVR-GCC
  6. //Version : 2.0
  7. //Author: CC Dharmani, Chennai (India)
  8. // www.dharmanitech.com
  9. //Date: 26 Feb 2009
  10. //**************************************************************
  11.  
  12. //**************************************************
  13. // ***** SOURCE FILE : UART_routines.c ******
  14. //**************************************************
  15.  
  16. #include "UART_routines.h"
  17. #include <avr/io.h>
  18. #include <avr/pgmspace.h>
  19.  
  20.  
  21. //**************************************************
  22. //Function to receive a single byte
  23. //*************************************************
  24. unsigned char receiveByte( void )
  25. {
  26. unsigned char data, status;
  27.  
  28. while(!(UCSR0A & (1<<RXC0)));   // Wait for incomming data
  29.  
  30. status = UCSR0A;
  31. data = UDR0;
  32.  
  33. return(data);
  34. }
  35.  
  36. //***************************************************
  37. //Function to transmit a single byte
  38. //***************************************************
  39. void transmitByte( unsigned char data )
  40. {
  41. while ( !(UCSR0A & (1<<UDRE0)) );  // Wait for empty transmit buffer
  42. UDR0 = data;                      //Start transmition
  43.  
  44. }
  45.  
  46.  
  47.  
  48.  
  49. //***************************************************
  50. //Function to transmit hex format data
  51. //first argument indicates type: CHAR, INT or LONG
  52. //Second argument is the data to be displayed
  53. //***************************************************
  54. /*void transmitHex( unsigned char dataType, unsigned long data )
  55. {
  56. unsigned char count, i, temp;
  57. unsigned char dataString[] = "0x        ";
  58.  
  59. if (dataType == CHAR) count = 2;
  60. if (dataType == INT) count = 4;
  61. if (dataType == LONG) count = 8;
  62.  
  63. for(i=count; i>0; i--)
  64. {
  65.   temp = data % 16;
  66.   if((temp>=0) && (temp<10)) dataString [i+1] = temp + 0x30;
  67.   else dataString [i+1] = (temp - 10) + 0x41;
  68.  
  69.   data = data/16;
  70. }
  71.  
  72. transmitString (dataString);
  73. }
  74. */
  75.  
  76.  
  77. //***************************************************
  78. //Function to transmit a string in Flash
  79. //***************************************************
  80. void transmitString_F(char* string)
  81. {
  82.   while (pgm_read_byte(&(*string)))
  83.    transmitByte(pgm_read_byte(&(*string++)));
  84. }
  85.  
  86. //***************************************************
  87. //Function to transmit a string in RAM
  88. //***************************************************
  89. void transmitString(unsigned char* string)
  90. {
  91.   while (*string)
  92.    transmitByte(*string++);
  93. }
  94.  
  95. //************ END ***** www.dharmanitech.com *******
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement