반응형
앞서 Bluetooth를 MCU와 Bluetooth를 연결하고, Mobile과 연결하여 제어하는 예시 실습했다.
다음으로는 Bluetooth Module 사이에서 연결하는 것을 실습할 예정이다.
실습보드는 Atmega128을 기본으로 한다.
ATcommand를 통해 Master mode와 Slave mode를 설정한다.
Usart0 Header File
/*
Header file for USART #0
*/
#define sbi(reg, bit) reg |= (1<<(bit))
#define cbi(reg, bit) reg &= ~(1<<(bit))
void USART0_Init( INT16 ubrr );
void USART0_Transmit( char data );
void USART0_Transmit_String(char *str);
void USART0_Transmit_NewLine(void);
char USART0_Receive( void );
void USART0_Init( INT16 ubrr )
{
// Set baud rate
UBRR0H = (INT8)(ubrr>>8);
UBRR0L = (INT8)ubrr;
// Enable U2X
if(U2X_S == 2)
sbi(UCSR0A, U2X);
else
cbi(UCSR0A, U2X);
// Enable receiver and transmitter
sbi(UCSR0B, RXEN);
sbi(UCSR0B, TXEN);
// Set frame format: 8data, 1stop bit
cbi(UCSR0C, UMSEL); // asynch
cbi(UCSR0C, USBS); // 1 Stop bit
cbi(UCSR0C, UPM01); // No parity
cbi(UCSR0C, UPM00);
cbi(UCSR0B, UCSZ02); // 8-bit
sbi(UCSR0C, UCSZ01);
sbi(UCSR0C, UCSZ00);
// Enable interrupt
// sbi(UCSR0B, RXCIE); // RX
// sbi(UCSR0B, TXCIE); // TX
// sbi(UCSR0B, UDRIE); // UDR Empty
// sei(); // Enable all interrupt
}
void USART0_Transmit( char data )
{
// Wait for empty transmit buffer
while ( !( UCSR0A & 0x20 ) ) // (1<<UDRE)
;
// Put data into buffer, sends the data
UDR0 = data;
}
void USART0_Transmit_String(char *str)
{
while(*str != '\0')
{
if(*str == '\n')
{
USART0_Transmit_NewLine();
}
else
{
USART0_Transmit(*str);
}
str++;
}
}
void USART0_Transmit_NewLine(void)
{
USART0_Transmit(0x0A); // LF
USART0_Transmit(0x0D); // CR
}
char USART0_Receive( void )
{
// Wait for data to be received
while ( !(UCSR0A & 0x80) ) //(1<<RXC)
{
;
}
// Get and return received data from buffer
return UDR0;
}
Usart1 Header File
/*
* USART_ch1.h
* Author: kimsangmin
*/
#ifndef USART_CH1_H_
#define USART_CH1_H_
typedef unsigned char INT8;
typedef unsigned int INT16;
#define sbi(reg, bit) reg |= (1<<(bit))
#define cbi(reg, bit) reg &= ~(1<<(bit))
void USART1_Init( INT16 ubrr );
void USART1_Transmit( char data );
void USART1_Transmit_String(char *str);
void USART1_Transmit_NewLine(void);
char USART1_Receive( void );
void USART1_Init( INT16 ubrr )
{
// Set baud rate
UBRR1H = (INT8)(ubrr>>8);
UBRR1L = (INT8)ubrr;
// Enable U2X
if(U2X_S == 2)
sbi(UCSR1A, U2X);
else
cbi(UCSR1A, U2X);
// Enable receiver and transmitter
sbi(UCSR1B, RXEN);
sbi(UCSR1B, TXEN);
// Set frame format: 8data, 1stop bit
cbi(UCSR1C, UMSEL); // asynch
cbi(UCSR1C, USBS); // 1 Stop bit
cbi(UCSR1C, UPM11); // No parity
cbi(UCSR1C, UPM10);
cbi(UCSR1B, UCSZ12); // 8-bit
sbi(UCSR1C, UCSZ11);
sbi(UCSR1C, UCSZ10);
// Enable interrupt
// sbi(UCSR1B, RXCIE); // RX
// sbi(UCSR1B, TXCIE); // TX
// sbi(UCSR1B, UDRIE); // UDR Empty
// sei(); // Enable all interrupt
}
void USART1_Transmit( char data )
{
// Wait for empty transmit buffer
while ( !( UCSR1A & 0x20 ) ) // (1<<UDRE)
;
// Put data into buffer, sends the data
UDR1 = data;
}
void USART1_Transmit_String(char *str)
{
while(*str != '\0')
{
if(*str == '\n')
{
USART1_Transmit_NewLine();
}
else
{
USART1_Transmit(*str);
}
str++;
}
}
void USART1_Transmit_NewLine(void)
{
USART1_Transmit(0x0A); // LF
USART1_Transmit(0x0D); // CR
}
char USART1_Receive( void )
{
// Wait for data to be received
while ( !(UCSR1A & 0x80) ) //(1<<RXC)
{
;
}
// Get and return received data from buffer
return UDR1;
}
#endif /* USART_CH1_H_ */
Bluetooth Master
typedef unsigned char INT8;
typedef unsigned int INT16;
#define F_CPU 8000000
#define BAUD 9600
#define U2X_S 2
#define MYUBRR ((F_CPU*U2X_S)/(16L*BAUD)-1)
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "USART_ch0.h"
int main(void)
{
//INT8 i=0x03;
USART0_Init(MYUBRR);
USART0_Transmit_NewLine(); //USART0_Transmit 단어 한글자 보내는 것
USART0_Transmit('M'); USART0_Transmit('A');
USART0_Transmit('S'); USART0_Transmit('T');
USART0_Transmit('E'); USART0_Transmit('R');
while(1)
{
USART0_Transmit_NewLine();
USART0_Transmit('K'); //띄우고 싶은거 담아서 보내면 터미널에 뜬다
_delay_ms(500);
}
}
Bluetooth Slave
typedef unsigned char INT8;
typedef unsigned int INT16;
#define F_CPU 8000000
#define BAUD 9600
#define U2X_S 2
#define MYUBRR ((F_CPU*U2X_S)/(16L*BAUD)-1)
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "USART_ch0.h"
#include "USART_ch1.h"
volatile INT16 KeyScan;
ISR(USART0_RX_vect)
{
KeyScan = UDR0;
}
int main(void)
{
USART0_Init(MYUBRR); // BL
USART1_Init(MYUBRR); // RS-232
sbi(UCSR0B, RXCIE); // RX
sei();
USART1_Transmit_NewLine();//USART0_Transmit 단어 한글자 보내는 것
USART1_Transmit('S'); USART1_Transmit('L');
USART1_Transmit('A'); USART1_Transmit('V');
USART1_Transmit('E');
char ch;
while(1)
{
ch = USART0_Receive();
USART1_Transmit_NewLine();
USART1_Transmit(ch); //띄우고 싶은거 담아서 보내면 터미널에 뜬다
_delay_ms(500);
}
}
반응형
'Embedded System > ATmega128' 카테고리의 다른 글
Balancing Robot (0) | 2020.04.04 |
---|---|
C-LCD 8-bit Control and 4-bit Control (4) | 2020.04.04 |
NEO-7M (GPS module) (1) | 2020.03.28 |
HC06 (Bluetooth Module) Mobile 연동하기 (0) | 2020.03.27 |
HC-SR04 Module (초음파 센서 모듈) (3) | 2020.03.27 |
댓글