spacepaste

  1.  
  2. /*
  3. * target cpu: atmega168
  4. * registers like UCSR0A are named differently on a atmega32 for example, watch out!
  5. *
  6. * this code receives data over the uart from a HC-05 bluetooth module
  7. * and switches on led1 if the character that was received is: h
  8. * and led2 if the character is z
  9. *
  10. *
  11. * ----
  12. * tested with:
  13. * bluetooth terminal
  14. * qwerty.bluetooth terminal
  15. *
  16. * i know the name looks stupid, but thats how the coder decided to go
  17. * https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal&hl=en
  18. *
  19. * version: 6.1104
  20. * e-mail: aries156@gmail.com
  21. *
  22. * in the setup, activate '\r\n' line endings
  23. * note: tested other bluetooth apps, but this free one just worked
  24. *
  25. * to use the software:
  26. * ONLY THE FIRST TIME: you need to "pair" the devices - for whatever reason, bluetooth likes to get to know each other
  27. * when you first make contact (like that worked out with the bork) - so you "pair" your device with a PIN code.
  28. * the hc05 modules pin code is the extremely secure "1234" just to make pairing absolutely useless. (from a security point of view)
  29. *
  30. *
  31. * start the app, choose: connect a device - insecure
  32. * select your hc-05 module (default name: HC-05)
  33. * type a h to activate led1
  34. * type a z to activate led2
  35. * ----
  36. *
  37. * circuit:
  38. * hc05 rx -> atmega168 tx (PD1)
  39. * hx05 tx -> atmega168 rx (PD0)
  40. *
  41. * voltages:
  42. * 5V supply voltage:
  43. * i was running the circuit at 5V, so i needed a voltage devider between the atmega TX and the HC05 RX
  44. * to get the voltage down from 5v to about 3.3v
  45. *
  46. * the modules operating voltage is 3.3v - 6v, so yes you can attach 5v to VCC of the module
  47. * but the voltage for the RX on the HC05 is 3.3V - so do a voltage divider
  48. * 3.3v supply voltage:
  49. * NOT TESTED, but should work just fine. skip the voltage divider.
  50. *
  51. * ----
  52. *
  53. *
  54. *
  55. * code by: julius junghans
  56. */
  57. // atmega 168
  58. #include <avr/io.h>
  59. #include <avr/interrupt.h>
  60. #include <util/delay.h>
  61. #include <string.h>
  62. // you always read online that the modules are preset to 9600 bauds, i just found one source who said otherwies.
  63. // but that source could have been confused by the fact that the AT command mode works with 38400
  64. #define BAUD 9600
  65. /*
  66. * dont forget to define F_CPU in the Makefile.
  67. * something like:
  68. * F_OSC = 16000000
  69. * F_CPU = $(F_OSC)
  70. * CFLAGS = -mmcu=$(DEVICE) -DF_CPU=$(F_CPU)
  71. *
  72. * sometimes F_OSC is used, probably just another name for F_CPU
  73. */
  74. #define MYUBRR ((uint16_t) ((F_CPU / ((BAUD) * 16.0)) + .5) - 1)
  75. #define LED1 PB0
  76. #define LED2 PC5
  77. // contains what the bluetooth module received
  78. uint8_t data;
  79. void USART_Init(unsigned int ubrr)
  80. {
  81. // set baud rate
  82. UBRR0H = (unsigned char)(ubrr>>8);
  83. UBRR0L = (unsigned char)(ubrr);
  84. // enable receiver, transmitter and interrupts for rx/tx
  85. UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<< TXCIE0);
  86. // frame format: 8 bits data, 1 stop bit, no parity - these are the default settings for atmega168.
  87. // no change needed
  88. // could be changed here: //(0<<UPM00) not tested
  89. // UCSR0C = (1<<UCSZ00) | (1<<UCSZ01) | (0<<UPM00) | (0<<UPM01);
  90. }
  91. void USART_transmit(char c)
  92. {
  93. // the commented out test if we are ready to send should work too
  94. //while ((UCSR0A & (1 << UDRE0)) == 0) {};
  95. while ( !(UCSR0A & (1<<UDRE0)) ) {}
  96. UDR0 = c;
  97. }
  98. //char char_array[] = {'a','b','c','\r','\n',0x00};
  99. /*void send_string(char s[])
  100. {
  101. int i =0;
  102. while (s[i] != 0x00) {
  103. USART_transmit(s[i]);
  104. i++;
  105. }
  106. }*/
  107. /*
  108. * turn on led1 and led2, wait 200ms, turn them off - do this three times
  109. * looks like a flashing led
  110. */
  111. void flash_leds(void) {
  112. for (int n=0; n<3; n++) {
  113. PORTB |= (1 << LED1); // Turn on LED1
  114. PORTC |= (1 << LED2); // Turn on LED2
  115. _delay_ms(200);
  116. PORTB &= ~(1 << LED1); // Turn off LED1
  117. PORTC &= ~(1 << LED2); // Turn off LED2
  118. _delay_ms(200);
  119. }
  120. }
  121. int main(void)
  122. {
  123. DDRB |= (1 << LED1);
  124. DDRC |= (1 << LED2);
  125. // led test
  126. // turn on both leds (LEDx is defined at the start of the code) to see if we did setup the
  127. // ports/pins correctly and if they are really connected in the circuit
  128. // turn them off 3 seconds later
  129. // note: if you compiled your code with F_CPU=8000000 but the chip is really running with a 16Mhz Crystal,
  130. // they will not stay on for 3 seconds....they will go out earlier.
  131. PORTB |= (1 << LED1); // Turn on LED1
  132. PORTC |= (1 << LED2); // Turn on LED2
  133. _delay_ms(3000);
  134. PORTB &= ~(1 << LED1); // Turn off LED1
  135. PORTC &= ~(1 << LED2); // Turn off LED2
  136. sei();
  137. USART_Init(MYUBRR);
  138. _delay_ms(10);
  139. while(1){}
  140. }
  141. // this is the interrupt service routine for receiving (its named differently on atmega32 for example)
  142. ISR(USART_RX_vect)
  143. {
  144. data = UDR0;
  145. if (data == 'h') {
  146. PORTB |= (1 << LED1); // Turn on LED1
  147. PORTC &= ~(1 << LED2); // Turn off LED2
  148. }
  149. else if (data == 'z') {
  150. PORTB &= ~(1 << LED1); // Turn off LED1
  151. PORTC |= (1 << LED2); // Turn on LED2
  152. }
  153. // the terminal app terminates each string with '\r\n' - the windows way of a newline
  154. // (linux only uses '\n')
  155. // so, if we dont skip '\n' and '\r' here and send z led1 will go on for some microseconds? or shorter
  156. // - you wont see led 1 going on.
  157. // because after the code read the z it will immediately receive the next character, which is a '\r'
  158. // and the leds will blink. same for '\n'
  159. else if (data == '\n') {
  160. // do nothing
  161. }
  162. else if (data == '\r') {
  163. // do nothing
  164. }
  165. else {
  166. //send_string(data);
  167. flash_leds();
  168. }
  169. }
  170. // same as above for transfer
  171. ISR(USART_TX_vect)
  172. {
  173. data = 0;
  174. PORTB &= ~(1 << LED1); // Turn off LED1
  175. PORTC |= (1 << LED2); // Turn on LED2
  176. }
  177.