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

📄 mcu_main.c

📁 LPC2102的keil vendor code
💻 C
字号:
/*****************************************************************************
 *   mcu_main.c:  MCU_MAIN C file for NXP LPC210x Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2005.10.01  ver 1.00    Prelimnary version, first Release
*****************************************************************************/
#include "LPC210x.H"                        /* LPC21xx definitions */
#include "type.h"
#include "irq.h"
#include "i2c.h"
#include "spi.h"
#include "gpio.h"
#include "uart.h"
#include "mcu_main.h"

/*******************************************
**	Format of gInterrupt_Status
**
**	bits 5~7:	Not used
**  bit 4:		UART Rx flag
**	bit 3:		IO state flag
**	bit 2: 		EINT2 status flag
**	bit 1: 		EINT1 status flag
**	bit 0: 		EINT0 status flag
********************************************/
BYTE gInterrupt_Status = 0;

BYTE ApplnHandler_Flag = 0;		// Application handler flag
BYTE i2c_send_flag = 1;

UINT32 IO_Pin = 0;
UINT32 Saved_input_state = 0;

/***********************************************************************
** Function name:	main                                                             
** Description: 	This example code demonstrates how LPC2102 can be used 
**					as an I2C slave device to perform an IO Expander 
**					function as well as two interface functions in an
**					embedded system.
**
**					The functionalities include:
**					(1) IO Expander
**					(2) I2C to SPI bridge
**					(3) I2C to UART bridge
** Parameter:		None
** Returned value:	None                                                                            
************************************************************************/
int main(void)
{
	/* Loads RW components of the User Code */
	load_overlay(1);

	/* Initialize VIC interrupt controller */
	init_VIC();

	/* Configuare default IO states for the 32 general purpose I/O ports */
	IO_Init();

	/* Initialize I2C controller unit 0 as a Slave Device */
	I2C0_Init(I2CSLAVE);

	/* Initialize SPI0 */
	SPI_Init();

	/* Initialize UART1 */
	UART_Init(115200);		// Set Baud Rate as 115200 bps

#ifdef LPC2102_I2C1_MASTER
	/* Demonstrates how I2C1 controller can be used as an I2C Master Mode */
	I2C1_Master_Demo();
#endif

	/* Execution waits here for I2C events */
	while(1)
	{
		if(ApplnHandler_Flag)
		{
			LPC2102_ApplnHandler(I2C0RxBuffer[0]);
			ApplnHandler_Flag = 0;
		}

		/* Polling for Uart Rx data */
		if(i2c_send_flag && UART1Count)
		{
			gInterrupt_Status |= UART_BITMASK;
			
			I2C0TxCounter = 2;
			I2C0TxBuffer[0] = gInterrupt_Status;
			I2C0TxBuffer[1] = UartGet1ByteData();

			IODIR |= P0_0_MASKBITPATTERN;		/* Ensure P0.0 is configured as output */
			IOCLR |= P0_0_MASKBITPATTERN;		/* Set P0.0 as low to interrupt I2C host */
			i2c_send_flag = 0;
		}

		/**********************************************************************
		** Input ports state change handling:
		** - Checks for input port state changes
		** - In the event of input port state change, P0.0 will be pulled Low 
		**   to interrupt the I2C host
		**********************************************************************/
		IO_Pin = (IOPIN & ~IODIR) & P0_MASKBITPATTERN & InputMonitor_Selection;					// Current input state status
		Saved_input_state = (IO_State & ~IODIR) & P0_MASKBITPATTERN & InputMonitor_Selection;	// Saved input state status
	 
		if(Saved_input_state != IO_Pin)
		{
			gInterrupt_Status |= IO_STATE_BITMASK;
	
			I2C0TxCounter = 1;
			I2C0TxBuffer[0] = gInterrupt_Status;

			/* Use P0.0 to inform the host of IO state change */
			IODIR |= P0_0_MASKBITPATTERN;	// Ensure P0.0 is configured as output
			IOCLR |= P0_0_MASKBITPATTERN;	// Set P0.0 as Low

			IO_State = IOPIN;		// Saved the current IO states
		}
	}
} // main

/***********************************************************************
** Function name:	LPC2102_ApplnHandler
** Description:    	This function is called after an I2C receive transfer
**					event is completed.	It handles all the functions in
**					the application, which includes:
**					(1) IO Expander
**					(2) I2C to SPI bridge
**					(3) I2C to UART bridge
**
**					I2C data format used for this appln:
**					Byte 1: Function ID
**						0x01 - I/O Direction Configuration
**						0x02 - Read I/O Logic States
**						0x03 - Set I/O Logic States
**						0x04 - Clear I/O Logic States
**						0x05 - Selection of input ports for state change event monitoring
**						0x06 - Write I/O (High/Low) Logic States
** 						0x07 - Open-drain ports selection
**						0x11 - SPI settings configuration
**						0x12 - Send data to SPI slave device
**						0x13 - Enable/Disable SPI function
**						0x21 - Send data to UART device
**						0x31 - Read firmware Version ID
**						0x32 - In-Application Programming (Firmware upgrade)
**					Byte 2~33: (I2C Rx buffer size is 33 bytes)
**						Data bytes that will be used by the 
**						three functions 
** Parameter:		FunctionID is the first byte of the receive buffer
**					which indicates the type of function to perform
** Returned value:	None
************************************************************************/
void LPC2102_ApplnHandler(BYTE FunctionID)
{
	switch(FunctionID)
	{
		case IOExpander_IOConf:			/* 0x01: IO Configuration - IO Expander */
			IO_Config();
			break;
		case IOExpander_IORead:			/* 0x02: Read IO States - IO Expander */
			IO_ReadStatus();
			break;
		case IOExpander_IOSet:			/* 0x03: Set IO States - IO Expander */
			IO_Set();
			break;
		case IOExpander_IOClear:		/* 0x04: Clear IO States - IO Expander */
			IO_Clear();
			break;
		case IOExpander_IOMonitor:		/* 0x05: Selection of input ports for state change monitoring - IO Expander */
			IO_MonitorInput();
			break;
		case IOExpander_IOWrite:		/* 0x06: Write IO States (High/Low) - IO Expander */
			IO_Write();
			break;
		case IO_Expander_IOOpenDrain:	/* 0x07: Selection of open-drain handling ports - IO Expander */
			IO_OpenDrain();			
			break;
		case SpiComm_Config:			/* 0x11: SPI settings configuration - SPI Communication */
			SPI_Config((BYTE*)I2C0RxBuffer);
			break;
		case SpiComm_Write:				/* 0x12: Transmit data to SPI slave device - SPI Communication */
			break;						// Handled in the I2C interrupt handler
		case SpiComm_OnOff:				/* 0x13: Enable/Disable SPI function */
			SPI_OnOff();
			break;
		case Uart_Transmit:				/* 0x21: Transmit data to UART device - UART Communication */
			U1IER = IER_THRE | IER_RLS;				/* Disable RBR */
		    UART_Send( (BYTE*)&I2C0RxBuffer[1], I2C0RxCounter-1 );
			U1IER = IER_THRE | IER_RLS | IER_RBR;	/* Re-enable RBR */
			break;
		case Firmware_VersionID:		/* 0x31: Provides firmware version ID for I2C host */
			FirmwareID_Read();
			break;
		case Firmware_Upgrade:			/* 0x32: In-Application Programming (Firmware upgrade) */
			sbl_entry();
			break;
		default:
			break;
	}
} // LPC2102_ApplnHandler

/*****************************************************************************
** Function name:	FirmwareID_Read
** Description:		This function allows the I2C host to read the firmware
**					version ID.
**
**					Release ID format:
**					- Major release ID: 0-255
**					- Minor release ID: 0-255
**					- Revision ID: 0, 0xA-0xF
**
** Parameter:		None
** Returned value:	None
*****************************************************************************/
void FirmwareID_Read(void)
{
	I2C0TxCounter = 3;
	I2C0TxBuffer[0] = FIRMWARE_VER_ID_MAJOR;
	I2C0TxBuffer[1] = FIRMWARE_VER_ID_MINOR;
	I2C0TxBuffer[2] = FIRMWARE_VER_ID_REVISION;
} // FirmwareID_Read

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

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