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

📄 basic.c

📁 在Freescale HCS12中使用I2C总线源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************
* 		                                         COPYRIGHT (c) MOTOROLA 2002   
* FILE NAME: basic.c                                                      
*                                                                           
* PURPOSE: I2C demo - master 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 0xF0
#define PACKETSIZE 8

/* 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) 
{									
	TxPacketpositionptr = &TxPacket[0];	/* point to first TxPacket element */
	TxPacketendptr = &TxPacket[(PACKETSIZE-1)]; /* point to last TxPacket element */
	TxBufferemptyflag = CLEAR;
	RxPacketpositionptr = &RxPacket[0];	/* point to first TxPacket element */
	RxPacketendptr = &RxPacket[(PACKETSIZE-1)]; /* point to last TxPacket element */
	RxBufferfullflag = CLEAR;
	
	EnableInterrupts;					/* enable interrupts */
	ConfigureI2CModule();				/* call initialisation function */
	
	TxPacket[0] = 0x01;
	TxPacket[1] = 0x02;
	TxPacket[2] = 0x03;
	TxPacket[3] = 0x04;
	TxPacket[4] = 0x05;
	TxPacket[5] = 0x06;
	TxPacket[6] = 0x07;
	TxPacket[7] = 0x08;	
	
	MasterTransmitPacket2Slave(SLAVE);		/* transmit initial packet to slave */
	
	while(FOREVER)							/* forever loop */
	{
		MasterReceivePacketFromSlave(SLAVE);	/* receive data from slave */
		TxPacket[0] = RxPacket[0] + 1;
		TxPacket[1] = RxPacket[1] + 1;
		TxPacket[2] = RxPacket[2] + 1;
		TxPacket[3] = RxPacket[3] + 1;
		TxPacket[4] = RxPacket[4] + 1;
		TxPacket[5] = RxPacket[5] + 1;	
		TxPacket[6] = RxPacket[6] + 1;
		TxPacket[7] = RxPacket[7] + 1;		
		MasterTransmitPacket2Slave(SLAVE);	/* transmit new packet to slave */		
	}
}

/******************************************************************************
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 = 0xA0;			/* slave address of this module */
	Iic.ibcr.byte = (IBEN|IBIE); 	/* enable Iic module */
}

/******************************************************************************
Function Name	:	MasterTransmitPacket2Slave
Engineer		:	G.More
Date			:	23/05/02
Parameters		:	tU08 slaveAddress
Returns			:	NONE
Notes			:	Procedure which sends data on the I2C bus
******************************************************************************/
void MasterTransmitPacket2Slave (tU08 slaveAddress)
{
	while(Iic.ibsr.bit.ibb == SET) 		/* while bus is busy */
	{									/* wait until bus is free */
	}

	Iic.ibcr.byte = (IBEN|IBIE|MSSL|TXRX);	/* grab bus - start condition initiated */
	
	Iic.ibdr.byte = slaveAddress;		/* address the slave */				
	
	while(TxBufferemptyflag == CLEAR)	/* wait while message is transmitted */
	{										
	}
	TxPacketpositionptr = &TxPacket[0];	/* point to first TxPacket element */
	TxBufferemptyflag = CLEAR;
}
	
/******************************************************************************
Function Name	:	MasterReceivePacketFromSlave
Engineer		:	G.More
Date			:	23/05/02
Parameters		:	tU08 slaveAddress
Returns			:	NONE
Notes			:	Procedure which sends data on the I2C bus
******************************************************************************/
void MasterReceivePacketFromSlave (tU08 slaveAddress)
{
	MasterRxFlag = SET;

	while(Iic.ibsr.bit.ibb == SET) 		/* while bus is busy */
	{									/* wait until bus is free */
	}

	Iic.ibcr.byte = (IBEN|IBIE|MSSL|TXRX);	/* grab bus - start condition initiated */
	
	Iic.ibdr.byte = (slaveAddress | 0x01);	/* address the slave, insruct slave to transmit */				
	
	while(RxBufferfullflag == CLEAR)		/* wait while message is being received */
	{									
	}
	
	RxPacketpositionptr = &RxPacket[0];	/* point to first TxPacket element */
	RxBufferfullflag = CLEAR;
	MasterRxFlag = CLEAR;
}	
	
/*****************************************************************************/
/* 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	

⌨️ 快捷键说明

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