Hi All,
I am facing an issue while transferring data to bluetooth. My experimental setup is as mentioned below.
I have two PCBs, say PCB1 and PCB2. PCB1 has my Master PIC16F1508(http://www.kynix.com/uploadfiles/pdf8798/PIC16F1508-I2fP_71292.pdf) while PCB2 has Slave PIC (PIC16F1508) and a bluetooth module (HC-05). I want to send data from Master PIC to Slave PIC via I2C and then Slave PIC should send this data to bluetooth via UART.
I have individually tested I2C communication and bluetooth communication. Both seems to be working fine. But when I integrate both I2C code and bluetooth code, I am not receiving any data via bluetooth while I am receiving acknowledge signal from Slave PIC. I could see the data sent from Master PIC via oscilloscope too.
My codes are as given below
Master code
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "mcc_generated_files/mcc.h"
#include"pic16f1508.h"
void OpenI2C1();
void WriteI2C1(unsigned char data_out);
void StartI2C1();
void StopI2C1();
#define SDA_TRIS TRISB4
#define SCL_TRIS TRISB6
#define SDA_LAT LATB4
#define SCL_LAT LATB6
void I2C_Init()
{
SDA_TRIS = 1;
SCL_TRIS = 1;
OpenI2C1();
SSP1ADD = 19;
}
void StartI2C1()
{
SSP1CON2bits.SEN=1;
while(SSP1CON2bits.SEN);
}
void StopI2C1()
{
SSP1CON2bits.PEN=1;
while(SSP1CON2bits.PEN);
}
void OpenI2C1()
{
PIE1bits.SSP1IE=0;
SSP1STAT=0x80;
SSP1CON1=0x28;
SSP1CON2=0x00;
}
void WriteI2C1(unsigned char data_out)
{
SSP1BUF = data_out;
while(SSP1STATbits.BF);
}
void I2C_Wait()
{
while ((SSPCON2 & 0x1F) | (SSPSTATbits.R_nW));
}
void main()
{
char b='A';
SYSTEM_Initialize();
I2C_Init();
I2C_Wait();
StartI2C1();
I2C_Wait();
WriteI2C1(0x30);
I2C_Wait();
if(SSP1CON2bits.ACKSTAT == 1)
{
StopI2C1();
}
else
{
while (1)
{
WriteI2C1(b);
I2C_Wait();
}
}
StopI2C1();
}
Slave code
char z;
char c;
void I2C_Slave_Init()
{
SSP1STAT = 0x80;
SSP1ADD = 0x30; //Setting address
SSP1CON1 = 0x36; //As a slave device
// SSP1CON2 = 0x01;
GIE = 1; //Global interrupt enable
PEIE = 1; //Peripheral interrupt enable
PIR1bits.SSP1IF=0; //Clear interrupt flag
PIE1bits.SSP1IE=1; //Synchronous serial port interrupt enable
}
void interrupt I2C_Slave_Read()
{
PIR1bits.SSP1IF = 0;
c = SSPBUF;
if(!SSP1STATbits.D_nA && !SSP1STATbits.R_nW) //If last byte was Address + Write
{
z = SSPBUF;
while(!BF);
c = SSPBUF;
TXREG = c;
SSPM3 = 0;
}
}
void bluetoothinit()
{
TRISBbits.TRISB5=1;
TRISBbits.TRISB7=0;
SPBRGL=0XA0;
SPBRGH=0X01;
BAUDCON=0X48;
RCSTA=0X80;
TXSTA=0X26;
}
void main(void)
{
SYSTEM_Initialize();
I2C_Slave_Init();
bluetoothinit();
while (1)
{
}
}
I am fairly certain of the bluetooth initialization as I was able to get the bluetooth working in a separate stand alone project with the same settings. But I am not sure where I should place the
TXREG=SSPBUF
statement. Please help me in any way you can as I have been stuck with this project for quite some time now. Any help would be highly appreciated.
Added 8 years, 6 months ago.