[DemoCode下载] 新唐M051定时器2测试程序

[复制链接]
 楼主| 仙女山 发表于 2015-7-9 09:19 | 显示全部楼层 |阅读模式

#include "SmartM_M0.h"

bool flag = 0; //中断标志位
/****************************************
*函数名称:TMR2Init
*输    入:无
*输    出:无
*功    能:定时器2初始化
******************************************/
VOID TMR2Init(VOID)
{
    PROTECT_REG
(
  /* 使能TMR2时钟源 */
     APBCLK |= TMR2_CLKEN;
  /* 选择TMR2时钟源为外部晶振12MHz */
  CLKSEL1 = (CLKSEL1 & (~TM2_CLK)) | TM1_12M;  
  /* 复位TMR2 */
  IPRSTC2 |=  TMR2_RST;
  IPRSTC2 &= ~TMR2_RST;
  /* 选择TMR2的工作模式为周期模式*/
  TCSR2 &= ~TMR_MODE;
  TCSR2 |=  MODE_PERIOD;  
  /* 溢出周期 = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
  /* 溢出周期 = (1/12MHz) * (0 + 1) * (12000*500)=500ms (0.5S)*/
  TCSR2  = TCSR2 & 0xFFFFFF00;  // 设置预分频值 [0~255]
  TCMPR2 = 12000*500;        // 设置比较值 [0~16777215]
  TCSR2 |= TMR_IE;     //使能TMR0中断
  NVIC_ISER |= TMR2_INT;
  TCSR2 |= CRST;      //复位TMR0计数器   
  TCSR2 |= CEN;      //使能TMR0
)
}
/****************************************
*函数名称:main
*输    入:无
*输    出:无
*功    能:函数主体
******************************************/
INT32 main(VOID)
{
  u8 i =0;
     PROTECT_REG            //ISP下载时保护FLASH存储器
  (      
   PWRCON |= XTL12M_EN;      //默认时钟源为外部晶振
   while((CLKSTATUS & XTL12M_STB) == 0);   //等待12MHz时钟稳定      
   CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //设置外部晶振为系统时钟
   P2_PMD = 0x5555;       //P2口设置为输出模式
      P2_DOUT= 0x00;        //P2口输出低电平
  )
  TMR2Init();
    while(1)
  {
   if(flag)
  {
   flag = 0;
   i++;
  }
  if(i>7)
  {
   i = 0;
  }
  P2_DOUT = 1UL<<i;         //P2口输出相应的电平
  }
}
/****************************************
*函数名称:TMR2_IRQHandler
*输    入:无
*输    出:无
*功    能:定时器2中断服务函数
******************************************/
VOID TMR2_IRQHandler(VOID)
{
/* 清除TMR1中断标志位 */
TISR2 |= TMR_TIF;
flag = 1;
}

 楼主| 仙女山 发表于 2015-7-16 15:45 | 显示全部楼层
这么好的帖子为啥没人看呢
643757107 发表于 2015-7-16 19:31 | 显示全部楼层
谁说没人看,我来看看,不知道这个定时器,有没有库函数的方法,我找找去。
643757107 发表于 2015-7-16 19:33 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     timer.h
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 6 $
  5. * $Date: 14/01/28 10:49a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 series Timer driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __TIMER_H__
  12. #define __TIMER_H__

  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif


  17. /** @addtogroup M051_Device_Driver M051 Device Driver
  18.   @{
  19. */

  20. /** @addtogroup M051_TIMER_Driver TIMER Driver
  21.   @{
  22. */

  23. /** @addtogroup M051_TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
  24.   @{
  25. */

  26. #define TIMER_ONESHOT_MODE                  (0UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in one-shot mode */
  27. #define TIMER_PERIODIC_MODE                 (1UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in periodic mode */
  28. #define TIMER_TOGGLE_MODE                   (2UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in toggle-output mode */
  29. #define TIMER_CONTINUOUS_MODE               (3UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in continuous counting mode */
  30. #define TIMER_CONTINUOUS_MODE               (3UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in continuous counting mode */
  31. #define TIMER_CAPTURE_FROM_TXEX_PIN         (0UL << TIMER_TCSR_CAP_SRC_Pos) /*!< Timer capture source is from TxEX pin */
  32. #define TIMER_CAPTURE_FROM_INTERNAL_ACMP    (1UL << TIMER_TCSR_CAP_SRC_Pos) /*!< Timer capture source is from internal ACMP output signal */
  33. #define TIMER_TOUT_PIN_FROM_TX_PIN          (0UL << TIMER_TCSR_TOUT_SEL_Pos) /*!< Timer toggle-output pin is from Tx pin */
  34. #define TIMER_TOUT_PIN_FROM_TXEX_PIN        (1UL << TIMER_TCSR_TOUT_SEL_Pos) /*!< Timer toggle-output pin is from TxEX pin */
  35. #define TIMER_CAPTURE_FREE_COUNTING_MODE    (0UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to get timer counter value */
  36. #define TIMER_CAPTURE_COUNTER_RESET_MODE    (1UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to reset timer counter */
  37. #define TIMER_CAPTURE_FALLING_EDGE              (0UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Falling edge trigger timer capture */
  38. #define TIMER_CAPTURE_RISING_EDGE               (1UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Rising edge trigger timer capture */
  39. #define TIMER_CAPTURE_FALLING_AND_RISING_EDGE   (2UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Both falling and rising edge trigger timer capture */
  40. #define TIMER_COUNTER_FALLING_EDGE          (0UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on falling edge */
  41. #define TIMER_COUNTER_RISING_EDGE           (1UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on rising edge */

  42. /*@}*/ /* end of group M051_TIMER_EXPORTED_CONSTANTS */


  43. /** @addtogroup M051_TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  44.   @{
  45. */

  46. /**
  47.   * @brief      Set Timer Compare Value
  48.   *
  49.   * @param[in]  timer       The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  50.   * @param[in]  u32Value    Timer compare value. Valid values are between 2 to 0xFFFFFF.
  51.   *
  52.   * [url=home.php?mod=space&uid=266161]@return[/url]     None
  53.   *
  54.   * @details    This macro is used to set new Timer compared value.
  55.   */
  56. #define TIMER_SET_CMP_VALUE(timer, u32Value)        ((timer)->TCMPR = (u32Value))

  57. /**
  58.   * @brief      Set Timer Prescale Value
  59.   *
  60.   * @param[in]  timer       The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  61.   * @param[in]  u32Value    Timer prescale value. Valid values are between 0 to 0xFF.
  62.   *
  63.   * @return     None
  64.   *
  65.   * @details    This macro is used to set new Timer prescale value.
  66.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]       Clock input is divided by (prescale + 1) before it is fed into timer.
  67.   */
  68. #define TIMER_SET_PRESCALE_VALUE(timer, u32Value)   ((timer)->TCSR = ((timer)->TCSR & ~TIMER_TCSR_PRESCALE_Msk) | (u32Value))

  69. /**
  70.   * @brief      Check specify Timer Status
  71.   *
  72.   * @return     Timer counter is activate or inactivate
  73.   * @retval     0   Timer 24-bit up counter is inactive
  74.   * @retval     1   Timer 24-bit up counter is active
  75.   *
  76.   * @details    This macro is used to check if specify Timer is inactive or active.
  77.   */
  78. #define TIMER_IS_ACTIVE(timer)                      ((timer)->TCSR & TIMER_TCSR_CACT_Msk ? 1 : 0)

  79. /**
  80.   * @brief      Select Toggle-output Pin
  81.   *
  82.   * @param[in]  timer       The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  83.   * @param[in]  u32ToutSel  Toggle-output pin selection, valid values are:
  84.   *                         - \ref TIMER_TOUT_PIN_FROM_TX_PIN
  85.   *                         - \ref TIMER_TOUT_PIN_FROM_TXEX_PIN
  86.   *
  87.   * @return     None
  88.   *
  89.   * @details    This macro is used to select Timer toggle-output pin.
  90.   */
  91. #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel)    ((timer)->TCSR = ((timer)->TCSR & ~TIMER_TCSR_TOUT_SEL_Msk) | (u32ToutSel))

  92. /**
  93.   * @details    This function is used to start Timer counting.
  94.   */
  95. static __INLINE void TIMER_Start(TIMER_T *timer)
  96. {
  97.     timer->TCSR |= TIMER_TCSR_CEN_Msk;
  98. }

  99. /**
  100.   * @details    This function is used to stop Timer counting.
  101.   */
  102. static __INLINE void TIMER_Stop(TIMER_T *timer)
  103. {
  104.     timer->TCSR &= ~TIMER_TCSR_CEN_Msk;
  105. }

  106. /**
  107.   * @details    This function is used to enable the Timer wake-up function.
  108.   * @note       To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
  109.   */
  110. static __INLINE void TIMER_EnableWakeup(TIMER_T *timer)
  111. {
  112.     timer->TCSR |= TIMER_TCSR_WAKE_EN_Msk;
  113. }

  114. /**
  115.   * @details    This function is used to disable the Timer wake-up function.
  116.   */
  117. static __INLINE void TIMER_DisableWakeup(TIMER_T *timer)
  118. {
  119.     timer->TCSR &= ~TIMER_TCSR_WAKE_EN_Msk;
  120. }

  121. /**
  122.   * @details    This function is used to enable the capture pin detection de-bounce function.
  123.   */
  124. static __INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
  125. {
  126.     timer->TEXCON |= TIMER_TEXCON_TEXDB_Msk;
  127. }

  128. /**
  129.   * @details    This function is used to disable the capture pin detection de-bounce function.
  130.   */
  131. static __INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
  132. {
  133.     timer->TEXCON &= ~TIMER_TEXCON_TEXDB_Msk;
  134. }

  135. /**
  136.   * @details    This function is used to enable the counter pin detection de-bounce function.
  137.   */
  138. static __INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
  139. {
  140.     timer->TEXCON |= TIMER_TEXCON_TCDB_Msk;
  141. }

  142. /**
  143.   * @details    This function is used to disable the counter pin detection de-bounce function.
  144.   */
  145. static __INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
  146. {
  147.     timer->TEXCON &= ~TIMER_TEXCON_TCDB_Msk;
  148. }

  149. /**
  150.   * @details    This function is used to enable the Timer time-out interrupt function.
  151.   */
  152. static __INLINE void TIMER_EnableInt(TIMER_T *timer)
  153. {
  154.     timer->TCSR |= TIMER_TCSR_IE_Msk;
  155. }

  156. /**
  157.   * @details    This function is used to disable the Timer time-out interrupt function.
  158.   */
  159. static __INLINE void TIMER_DisableInt(TIMER_T *timer)
  160. {
  161.     timer->TCSR &= ~TIMER_TCSR_IE_Msk;
  162. }

  163. /**
  164.   * @details    This function is used to enable the Timer capture trigger interrupt function.
  165.   */
  166. static __INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
  167. {
  168.     timer->TEXCON |= TIMER_TEXCON_TEXIEN_Msk;
  169. }

  170. /**
  171.   * @details    This function is used to disable the Timer capture trigger interrupt function.
  172.   */
  173. static __INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
  174. {
  175.     timer->TEXCON &= ~TIMER_TEXCON_TEXIEN_Msk;
  176. }

  177. /**
  178.   * @brief      Get Timer Time-out Interrupt Flag
  179.   *
  180.   * @param[in]  timer   The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  181.   *
  182.   * @return     Timer time-out interrupt occurred or not
  183.   * @retval     0   Timer time-out interrupt did not occur
  184.   * @retval     1   Timer time-out interrupt occurred
  185.   *
  186.   * @details    This function indicates Timer time-out interrupt occurred or not.
  187.   */
  188. static __INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
  189. {
  190.     return (timer->TISR & TIMER_TISR_TIF_Msk ? 1 : 0);
  191. }

  192. /**
  193.   * @details    This function clears Timer time-out interrupt flag.
  194.   */
  195. static __INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
  196. {
  197.     timer->TISR = TIMER_TISR_TIF_Msk;
  198. }

  199. /**
  200.   * @brief      Get Timer Capture Interrupt Flag
  201.   *
  202.   * @param[in]  timer   The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  203.   *
  204.   * @return     Timer capture interrupt occurred or not
  205.   * @retval     0   Timer capture interrupt did not occur
  206.   * @retval     1   Timer capture interrupt occurred
  207.   *
  208.   * @details    This function indicates Timer capture interrupt occurred or not.
  209.   */
  210. static __INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
  211. {
  212.     return timer->TEXISR;
  213. }

  214. /**
  215.   * @details    This function clears Timer capture interrupt flag.
  216.   */
  217. static __INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
  218. {
  219.     timer->TEXISR = TIMER_TEXISR_TEXIF_Msk;
  220. }

  221. /**
  222.   * @brief      Get Timer Wakeup Flag
  223.   *
  224.   * @param[in]  timer   The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  225.   *
  226.   * @return     Timer has waked up system or not
  227.   * @retval     0   Timer did not wake up system
  228.   * @retval     1   Timer Timer wake up system
  229.   *
  230.   * @details    This function indicates Timer has waked up system or not.
  231.   */
  232. static __INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
  233. {
  234.     return (timer->TISR & TIMER_TISR_TWF_Msk ? 1 : 0);
  235. }

  236. /**
  237.   * @details    This function clears the Timer wake-up system flag.
  238.   */
  239. static __INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
  240. {
  241.     timer->TISR = TIMER_TISR_TWF_Msk;
  242. }

  243. /**
  244.   * @details    This function reports the current timer capture data value.
  245.   */
  246. static __INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
  247. {
  248.     return timer->TCAP;
  249. }

  250. /**
  251.   * @details    This function reports the current timer counter value.
  252.   */
  253. static __INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
  254. {
  255.     return timer->TDR;
  256. }

  257. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
  258. void TIMER_Close(TIMER_T *timer);
  259. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
  260. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
  261. void TIMER_DisableCapture(TIMER_T *timer);
  262. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
  263. void TIMER_DisableEventCounter(TIMER_T *timer);
  264. uint32_t TIMER_GetModuleClock(TIMER_T *timer);

  265. /*@}*/ /* end of group M051_TIMER_EXPORTED_FUNCTIONS */

  266. /*@}*/ /* end of group M051_TIMER_Driver */

  267. /*@}*/ /* end of group M051_Device_Driver */

  268. #ifdef __cplusplus
  269. }
  270. #endif

  271. #endif //__TIMER_H__

  272. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


