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

📄 dec643_timer.c

📁 这是学习dsp定时器计数器的实验
💻 C
字号:
/************************************************************
  Copyright (C), 2007 by SEED Electronic Technology LTD.
  FileName: DEC643_Timer.c
  Author:  Ya.X      Version : V1.0         Date:2007-09-20
  Description:  定时器的使用:
  				程序将产生特定频率的方波,通过Tout0输出,可以
  				通过示波器察看波形
  				产生定时的中断,并执行中断处理程序 
*************************************************************/
#include <stdio.h>
#include <csl.h>
#include <csl_timer.h>
#include <csl_irq.h>

#include "DEC643.h"

#define TIMER_CNT  60        //定义中断最大计数次数
static  Uint32 cnt = 0;      //中断次数计数

void   TimerEventHandler(void);
extern far void vectors();

static TIMER_Handle hTimer1;

//Timer 控制寄存器配置
static Uint32 TimerControl = TIMER_CTL_RMK( /* Timer control register (CTL)*/
	TIMER_CTL_SPND_EMUSTOP,
  	TIMER_CTL_INVINP_NO, /* TINP inverter control(INVINP). Only affects operation
					      if CLKSRC =0.
                           TIMER_CTL_INVINP_NO  - Uninverted TINP drives timer
                           TIMER_CTL_INVINP_YES - inverted TINP drives timer */
  	TIMER_CTL_CLKSRC_CPUOVR8,/* Timer input clock source (CLKSRC)
						   TIMER_CTL_CLKSRC_CPUOVR4 - CPU clock /8           */						   	
  	TIMER_CTL_CP_CLOCK, /* Clock/pulse mode(CP)
					       TIMER_CTL_CP_PULSE - Pulse mode.TSTAT is active one 
						        CPU clock after the timer reaches the timer
								period.PWID determines when it goes inactive.*/					    
  	TIMER_CTL_HLD_YES, /* Hold(HLD). Counter may be read or written regardless of 
					    HLD value.
						   TIMER_CTL_HLD_YES - Counter is disabled and held in
						        current value.
						   TIMER_CTL_HLD_NO - COunter is allowed to count.   */						   
  	TIMER_CTL_GO_NO, /* Go bit(GO). Resets and starts the timer counter.
				           TIMER_CTL_GO_NO - No effects on the timer.
						   TIMER_CTL_GO_YES - if HLD =1, the counter register
						        is zeroed and begins counting on next clock. */
  	TIMER_CTL_PWID_TWO, /* Pulse width(PWID). Only used in pulse mode.
					       TIMER_CTL_PWID_ONE - TSTAT goes inactive one timer 
						        input clock cycle after the timer counter value
								equals the timer period value.
                           TIMER_CTL_PWID_TWO -  TSTAT goes inactive one timer 
						        input clock cycle after the timer counter value
								equals the timer period value.               */
  	TIMER_CTL_DATOUT_0, /* Data output (DATOUT).
					       TIMER_CTL_DATOUT_0 - If FUNC  =0,the DATOUT is
						        driven on TOUT. 
                           TIMER_CTL_DATOUT_1 - If FUNC =1,The DATOUT is driven
						        on TOUT after inversion by INVOUT.           */
  	TIMER_CTL_INVOUT_NO, /* TOUT inverter control (INVOUT) 
					       TIMER_CTL_INVOUT_NO - Uninverted TSTAT drives TOUT
						   TIMER_CTL_INVOUT_YES - Inverted TSTAT drives TOUT.*/
  	TIMER_CTL_FUNC_TOUT /* Function of TOUT pin(FUNC).
					       TIMER_CTL_FUNC_GPIO - TOU is a general purpose 
						        output pin
                           TIMER_CTL_FUNC_TOUT - TOUT is a timer output pin  */
);  

void main()
{
	static Uint32 TimerEventId;

	/* initialize the CSL library */
	CSL_init();

	/* Initialize DEC643 board */
	DEC643_init();

	/* Open TIMER1 device, and reset them to power-on default state. */
	hTimer1 = TIMER_open(TIMER_DEV1, TIMER_OPEN_RESET);

	/* Configure the timer devices */
  	TIMER_configArgs(hTimer1,
	    TimerControl, /* use predefined control value  */
	    0x00000B72,   /* set period                    */
	    0x00000000    /* start count value at zero     */
  	);

	/* Obtain the event ID for the timer device. */
	TimerEventId = TIMER_getEventId(hTimer1);
	
	IRQ_globalDisable();
	IRQ_setVecs(vectors);       /* point to the IRQ vector table	*/ 	     
 	IRQ_map(TimerEventId, 14); 	/* Map TIMER events to physical interrupt number */  	
	IRQ_reset(TimerEventId);    /* Reset the timer events. */	
	IRQ_enable(TimerEventId);   /* Enable the timer events(events are disabled while resetting) */
 	IRQ_globalEnable();         /* Globally enable interrupts     */ 
  	IRQ_nmiEnable();            /* Enable NMI interrupt           */   
  
	/* Start the timers */
	TIMER_start(hTimer1);

	while(cnt <= TIMER_CNT); /* waiting for interrupt*/
}       

/*************************************************
  Function:       TimerEventHandler
  Description:    被中断服务程序调用
  Calls:          No
  Called By:      中断服务程序c_int14
  Input:          No
  Output:         No
  Return:         No
  Others:         No
*************************************************/
void TimerEventHandler(void)
{
	/* process timer event here */
	cnt++;  

	/* Exit from the program when certain count is reached */
	if ( cnt > TIMER_CNT )
	{   
		TIMER_pause(hTimer1);
		TIMER_close(hTimer1);
		printf("\nDone...");
		exit(0);
	}

	printf("\n Count : %3d ",cnt);    
}

/*************************************************
  Function:       c_int14
  Description:    定时器中断服务程序
  Calls:          No
  Called By:      Timer1产生的硬件中断14
  Input:          No
  Output:         No
  Return:         No
  Others:         No
*************************************************/
interrupt void c_int14(void)    
{
    TimerEventHandler();
    return;    
}

⌨️ 快捷键说明

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