/* 11/03/2012
* Project name:
Project : Bluetooth_click.mcppi
* VO Copyright (c) MikroElektronika, 2011. 20111214: initial release (FJ);
* Description:
This is a simple project which demonstrates the use of Bluetooth click board.
After establishing the connection, Master sends messages to the Slave.
The Slave receives them and displays them at the 2x16 Lcd.
As a Master any bluetooth device can be used: mobile phone, Bluetooth dongle, etc...
* Test configuration:
MCU: PIC18F46K22
Oscillator: HS-PLL 4disabled
10.0000 MHz Crystal
Ext. Modules: Bluetooth click Board - ac:BT_click
2x16 Lcd character display - ac:Lcd
SW: mikroC PRO for PIC
Test en mode 5 sur PIC
sur Archos43it :
trouve FireFly-DB49 puis plus tard
trouve BlueTooh-Click 00:06:66:07:DB:49 ID=1234
test avec f35-bluetooh.bas =>
coté Archos
Connect to listener
---------------------
ecran de selection
Select device
paired devices
N5310 ...
00:24:7D:7A:54:4F
Nokia 2330c-2
E0:A6:70:6C:BF:AD
BlueTooth-Click <---- this one !
00:06:66:07:DB:49
----------------------
listenning
connecting
connected
input Texte : Toto#
....
OK sur LCD
Received : 0
toto
nota le retour à la ligne n'est pas interprété coté PIC,
c'est pourqoui je mets # comme terminateur
CONFIG1H :$300001 : 0x0023
CONFIG2L :$300002 : 0x001F
CONFIG2H :$300003 : 0x003C
CONFIG3H :$300005 : 0x00BF
CONFIG4L :$300006 : 0x0081
CONFIG5L :$300008 : 0x000F
CONFIG5H :$300009 : 0x00C0
CONFIG6L :$30000A : 0x000F
CONFIG6H :$30000B : 0x00E0
* NOTES:
- Place Bluetooth click board in the mikroBUS socket 1 on the EasyPIC7 board.
- Put power supply jumper (J5) on the EasyPIC7 board in 3.3V position.
- Be sure to correctly establish connection between Bluetooth click board and Master.
- After this, the EasyPIC7 must be powered off/on, due to the Bluetooth data mode entering.
- At the Master side, connect to the appropriate virtual COM port using
Terminal and send message which will be displayed on the Lcd.
- Passkey used in this example is "1234".
*/
// responses to parse
const BT_CMD = 1;
const BT_AOK = 2;
const BT_CONN = 3;
const BT_END = 4;
// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_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[16];
char txt1[4];
unsigned short i, tmp, DataReady;
char CMD_mode;
char BT_state;
char response_rcvd;
char responseID, response = 0;
char BT_Get_Response(void) ;
void BT_Configure(void);
void interrupt(void);
void Write_String(char *txt);
void CRLF(void);
void BT_Configure() {
do {
UART1_Write_Text("$$$"); // Enter command mode
Delay_ms(500);
} while (BT_Get_Response() != BT_CMD);
do {
UART1_Write_Text("SN,BlueTooth-Click"); // Name of device
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_AOK);
do {
UART1_Write_Text("SO,Slave"); // Extended status string
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_AOK);
do {
UART1_Write_Text("SM,0"); // was 0 Set mode (0 = slave, 1 = master, 2 =
trigger, 3 = auto, 4 = DTR, 5 = ANY)
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_AOK);
do {
UART1_Write_Text("SA,1"); // Authentication (1 to enable, 0 to disable)
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_AOK);
do {
UART1_Write_Text("SP,1234"); // Security pin code (mikroe)
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_AOK);
do {
UART1_Write_Text("---"); // Security pin code (mikroe)
UART1_Write(13); // CR
Delay_ms(500);
} while (BT_Get_Response() != BT_END);
}
// Get BlueTooth response, if there is any
char BT_Get_Response() {
if (response_rcvd) {
response_rcvd = 0;
return responseID;
}
else
return 0;
}
// Uart Rx interrupt handler
void interrupt(){
if (RCIF_bit == 1)
{ // Do we have uart rx interrupt request?
tmp = UART1_Read(); // Get received byte
if (CMD_mode)
{
/* The responses expected from the EasyBT module:
CMD
AOK
AOK
AOK
AOK
AOK
END
SlaveCONNECTmikroE
EasyBlueTooth
mikroE ...
EasyBlueTooth*/
// Process reception through state machine
// We are parsing CMD<cr><lf>, AOK<cr><lf>, CONN<cr> and END<cr><lf> responses
switch (BT_state) {
case 0: {
response = 0; // Clear response
if (tmp == 'C') // We have 'C', it could be CMD<cr><lf> or CONN
BT_state = 1; // Expecting 'M' or 'N'
if (tmp == 'A') // We have 'A', it could be AOK<cr><lf>
BT_state = 11; // expecting 'O'
if (tmp == 'E') // We have 'E', it could be END<cr><lf>
BT_state = 31; // expecting 'N'
break; // ...
}
case 1: {
if (tmp == 'M')
BT_state = 2;
else if (tmp == 'O')
BT_state = 22;
else
BT_state = 0;
break;
}
case 2: {
if (tmp == 'D') {
response = BT_CMD; // CMD
BT_state = 40;
}
else
BT_state = 0;
break;
}
case 11: {
if (tmp == 'O')
BT_state = 12;
else
BT_state = 0;
break;
}
case 12: {
if (tmp == 'K'){
response = BT_AOK; // AOK
BT_state = 40;
}
else
BT_state = 0;
break;
}
case 22: {
if (tmp == 'N')
BT_state = 23;
else
BT_state = 0;
break;
}
case 23: {
if (tmp == 'N') {
response = BT_CONN; // SlaveCONNECTmikroE
response_rcvd = 1;
responseID = response;
}
BT_state = 0;
break;
}
case 31: {
if (tmp == 'N')
BT_state = 32;
else
BT_state = 0;
break;
}
case 32: {
if (tmp == 'D') {
response = BT_END; // END
BT_state = 40;
}
else
BT_state = 0;
break;
}
case 40: {
if (tmp == 13)
BT_state = 41;
else
BT_state = 0;
break;
}
case 41: {
if (tmp == 10){
response_rcvd = 1;
responseID = response;
}
BT_state = 0;
break;
}
default: {
BT_state = 0;
break;
}
}
}
else
{
if ((tmp == 13)||(tmp == '#')) {
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++; // Increment counter
}
RCIF_bit = 0; // Disable UART RX Flag
}
}
}
void Write_String(char *txt)
{
short int i1,j1,k;
j1=strlen(txt);
i1=0;
do
{
k=txt[i1];
UART1_Write(k);
i1++;
}while (i1<j1);
}
void CRLF()
{
UART1_Write(13);
UART1_Write(10);
}
void main() {
// init hardware
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
ANSELB=0;
ANSELC=0;
PORTB = 0x00; // set PORTB to FF
TRISB = 0b10000000; // portb pins RB7 RB6 as input others as output
PORTA = 0xFF;
TRISA = 0xFE; // PORTA is RA1,2,3,5=input RA0, RA4 out
TRISE=0;
PORTE=0xFF;
TRISD=0x00; // all outputs
// portA A0 A1 analog AN2=-Vref=3,0V An3=+ref=4.096V RA5,RE0,RE1,RE2 digitals,
ANSELA=0;
ANSELA.F1=1;
ADCON1 = 0x8D ; //B'10001101'
PORTC = 0xFE; // set PORTC to $FF
TRISC = 0; // designate PORTC pins as output
ADCON0 =2 ;
LATA.F0=0;
Delay_ms(1000);
LATA.F0=1;
// Initialize variables
CMD_mode = 1;
BT_state = 0;
response_rcvd = 0;
responseID = 0;
response = 0;
tmp = 0;
CMD_mode = 1;
DataReady = 0;
RCIE_bit = 1; // Enable UART RX interrupt
PEIE_bit = 1; // Enable Peripheral interrupt
GIE_bit = 1; // Enable Global interrupt
Lcd_Init(); // Lcd Init
UART1_init(115200); // Initialize UART1 module
// set UART1 active
UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle);
//UART1_Write(12);
LATA.F0=0;
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
// Display starting messages
// 1234567890123456
Lcd_Out(1,1,"MyBlueToothClick");
Lcd_Out(2,1,"Demo 11/02/2012 ");
Delay_ms(2500);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Connecting!");
Lcd_Out(2,1,"Please, wait...");
Delay_ms(2500);
LATA.F0=0;
// Configure BlueTooth-Click module
BT_Configure();
// Wait until connected
while (BT_Get_Response() != BT_CONN);
Lcd_Cmd(_LCD_CLEAR); // Clear display
CMD_mode = 0;
GIE_bit = 0; // Disable Global interrupt
DataReady = 0; // Data not received
LCD_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Connected!"); // Display message
Delay_ms(1000);
//UART1_Write_Text("Bluetooth Click Connected!"); // Send message on connection
//UART1_Write(13); // CR
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Receiving..."); // Display message
while (1)
{
i = 0; // Initialize counter
memset(txt, 0, 16); // Clear array of chars
GIE_bit = 1; // Interrupts allowed
while (!DataReady) ; // Wait while the data is received
GIE_bit = 0; // Interrupts forbiden
DataReady = 0; // Data not received
LCD_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,"Received:"); // Display message
ByteToStr(BT_state,txt1);
Lcd_Out(1,11,txt1);
Lcd_Cmd(_LCD_SECOND_ROW); // Write in second row
i = 0; // Reset counter
while (txt[i] != 0) {
Lcd_Chr_CP(txt[i]); // Displaying the received text on the LCD
i++; // Increment counter
}
}
}