timer.c

来自「ucos在所有cpu下的移植范例」· C语言 代码 · 共 75 行

C
75
字号
#include <ioas.h>
#include "timer.h"
#include "includes.h"

/*****************************************************************************************************                                                                        
*   Function: TimerInit()
*
*   Description: This is the initialization function. This function sets the channel 4 timer to be
*                interrupted every 20 msec (1 tick).                                                                           
*
*   Caveats:
*****************************************************************************************************/ 

void TimerInit(void)
	{
	/* Stop the timer and reset the counter to 0 */
	TSC = TMR_TSTOP | TMR_TRST;

	/* Set the initial modulus for the timer */
	TMODH = (unsigned char)(40000 >> 8);
	TMODL = (unsigned char)(40000);

	/* set interrupt at 20ms */
	TCH4H = (20000 >> 8);
	TCH4L = 20000;

	/* Enable interrupt, Pin under port control, Initial output low */
	TSC4 = TMR_CHIE | TMR_MSXA;
 
	/* Select TIM clock and start timer */
	TSC = TMR_PRESCALER;
	}

/***************************************************************************************************** 
*   Function: TimerISR()
*
*   Description: This is RTOS interrupt handler function that gets called every 20msec.
*
*   Caveats:
*****************************************************************************************************/ 

@interrupt void TimerISR(void)
	{
	unsigned short tmp;
   
    tmp = TCH4H << 8;
    tmp |= TCH4L;

	/* Setup for new output compare value and watch for overflow */
    if ((tmp += 20000) > 40000)
		tmp -= 40000;

    TCH4H = (unsigned char)(tmp >> 8);
    TCH4L = (unsigned char)(tmp);

    while (TSC4 & 0x80)
	    {
    	/* clear the interrupt */
    	TSC4 &= ~0x80;
	    }

	/* Notify uC/OS-II about ISR */
	OSIntNesting++;

	/* Enable interrupts to allow interrupt nesting */
	_asm("cli\n");

	/* Call uC/OS-II's tick updating function */
    OSTimeTick();

	/* Notify uC/OS-II about end of ISR */
    OSIntExit();
	}

⌨️ 快捷键说明

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