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. //#include <avr/interrupt.h>
  23. #define F_CPU 16000000 // 16 MHz oscillator.
  24. #define BaudRate 9600
  25. #define MYUBRR (F_CPU / 16 / BaudRate ) - 1
  26. void delayLong()
  27. {
  28. unsigned int delayvar;
  29. delayvar = 0;
  30. while (delayvar <= 65500U)
  31. {
  32. asm("nop");
  33. delayvar++;
  34. }
  35. }
  36. unsigned char serialCheckRxComplete(void)
  37. {
  38. return( UCSR0A & _BV(RXC0)) ; // nonzero if serial data is available to read.
  39. }
  40. unsigned char serialCheckTxReady(void)
  41. {
  42. return( UCSR0A & _BV(UDRE0) ) ; // nonzero if transmit register is ready to receive new data.
  43. }
  44. unsigned char serialRead(void)
  45. {
  46. while (serialCheckRxComplete() == 0) // While data is NOT available to read
  47. {;;}
  48. return UDR0;
  49. }
  50. void serialWrite(unsigned char DataOut)
  51. {
  52. while (serialCheckTxReady() == 0) // while NOT ready to transmit
  53. {;;}
  54. UDR0 = DataOut;
  55. }
  56. void establishContact() {
  57. while (serialCheckRxComplete() == 0) {
  58. serialWrite('H');
  59. // serialWrite(65U);
  60. delayLong();
  61. delayLong();
  62. delayLong();
  63. delayLong();
  64. delayLong();
  65. delayLong();
  66. delayLong();
  67. }
  68. }
  69. int main (void)
  70. {
  71. //Interrupts are not needed in this program. You can optionally disable interrupts.
  72. //asm("cli"); // DISABLE global interrupts.
  73. DDRD = _BV(1);
  74. DDRB = _BV(0) | _BV(1) | _BV(3) | _BV(5);
  75. //Serial Initialization
  76. /*Set baud rate */
  77. UBRR0H = (unsigned char)(MYUBRR>>8);
  78. UBRR0L = (unsigned char) MYUBRR;
  79. /* Enable receiver and transmitter */
  80. UCSR0B = (1<<RXEN0)|(1<<TXEN0);
  81. /* Frame format: 8data, No parity, 1stop bit */
  82. UCSR0C = (3<<UCSZ00);
  83. int firstSensor = 0; // first analog sensor
  84. int secondSensor = 0; // second analog sensor
  85. int thirdSensor = 0; // digital sensor
  86. int inByte = 0; // incoming serial byte
  87. PORTB |= _BV(1); // Turn on LED @ PB1
  88. establishContact(); // send a byte to establish contact until Processing responds
  89. PORTB &= 253U; // Turn off LED
  90. for (;;) // main loop
  91. {
  92. if (serialCheckRxComplete()) {
  93. PORTB |= _BV(1); // Turn on LED @ PB1
  94. inByte = serialRead();
  95. // Simulated data!
  96. firstSensor++;
  97. secondSensor = firstSensor * firstSensor;
  98. thirdSensor = firstSensor + secondSensor;
  99. serialWrite(firstSensor & 255U);
  100. serialWrite(secondSensor & 255U);
  101. serialWrite(thirdSensor & 255U);
  102. //serialWrite(inByte);
  103. PORTB &= 253U; // Turn off LED
  104. }
  105. } //End main loop.
  106. return 0;
  107. }
  108.