SnapMagic Search logo ☰
About
For Engineers >
Build Parts Request Parts Browse Parts Pricing Q & A PCB Suppliers
For Part Vendors >
Publish Media Kit SnapInsights Get CAD Models Syndication Program Contact Us
Log In Sign Up
  • Questions /
  • PIC16F1508 I2C and Bleutooth communication

PIC16F1508 I2C and Bleutooth communication

0

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.

General Question

Added 8 years, 6 months ago.

C
crystal625

0 Answers

Add a Response

Sign up or log in to respond.

SnapMagic

  • About
  • Contact
  • Pricing
  • Careers
  • 💎 What's new

Community

  • Our Community
  • Q & A
  • Blog
  • Made With SnapMagic Search

Product

  • Parts Library
  • InstaPart
  • InstaBuild
  • Plugins
  • API
  • PCB Suppliers
  • SnapMagic Search Desktop App

Tools

  • Allegro
  • Altium
  • Autodesk Fusion
  • CircuitStudio
  • CR-8000/CR-5000
  • DesignSpark
  • DipTrace
  • Eagle
  • Easy-PC
  • eCADSTAR
  • ExpressPCB Plus
  • KiCad
  • OrCAD
  • PADS & DxDesigner
  • PCB123
  • P-CAD
  • Proteus
  • Pulsonix
  • Target 3001!

Support

  • FAQ
  • How to Import
  • Standards
  • Contact Us
  • Design Resources
  • Terms Of Service
  • Privacy

Join Our Newsletter

Your subscription could not be saved. Please try again.
Thanks for subscribing to the SnapMagic Search newsletter. We're excited to have you as part of our community.
  • Facebook
  • LinkedIn
  • Twitter

1-844-625-8890

© 2013 - 2025 SnapMagic

7b4ad880c139480b95443d07f999b383