Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Quick and dirty functions that make serial communications work.
  3. Note that receiveByte() blocks -- it sits and waits _forever_ for
  4. a byte to come in. If you're doing anything that's more interesting,
  5. you'll want to implement this with interrupts.
  6. initUSART requires BAUDRATE to be defined in order to calculate
  7. the bit-rate multiplier. 9600 is a reasonable default.
  8. May not work with some of the older chips:
  9. Tiny2313, Mega8, Mega16, Mega32 have different pin macros
  10. If you're using these chips, see (e.g.) iom8.h for how it's done.
  11. These old chips don't specify UDR0 vs UDR1.
  12. Correspondingly, the macros will just be defined as UDR.
  13. */
  14. #include <avr/io.h>
  15. #include "USART.h"
  16. #include <util/setbaud.h>
  17. void initUSART(void) { /* requires BAUD */
  18. UBRR0H = UBRRH_VALUE; /* defined in setbaud.h */
  19. UBRR0L = UBRRL_VALUE;
  20. #if USE_2X
  21. UCSR0A |= (1 << U2X0);
  22. #else
  23. UCSR0A &= ~(1 << U2X0);
  24. #endif
  25. /* Enable USART transmitter/receiver */
  26. UCSR0B = (1 << TXEN0) | (1 << RXEN0);
  27. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop bit */
  28. }
  29. void transmitByte(uint8_t data) {
  30. /* Wait for empty transmit buffer */
  31. loop_until_bit_is_set(UCSR0A, UDRE0);
  32. UDR0 = data; /* send data */
  33. }
  34. uint8_t receiveByte(void) {
  35. loop_until_bit_is_set(UCSR0A, RXC0); /* Wait for incoming data */
  36. return UDR0; /* return register value */
  37. }
  38. /* Here are a bunch of useful printing commands */
  39. void printString(const char myString[]) {
  40. uint8_t i = 0;
  41. while (myString[i]) {
  42. transmitByte(myString[i]);
  43. i++;
  44. }
  45. }
  46. void readString(char myString[], uint8_t maxLength) {
  47. char response;
  48. uint8_t i;
  49. i = 0;
  50. while (i < (maxLength - 1)) { /* prevent over-runs */
  51. response = receiveByte();
  52. transmitByte(response); /* echo */
  53. if (response == '\r') { /* enter marks the end */
  54. break;
  55. }
  56. else {
  57. myString[i] = response; /* add in a letter */
  58. i++;
  59. }
  60. }
  61. myString[i] = 0; /* terminal NULL character */
  62. }
  63. void printByte(uint8_t byte) {
  64. /* Converts a byte to a string of decimal text, sends it */
  65. transmitByte('0' + (byte / 100)); /* Hundreds */
  66. transmitByte('0' + ((byte / 10) % 10)); /* Tens */
  67. transmitByte('0' + (byte % 10)); /* Ones */
  68. }
  69. void printWord(uint16_t word) {
  70. transmitByte('0' + (word / 10000)); /* Ten-thousands */
  71. transmitByte('0' + ((word / 1000) % 10)); /* Thousands */
  72. transmitByte('0' + ((word / 100) % 10)); /* Hundreds */
  73. transmitByte('0' + ((word / 10) % 10)); /* Tens */
  74. transmitByte('0' + (word % 10)); /* Ones */
  75. }
  76. void printBinaryByte(uint8_t byte) {
  77. /* Prints out a byte as a series of 1's and 0's */
  78. uint8_t bit;
  79. for (bit = 7; bit < 255; bit--) {
  80. if (bit_is_set(byte, bit))
  81. transmitByte('1');
  82. else
  83. transmitByte('0');
  84. }
  85. }
  86. char nibbleToHexCharacter(uint8_t nibble) {
  87. /* Converts 4 bits into hexadecimal */
  88. if (nibble < 10) {
  89. return ('0' + nibble);
  90. }
  91. else {
  92. return ('A' + nibble - 10);
  93. }
  94. }
  95. void printHexByte(uint8_t byte) {
  96. /* Prints a byte as its hexadecimal equivalent */
  97. uint8_t nibble;
  98. nibble = (byte & 0b11110000) >> 4;
  99. transmitByte(nibbleToHexCharacter(nibble));
  100. nibble = byte & 0b00001111;
  101. transmitByte(nibbleToHexCharacter(nibble));
  102. }
  103. uint8_t getNumber(void) {
  104. // Gets a numerical 0-255 from the serial port.
  105. // Converts from string to number.
  106. char hundreds = '0';
  107. char tens = '0';
  108. char ones = '0';
  109. char thisChar = '0';
  110. do { /* shift over */
  111. hundreds = tens;
  112. tens = ones;
  113. ones = thisChar;
  114. thisChar = receiveByte(); /* get a new character */
  115. transmitByte(thisChar); /* echo */
  116. } while (thisChar != '\r'); /* until type return */
  117. return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0');
  118. }