hal_timer.c

来自「非常全的nrf2401设计资料」· C语言 代码 · 共 114 行

C
114
字号
/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 * $LastChangedRevision: 2290 $
 */ 

/** @file
 * Hardware Abstraction Layer for the timers on the nRF24LU1.
 */
#include <Nordic\reg24lu1.h>
#include "hal_timer.h"

static volatile uint8_t tick_0;

void hal_timer_init(uint8_t tn, uint8_t mode, uint16_t t)
{
  switch(tn)                         // select timer, 0 or 1
  {
    case 0:                          // init timer0
      switch(mode)
      {
        case 1:
          TH0 = ~((t*1333)/256);     // t in msec, 1 to 49 msec
          TL0 = ~((t*1333)%256);
          TMOD &= 0xfc;              // clear mode bits
          TMOD |= 0x01;              // select mode 1
          break;

        case 2:
          TH0 = TL0 = ~((t*4)/3);    // start and reload value, in 祍ec
          TMOD &= 0xfc;              // clear mode bits
          TMOD |= 0x02;              // select mode 1
          break;
      }
      break;

    case 1:                          // init timer1
      switch(mode)
      {
        case 1:
          TH1 = ~((t*1333)/256);     // t in msec
          TL1 = ~((t*1333)%256);
          TMOD &= 0xcf;              // clear mode bits
          TMOD |= 0x10;              // select mode 1
          break;

        case 2:
          TH1 = TL1 = ~((t*4)/3);    // start and reload value, in 祍ec
          TMOD &= 0xcf;              // clear mode bits
          TMOD |= 0x20;              // select mode 1
          break;
      }
      break;

    case 2:                           // t2 init, NOT complete
      ET2 = 0;                        // T2 interrupt disable
      T2I0 = 0;                       // Stop T2
      T2PS = 1;                       // T2 clk = OSC/24
      T2R1 = 1;                       // Auto Reload; Mode 0
      T2 = CRC = ~((t * 2) / 3);      // set reload value
      TF2 = 0;                        // Clear pending flag
      T2I0 = 1;                       // Start T2, f/24
      ET2 = 1;                        // T2 interrupt enable
      break;
  }
}

void hal_timer_state(uint8_t tn, uint8_t state)
{
  switch(tn)                        // select timer, 0, 1 or 2
  {
    case 0:                         // start/ stop timer 0
      if(state == 1)  
        TR0 = 1;                    // timer start
          else
        TR0 = 0;                    // timer stop
      break;
 
    case 1:                         // start/ stop timer 1
      if(state == 1)
        TR1 = 1;                    // timer start
      else
        TR1 = 0;                    // timer stop
      break;
 
    case 2:                         // start/ stop timer 2
      if(state == 1)
        T2I0 = 1;                   // timer start
      else
        T2I0 = 0;                   // timer stop
      break;
  }    
}

void hal_timer_t1_delay(uint8_t time, uint8_t wait)
{
  tick_0 = time;
  if(wait == 1)
    while(tick_0);
}

static void timer1_isr(void) interrupt 3
{
  if(tick_0 != 0)
    --tick_0;
}

⌨️ 快捷键说明

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