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

📄 lpc_timer.c

📁 IAR EWARM嵌入式系统编程与实践光盘源码
💻 C
📖 第 1 页 / 共 2 页
字号:
// 定义时钟系统API函数

#include "LPC_Timer.h"

LPC_Timer_Config_t Timer0Config, Timer1Config;

/*************************************************************************
 * 说明:TIMER_Init函数,初始化定时器,设置预分频寄存器PR
 * 参数:LPC_TimerChanel_t DevNum
 *       unsigned long precision -- 定时器分频值(单位us), 一般设为 10 us
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_Init(LPC_TimerChanel_t DevNum, unsigned long precision) {
int i;
  switch (DevNum) { // 所有寄存器均设为0
  case TIMER0:
    Timer0Config.Precision = precision; // 设置全局变量
    Timer0Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000; // PR = Precision(us) * Pclk
    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;
    }
    T0IR=0xFF;  // 清除中断标志
    T0TCR=0;    // 禁止计数
    T0TC=0;     // 清除定时计数器
    T0PR= Timer0Config.Prescaler - 1; // PR = Presclare - 1
    T0PC=0;     // 清除预分频定时计数器
    T0MCR=0;    // 复位比较模块
    T0MR0=0;
    T0MR1=0;
    T0MR2=0;
    T0MR3=0;
    T0CCR=0;    // 复位捕获模块
    T0EMR=0;    // 复位外部比较模块
    break;
  case TIMER1:
    Timer1Config.Precision = precision; // 设置全局变量
    Timer1Config.Prescaler = (precision * SYS_GetFpclk()) / 1000000; // PR = Precision(us) * Pclk
    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;
    }
    T1IR=0xFF;   // 清除中断标志
    T1TCR=0;     // 禁止计数
    T1TC=0;      // 清除定时计数器
    T1PR=Timer1Config.Prescaler - 1; // PR = Prescaler - 1
    T1PC=0;    // 清除预分频定时计数器
    T1MCR=0;    // 复位比较模块
    T1MR0=0;
    T1MR1=0;
    T1MR2=0;
    T1MR3=0;
    T1CCR=0;    // 复位捕获模块
    T1EMR=0;    // 复位外部比较模块
    break;
  default:
    return 1;
  }
  return 0;
}

/*************************************************************************
 * 说明:TIMER_GetPrescaler函数,返回预分频值
 * 参数:LPC_TimerChanel_t DevNum
 * 返回值:unsigned int
 *************************************************************************/
unsigned long TIMER_GetPrescaler(LPC_TimerChanel_t DevNum) {
  switch (DevNum) {
  case TIMER0:
    return Timer0Config.Prescaler;
  case TIMER1:
    return Timer1Config.Prescaler;
  }
  return 0;
}

/*************************************************************************
 * 说明:TIMER_Reset函数,当下一个pclk到达时,仅复位 TC 和 PC,
 *       其他寄存器保持不变。
 * 参数:LPC_TimerChanel_t DevNum
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_Reset(LPC_TimerChanel_t DevNum) {
  switch (DevNum) {
  case TIMER0:
    T0TCR |= 2;
    return 0;
  case TIMER1:
    T1TCR |= 2;
    return 0;
  }
  return 1;
}

/*************************************************************************
 * 说明:TIMER_Start函数,启动定时器。
 * 参数:LPC_TimerChanel_t DevNum
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_Start(LPC_TimerChanel_t DevNum) {
  switch (DevNum) {
  case TIMER0:
    T0TCR &= ~2;         // 清除复位标志
    T0TCR_bit.CE = true; // 允许计数
    return 0;
  case TIMER1:
    T1TCR &= ~2;         // 清除复位标志
    T1TCR_bit.CE = true; // 允许计数
    return 0;
  }
  return 1;
}

/*************************************************************************
 * 说明:TIMER_Stop函数,停止定时器,所有寄存器保持不变。
 * 参数:LPC_TimerChanel_t DevNum
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_Stop(LPC_TimerChanel_t DevNum) {
  switch (DevNum) {
  case TIMER0:
    T0TCR_bit.CE = false;
    return 0;
  case TIMER1:
    T1TCR_bit.CE = false;
    return 0;
  }
  return 1;
}

/*************************************************************************
 * 说明:TIMER_GetTimerMatch函数,获得指定定时器的匹配响应信息。
 * 参数:LPC_TimerChanel_t DevNum,
 *       unsigned int MRNum,
 *       unsigned int * pAction ,
 *       unsigned int * pMatchValue
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_GetTimerMatch(LPC_TimerChanel_t DevNum, unsigned int MRNum,
           unsigned int * pAction , unsigned int * pMatchValue) {
  if (MRNum >= CH_MAXNUM)
    return 1;
  switch(DevNum) {
  case TIMER0:
    *pMatchValue = Timer0Config.MatchCH[MRNum].TimeValue;
    *pAction = Timer0Config.MatchCH[MRNum].Action;
    break;
  case TIMER1:
    *pMatchValue = Timer1Config.MatchCH[MRNum].TimeValue;
    *pAction = Timer1Config.MatchCH[MRNum].Action;
    break;
  default:
    return 1;
  }
  return 0;
}

/*************************************************************************
 * 说明:TIMER_SetMatchAction函数,为指定定时器建立匹配响应和其他信息
 * 参数:LPC_TimerChanel_t DevNum -- Device Number
 *       unsigned int MRNum -- Match Channel Number
 *       unsigned int action -- General Interrupt | Reset Timer | Stop Timer
 *       unsigned int 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
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_SetMatchAction(LPC_TimerChanel_t DevNum,
                          unsigned int MRNum,
                          unsigned int action ,
                          unsigned long TimeValue,
                          void (* Fnpr)(void *),
                          void * FnprArg,
                          LPC_Timer_ExtAction_t ExtAction) {
  if (action>TimerAction_StopTimer+TimerAction_ResetTimer+TimerAction_Interrupt) // 检测有效参数
    return 1;
  if (ExtAction > TOGGLE)
    return 1;
  switch (DevNum) {
  case TIMER0:
    switch (MRNum) { // 设置匹配寄存器
    case CH0:
      T0MR0=TimeValue;
      break;
    case CH1:
      T0MR1=TimeValue;
      break;
    case CH2:
      T0MR2=TimeValue;
      break;
    case CH3:
      T0MR3=TimeValue;
      break;
    default:
      return 1;
    }
    Timer0Config.MatchCH[MRNum].Enable = true;
    Timer0Config.MatchCH[MRNum].Action = action;
    Timer0Config.MatchCH[MRNum].TimeValue= TimeValue;
    Timer0Config.ExtAction[MRNum]= ExtAction;
    T0MCR &= ~(7<<(MRNum*3));            // 清除作用
    if (action & TimerAction_ResetTimer) // 复位匹配
      T0MCR |= TimerAction_ResetTimer << (3*MRNum);
    if (action & TimerAction_StopTimer) // 设置匹配的StopWatch
      T0MCR |= TimerAction_StopTimer << (3*MRNum);
    if (action & TimerAction_Interrupt) { //设置匹配中断
      Timer0Config.MatchCH[MRNum].Fnpr = Fnpr;
      Timer0Config.MatchCH[MRNum].FnprArg = FnprArg;
      T0MCR |= TimerAction_Interrupt << (3*MRNum);
    }
    T0EMR &= ~(3 << (4 + 2*MRNum));      // 清除外部作用
    switch (MRNum) {                     // 设置外部作用
    case CH0:
      T0EMR_bit.EMC0 = ExtAction;        // 设置外部作用类型
      if (ExtAction != DONOTHING)        // 分配匹配模块引脚
        PINSEL0_bit.P0_3 = 0x2;
      break;
    case CH1:
      T0EMR_bit.EMC1 = ExtAction;        // 设置外部作用类型
      if (ExtAction != DONOTHING)        // 分配匹配模块引脚
        PINSEL0_bit.P0_5 = 0x2;
      break;
    case CH2:
      T0EMR_bit.EMC2 = ExtAction;        // 设置外部作用类型
      if (ExtAction != DONOTHING)        // 分配匹配模块引脚
        PINSEL1_bit.P0_16 = 0x2;
      break;
    case CH3:
      T0EMR_bit.EMC3 = ExtAction;        // 设置外部作用类型
      break;
    }
    return 0;
  case TIMER1:
    switch (MRNum) {                      // 设置匹配寄存器
    case CH0:
      T1MR0=TimeValue;
      break;
    case CH1:
      T1MR1=TimeValue;
      break;
    case CH2:
      T1MR2=TimeValue;
      break;
    case CH3:
      T1MR3=TimeValue;
      break;
    default:
      return 1;
    }

    Timer1Config.MatchCH[MRNum].Enable = true;
    Timer1Config.MatchCH[MRNum].Action = action;
    Timer1Config.MatchCH[MRNum].TimeValue= TimeValue;
    Timer1Config.ExtAction[MRNum]= ExtAction;
    T1MCR &= ~(7<<(MRNum*3));            // 清除作用
    if (action & TimerAction_ResetTimer) // 复位匹配
      T1MCR |= TimerAction_ResetTimer << (3*MRNum);
    if (action & TimerAction_StopTimer) // 设置匹配的StopWatch
      T1MCR |= TimerAction_StopTimer << (3*MRNum);
    if (action & TimerAction_Interrupt) { // 设置匹配中断
      Timer1Config.MatchCH[MRNum].Fnpr = Fnpr;
      Timer1Config.MatchCH[MRNum].FnprArg = FnprArg;
      T1MCR |= TimerAction_Interrupt << (3*MRNum);
    }
    T1EMR &= ~(3 << (4 + 2*MRNum));       // 清除外部中断
    switch (MRNum) {                      // 设置外部作用
    case CH0:
      T1EMR_bit.EMC0 = ExtAction;         // 设置外部作用类型
      if (ExtAction != DONOTHING)         // 分配匹配模块引脚
        PINSEL0_bit.P0_12 = 0x2;
      break;
    case CH1:
      T1EMR_bit.EMC1 = ExtAction;         // 设置外部作用类型
      if (ExtAction != DONOTHING)         // 分配匹配模块引脚
        PINSEL0_bit.P0_13 = 0x2;
      break;
    case CH2:
      T1EMR_bit.EMC2 = ExtAction;         // 设置外部作用类型
      if (ExtAction != DONOTHING)         // 分配匹配模块引脚
        PINSEL1_bit.P0_19 = 0x2;
      break;
    case CH3:
      T0EMR_bit.EMC3 = ExtAction;         // 设置外部作用类型
      if (ExtAction != DONOTHING)         // 分配匹配模块引脚
        PINSEL1_bit.P0_20 = 0x2;
      break;
    }
    return 0;
  }
  return 1;
}

/*************************************************************************
 * 说明:TIMER_GetTimerExternalMatch函数,获得指定定时器外部匹配响应信息。
 * 参数:LPC_TimerChanel_t DevNum
 *       unsigned int MRNum,
 *       unsigned int *pAction,
 *       unsigned int *pExternalMatchValue
 * 返回值:int
 *         0:成功
 *         非0:错误号
 *************************************************************************/
int TIMER_GetTimerExternalMatch(LPC_TimerChanel_t DevNum, unsigned int MRNum,
            unsigned int * pAction , unsigned int *pExternalMatchValue) {
  if (MRNum >= CH_MAXNUM)
    return 1;

⌨️ 快捷键说明

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