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

📄 ect.c

📁 hcs12_ECT增强型定时器源代码
💻 C
字号:
#include <mc9s12dp256.h>     /* derivative information */
#include "ECT.h"

extern byte Flag1s;
word TC1Value;
word TC1Last;
word TC2Value;
word TC2Last;
/***********************************************************************
*   ECT free counter Initialization Rotine														 *
*		bus clock	= 8MHz; Prescaler = 128; Resolution is 8MHz/128 or 16us  *	   																										 
*		Timer Overflow period is 16us*0xFFFF=	1048.560ms									 *																					
************************************************************************/
void ECT_Init(void) 
{
  TSCR2_PR   = 7;  //prescale factor is 8, bus clock/128=8Mhz/128
  TFLG2_TOF  = 1;  //Reset flag
  TSCR2_TOI  = 1;  //timer overflow interrupt enable   
  TSCR1_TEN  = 1;  //timer enable
}

/***********************************************************************
*   ECT Channel 0 Initialization Rotine														     *
*		used as output compare function                    								 *																					
************************************************************************/
void TOC0_Init(void)
{
  word Val;
  Val  = 500;          //Pulse Width = 500*Resolution  = 500*16us = 8ms
  DDRT_DDRT0  = 1;      //define PORT T0 as output pin
  PTIT_PTIT0  = 0;
  TIOS_IOS0 = 1;        //Select Channel0 as output compare
  TC0 = TCNT + Val;     //Store new value to the compare register 
  TFLG1_C0F = 1;        //Clear interrupt request flag 
  TIE_C0I = 1;          //Enable interrupt 
  TCTL2_OL0 = 1;        //Connect timer to output line   Toggle OC0 
  
}

/***********************************************************************
*   ECT Channel 1 Initialization Rotine														     *
*		used as input capture function                    								 *																					
************************************************************************/
void TOC1_Init(void)
{
  TC1Value  = 0;    //Init the TC1 Value
  TC1Last   = 0;
  
  TCTL4_EDG1B = 0;
  TCTL4_EDG1A = 1;  //Capture on rising edges only  
  
  //TIE_C1I = 0;      //Disable input capture interrupt
  //TIOS_IOS1 = 0;    //Select Channel 1 as input capture
  
  //ICOVW_NOVW1 = 0;  //The contents can be overwritten when a new input capture or latch occurs.
  
  //PPST_PPST1  = 0;  //pull-up device is connected to the associated port T pin
  PERT_PERT1  = 1;  //Either a pull-up or pull-down device is enabled
  
  TFLG1_C1F = 1;    //Reset interrupt request flag
  TIE_C1I = 1;      //Enable interrupt
  
}

/***********************************************************************
*   ECT Channel 2 Initialization Rotine														     *
*		used as input capture function                    								 *																					
************************************************************************/
void TOC2_Init(void)
{
  TC2Value  = 0;    //Init the TC2 Value
  TC2Last   = 0;
  
  // TCTL4: EDG1B=0,EDG1A=0 
  TCTL4_EDG2B = 0;
  TCTL4_EDG2A = 0;
  // TIE: C1I=0
  TIE_C1I = 0;
  //TIOS: IOS1=0 

  INTCR_IRQEN = 0;                     /* Disable the IRQ interrupt. IRQ interrupt is enabled after CPU reset by default. */
                               /* Enable interrupts */
  TCTL4_EDG2B = 0;
  TCTL4_EDG2A = 1;  //Capture on rising edges only  
  
  //TIE_C2I = 0;      //Disable input capture interrupt
  //TIOS_IOS2 = 0;    //Select Channel 2 as input capture
  
  //ICOVW_NOVW2 = 0;  //The contents can be overwritten when a new input capture or latch occurs.
  
  //PPST_PPST2  = 0;  //pull-up device is connected to the associated port T pin
  PERT_PERT2  = 1;  //Either a pull-up or pull-down device is enabled
  
  TFLG1_C2F = 1;    //Reset interrupt request flag
  TIE_C2I = 1;      //Enable interrupt
  
}
#pragma CODE_SEG NON_BANKED
#pragma TRAP_PROC

/***********************************************************************
*   Timer Overflow Interrupt Service Rotine												     *
*		used as time intterupt function                    								 *																					
************************************************************************/
void Int_TimerOverFlow(void)
{
  TFLG2_TOF  = 1;  //clear timer overflow flag
  Flag1s = 1;      //Set Flag 1s
   
}
//#pragma TRAP_PROC
/***********************************************************************
*   ECT Channel 0 Interrupt Service Rotine												     *
*		used as output compare function                    								 *																					
************************************************************************/
void interrupt 8 Int_TimerChannel0(void)
{
  TFLG1_C0F = 1;    //Clear interrupt request flag 
  TC0 += 500;       //Add value corresponding with periode
  
}

//#pragma TRAP_PROC
/***********************************************************************
*   ECT Channel 1 Interrupt Service Rotine												     *
*		used as input capture function                    								 *																					
************************************************************************/
void interrupt 9 Int_TimerChannel1(void)
{
  TFLG1_C1F = 1;    //Clear interrupt request flag
  TC1Value  = TC1 - TC1Last;  //Read TC1 and substract the last value and store value to TC1Value
  TC1Last   = TC1;
}

/***********************************************************************
*   ECT Channel 2 Interrupt Service Rotine												     *
*		used as input capture function                    								 *																					
************************************************************************/
void interrupt 10 Int_TimerChannel2(void)
{
  TFLG1_C2F = 1;    //Clear interrupt request flag
  TC2Value  = TC2 - TC2Last;  //Read TC2 and substract the last value and store value to TC1Value
  TC2Last   = TC2;
}

#pragma CODE_SEG DEFAULT

⌨️ 快捷键说明

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