Advertisement
Utshaw

Bluetooth_interfacing_ATmega32

Jul 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #define F_CPU 1000000UL // 1MHz clock speed
  2. /* Define CPU clock Frequency e.g. here its 8MHz */
  3. #include <avr/io.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdbool.h>
  8. #include <util/delay.h> /* Include AVR std. library file */
  9. #include <avr/interrupt.h>
  10.  
  11. #define BAUD_PRESCALE ((F_CPU / (BAUDRATE * 16UL)) - 1) /* Define prescale value */
  12.  
  13. void USART_Init(unsigned long BAUDRATE) /* USART initialize function */
  14. {
  15. UCSRA = 0b00000000;
  16. //UCSRB |= (1 << RXEN) | (1 << TXEN) | (1 << RXCIE); /* Enable USART transmitter and receiver and Interrupt */
  17. UCSRB |= (1 << RXEN) | (1 << TXEN) ; /* Enable USART transmitter and receiver */
  18.  
  19. UCSRC |= (1 << URSEL)| (1 << UCSZ0) | (1 << UCSZ1); /* Write USCRC for 8 bit data and 1 stop bit */
  20.  
  21. UBRRL = BAUD_PRESCALE; /* Load UBRRL with lower 8 bit of prescale value */
  22. UBRRH = (BAUD_PRESCALE >> 8); /* Load UBRRH with upper 8 bit of prescale value */
  23.  
  24. //UBRRL = 0;
  25. //UBRRH = 12; // Assuming 1 Mhz Clock
  26. }
  27.  
  28. void UART_init(){
  29. UCSRA = 0x02 ;
  30. UCSRB = 0x18 ;
  31. UCSRC = 0x86 ;
  32. UBRRL = 12 ;
  33. UBRRH = 0 ;
  34. }
  35.  
  36. void UART_send(unsigned char data){
  37. while((UCSRA&(1<<UDRE))==0) ;
  38. UDR = data ;
  39. }
  40.  
  41.  
  42. char USART_RxChar() /* Data receiving function */
  43. {
  44. while (!(UCSRA & (1 << RXC))); /* Wait until new data receive */
  45. return(UDR); /* Get and return received data */
  46. }
  47.  
  48. void USART_TxChar(char data) /* Data transmitting function */
  49. {
  50. UDR = data; /* Write data to be transmitting in UDR */
  51. while (!(UCSRA & (1<<UDRE))); /* Wait until data transmit and buffer get empty */
  52. }
  53.  
  54.  
  55. int main(void)
  56. {
  57. DDRA = 0xFF;
  58. PORTA |= (1<<PORTA7);
  59. // USART_Init(9600); /* initialize USART with 9600 baud rate */
  60. //sei();
  61. UART_init() ;
  62.  
  63. unsigned char data ;
  64.  
  65. while (1)
  66. {
  67. //data = USART_RxChar();
  68.  
  69. while((UCSRA&(1<<RXC))==0) ;
  70. data = UDR ;
  71. if(data=='1'){
  72. PORTA |= (1<<PORTA0);
  73. UART_send(data) ;
  74. }
  75. else if(data=='0'){
  76. PORTA |= (1<<PORTA1);
  77. UART_send(data) ;
  78. }
  79. else{
  80. UART_send(data) ;
  81. }
  82.  
  83. PORTA = data;
  84.  
  85. //_delay_ms(1000);
  86.  
  87. //PORTA= 0x00;
  88. //_delay_ms(300);
  89.  
  90.  
  91. //PORTA |= (1<<PORTA7);
  92.  
  93. }
  94.  
  95. }
  96.  
  97. ISR (USART_RXC_vect)
  98. {
  99. uint8_t oldsrg = SREG;
  100.  
  101. cli();
  102. char received_char = UDR;
  103.  
  104. PORTA = received_char;
  105. _delay_ms(600);
  106.  
  107. PORTA= 0x00;
  108. _delay_ms(50);
  109.  
  110. PORTA= 0xFF;
  111. _delay_ms(100);
  112.  
  113. PORTA= 0x00;
  114. _delay_ms(100);
  115.  
  116. SREG = oldsrg;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement