PIC16F887 Based
Master Code
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
/*
* Project name:
Master;
* Copyright:
(c) MikroElektronika, 2005-2011.
* Revision History:
20110426:
- initial release (SZ);
* Description:
This is a simple project which demonstrates the use of the BlueTooth Stick2 module.
After establishing the connection Master repeatedly sends two strings ("mikroE" and "BlueTooth Stick2")
to the Slave. The Slave receives them and displays them at the 2x16 COG LCD.
* Test configuration:
MCU: PIC16F887
http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf#page=123
Dev.Board: EasyPIC6
http://www.mikroe.com/eng/products/view/297/easypic6-development-system/
Oscillator: HS, 8.00000 MHz
Ext. Modules: BlueTooth Stick2 at PORTC
ac:BlueTooth_Stick2_Board
http://www.mikroe.com/eng/products/view/680/bluetooth-2-stick/
SW: mikroC PRO for PIC
http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
* NOTES:
- BlueTooth Stick2: Connect only Vcc, Gnd, Rx and Tx lines. You could use our Wire Jumpers:
http://www.mikroe.com/eng/products/view/495/wire-jumpers/
Also, 5V-3.3V Voltage Translator Board is required:
http://www.mikroe.com/eng/products/view/182/5v-3-3v-voltage-translator-board/
- Note that BlueGiga WT41 has default baudrate 115200bps and that you need to change the
its firmware in order to run this example at 19200bps. Find out more here: http://www.bluegiga.com
- Turn on both modules at the same time and wait for about one minute until the
connection between the Master and the Slave establishes.
*/
void Wait() //Wait 메소드
{
Delay_ms(1000);
}
void Wait2() //Wait2 메소드
{
char i;
for(i = 0; i < 30; i++) Delay_ms(1000);
}
void main()
{
// All pins as digital I/Os
ANSEL = 0;
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
UART1_Init(19200); // Initialize UART1 module
Wait(); Wait(); Wait();
UART1_Write_Text("SET RESET"); // The SET RESET command returns the factory settings of the module
UART1_Write(0x0D); // CR
Wait();
UART1_Write_Text("SET BT PAIR *");
// If command SET BT PAIR * is given, all pairings will be removed
UART1_Write(0x0D); // CR
Wait();
UART1_Write_Text("RESET"); // Command RESET is used to perform a software reset
UART1_Write(0x0D); // CR
Wait();
// The CALL command is used to initiate Bluetooth connections to the remote devices
// Instead of xx:xx:xx:xx:xx:xx enter real MAC address of the BT module you want to call
UART1_Write_Text("CALL xx:xx:xx:xx:xx:xx 1 RFCOMM");
UART1_Write(0x0D); // CR
Wait2();
while (1) {
UART1_Write_Text("mikroE"); // Sending Data to Slave module
UART1_Write(13); // CR
Wait();
UART1_Write_Text("BlueTooth Stick2"); // Data
UART1_Write(13); // CR
Wait();
}
}
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
/*
* Project name:
Slave;
* Copyright:
(c) MikroElektronika, 2005-2011.
* Revision History:
20110426:
- initial release (SZ);
* Description:
This is a simple project which demonstrates the use of the BlueTooth Stick2 module.
After establishing the connection Master repeatedly sends two strings ("mikroE" and "BlueTooth Stick2")
to the Slave. The Slave receives them and displays them at the 2x16 LCD.
* Test configuration:
MCU: PIC16F887
http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf#page=123
Dev.Board: EasyPIC6
http://www.mikroe.com/eng/products/view/297/easypic6-development-system/
Oscillator: HS, 8.00000 MHz
Ext. Modules: BlueTooth Stick2 at PORTC
ac:BlueTooth_Stick2_Board
http://www.mikroe.com/eng/products/view/680/bluetooth-2-stick/
SW: mikroC PRO for PIC
http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
* NOTES:
- BlueTooth Stick2: Connect only Vcc, Gnd, Rx and Tx lines. You could use our Wire Jumpers:
http://www.mikroe.com/eng/products/view/495/wire-jumpers/
Also, 5V-3.3V Voltage Translator Board is required:
http://www.mikroe.com/eng/products/view/182/5v-3-3v-voltage-translator-board/
- Note that BlueGiga WT41 has default baudrate 115200bps and that you need to change the
its firmware in order to run this example at 19200bps. Find out more here: http://www.bluegiga.com
- Turn on both modules at the same time and wait for about one minute until the
connection between the Master and the Slave establishes.
- Turn ON the Lcd Backlight.
*/
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char txt[17];
unsigned short i, tmp, DataReady;
// Uart Rx interrupt handler
void interrupt(){
if (PIR1.RCIF == 1) { // Do we have uart rx interrupt request?
tmp = UART1_Read(); // Get received byte
if (tmp == 13) {
txt[i] = 0; // Puting 0 at the end of the string
DataReady = 1; // Data is received
}
else {
txt[i] = tmp; // Moving the data received from UART to string txt[]
i++;
}
PIR1.RCIF = 0;
}
}
void Wait(){
Delay_ms(1000);
}
void main() {
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
UART1_Init(19200); // Initialize UART1 module
Wait();
LCD_Init(); // LCD Init
LCD_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Connecting!");
Lcd_Out(2,1,"Please, wait...");
UART1_Write_Text("SET RESET"); // The SET RESET command returns the factory settings of the module
UART1_Write(0x0D); // CR
Wait();
UART1_Write_Text("SET BT PAIR *"); // If command 밪ET BT PAIR *?is given, all pairings will be removed
UART1_Write(0x0D); // CR
Wait();
UART1_Write_Text("RESET"); // Command RESET is used to perform a software reset
UART1_Write(0x0D); // CR
Wait();
UART1_Write_Text("SET BT AUTH *"); // PIN code will be disabled and no encryption can be used during BT connections
UART1_Write(0x0D); // CR
Wait();
Wait();
Wait();
Wait();
LCD_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Connected!");
DataReady = 0;
// setup interrupts
PIE1.RCIE = 1;
INTCON.PEIE = 1;
INTCON.GIE = 0;
Wait();
LCD_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Receiving...");
Wait();
while (1) {
tmp = 0; // Init variables
i = 0;
memset(txt, 0, 17); // Clear array of chars
INTCON.GIE = 1; // Interrupts allowed
while (!DataReady) { // Wait while the data is received
;
}
INTCON.GIE = 0; // Interrupts forbiden
DataReady = 0;
LCD_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Received:");
Lcd_Cmd(_LCD_SECOND_ROW);
i = 0;
while (txt[i] != 0){
Lcd_Chr_CP(txt[i]); // Displaying the received text on the LCD
i++;
}
}
}
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