spacepaste

  1.  
  2. /*
  3. Title: SerialCom.c
  4. Date Created: 6/9/2009
  5. Last Modified: 6/9/2009
  6. Target: Atmel ATmega168
  7. Environment: AVR-GCC
  8. Note: the makefile is expecting a '168 with a 16 MHz crystal.
  9. Adapted from the Arduino sketch "Serial Call and Response," by Tom Igoe.
  10. // This program sends an ASCII A (byte of value 65) on startup
  11. // and repeats that until it gets some data in.
  12. // Then it waits for a byte in the serial port, and
  13. // sends three (faked) sensor values whenever it gets a byte in.
  14. Written by Windell Oskay, http://www.evilmadscientist.com/
  15. Copyright 2009 Windell H. Oskay
  16. Distributed under the terms of the GNU General Public License, please see below.
  17. Additional license terms may be available; please contact us for more information.
  18. More information about this project is at
  19. http://www.evilmadscientist.com/article.php/peggy
  20. */
  21. #include <avr/io.h>
  22. #define F_CPU 16000000 // 16 MHz oscillator.
  23. #define BaudRate 9600
  24. #define MYUBRR (F_CPU / 16 / BaudRate ) - 1
  25. void delayLong()
  26. {
  27. unsigned int delayvar;
  28. delayvar = 0;
  29. while (delayvar <= 65500U)
  30. {
  31. asm("nop");
  32. delayvar++;
  33. }
  34. }
  35. unsigned char serialCheckRxComplete(void)
  36. {
  37. return( UCSR0A & (1<<RXC0)) ; // nonzero if serial data is available to read.
  38. }
  39. unsigned char serialCheckTxReady(void)
  40. {
  41. return( UCSR0A & (1<<UDRE0) ) ; // nonzero if transmit register is ready to receive new data.
  42. }
  43. unsigned char serialRead(void)
  44. {
  45. while (serialCheckRxComplete() == 0) // While data is NOT available to read
  46. {;;}
  47. return UDR0;
  48. }
  49. void serialWrite(unsigned char DataOut)
  50. {
  51. while (serialCheckTxReady() == 0) // while NOT ready to transmit
  52. {;;}
  53. UDR0 = DataOut;
  54. }
  55. void establishContact() {
  56. while (serialCheckRxComplete() == 0) {
  57. serialWrite('H');
  58. // serialWrite(65U);
  59. delayLong();
  60. delayLong();
  61. delayLong();
  62. delayLong();
  63. delayLong();
  64. delayLong();
  65. delayLong();
  66. }
  67. }
  68. int main (void)
  69. {
  70. //Interrupts are not needed in this program. You can optionally disable interrupts.
  71. //asm("cli"); // DISABLE global interrupts.
  72. DDRD = (1<<1); // portd, pin 1 as output, TXD pin
  73. DDRB = (1<<0) | (1<<1) | (1<<3) | (1<<5);
  74. //Serial Initialization
  75. /*Set baud rate */
  76. UBRR0H = (unsigned char)(MYUBRR>>8);
  77. UBRR0L = (unsigned char) MYUBRR;
  78. /* Enable receiver and transmitter */
  79. UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  80. /* Frame format: 8data, No parity, 1stop bit */
  81. //UCSR0C = (3<<UCSZ00); //== 00000110
  82. //UCRS0C |= (1<<1) | (1<<2); //these settings are also the chip default, so not needed
  83. int inByte = 0; // incoming serial byte
  84. PORTB |= (1<<1); // Turn on LED @ PB1
  85. establishContact(); // send a byte to establish contact until Processing responds
  86. PORTB &= 253U; // Turn off LED
  87. for (;;) {
  88. if (serialCheckRxComplete()) {
  89. PORTB |= (1<<1); // Turn on LED @ PB1
  90. inByte = serialRead();
  91. serialWrite(inByte);
  92. PORTB &= 253U; // Turn off LED
  93. }
  94. }
  95. return 0;
  96. }
  97.