I2C Communication getting hang and not retrieve

Beta Testing discussion on mikroC PRO for PIC.
Post Reply
Author
Message
n_vikranth
Posts: 35
Joined: 11 Jan 2012 15:06

I2C Communication getting hang and not retrieve

#1 Post by n_vikranth » 30 May 2015 11:30

Hi Team,
Myself designed I2C Master and Slave device communication using PIC microcontroller
Master IC : PIC24EP512GP806
Slave IC : PIC16F722A

Communication is working for some time, then suddenly slave getting reset and after some time slave getting totally hangs and communication fails and slave is not sending ACK signal to Master. Then total communication fails but slave controller is working fine.

some time slave not displayed by communication failure immediately.

I attached the code below
Master Code
--------------
sbit BUZ at LATE7_bit;
sbit BUZ_Direction at TRISE7_bit;


unsigned char Data;

void I2C_Send_Data(unsigned char address,
unsigned char send_data); // Escribe al ESCLAVO

void Init_Peripherals(); //Iniciliza el PIC

void I2C_Send_Data(unsigned char address, unsigned char send_data)
{
I2C1_Start(); //Inicia Bus I2C
I2C1_Write(0xB0); //Envía dirección del ESCLAVO (dirección + W)
I2C1_Write(address); //Envía dirección de almacenamiento en el ESCLAVO
I2C1_Write(send_data); //Envía el dato a escribir en el ESCLAVO
I2C1_Stop(); //Detiene Bus I2C
Delay_100ms();
}



void Init_Peripherals()
{
PLLPRE_0_bit = 0;
PLLPRE_1_bit = 0;
PLLPRE_2_bit = 0;
PLLPRE_3_bit = 0;
PLLPRE_4_bit = 0;
PLLFBD = 38; // PLL multiplier M=38
PLLPOST_0_bit = 0;
PLLPOST_1_bit = 0;


ANSELB = 0x00; // Convert all I/O pins to digital
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
ANSELG = 0x00;
TRISD9_bit = 0;
TRISD10_bit = 0;
BUZ_Direction = 0;
I2C1_Init(100000); //Inicializar Bus I2C a 100kHz
delay_ms(500);
//I2C1STAT.TRSTAT =1;
}

void main()
{
Init_Peripherals();
while (1)
{
BUZ = 0;
delay_ms(2000);
BUZ = 1;
delay_ms(2000);
//Data = 31;
//I2C_Send_Data(0x11,Data);
//delay_ms(5000);
//Data = 50;
//I2C_Send_Data(0x12,Data);
//delay_ms(5000);
Data = '@';
I2C_Send_Data(0x01,Data);
delay_ms(2000);
Data = '@';
I2C_Send_Data(0x02,Data);
delay_ms(2000);
Data = '@';
I2C_Send_Data(0x03,Data);
delay_ms(2000);
Data = '@';
I2C_Send_Data(0x04,Data);
delay_ms(2000);
Data = 'P';
I2C_Send_Data(0x01,Data);
delay_ms(2000);
Data = 'C';
I2C_Send_Data(0x02,Data);
delay_ms(2000);
Data = 'P';
I2C_Send_Data(0x03,Data);
delay_ms(2000);
Data = 'L';
I2C_Send_Data(0x04,Data);
delay_ms(2000);

}
}

Slave Code
-------------
sbit A_line at RB0_bit;
sbit B_line at RB1_bit;
sbit C_line at RB2_bit;
sbit D_line at RB3_bit;
sbit E_line at RB4_bit;
sbit F_line at RB5_bit;
sbit G_line at RB6_bit;
sbit DP_line at RB7_bit;
sbit A_line_Direction at TRISB0_bit;
sbit B_line_Direction at TRISB1_bit;
sbit C_line_Direction at TRISB2_bit;
sbit D_line_Direction at TRISB3_bit;
sbit E_line_Direction at TRISB4_bit;
sbit F_line_Direction at TRISB5_bit;
sbit G_line_Direction at TRISB6_bit;
sbit DP_line_Direction at TRISB7_bit;

/************************** 7Segment Digit *********************/

sbit DISP1_line at RA4_bit;
sbit DISP2_line at RA3_bit;
sbit DISP3_line at RA2_bit;
sbit DISP4_line at RA1_bit;
sbit DISP5_line at RA0_bit;
sbit DISP6_line at RA5_bit;
sbit DISP7_line at RA6_bit;
sbit DISP8_line at RA7_bit;
sbit DISP1_Direction at TRISA0_bit;
sbit DISP2_Direction at TRISA1_bit;
sbit DISP3_Direction at TRISA2_bit;
sbit DISP4_Direction at TRISA3_bit;
sbit DISP5_Direction at TRISA4_bit;
sbit DISP6_Direction at TRISA5_bit;
sbit DISP7_Direction at TRISA6_bit;
sbit DISP8_Direction at TRISA7_bit;
/*************************************************************/

/************************** 6 Led *********************/
sbit Led1_line at RC0_bit;
sbit Led2_line at RC1_bit;
sbit Led3_line at RC2_bit;
sbit Led4_line at RC5_bit;
sbit Led5_line at RC6_bit;
sbit Led6_line at RC7_bit;
sbit Led1_Direction at TRISC0_bit;
sbit Led2_Direction at TRISC1_bit;
sbit Led3_Direction at TRISC2_bit;
sbit Led4_Direction at TRISC5_bit;
sbit Led5_Direction at TRISC6_bit;
sbit Led6_Direction at TRISC7_bit;
/*****************************************************/

sbit SDA at RC4_bit;
sbit SCL at RC3_bit;
sbit SDA_Direction at TRISC4_bit;
sbit SCL_Direction at TRISC3_bit;



#include "Display_Utils.h"

// ================================================= =============================
// DECLARATION OF GLOBAL VARIABLES.
// ================================================ ==============================
unsigned char rxbuffer, address, buff;//, data1, address2;
unsigned short tx_data;
int a_receiver = 0;

unsigned char display_shifter, display_index;
unsigned int digit;
unsigned int Blink_Time=0;

unsigned short display_data_array[8];


/****************To be considered for I2C******************/
unsigned char Display_text[9] = "0PCPLPCP"; //Display Alapha-Numeric content
unsigned char Display_text_Temp[9] = " "; //Display Alapha-Numeric content
char Blank = ' ';
char All = '@';
char decimal_point_data=0; //Control decimal point of all 7-Segment Displays
char Common_Anode_Cathode=1; //Selection of display type(Common Anode/Common Cathode) 1=Common Anode , 0=Common Cathode
char Display_off = 0; //Display On-Off option
char Display_Test = 0; //1- Display_Test, 0 -No Action
char Led_Value = 0; // 0 to 255 1-bit per led
char Led_Test = 0; //1- Led_Test, 0 -No Action
unsigned char Blink_Digit=0; // 0 to 255 1-bit per digit
unsigned char Blink_Time_period=250; // 0 to 255 Delay
//char Segment_control=8; //To on-off each segments seperately


//char uart_rd;



void interrupt()
{
if(TMR0IF_bit)
{
DISP1_line = DISP2_line = DISP3_line = DISP4_line = DISP5_line = 1;//DISP6_line = DISP7_line = DISP8_line = 0; // Turn off all 7seg displays
A_line = display_data_array[display_index].B0; // bring appropriate value to PORTB
B_line = display_data_array[display_index].B1; // bring appropriate value to PORTB
C_line = display_data_array[display_index].B2; // bring appropriate value to PORTB
D_line = display_data_array[display_index].B3; // bring appropriate value to PORTB
E_line = display_data_array[display_index].B4; // bring appropriate value to PORTB
F_line = display_data_array[display_index].B5; // bring appropriate value to PORTB
G_line = display_data_array[display_index].B6; // bring appropriate value to PORTB
DP_line = display_data_array[display_index].B7; // bring appropriate value to PORTB
DISP1_line = display_shifter.B0; // turn on appropriate 7seg. display
DISP2_line = display_shifter.B1; // turn on appropriate 7seg. display
DISP3_line = display_shifter.B2; // turn on appropriate 7seg. display
DISP4_line = display_shifter.B3; // turn on appropriate 7seg. display
DISP5_line = display_shifter.B4; // turn on appropriate 7seg. display
DISP6_line = display_shifter.B5; // turn on appropriate 7seg. display
DISP7_line = display_shifter.B6; // turn on appropriate 7seg. display
DISP8_line = display_shifter.B7; // turn on appropriate 7seg. display

if(Blink_Time > Blink_Time_period)
{
if(Blink_Digit.B0 == 1)
{
if(Display_text[0] == Blank)
{
Display_text[0] = Display_text_Temp[0];
}
else
{
Display_text_Temp[0] = Display_text[0];
Display_text[0] = Blank;
}
}
if(Blink_Digit.B0 == 0)
{
if(Display_text[0] == Blank)
{
Display_text[0] = Display_text_Temp[0];
}
}
if(Blink_Digit.B1 == 1)
{
if(Display_text[1] == Blank)
{
Display_text[1] = Display_text_Temp[1];
}
else
{
Display_text_Temp[1] = Display_text[1];
Display_text[1] = Blank;
}
}
if(Blink_Digit.B1 == 0)
{
if(Display_text[1] == Blank)
{
Display_text[1] = Display_text_Temp[1];
}
}
if(Blink_Digit.B2 == 1)
{
if(Display_text[2] == Blank)
{
Display_text[2] = Display_text_Temp[2];
}
else
{
Display_text_Temp[2] = Display_text[2];
Display_text[2] = Blank;
}
}
if(Blink_Digit.B2 == 0)
{
if(Display_text[2] == Blank)
{
Display_text[2] = Display_text_Temp[2];
}
}
if(Blink_Digit.B3 == 1)
{
if(Display_text[3] == Blank)
{
Display_text[3] = Display_text_Temp[3];
}
else
{
Display_text_Temp[3] = Display_text[3];
Display_text[3] = Blank;
}
}
if(Blink_Digit.B3 == 0)
{
if(Display_text[3] == Blank)
{
Display_text[3] = Display_text_Temp[3];
}
}
if(Blink_Digit.B4 == 1)
{
if(Display_text[4] == Blank)
{
Display_text[4] = Display_text_Temp[4];
}
else
{
Display_text_Temp[4] = Display_text[4];
Display_text[4] = Blank;
}
}
if(Blink_Digit.B4 == 0)
{
if(Display_text[4] == Blank)
{
Display_text[4] = Display_text_Temp[4];
}
}
if(Blink_Digit.B5 == 1)
{
if(Display_text[5] == Blank)
{
Display_text[5] = Display_text_Temp[5];
}
else
{
Display_text_Temp[5] = Display_text[5];
Display_text[5] = Blank;
}
}
if(Blink_Digit.B5 == 0)
{
if(Display_text[5] == Blank)
{
Display_text[5] = Display_text_Temp[5];
}
}
if(Blink_Digit.B6 == 1)
{
if(Display_text[6] == Blank)
{
Display_text[6] = Display_text_Temp[6];
}
else
{
Display_text_Temp[6] = Display_text[6];
Display_text[6] = Blank;
}
}
if(Blink_Digit.B6 == 0)
{
if(Display_text[6] == Blank)
{
Display_text[6] = Display_text_Temp[6];
}
}
if(Blink_Digit.B7 == 1)
{
if(Display_text[7] == Blank)
{
Display_text[7] = Display_text_Temp[7];
}
else
{
Display_text_Temp[7] = Display_text[7];
Display_text[7] = Blank;
}
}
if(Blink_Digit.B7 == 0)
{
if(Display_text[7] == Blank)
{
Display_text[7] = Display_text_Temp[7];
}
}
Blink_Time=0;
}
Blink_Time++;




// move shifter to next digit
display_shifter >>= 1;
display_shifter = display_shifter | 128;
if (display_shifter > 254u)
{
display_shifter = 127u;
}
// increment portd_index
display_index++ ;
if (display_index > 7u)
{
display_index = 0; // turn on 1st, turn off 2nd 7seg.
}
TMR0 = 96; // reset TIMER0 value
TMR0IF_bit = 0; // Clear TMR0IF
}


if(PIR1.SSPIF)
{ // Checks if the interruption is I2C
if(!SSPSTAT.R_W & SSPSTAT.D_A & SSPSTAT.BF)
{
//address = SSPBUF;
//UART1_Write(address);
a_receiver++;
// place to insert data receive indication
if (a_receiver == 1)
{
address = SSPBUF; // We store the storage address
//UART1_Write(SSPBUF);
} // The data received.
if (a_receiver == 2)
{
rxbuffer = SSPBUF; // Save the data received
a_receiver = 0;
if(address == 0x01)
{
Display_text[0] = rxbuffer;
}
if(address == 0x02)
{
Display_text[1] = rxbuffer;
}
if(address == 0x03)
{
Display_text[2] = rxbuffer;
}
if(address == 0x04)
{
Display_text[3] = rxbuffer;
}
if(address == 0x05)
{
Display_text[4] = rxbuffer;
}
if(address == 0x06)
{
Display_text[5] = rxbuffer;
}
if(address == 0x07)
{
Display_text[6] = rxbuffer;
}
if(address == 0x08)
{
Display_text[7] = rxbuffer;
}
if(address == 0x09)
{
decimal_point_data = rxbuffer;
}
if(address == 0x0C)
{
Display_off = rxbuffer;
}
if(address == 0x0D)
{
Common_Anode_Cathode = rxbuffer;
Blank = ' ';
All = '@';
/*if(Common_Anode_Cathode == 0)
{
Blank = ' ';
All = '@';
}
else
{
Blank = '@';
All = ' ';
}*/
}
if(address == 0x0E)
{
Led_Value = rxbuffer;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
//Led7_line = Led_Value.B6; // turn on appropriate Led
//Led8_line = Led_Value.B7; // turn on appropriate Led
}
if(address == 0x0F)
{
Display_Test = rxbuffer;
}
if(address == 0x10)
{
Led_Test = rxbuffer;
}
if(address == 0x11)
{
Blink_Digit = rxbuffer;
}
if(address == 0x12)
{
Blink_Time_period = rxbuffer;
}
}
buff = SSPBUF;
PIR1.SSPIF = 0; // We turned the SSP flag
}
if(!SSPSTAT.R_W & !SSPSTAT.D_A & SSPSTAT.BF)
{
buff = SSPBUF;
PIR1.SSPIF = 0;
}
PIR1.SSPIF = 0; // We turned the SSP flag
}
}

void main()
{
OPTION_REG = 0x83; // Assign prescaler to TMR0
ANSELA = 0;
ANSELB = 0;
TRISA = 0; // Configure PORTA as output
TRISB = 0; // Configure PORTA as output
//TRISC.B3 = 1; // September SCL (PORTC, 3) pin as income
//TRISC.B4 = 1; // September SDA (PORTC, 4) pin as income
TRISC.B0 = 0;
TRISC.B1 = 0;
TRISC.B2 = 0;
TRISC.B5 = 0;
TRISC.B6 = 0;
TRISC.B7 = 0;

PORTA = 0x00;
PORTB = 0x00;

TMR0 = 96; // Timer0 initial value
INTCON = 0xA0; // Enable TMRO interrupt
GIE_bit = 1;
TMR0IE_bit = 1;

SSPCON = 0x3E;
SSPSTAT.SMP = 1;
SSPSTAT.CKE = 0;
SSPSTAT.BF = 0;
SSPADD = 0xB0;
PIR1.SSPIF = 0;
PIE1.SSPIE = 1; // Enables the SSP interrupt module



INTCON.PEIE = 1; // Enable interrupts of peripheral
INTCON.GIE = 1; // Enable global interrupt!

//UART1_Init(9600); // Initialize UART module at 9600 bps
//Delay_ms(100); // Wait for UART module to stabilize

//UART1_Write_Text("Start");
//UART1_Write(10);
//UART1_Write(13);



display_index = 0;
display_shifter = 127u;
Led_Value = 0;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
while(1) // endless loop
{
display_data_Alpha_numeric_update(Display_text);
Delay_ms(10);
if(Display_off == 1)
{
Display_text[0] = Display_text[1] = Display_text[2] = Display_text[3] = Display_text[4] = Display_text[5] = Display_text[6] = Display_text[7] = Blank;
Display_off = 0;
}
if(Display_Test == 1)
{
Display_text[0] = Display_text[1] = Display_text[2] = Display_text[3] = Display_text[4] = Display_text[5] = Display_text[6] = Display_text[7] = All;
decimal_point_data = 255;
display_data_Alpha_numeric_update(Display_text);
Delay_ms(2000);
Display_text[0] = Display_text[1] = Display_text[2] = Display_text[3] = Display_text[4] = Display_text[5] = Display_text[6] = Display_text[7] = Blank;
decimal_point_data = 0;
display_data_Alpha_numeric_update(Display_text);
Delay_ms(2000);
Display_text[0] = Display_text[1] = Display_text[2] = Display_text[3] = Display_text[4] = Display_text[5] = Display_text[6] = Display_text[7] = All;
decimal_point_data = 255;
display_data_Alpha_numeric_update(Display_text);
Delay_ms(2000);
Display_text[0] = Display_text[1] = Display_text[2] = Display_text[3] = Display_text[4] = Display_text[5] = Display_text[6] = Display_text[7] = Blank;
decimal_point_data = 0;
display_data_Alpha_numeric_update(Display_text);
Delay_ms(2000);
Display_Test = 0;
}
if(Led_Test == 1)
{
Led_Value = 255;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
//Led7_line = Led_Value.B6; // turn on appropriate Led
//Led8_line = Led_Value.B7; // turn on appropriate Led
Delay_ms(2000);
Led_Value = 0;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
//Led7_line = Led_Value.B6; // turn on appropriate Led
//Led8_line = Led_Value.B7; // turn on appropriate Led
Delay_ms(2000);
Led_Value = 255;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
//Led7_line = Led_Value.B6; // turn on appropriate Led
//Led8_line = Led_Value.B7; // turn on appropriate Led
Delay_ms(2000);
Led_Value = 0;
Led1_line = Led_Value.B0; // turn on appropriate Led
Led2_line = Led_Value.B1; // turn on appropriate Led
Led3_line = Led_Value.B2; // turn on appropriate Led
Led4_line = Led_Value.B3; // turn on appropriate Led
Led5_line = Led_Value.B4; // turn on appropriate Led
Led6_line = Led_Value.B5; // turn on appropriate Led
//Led7_line = Led_Value.B6; // turn on appropriate Led
//Led8_line = Led_Value.B7; // turn on appropriate Led
Delay_ms(2000);
Led_Test =0;
}
}
}
}
Attachments
Slave configuration
Slave configuration
Actual Slave.PNG (58.47 KiB) Viewed 3884 times
Master configuration
Master configuration
Actual.PNG (54.72 KiB) Viewed 3884 times

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: I2C Communication getting hang and not retrieve

#2 Post by darko.minic » 01 Jun 2015 09:27

Hi,

It is hard to figure out from your code where problem occurs (it is huge).
I can suggest you to try with some of our simple examples for I2C.

Regards,
Darko

n_vikranth
Posts: 35
Joined: 11 Jan 2012 15:06

Re: I2C Communication getting hang and not retrieve

#3 Post by n_vikranth » 02 Jun 2015 07:02

Hi Mr.Darko,
Kindly help me in setting Configuration bits and other basic setting registers for I2C in PIC24EP512GP806, because the same code is working good in other controllers.
I think the problem in Setting only,thats y suggest complete basic configuration
XT oscillator = 8MHZ
operating Frequency = 140MHZ

I2C needed 100KHZ

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: I2C Communication getting hang and not retrieve

#4 Post by darko.minic » 02 Jun 2015 08:53

Hi,

Please try to load Project scheme from attachment and set MCU clock frequency at 80MHz.
Just go to Project -> Edit Project -> Load Scheme

Regards,
Darko
Attachments
P24EP512GP806_PLL.zip
(954 Bytes) Downloaded 163 times

Post Reply

Return to “mikroC PRO for PIC Beta Testing”