📄 main.c
字号:
/******************************************************************************
COPYRIGHT 2003s STMicroelectronics
Source File Name : main_slave.c
Group : IPSW,CMG-IPDF
Author : MCD Application Team
Date First Issued: 26/08/2003
********************************Revision History*******************************
_______________________________________________________________________________
Date :26/08/2003 Release:2.0
******************************************************************************/
/* This example code explains the usage of I2C slave in the transmitter
and receiver mode.First the slave is configured as a transmitter and it
transmits 10 bytes of data from the user buffer.Then it is configured as a
receiver and it reads the same 10 bytes of data and they are compared.
In case of any mismatch control gets struck in a loop*/
#include "ST7lib_config.h"
#include "main.h"
unsigned char Buff_In[9]= {0x00};
unsigned char Buff_Out[]={1,2,3,4,5,6,7,8,9};
void main(void) /* This is for polling & interrupt driven */
{
unsigned char OAR1Value = 0x30;
unsigned char OAR2Value = 0x00;
unsigned char maxsize = 9,single_byte = 0x05,first_byte = 0x00;
BOOL TX_STATUS = TRUE;
I2Cs_ErrCode_t Error_Status = I2Cs_DEFAULT_STATUS;
I2Cs_Mode Comm_Mode = I2Cs_DEFAULT;
unsigned int i;
EnableInterrupts
I2Cs_Init(((unsigned char)I2Cs_ENABLE_ACK)|((unsigned char)
I2Cs_ENABLE_ENGC),OAR1Value,OAR2Value); /* ACK bit is set */
Error_Status=I2Cs_IsReceptionCompleted();
while((!Time_Out()) && (Error_Status != I2Cs_ADDRESS_DETECTED))
/* Time_out() is bring before to remove side-efffect error */
{
Error_Status=I2Cs_IsReceptionCompleted();
}
Comm_Mode=I2Cs_GetCommMode();
while(Comm_Mode == I2Cs_DEFAULT)
{
Comm_Mode=I2Cs_GetCommMode(); /* checking for communication mode */
}
/***************** Polling Mode Transmission ****************************/
if(Comm_Mode == I2Cs_TX_MODE) /* transmitter mode */
{
/* SINGLE BYTE TRANSMISSION */
I2Cs_PutByte(single_byte);
Error_Status=I2Cs_IsTransmitCompleted();
while((!(Time_Out())) && (Error_Status != I2Cs_TX_DATA_OK))
{
Error_Status=I2Cs_IsTransmitCompleted();
}
switch( Error_Status)
{
case I2Cs_TX_AF:
case I2Cs_BERR:
case I2Cs_DEFAULT_STATUS:
case I2Cs_STOP_DETECTED:
case I2Cs_GENERAL_CALL:
case I2Cs_ADDRESS_DETECTED:
User_Function();
break;
default:
User_Function();
break;
}
/********** BUFFER TRANSMISSION ****************************/
/******** POLLING MODE *************************************/
#ifdef I2C_POLLING_TX
Error_Status = I2Cs_PutBuffer(Buff_Out,maxsize);
switch(Error_Status)
{
case I2Cs_TX_DATA_OK:
case I2Cs_OVERFLOW_TX:
break;
case I2Cs_BERR:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
default:
User_Function();
break;
}
#endif
/******* end of polling mode ******************************/
/******** INTERRUPT DRIVEN MODE *************************************/
#ifdef I2C_ITDRV_WITHOUTBUF_TX
I2Cs_PutBuffer(Buff_Out,maxsize);
Error_Status=I2Cs_IsTransmitCompleted();
while((!(Time_Out())) && ((Error_Status != I2Cs_TX_DATA_OK) && (Error_Status != I2Cs_OVERFLOW_TX)))
{
Error_Status=I2Cs_IsTransmitCompleted();
}
switch( Error_Status)
{
case I2Cs_BUFF_TX_ONGOING:
Error_Status=I2Cs_IsTransmitCompleted();
while((Error_Status != I2Cs_TX_DATA_OK) && (Error_Status != I2Cs_OVERFLOW_TX))
{
Error_Status=I2Cs_IsTransmitCompleted();
}
break;
case I2Cs_TX_DATA_OK:
case I2Cs_OVERFLOW_TX:
break;
case I2Cs_TX_AF:
case I2Cs_BERR:
case I2Cs_GENERAL_CALL:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_DEFAULT_STATUS:
User_Function();
break;
default:
User_Function();
break;
}
#endif
/****************END OF INTERRUPT DRIVEN MODE *******************/
}
/***********************************************************/
/******* RECEIVER ROUTINE ******************/
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out()))&& ((Error_Status != I2Cs_ADDRESS_DETECTED) && (Error_Status != I2Cs_GENERAL_CALL)))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
Comm_Mode=I2Cs_GetCommMode();
while((Comm_Mode == I2Cs_DEFAULT))
{
Comm_Mode=I2Cs_GetCommMode();
}
if(Comm_Mode == I2Cs_RX_MODE) /* POLLING AND INTERRUPT DRIVEN RECEIVER */
{
Error_Status=I2Cs_IsReceptionCompleted();
/* ONE BYTE RECEPTION */
while((!(Time_Out())) && (Error_Status != I2Cs_RX_DATA_OK))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
switch(Error_Status)
{
case I2Cs_RX_DATA_OK:
first_byte=I2Cs_GetByte();
break;
case I2Cs_STOP_DETECTED:
case I2Cs_BERR:
case I2Cs_GENERAL_CALL:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_EMPTY:
case I2Cs_DEFAULT_STATUS:
User_Function();
break;
default:
User_Function();
break;
}
/**********************BUFFER RECEPTION ***********************/
/*****************POLLING MODE ********************************/
#ifdef I2C_POLLING_RX
Error_Status=I2Cs_GetBuffer(Buff_In,maxsize);
switch(Error_Status)
{
case I2Cs_RX_DATA_OK:
case I2Cs_OVERFLOW_RX:
break;
case I2Cs_BERR:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
default:
User_Function();
break;
}
#endif
/********* end of polling mode **********************************/
/************INTERRUPT DRIVEN MODE *****************************/
#ifdef I2C_ITDRV_WITHOUTBUF_RX
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out())) && (Error_Status != I2Cs_RX_DATA_OK ))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
switch(Error_Status)
{
case I2Cs_BUFF_RX_ONGOING:
case I2Cs_OVERFLOW_RX:
case I2Cs_BERR:
case I2Cs_EMPTY:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
case I2Cs_RX_DATA_OK:
break;
default:
User_Function();
break;
}
I2Cs_GetBuffer(Buff_In,maxsize);
Error_Status=I2Cs_IsReceptionCompleted();
while((!(Time_Out())) && (Error_Status != I2Cs_RX_DATA_OK ))
{
Error_Status=I2Cs_IsReceptionCompleted();
}
switch(Error_Status)
{
case I2Cs_BUFF_RX_ONGOING:
case I2Cs_OVERFLOW_RX:
case I2Cs_BERR:
case I2Cs_EMPTY:
case I2Cs_ADDRESS_DETECTED:
case I2Cs_GENERAL_CALL:
User_Function();
break;
case I2Cs_RX_DATA_OK:
break;
default:
User_Function();
break;
}
#endif
/****************END OF INTERRUPT DRIVEN MODE *******************/
} /* END OF RECEIVER ROUTINE */
/****************************************************************/
/**** COMPARE THE TRANSMITTED AND RECEIVED BYTES ******/
while (single_byte != first_byte);
for (i=0;i<maxsize;i++)
{ //add braces
while(Buff_Out[i] != Buff_In[i]);
}
/****************************************************************/
while(1);
} /* End of the main */
/*------------------------------------------------------------------------------
ROUTINE NAME : User_IT_Routine
INPUT : None
OUTPUT : None
DESCRIPTION : Control comes into this routine when an interrupt is generated.
User can use the I2C interrupt service routine function for
slave or he can write his own code inside this routine at his
own risk.The data transfer syncronisation may be affected if
user includes his own code along with I2C ISR function.
COMMENTS : None
-----------------------------------------------------------------------------*/
#ifdef _HIWARE_ /* Test for HIWARE Compiler */
#pragma TRAP_PROC SAVE_REGS /* Additional registers will be saved */
#else
#ifdef _COSMIC_ /* Test for Cosmic Compiler */
@interrupt /* Cosmic interrupt handling */
#else
#error"Unsupported Compiler!" /* Compiler Defines not found! */
#endif
#endif
void I2Cs_User_IT_Routine (void)
{
I2Cs_ITFunction() ;
}
/*****************************************************************************
Time_Out Function
******************************************************************************/
BOOL Time_Out(void)
{
while(count < 5000)
{
count++ ;
return (FALSE); /* Time-out not elapsed */
}
return (TRUE) ; /* Time-out elapsed */
}
void User_Function(void)
{
I2Cs_ErrorClear();
/* user can include his code here */
}
/*** (c) 2003 ST Microelectronics ****************** END OF FILE ***********/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -