Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39559
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-03 14:41
个人简介

路漫漫其修远兮,吾将上下而求索

文章分类

全部博文(31)

文章存档

2015年(31)

我的朋友

分类: C/C++

2015-04-22 15:33:52

项目上用了KL芯片,其中需要一个1ms的周期中断,采用了LPTMR定时器来完成驱动,配置如下:

点击(此处)折叠或打开

  1. WORD32 Timer_Init(WORD16 wTime)
  2. {
  3.     WORD32 dwData = 0;                       // For temp value of register

  4.     if (MAX_TIME_PERIOD_MS < wTime)          // If the period time is bigger than 65(65535)
  5.     {
  6.         return SW_ERR;                       // Then return with error flag;
  7.     }
  8.     if (0 == wTime)                          // If the period time is 0, than turn off LPTMR
  9.     {
  10.         LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;    // It is the way to turn OFF LPTMR
  11.         return SW_OK;
  12.     }

  13.     MCG_SC &= (~MCG_SC_FCRDIV_MASK);         // Do this before set value of FCRDIV to evoid wrong masking
  14.     MCG_SC |= MCG_SC_FCRDIV(2);              // We choose to divided clock by 4
  15.     MCG_C2 |= MCG_C2_IRCS_MASK;              // And we choose the fast internal refenced clock(4MHz),
  16.                                              // So final clock is 1Mhz.
  17.     MCG_C1    |= MCG_C1_IRCLKEN_MASK;        // Then turn on MCGIRCLK and it works even in stop mode
  18.     SIM_SCGC5 |= SIM_SCGC5_LPTMR_MASK;       // Enable LPTMR software accessing

  19.     LPTMR0_CSR &= ~LPTMR_CSR_TEN_MASK;       // Turn off LPTMR before LPTMR register configration
  20.     dwData     |= (LPTMR_CSR_TIE_MASK\
  21.                   |LPTMR_CSR_TCF_MASK);      // We do this to reduce operations of register to save time
  22.     LPTMR0_CSR  = dwData;                    // In this case, we just operate register for once.
  23.     LPTMR0_PSR |= LPTMR_PSR_PBYP_MASK;       // We do not need to divided the clock any more here
  24.     LPTMR0_PSR &= (~LPTMR_PSR_PCS_MASK);     // Choose MCGIRCLK as the clock source (1MHz)
  25.     LPTMR0_CMR  = (wTime*1000);              // Then turn it as a period in 1ms

  26.     LPTMR0_CSR |= LPTMR_CSR_TEN_MASK;        // After all, we turn on LPTMR and start counting
  27.     return SW_OK;                            // That is all
  28. }

I write the comments with English, please forgive me for my zhuangbility. Everybody have fun^-^

Summry: The MCGIRCLK is an internal clock with two clock sources : 32KHz and 4MHz, there seem to be fixed, so you can use them without any care of you crystal clock. In this case, it is a good way for you to realise a driver because it is fixed. May we still need to modify something for other situations, but it works for my application. By the way, 32KHz and 4MHz can be chose by the field IRCS of the register MCG_C2, clear this field will choose the slow clock source 32KHz ,or you can get a 4MHz as mentioned fast clock source by setting this field. Do I need to explain what is "clear" and "set"? I will never tell you that "clear" is writing "0" to a field and "set" is writing "1" to this field.
阅读(662) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~