spacepaste

  1.  
  2. /*
  3. * * Bluetooth_Dongle_test.cpp
  4. * *
  5. * * Created: 18.01.2014 16:59:18
  6. * * Author: h0d3nt3uf3l
  7. * */
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <util/delay.h>
  11. #define BAUD 9600
  12. //#define MYUBRR F_CPU/16/BAUD-1
  13. #define MYUBRR ((uint16_t) ((F_CPU / ((BAUD) * 16.0)) + .5) - 1)
  14. uint8_t data;
  15. void USART_Init(unsigned int ubrr)
  16. {
  17. /*Set baud rate*/
  18. UBRRH = (unsigned char)(ubrr>>8);
  19. UBRRL = (unsigned char)(ubrr);
  20. /* Enable receiver and transmitter & Interrupts for RX & TX*/
  21. UCSRB = (1<<RXEN) | (1<<TXEN) | (1<<RXCIE) | (1<< TXCIE);
  22. /* Set frame format: 8data, 1stop bit */
  23. UCSRC = (1<<UCSZ0) | (1<<UCSZ1);
  24. }
  25. void USART_transmit()
  26. {
  27. UDR = (data);
  28. }
  29. int main(void)
  30. {
  31. DDRD |= (1 << PD6) | (1 << PD7);
  32. //led test
  33. PORTD |= (1 << PD6); // Turn on LED1
  34. PORTD |= (1 << PD7); // Turn on LED2
  35. _delay_ms(3000);
  36. PORTD &= ~(1 << PD6); // Turn off LED1
  37. PORTD &= ~(1 << PD7); // Turn off LED2
  38. sei();
  39. USART_Init(MYUBRR);
  40. _delay_ms(10);
  41. while(1)
  42. ;
  43. }
  44. ISR(USART_RXC_vect) //Receive complete
  45. {
  46. data = UDR;
  47. USART_transmit();
  48. PORTD |= (1 << PD6); // Turn on LED1
  49. PORTD &= ~(1 << PD7); // Turn off LED2
  50. }
  51. ISR(USART_TXC_vect) //Transmit complete
  52. {
  53. data = 0;
  54. PORTD &= ~(1 << PD6); // Turn off LED1
  55. PORTD |= (1 << PD7); // Turn on LED2
  56. }
  57.