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

📄 basic.c

📁 在Freescale HCS12中使用I2C总线源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
* 		                                         COPYRIGHT (c) MOTOROLA 2002   
* FILE NAME: basic.c                                                      
*                                                                           
* PURPOSE: I2C demo - slave software      											 
*                                                                           
*******************************************************************************
*******************************************************************************  
**  THIS CODE IS ONLY INTENDED AS AN EXAMPLE FOR THE METROWERKS COMPILER AND **  
**      THE STAR12 EVB AND HAS ONLY BEEN GIVEN A MIMIMUM LEVEL OF TEST.      **  
**  IT IS PROVIDED 'AS SEEN' WITH NO GUARANTEES AND NO PROMISE OF SUPPORT.   **  
*******************************************************************************  
*******************************************************************************
*                                                                           
* DOCUMENTATION SOURCE: EKB Apps                                          
*                                                                           
* TARGET DEVICE: 68HC912DP256                                               
*                                                                                                                                                                                                                                                            
* COMPILER: Metrowerks                     VERSION: MOT v1.2                     
*                                                                           
* DESCRIPTION:                                                                                                                                
*                                                                         
* AUTHOR: Grant More       LOCATION: EKB      LAST EDIT DATE: 20/05/02
*                                                                         
* UPDATE HISTORY                                                         
* REV      AUTHOR       DATE       DESCRIPTION OF CHANGE                  
* ---      ------      ---------    ---------------------                 
* 1.0	   G.More	   20/05/02		Initial Coding                     
*
******************************************************************************/                                                                        
/*===========================================================================*/
/* Motorola reserves the right to make changes without further notice to any */
/* product herein to improve reliability, function, or design. Motorola does */
/* not assume any  liability arising  out  of the  application or use of any */
/* product,  circuit, or software described herein;  neither  does it convey */
/* any license under its patent rights  nor the  rights of others.  Motorola */
/* products are not designed, intended,  or authorized for use as components */
/* in  systems  intended  for  surgical  implant  into  the  body, or  other */
/* applications intended to support life, or  for any  other application  in */
/* which the failure of the Motorola product  could create a situation where */
/* personal injury or death may occur. Should Buyer purchase or use Motorola */
/* products for any such intended  or unauthorized  application, Buyer shall */
/* indemnify and  hold  Motorola  and its officers, employees, subsidiaries, */
/* affiliates,  and distributors harmless against all claims costs, damages, */
/* and expenses, and reasonable  attorney  fees arising  out of, directly or */
/* indirectly,  any claim of personal injury  or death  associated with such */
/* unintended or unauthorized use, even if such claim alleges that  Motorola */
/* was negligent regarding the  design  or manufacture of the part. Motorola */
/* and the Motorola logo* are registered trademarks of Motorola Ltd.         */
/*****************************************************************************/

/* include files */
#include "basic.h"

/* additional #defines */
#define SLAVE 0x0A					/* slave address on I2C bus */
#define PACKETSIZE 8				/* size of packet/buffer */

/* Global variables */
tU08 TxPacket[PACKETSIZE];			/* global transmit packet */
tU08 *TxPacketpositionptr;			/* pointer to current location in transmit packet */ 
tU08 *TxPacketendptr;				/* (fixed) pointer to end of transmit packet */
tU08 TxBufferemptyflag;				/* global flag for transmit functionality */
tU08 RxPacket[PACKETSIZE];			/* global receive packet */
tU08 *RxPacketpositionptr;			/* pointer to current location in receive packet */ 
tU08 *RxPacketendptr;				/* (fixed) pointer to end of receive packet */
tU08 RxBufferfullflag = 0, MasterRxFlag = 0;	/* global flags for receive functionality */
tU08 TxCompleteflag = 0, RxCompleteflag = 0;	/* global flags for ISR functionality */

/******************************************************************************
Function Name	:	main
Engineer		:	G.More
Date			:	23/05/02
Parameters		:	NONE
Returns			:	NONE
Notes			:	Main program functionality
******************************************************************************/
void main(void) 
{	
	EnableInterrupts;				/* enable interrupts */

	ConfigureI2CModule();			/* call initialisation function */			
	
	while(FOREVER)
	{	
		SlaveReceiveFromMaster();		/* receive data from master */
		TxPacket[0] = RxPacket[0];		/* configure TxPacket from RxPacketData */	
		TxPacket[1] = RxPacket[1];					
		TxPacket[2] = RxPacket[2];
		TxPacket[3] = RxPacket[3];
		TxPacket[4] = RxPacket[4];					
		TxPacket[5] = RxPacket[5];
		TxPacket[6] = RxPacket[6];
		TxPacket[7] = RxPacket[7];
		SlaveTransmitToMaster();		/* retransmit received data back to master */	
	}
}

/******************************************************************************
Function Name	:	SlaveTransmitToMaster
Engineer		:	G.More
Date			:	28/05/02
Parameters		:	NONE
Returns			:	NONE
Notes			:	Procedure that demonstrates code to transmit data as a 
					slave on an I2C bus.
******************************************************************************/
void SlaveTransmitToMaster(void)
{
	TxPacketpositionptr = &TxPacket[0];	/* point to first TxPacket element */
	TxPacketendptr = &TxPacket[(PACKETSIZE-1)]; /* point to last TxPacket element */
	TxBufferemptyflag = CLEAR;				

	while(TxBufferemptyflag == CLEAR)	/* wait while message is transmitted */
	{										
	}
}

/******************************************************************************
Function Name	:	SlaveReceiveFromMaster
Engineer		:	G.More
Date			:	28/05/02
Parameters		:	NONE
Returns			:	NONE
Notes			:	Procedure that demonstrates code to receive data as a slave
					on an I2C bus.
******************************************************************************/
void SlaveReceiveFromMaster(void)
{
	RxPacketpositionptr = &RxPacket[0]; /* set position pointer to point to beginning of rx packet */
	RxPacketendptr = &RxPacket[(PACKETSIZE-1)];		/* set end pointer to point to end of rx packet */
	RxBufferfullflag = CLEAR;
	
	while(RxBufferfullflag == CLEAR)		/*wait until a packet has been received */
	{
	}		
	RxBufferfullflag = CLEAR;	/* clear the packet received flag */
	Iic.ibcr.byte = (IBEN|IBIE);	/* re-enable the iic module */
}

/******************************************************************************
Function Name	:	ConfigureI2CModule
Engineer		:	G.More
Date			:	23/05/02
Parameters		:	NONE
Returns			:	NONE
Notes			:	Procedure that configures and sets up I2C bus.
******************************************************************************/
void ConfigureI2CModule(void)
{
	Iic.ibfd.byte = 0x00; 			/* frequency divider register */
	Iic.ibad.byte = 0xF0;			/* slave address of this module */
	Iic.ibcr.byte = (IBEN|IBIE);	/* enable iic module */
}

/*****************************************************************************/
/* Start of interrupt service routines  */
/* placed into non-banked memory segment as vectors are only 2 bytes */

#pragma CODE_SEG NON_BANKED	
#pragma MESSAGE DISABLE C12056 /* SP debug info */
/******************************************************************************
Function Name	:	_I2CISR
Engineer		:	G.More	
Date			:	31/05/02
Parameters		:	NONE
Returns			:	NONE
Notes			:	Full-function interrupt service routine for I2C module.
******************************************************************************/
#pragma TRAP_PROC
void _I2CISR( void )
{
	volatile tU08 dummyibdrRead;				/* variable to allow dummy reads of IBDR */
	
	Iic.ibsr.bit.ibif = 1;						/* reset the interrupt */
	if(Iic.ibcr.bit.mssl == SET)				/* if module is in master mode */
	{
		if(Iic.ibcr.bit.txrx == SET)			/* if module is to transmit */

⌨️ 快捷键说明

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