⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 飞思卡尔S12系列Inter-IC_Bus__(IIC)应用范例
💻 C
字号:
/* ======================================================================
 * Project:  IIC_DRV     												*
 * file: 	 main.c														*
 * -------------------------------------------------------------------- *
 * Description:															*
 *			Driver for the IIC Module. 									*
 *		Description: All units try to send data to each other			*
 *			The one that gets the bus, sends the data					* 
 *		The receiver toggles his led							 		*
 *																		*
 * -------------------------------------------------------------------- *
 * Author:  Luis Fdo. Reynoso Covarrubias								*
 * 			Motorola Mexico FAE Team 									*
 * 																		*
 * Last Modification:  10/Dec/02										*
 * =====================================================================*/

#define __DECL__6812DP256_H__
//Libraries Included
#include <hidef.h>
#include <6812dp256.h>


//Global Variables to be used in interrupt
static unsigned char *Position;							// Store actual position
static unsigned int Error_count=0;				//Flag used to get out of the loop if an error ocurred

static union {                            
  struct {                             
     	unsigned char _LastByte:1;               		// Set if last byte transmitted
	    unsigned char _Sec_LastByte:1;              	// Set if 2nd last byte to read (NA)
	    unsigned char _Transmition_OK:1   ;             // Set if transmition successful
	    unsigned char _Reception_OK:1  ;				// Set if reception was  successful
	    unsigned char _Master_Rec_Trans:1;				// Set if master wants to receive 
	    unsigned char _Master_Transmition:1;   			// Set if master is transmitting (NA)
	    unsigned char _TXComplete:1;          			// Set if Transmition Finishes
	    unsigned char _RXComplete:1;          			// Set if Reception Finishes
  } FLAGS_BITS;                      
  unsigned char FLAGS_BYTE;                 
} FLAGS1;

/*DEFINE FLAG BYTE*/                          
#define IIC_FLAGS FLAGS1.FLAGS_BYTE            			// Byte to access the entire flags

/*DEFINE FLAG BITS*/                     
#define IIC_Last 		FLAGS1.FLAGS_BITS._LastByte				//
#define IIC_2nd 		FLAGS1.FLAGS_BITS._Sec_LastByte			//
#define IIC_TXOK	 	FLAGS1.FLAGS_BITS._Transmition_OK		//
#define IIC_RXOK	 	FLAGS1.FLAGS_BITS._Reception_OK			//
#define IIC_MS_RXTX		FLAGS1.FLAGS_BITS._Master_Rec_Trans		// Definition of Flag bits 
#define IIC_MSTX		FLAGS1.FLAGS_BITS._Master_Transmition	//
#define IIC_TXComplete	FLAGS1.FLAGS_BITS._TXComplete			//
#define IIC_RXComplete 	FLAGS1.FLAGS_BITS._RXComplete			//

#define OWN_ADDRESS 2
#define RECEIVER_ADDRESS 1


void main(void){ 
static unsigned char *send="2";					// String to send				*This Strings must be placed in RAM because they are changed.*
unsigned long i=0;								// Counter for delay

PTP=0x00;										// Turn off the Leds
DDRP=0x80;										// Set PORTP as output

// Initializes the IIC Module with 100Kbps & Hold Time of 2.5us. Address = 1 (Change for other uC)
//	 SCL Divider = (8Mhz/100Hkz=80), Hold time = 20 clock cycles (table 3-4 IIC Block Guide V02.06)	
 IBCR=0;								// Set IIC Control register to default (0)
 IIC_FLAGS=0;							// Set all flags in 0
 IBFD=0x47;								// Set IIC Frequency Divider Register
 IBAD=OWN_ADDRESS<<1;					// Set Slave Address 
 IBEN=1;								// Enable IIC Module
 IBCR= 0xD0;							// Set TX/RX Mode and Enable/Disable Interrupt

EnableInterrupts;								// Enable Interrupts

do{
while (IBB==1) {};						// Check if BUS is busy
TX_RX_=1;							//	Set Transmition request
MS_SL_=1;							// Set Start Condition
IIC_MS_RXTX=0;						// Set transmition wanted flag

	if (*send=='1')								//
	*send='2';									// Change the output string every time
	else										//
	*send='1';									//
	
IIC_FLAGS=0;					// Reset Flags
Position=&send[0];				// Set the actual position to the first position of the array


IBDR= (RECEIVER_ADDRESS<<1)|0;	// Send Address

	for (Error_count=5000;Error_count>0;Error_count--)
	{
		if (IIC_TXComplete==1)
		Error_count=1;
		
		if (Error_count==1&&IIC_TXComplete==0)
			{MS_SL_=0; IIC_TXComplete=1; IIC_TXOK=0;}
	}

IIC_FLAGS=0;			// Ensure Flags are clean
for (i=100000;i>0;i--){};
}
while (1);										// Repeat forever	

}



