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

📄 timers.c

📁 freesacle的实时操作系统UCOS的开发程序
💻 C
字号:
 /*
 ***************************************************************************************
 * 文件名: Timer.c
 *
 * 功能 : 作为系统时钟,脉冲计数器 /捕捉
 *
 * 说明 :  
 *          (1)IO引脚的复用:C0---C7 
 *          (2)定时器的输入时钟:busclock 和 inputPuls
 *
 * 状态:
 ****************************************************************************************
 */ 
 /*----------------------------------------------------------------------
 * 系统时钟部分::
 *	        (1)占用Timer,作为系统的时钟源/....
 *          (2)系统的时钟的频率: OS_TICKS_PER_SEC=BusCLK/(预分因子*计数值)
 *                                BusCLK=SYSCLK/2=24M
 */
#include <hidef.h>      
#include <mc9s12db128.h> 
#include "includes.h" 
#include   "display.h"
#define BUSCLK     24000000      
#define PREDIV  	 8                  //预分因子 =16
#define SYSCNTVAL	 BUSCLK/(OS_TICKS_PER_SEC*PREDIV) //系统时钟定时器的计数值

unsigned int TC0_OLD;
unsigned int speed ;
	                                   
void SystemCLK(void){
          
    MCCTL|=0xce;        //减数计数器控制设置 中断允许,强制加载,自动更新加载,
                        //开始计数。预置分频因子 
    MCCNT =SYSCNTVAL;   //系统时钟定时器的计数值                             
}
/*----------------------------------------------------------------------------
* 输入脉冲计数器PA
*
*说明:使用PT7==P1_18引脚作为输入脉冲计数器,一个16位的计数器,返回计数值
*      当PT7作为输入,并PT7=1/0时,PACLK可以作的主计数器的输入频率
*/
void PACounterInit(void){
     TSCR1|=0x80; 
     TIOS &=~0x80;
     PACTL|=0x40;		 //打开PA计数器
     PACTL|=0x30;		//PT7==0使用内部时钟,PACLK=busCLK/64,关中断溢出允许,整合方式PC2-3
     PAFLG|=0x02;		//PA有标志位。清除溢出中断标志位
}    
unsigned int PACounter(void){    
    unsigned int temp;
  	 temp=PACN32;
		 return temp;
}
/*----------------------------------------------------------------------------
*输入捕捉:计数速度: (16位)
*说明 :PT0==P2_12引脚作为输入脉冲引脚.
*/
void Capture0Init(void){

  PACTL|=0x08;      //select the main timer source clock, Fin=PACLK/256=1464HZ
  //TSCR2|=0x03;      //prescaler (1-128) 128	 Fin=Bus/128
  TSCR1|=0x80;		  //begin runnig the main timer 
  TIOS &=~0x01;		  //use the PT0 as the input pin
  TCTL4|=0x01;      //capture on rising edge only
  DLYCT=0x00;       //input delay =(0/256/512/1024)*BusClock
 // ICOVW=          //is overwrite default for overwrite
  ICSYS|=0x02;      //allow two capture generate  one interrupt
  TFLG1|=0x01;      //clear the interrupt flag
  TIE  |=0x01;      //enable the capture interrupt 
}
 /*--------------------------------------------------------------------------
*
* 输出两脉冲之间的时间间隔:单位为cm/s
*/
unsigned int Speed(void){
  
  unsigned int temp;
  float temp2,temp3;

  if(TC0_OLD!=TC0) 
    { 
      if(TC0H>TC0) 
         temp=65536-(TC0H-TC0);
      else 
         temp=(TC0-TC0H);
      temp2=temp;
      //temp2=temp2*2048/3+0.5;
      //temp3=1000000/temp2;
      temp3=1464.84375/temp2+0.5;
      TC0_OLD=TC0;
      return (unsigned int)temp3;
    }
  else 
    {
      
      return 0;
    }
  
}  
 /*-------------------------------------------------------------------------
 *
 * 测试程序。test_Speed
 */
void test_Speed(void){
  unsigned int temp;
  temp=Speed();
  speed=temp;
  display4(temp);
}






⌨️ 快捷键说明

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