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

📄 lpc_timer.c

📁 给大家提供一个在inram/exram中调试的示例,在周公的lpc2200上调试过.
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************
 *         BU MMS China, Philips Semiconductor Software Support
 *         Embest info&Tech Co. Software Support
 *---------------------------------------------------------------------------
 * The software is delivered "AS IS" without warranty or condition of any
 * kind, either express, implied or statutory.  Everybody can use it as 
 * it is opened and without copyright. We will not take any law responsibility
 * for any problem produced by using this software.
 *---------------------------------------------------------------------------                                                    
 *    File name: 	LPC_Timer.c
 *    Description: 	Define API for Timer system
 *                                                                                      
 *    History:                                                                     
 *    1. Date: 		Aug 12, 2004                                              
 *       Author: 	Shawn Zhang                                                 
 *       Description: Create
 *
 **********************************************************************/

#include "LPC_Timer.h"

LPC_Timer_Config_t Timer0Config, Timer1Config;

/*************************************************************************
 * Function Name: TIMER_Init
 * Parameters: LPC_Timer_Channel_t DevNum
 *			unsigned long precision -- the timer precision (Unit: us), general setting is 10 us
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Initialize Timer, Set the PR register that represent the precision of timer.
 *  
 *************************************************************************/
int TIMER_Init(LPC_Timer_Channel_t DevNum, unsigned long precision)
{
	int i;
	
	//all registers are set to 0;
	switch (DevNum)
	{
		case TIMER0:
			// Set globe variable 
			Timer0Config.Precision = precision;
			 // PR = Precision(us) * Pclk
			Timer0Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
			for (i=0; i<CH_MAXNUM; i++)
			{
				Timer0Config.MatchCH[i].Enable = false;
				Timer0Config.MatchCH[i].Action = 0;
				Timer0Config.MatchCH[i].TimeValue =0;
				Timer0Config.MatchCH[i].Fnpr = NULL;
				Timer0Config.MatchCH[i].FnprArg = (void *)0;

				Timer0Config.CaptureCH[i].Enable = false;
				Timer0Config.CaptureCH[i].TriggerType= 0;
				Timer0Config.CaptureCH[i].EnableInt = 0;
				Timer0Config.CaptureCH[i].Fnpr = NULL;
				Timer0Config.CaptureCH[i].FnprArg = (void *)0;
				Timer0Config.CaptureCH[i].CPValue= 0;

				Timer0Config.ExtAction[i]= DONOTHING;
				Timer0Config.ExtBitValue[i]= 0;
			}
			// Clear interrupts flags
			TIMER0_IR=0xFF;
			// Disable counting
			TIMER0_TCR=0;
			// Clear timer counter
			TIMER0_TC=0;
			// PR = Presclare - 1
			TIMER0_PR= Timer0Config.Prescaler - 1;
			// Clear prescaler timer counter
			TIMER0_PC=0;
			// Reset Compare modules
			TIMER0_MCR=0;
			TIMER0_MR0=0;
			TIMER0_MR1=0;
			TIMER0_MR2=0;
			TIMER0_MR3=0;
			// Reset Capture modules
			TIMER0_CCR=0;
			TIMER0_CR0=0;
			TIMER0_CR1=0;
			TIMER0_CR2=0;
			TIMER0_CR3=0;
			// Reset External Compare module
			TIMER0_EMR=0;		
			break;
			
		case TIMER1:
			// Set globe variable 
			Timer1Config.Precision = precision;
			 // PR = Precision(us) * Pclk
			Timer1Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000;
			for (i=0; i<CH_MAXNUM; i++)
			{
				Timer1Config.MatchCH[i].Enable = false;
				Timer1Config.MatchCH[i].Action = 0;
				Timer1Config.MatchCH[i].TimeValue =0;
				Timer1Config.MatchCH[i].Fnpr = NULL;
				Timer1Config.MatchCH[i].FnprArg = (void *)0;

				Timer1Config.CaptureCH[i].Enable = false;
				Timer1Config.CaptureCH[i].TriggerType= 0;
				Timer1Config.CaptureCH[i].EnableInt = 0;
				Timer1Config.CaptureCH[i].Fnpr = NULL;
				Timer1Config.CaptureCH[i].FnprArg = (void *)0;
				Timer1Config.CaptureCH[i].CPValue= 0;

				Timer1Config.ExtAction[i]= DONOTHING;
				Timer1Config.ExtBitValue[i]= 0;
			}
			// Clear interrupts flags
			TIMER1_IR=0xFF;
			// Disable counting
			TIMER1_TCR=0;
			// Clear timer counter
			TIMER1_TC=0;
			// PR = Presclare - 1
			TIMER1_PR= Timer1Config.Prescaler - 1;
			// Clear prescaler timer counter
			TIMER1_PC=0;
			// Reset Compare modules
			TIMER1_MCR=0;
			TIMER1_MR0=0;
			TIMER1_MR1=0;
			TIMER1_MR2=0;
			TIMER1_MR3=0;
			// Reset Capture modules
			TIMER1_CCR=0;
			TIMER1_CR0=0;
			TIMER1_CR1=0;
			TIMER1_CR2=0;
			TIMER1_CR3=0;
			// Reset External Compare module
			TIMER1_EMR=0;		
			break;

		default:
			return 1;
	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_Reset
 * Parameters: LPC_Timer_Channel_t DevNum
  * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: When next pclk arrives, only the TC and PC will be reset. 
 *              	Whilst other registers remain. 
 *  
 *************************************************************************/
int TIMER_Reset(LPC_Timer_Channel_t DevNum)
{
	switch (DevNum)
	{
		case TIMER0:
			TIMER0_TCR |= (1<<TCR_RESET_BIT);
			break;
		case TIMER1:
			TIMER1_TCR |= (1<<TCR_RESET_BIT);
			break;
		default:
			return 1;
	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_Start
 * Parameters: LPC_Timer_Channel_t DevNum
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Start Timer or enable the Timer. if the Timer stop now, the Timer will
 *			resume running after calling this function.
 *  
 *************************************************************************/
int TIMER_Start(LPC_Timer_Channel_t DevNum)
{
	switch (DevNum)
	{
		case TIMER0:
			TIMER0_TCR &= ~(1<<TCR_RESET_BIT);
			TIMER0_TCR |= (1<<TCR_ENABLE_BIT);
			break;
		case TIMER1:
			TIMER1_TCR &= ~(1<<TCR_RESET_BIT);
			TIMER1_TCR |= (1<<TCR_ENABLE_BIT);
			break;
		default:
			return 1;
	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_Stop
 * Parameters: LPC_Timer_Channel_t DevNum
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Just stop Timer or disable Timer, all registers remain.
 *  
 *************************************************************************/
int TIMER_Stop(LPC_Timer_Channel_t DevNum)
{
	switch (DevNum)
	{
		case TIMER0:
			TIMER0_TCR &= ~(1<<TCR_ENABLE_BIT);
			break;
		case TIMER1:
			TIMER1_TCR &= ~(1<<TCR_ENABLE_BIT);
			break;
		default:
			return 1;
	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_GetPrescaler
 * Parameters:  LPC_Timer_Channel_t DevNum
 * Return: unsigned long 
 *
 * Description: Return prescaler value
 *  
 *************************************************************************/
unsigned long TIMER_GetPrescaler(LPC_Timer_Channel_t DevNum)
{
	if (DevNum == TIMER0)
		return Timer0Config.Prescaler;
	else
		return Timer1Config.Prescaler;
}


/*************************************************************************
 * Function Name: TIMER_SetMatchAction
 * Parameters: 	LPC_Timer_Channel_t DevNum	-- Device Number
 *				lpc_uint8 CHNum -- Match Channel Number
 *				lpc_uint8 action -- General Interrupt | Reset Timer | Stop Timer
 *				lpc_uint32 Timevalue -- the time value (Unit: us)
 *				void (* Fnpr)(void *) -- ISR function pointer
 *				void * FnprArg -- relative argument
 *				LPC_Timer_ExtAction_t ExtAction -- External Match Control Action
 *
 * Return: int 
 *             	0: success
 *			non-zero: error number
 * Description: Set corresponding matching action and other information to the channel 
 *			for specific timer.
 *
 * NOTE:  Before setting timer match function, IO ports should be open. So you should
 *		configure relative PINSEL, which is not implemented in this function.
 *************************************************************************/
int TIMER_SetMatchAction(LPC_Timer_Channel_t DevNum, 
 							lpc_uint8 CHNum, 
							lpc_uint8 action , 
							unsigned long TimeValue,
							void (* Fnpr)(void *),
							void * FnprArg,
							LPC_Timer_ExtAction_t ExtAction)
{
	 // Check parameter valid
	if (CHNum >= CH_MAXNUM-1)
		return 1;
	if (action > TimerAction_StopTimer + TimerAction_ResetTimer + TimerAction_Interrupt)
		return 1;
			
	switch (DevNum)
	{
		case TIMER0:
			 // Set Match register
			switch (CHNum)
			{
				case CH0:
					TIMER0_MR0=TimeValue;
					break;
				case CH1:
					TIMER0_MR1=TimeValue;
					break;
				case CH2:
					TIMER0_MR2=TimeValue;
					break;
				case CH3:
					TIMER0_MR3=TimeValue;
					break;
				default:
					return 1;
			}
			Timer0Config.MatchCH[CHNum].Enable = true;
			Timer0Config.MatchCH[CHNum].Action = action;
			Timer0Config.MatchCH[CHNum].TimeValue= TimeValue;
			Timer0Config.ExtAction[CHNum]= ExtAction;

			// Clear all actions on Match
			TIMER0_MCR &= ~(7<<(CHNum*3));
			// Set Reset on Match
			if (action & TimerAction_ResetTimer)
				 TIMER0_MCR |= TimerAction_ResetTimer << (3*CHNum);
			// Set StopWatch on Match
			if (action & TimerAction_StopTimer)
				 TIMER0_MCR |= TimerAction_StopTimer << (3*CHNum);
			// Set Interrupt on Match
			if (action & TimerAction_Interrupt)
			{
				Timer0Config.MatchCH[CHNum].Fnpr = Fnpr;
				Timer0Config.MatchCH[CHNum].FnprArg = FnprArg;
				 TIMER0_MCR |= TimerAction_Interrupt << (3*CHNum);
			}

			// Clear and set external action
			TIMER0_EMR &= ~(7 << (4 + 2*CHNum));
			TIMER0_EMR |= (ExtAction << (4 + 2*CHNum));
			break;
			
		case TIMER1:
			 // Set Match register
			switch (CHNum)
			{
				case CH0:
					TIMER1_MR0=TimeValue;
					break;
				case CH1:
					TIMER1_MR1=TimeValue;
					break;
				case CH2:
					TIMER1_MR2=TimeValue;
					break;
				case CH3:
					TIMER1_MR3=TimeValue;
					break;
				default:
					return 1;
			}
			Timer1Config.MatchCH[CHNum].Enable = true;
			Timer1Config.MatchCH[CHNum].Action = action;
			Timer1Config.MatchCH[CHNum].TimeValue= TimeValue;
			Timer1Config.ExtAction[CHNum]= ExtAction;

			// Clear all actions on Match
			TIMER1_MCR &= ~(7<<(CHNum*3));
			// Set Reset on Match
			if (action & TimerAction_ResetTimer)
				 TIMER1_MCR |= TimerAction_ResetTimer << (3*CHNum);
			// Set StopWatch on Match
			if (action & TimerAction_StopTimer)
				 TIMER1_MCR |= TimerAction_StopTimer << (3*CHNum);
			// Set Interrupt on Match
			if (action & TimerAction_Interrupt)
			{
				Timer1Config.MatchCH[CHNum].Fnpr = Fnpr;
				Timer1Config.MatchCH[CHNum].FnprArg = FnprArg;
				 TIMER1_MCR |= TimerAction_Interrupt << (3*CHNum);
			}

			// Clear and set external action
			TIMER1_EMR &= ~(7 << (4 + 2*CHNum));
			TIMER1_EMR |= (ExtAction << (4 + 2*CHNum));
			break;
			
		default:
			return 1;
	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_GetTimerMatch
 * Parameters:  LPC_Timer_Channel_t DevNum
 *	        lpc_uint8 CHNum -- Match Channel Number
 *	        lpc_uint8 *pAction
 *	        unsigned long *pMatchValue
 * Return: int 
 *              	0: success
 *	 		non-zero: error number
 *
 * Description: Get correponding matching information for specific timer and match number.
 *************************************************************************/
int TIMER_GetTimerMatch(LPC_Timer_Channel_t DevNum, lpc_uint8 CHNum,
	          lpc_uint8 * pAction , unsigned long *pMatchValue)
{
	if (CHNum >= CH_MAXNUM -1)
		return 1;
	
	switch(DevNum)
	{
		case TIMER0:
			*pMatchValue = Timer0Config.MatchCH[CHNum].TimeValue;
			*pAction = Timer0Config.MatchCH[CHNum].Action;
			break;
		case TIMER1:
			*pMatchValue = Timer1Config.MatchCH[CHNum].TimeValue;
			*pAction = Timer1Config.MatchCH[CHNum].Action;
			break;
		default:
			return 1;
  	}
	return 0;
}


/*************************************************************************
 * Function Name: TIMER_GetTimerExternalMatch
 * Parameters:  LPC_Timer_Channel_t DevNum
 *	        	lpc_uint8CHNum -- Match Channel Number
 *	        	lpc_uint8 *pAction,
 *	        	unsigned long *pExternalMatchValue
 * Return: int 
 *              	0: success
 *	 		non-zero: error number
 *

⌨️ 快捷键说明

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