734774645 发表于 2015-7-16 20:12 | 显示全部楼层
看的我一头雾水啊。看来需要多多练习了。
稳稳の幸福 发表于 2015-7-16 21:22 | 显示全部楼层
   - \ref TIMER_TOUT_PIN_FROM_TX_PIN
  *                         - \ref TIMER_TOUT_PIN_FROM_TXEX_PIN
送TX,
598330983 发表于 2015-7-17 15:18 | 显示全部楼层
PROTECT_REG            //ISP下载时保护FLASH存储器
  (      
   PWRCON |= XTL12M_EN;      //默认时钟源为外部晶振
   while((CLKSTATUS & XTL12M_STB) == 0);   //等待12MHz时钟稳定      
   CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //设置外部晶振为系统时钟
   P2_PMD = 0x5555;       //P2口设置为输出模式
      P2_DOUT= 0x00;        //P2口输出低电平
  )

开头这个,是干啥用的。
李香兰 发表于 2015-7-18 22:05 | 显示全部楼层
  1. #include "SmartM_M0.h"
  2. bool flag = 0; //中断标志位
  3. /****************************************
  4. *函数名称:TMR2Init
  5. *输    入:无
  6. *输    出:无
  7. *功    能:定时器2初始化
  8. ******************************************/
  9. VOID TMR2Init(VOID)
  10. {
  11.     PROTECT_REG
  12. (
  13.   /* 使能TMR2时钟源 */
  14.      APBCLK |= TMR2_CLKEN;
  15.   /* 选择TMR2时钟源为外部晶振12MHz */
  16.   CLKSEL1 = (CLKSEL1 & (~TM2_CLK)) | TM1_12M;  
  17.   /* 复位TMR2 */
  18.   IPRSTC2 |=  TMR2_RST;
  19.   IPRSTC2 &= ~TMR2_RST;
  20.   /* 选择TMR2的工作模式为周期模式*/
  21.   TCSR2 &= ~TMR_MODE;
  22.   TCSR2 |=  MODE_PERIOD;  
  23.   /* 溢出周期 = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
  24.   /* 溢出周期 = (1/12MHz) * (0 + 1) * (12000*500)=500ms (0.5S)*/
  25.   TCSR2  = TCSR2 & 0xFFFFFF00;  // 设置预分频值 [0~255]
  26.   TCMPR2 = 12000*500;        // 设置比较值 [0~16777215]
  27.   TCSR2 |= TMR_IE;     //使能TMR0中断
  28.   NVIC_ISER |= TMR2_INT;
  29.   TCSR2 |= CRST;      //复位TMR0计数器   
  30.   TCSR2 |= CEN;      //使能TMR0
  31. )
  32. }
  33. /****************************************
  34. *函数名称:main
  35. *输    入:无
  36. *输    出:无
  37. *功    能:函数主体
  38. ******************************************/
  39. INT32 main(VOID)
  40. {
  41.   u8 i =0;
  42.      PROTECT_REG            //ISP下载时保护FLASH存储器
  43.   (      
  44.    PWRCON |= XTL12M_EN;      //默认时钟源为外部晶振
  45.    while((CLKSTATUS & XTL12M_STB) == 0);   //等待12MHz时钟稳定      
  46.    CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //设置外部晶振为系统时钟
  47.    P2_PMD = 0x5555;       //P2口设置为输出模式
  48.       P2_DOUT= 0x00;        //P2口输出低电平
  49.   )
  50.   TMR2Init();
  51.     while(1)
  52.   {
  53.    if(flag)
  54.   {
  55.    flag = 0;
  56.    i++;
  57.   }
  58.   if(i>7)
  59.   {
  60.    i = 0;
  61.   }
  62.   P2_DOUT = 1UL<<i;         //P2口输出相应的电平
  63.   }
  64. }
  65. /****************************************
  66. *函数名称:TMR2_IRQHandler
  67. *输    入:无
  68. *输    出:无
  69. *功    能:定时器2中断服务函数
  70. ******************************************/
  71. VOID TMR2_IRQHandler(VOID)
  72. {
  73. /* 清除TMR1中断标志位 */
  74. TISR2 |= TMR_TIF;
  75. flag = 1;
  76. }
改成这样的多好

598330983 发表于 2015-7-19 09:45 | 显示全部楼层
有些句子的写法很特别难以理解,但是却很实用。
跟屁虫 发表于 2015-7-19 20:26 | 显示全部楼层
还需要认真研究一下代码才好
643757107 发表于 2015-7-21 23:35 | 显示全部楼层
  /* 选择TMR2的工作模式为周期模式*/
  TCSR2 &= ~TMR_MODE;
  TCSR2 |=  MODE_PERIOD;
最好说明一下,是什么模式。
643757107 发表于 2015-7-21 23:36 | 显示全部楼层
噢,原来是周期模式啊,应该说明其他还有什么模式
玛尼玛尼哄 发表于 2015-7-21 23:39 | 显示全部楼层
主要就是测定时器工作如何配置的,所以中断内容几乎没有
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

340

帖子

2

粉丝

36

主题

340

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部