/*****************IIC_ISR****************************
*     			IIC Interrupt						*
*													*
****************************************************/
interrupt 31 void IIC_ISR(void)
{	
static unsigned char t1=0;

IBIF=1;					/* Clear IBIF*/

if (MS_SL_==1) //Master MODE
	{
	Error_count=5000;				// Reset counter to avoid error
	if (TX_RX_==1)	//Master is Tranmitting
		{	
		if (IIC_Last==1) 	//Last Data was Transmitted
			{
			MS_SL_=0;			/* Send stop Condition */
			IIC_TXOK=1;				/* Set Transmution successful flag */
			IIC_TXComplete=1;		/* Set transmition complete flag */
			}
		else				//This Data is not the Last
			{
			if (RXAK==1)		//Master did not receive Acknowledge
				{
				MS_SL_=0;		/* Send stop condition */
				IIC_TXOK=0;			/* Set transmition failed */
				IIC_TXComplete=1;	/* Set transmition complete flag */
				}
			else				//Master Receive Acknowledge
				{
					IBDR=*Position; /* Send a byte */
						IIC_Last=1;	/* Set if last data was tranmitted */
				}
			
			}
		}
	}
else	//Slave MODE
	{
	if (IAAS==1)	//Addressed as a slave	(Address transfer)
		{
			TX_RX_ =0;					/* Set reception mode */
			IBDR;						/* Dummy Read */
		}
	else			//Not Addressed as a slave
		{
		if (IBAL==1)	//Arbitration Lost
			{
			IBAL=1;						/* Clear Arbitration lost flag & return */
			}
		else			//Data Transfer
			{
				t1=IBDR;
				if (t1=='1')
					{
					PTP=0xFF;
					}
				else if (t1=='2')
					{
					PTP=0x00;
					}
				else
					PTP=0xAA;	//Set default
			}
		}
	}
} //int 31


										/*``````````````````````````````````
										````````````````ozkcox``````````````
						 				``````````vxMMMMMMMMMMMMMMsi````````
						 				````````MMMMMMM#MMMMMMMWMMMMMM``````
						 				``````MMMMMMMW/\MMMMMMjiMMMMMWMM````
						 				````oxMMMMMMff``MMMMMM``uoMWMMMMsi``
						 				````MMMMMMMM````MMMMMM````MMMMMMMM``
						 				````MMMMMMMM````A3MME*````MWMMMMMM..
						 				``+-MMMMMM@h``````MM..````QCMMMMMMox
						 				``oxMMMMMM``^.ji``M#``oz^^``MMMMMMkc
						 				``srMMMMMW..MMMWMWE*&$MMMW``MMMMMMox
						 				````MMMWMMMMMMMMMM..MWMMMMMM&*MMMM..
						 				````MMMW;;MMMMMMMMMMMMMMMMMM'`MMMM``
						 				````ozMMMWMMMMMMMMMMMMMMMMMMMMMMsv``
						 				``````MMMWMMMMMMMMMMMMMMMMMMMMMM````
						 				````````&$MMMMMMMMMMMMMMMMMMMW``````
						 				``````````^^MMMMMMMMMMMMMMsv````````
						 				``````````````..srffox'````````````*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -