I am communicating two pic micro controller through I2C. My Pic32 Btsk is Master and Pic24f16km202 is slave.
i want the data of ADC from Pic24f on Pic32 but initially what i am doing is , i am trying to get any constant data from pic24f micro controller.but i dont get any data.
i got acknowledged when i send address 0x00 but when i send 0x01 from master i don get any acknowledgement
- Code: Select all
: 1 for Master configuration of PIC32 BTSK
[/// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include <machine/types.h>
#include "app.h"
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
u_int8_t address; /* <machine/types.h> */
bool e1,e2,e3,e4,e5,e6,e7,e8,e9,e10; /* <stdbool.h> */
// u_int32_t timervalue,i2cvalue,i,j; /* Seem to be obsolete compatibility type definitions. */
unsigned long int timervalue; /* C language standard type */
__uint32_t i2cvalue1, i2cvalue2; /* C compiler type specified length type ? */
uint32_t i,j;
u_int32_t COUNT=0; /* C library type ? <stdint.h> */
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA appData;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback funtions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary local functions.
*/
void App_LED (bool e)
{ if (e)
#if defined (Comm_Demo_Board) || defined(__32MX270F256D)
BSP_LEDToggle(9);
else
BSP_LEDToggle(0);// to check the write returns true or false value. i toogled the LED
#else
BSP_LEDToggle(9);
else
BSP_LEDToggle (0);
#endif
}
/* TODO: Add any necessary local functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Remarks:
See prototype in app.h.
*/
void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
/* Check the application's current state. */
/* Application's initial state. */
switch ( appData.state )
{
case APP_STATE_INIT:
{
e1=0;e2=0;e3=0;e4=0;e5=0;e6=0;e7=0;e8=0;e9=0;e10=0;
appData.state = APP_STATE_I2C_START;
break;
}
case APP_STATE_I2C_START :
{
while(!e1)
{
e1 = DRV_I2C0_MasterBusIdle();//check that the bus is free
}
while(!e2)
{
e2= DRV_I2C0_MasterStart();
}
e1=0;
do {e1 = DRV_I2C0_MasterBusIdle();
COUNT=COUNT+1;
}while(!e1);
COUNT=COUNT+1;
e4 = DRV_I2C0_ByteWrite(0x00); /* General call address */
while(!DRV_I2C0_WaitForByteWriteToComplete());
e5 = DRV_I2C0_WriteByteAcknowledged(); /* Now get the ACK status, inserted by Mysil. */
// i2cvalue1 = DRV_I2C0_ByteRead();//newn
while(!DRV_I2C0_MasterStop());
while(!DRV_I2C0_MasterBusIdle()); /* Wait for operation to complete. */
e9=DRV_I2C0_MasterStart();
while(!DRV_I2C0_WaitForStartComplete()); /* This function do not really work as expected. */
while(!DRV_I2C0_MasterBusIdle()); /* Wait until Start is really completed. */
//
while(!DRV_I2C0_MasterRestart());
while(!DRV_I2C0_MasterBusIdle()); /* Wait until RepeatStart is really completed. */
while(!DRV_I2C0_ByteWrite(0x01); // I2C address with Read bit set
while(!DRV_I2C0_WaitForByteWriteToComplete());
e8 = DRV_I2C0_WriteByteAcknowledged();
App_LED (e8);*/
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
e8 = DRV_I2C0_SetUpByteRead();// master receive clock byte
App_LED (e8);
do{
e10 = DRV_I2C0_WaitForReadByteAvailable(); // receive byte available
}while(!e10);// this function gives false value
App_LED (e10);
i2cvalue1 = DRV_I2C0_ByteRead();// if receive available then read
DRV_I2C0_MasterNACKSend(); /* Send NACK, so Slave do not hang the bus. */
while(!DRV_I2C0_MasterBusIdle()); /* Wait until NACK signal completed, inserted. */
DRV_I2C0_MasterStop();
//// while(!DRV_I2C0_MasterBusIdle()); /* Wait until Stop signal is completed, inserted. */
/* TODO: implement your application state machine.*/
appData.state = APP_STATE_INIT;
break;
}
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
/////////////////////////////////////////////////
- Code: Select all
/////////////////////////////////////////////
code :2 for Slave PIC24F16KM202 microcontroller
/////////////////////////////////////////////
////////////////////////////////////////////////////
#include "main.h"
//#include "I2C.h"
#define FCY 8000000UL
#include<libpic30.h>
unsigned short tx_data=0x99;
unsigned short j;
unsigned short rxbuffer;
//void interrupt ISR(void);
/********************** MyCode**************************
int main(void)
{
CLKDIV=0;
TRISBbits.TRISB8 = 1;// make scl1 input
TRISBbits.TRISB9 = 1; // make sda1 input
SSP1STAT=0;
SSP1ADD = 0x00; // Set I2C address
SSP1CON1 = 0x36;//0x003E to enable interrupt // SSPEN: Synchronous Serial Port Enable bit - Enables the serial port and configures the SDA and SCL pins as the serial port pins
// CKP: SCK Release Control bit - Release clock
// SSPM3:SSPM0: SSP Mode Select bits - 0110 = I2C Slave mode, 7-bit address
//SSPSTAT = 0x00;
SSP1CON2 = 0x0080;// set genearal address enabled
// ox0081 for clock stretch with general call address enabled.
SSP1CON3 = 0x0010;//0b 0000 0000 0001 1000
return 0;
}
*/
/*..................edited code
.......*/
//*****************************************************************8
void Init()
{
CLKDIV = 0x0000;
SSP1ADD = 0x0000; // Get address (7bit). Lsb is read/write flag
SSP1CON1 = 0x36;
SSP1CON2 = 0x0080;// set genearal address enabled
// ox0081 for clock stretch with general call address enabled.
SSP1CON3 = 0x0010;//0b 0000 0000 0001 1000// Set to I2C slave with 7-bit address
void __attribute__((__interrupt__, no_auto_psv)) _SI2C1Interrupt(void)
{
LATBbits.LATB2=1;
//while(!IFS1bits.SSP1IF);
if(IFS1bits.SSP1IF == 1)
{ // I2C Interrupt
IFS1bits.SSP1IF = 0; // reset SSP interrupt flag
//transmit data to master
if (SSP1STATbits.R_W == 1){ // Read request from master
SSP1BUF = tx_data; // Get data to send
SSP1CON1bits.CKP = 1; // Release SCL line
j = SSP1BUF;
//return;
}
if (SSP1STATbits.BF == 0){ // all done,
j = SSP1BUF; // Nothing in buffer so exit
// return;
}
//recieve data from master
if (SSP1STATbits.D_A == 1){ // Data [not address]
rxbuffer = SSP1BUF; // get data
j = SSP1BUF; // read buffer to clear flag [address]
// return;
}
}
j = SSP1BUF;
LATBbits.LATB2=0;
}
int main()
{
int i;
ANSB=0x0000;
TRISBbits.TRISB0 = 0;// make scl1 input
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB8 = 1;// make scl1 input
TRISBbits.TRISB9 = 1;
Init();
while(1){
LATBbits.LATB0=1;
LATBbits.LATB1=0;
for(i=0;i<10;i++)
__delay_ms(50);
LATBbits.LATB0=0;
LATBbits.LATB1=1;
//PORTB = rxbuffer;
tx_data = tx_data +1;
for(i=0;i<10;i++)
__delay_ms(50);//__Delay_ms(Delay_Time);
}
return 0;
}
]
i have attached the code for Slave(pic24f16km202) microcontroller and app.c file of PIC32 BTSK in which i have written the code for master configuration.