How can I calculate machine cycle of MC9S12XHZ512 micro-controller.
I want to create time delay in MC9S12XHZ512 micro-controller using Timer, I have used 16MHz crystal on my micro-controller board with no prescale, Can someone explain me how can I calculate delay of 1ms if I am using 16-bit Timer.
Solved! Go to Solution.
Here is an example for similar MCU with a lot of description:
/*******************************************************************************
* Freescale Semiconductor Inc.
* (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
********************************************************************************
Services performed by FREESCALE in this matter are performed AS IS and without
any warranty. CUSTOMER retains the final decision relative to the total design
and functionality of the end product. FREESCALE neither guarantees nor will be
held liable by CUSTOMER for the success of this project.
FREESCALE DISCLAIMS ALL WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY INCLUDING,
BUT NOT LIMITED TO, IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
A PARTICULAR PURPOSE ON ANY HARDWARE, SOFTWARE OR ADVISE SUPPLIED TO THE PROJECT
BY FREESCALE, AND OR NAY PRODUCT RESULTING FROM FREESCALE SERVICES. IN NO EVENT
SHALL FREESCALE BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
THIS AGREEMENT.
CUSTOMER agrees to hold FREESCALE harmless against any and all claims demands or
actions by anyone on account of any damage, or injury, whether commercial,
contractual, or tortuous, rising directly or indirectly as a result of an advise
or assistance supplied CUSTOMER in connection with product, services or goods
supplied under this Agreement.
********************************************************************************
* File main.c
* Owner r62780
* Version 1.0
* Date Sep-15-2010
* Classification General Business Information
* Brief FLASH E/W
********************************************************************************
* Detailed Description:
*
* - the SW software demonstrates one of the possibilities how to generate SW
delay by means of ECT.
* - Generates only one impulse given length after the delay of given length
* - The hard delay, which means the CPU does not work on another task, is presented.
* It waits in loop.
*
* - Theory:
*
* interval = TCx * dt = TCx * (prescaler/fbus)
* TCx = interval / (prescaller/fbus) = interval * fbus / prescaler
* There are two ways for TCx and prescaler setup:
* 1) prescaler = constant (min and max interval is given)
* 2) prescaler and TCx are considered as a variables to calculate required interval
* interval = f(TCx, prescaler)
* this solution is more difficult
*
*
* This example solves item 1.
* - Prescaler = const = 8.
* - fbus = 8MHz
* - TCx = interval / (prescaller/fbus) = interval * fbus / prescaler
*
* Max interval can be calculated as interval[us]=65535*prescaler/fbus[MHz]
* and setup precision is dt=prescaler/fbus
*
* for fbus = 8MHz
* If prescaler = 1 then max interval is 8191.8 us; dt = 0.125 us
* If prescaler = 2 then max interval is 16383,7 us; dt = 0.250 us
* If prescaler = 4 then max interval is 32767,5 us; dt = 0.500 us
* If prescaler = 8 then max interval is 65535 us; dt = 1.000 us
* If prescaler = 16 then max interval is 131070 us; dt = 2.000 us
* If prescaler = 32 then max interval is 262140 us; dt = 4.000 us
* If prescaler = 64 then max interval is 524280 us; dt = 8.000 us
* If prescaler = 128 then max interval is 1048560 us; dt = 16.000 us
*
* - tested on: HCS12X STARTER KIT
* - OSCCLK = 16MHz, BUSCLK = 8MHz
* - Reference to documentation: MC9S12XDP512V2 Rev.2.17
********************************************************************************
Revision History:
Version Date Author Description of Changes
1.0 Sep-15-2010 R62780 Initial version
*******************************************************************************/
#include <hidef.h> // common defines and macros
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"
#include "map.h"
/*******************************************************************************
* Local function prototypes
*******************************************************************************/
static void Delay(UWORD interval);
static void ECT_Init(void);
/*******************************************************************************
* Local variables
*******************************************************************************/
// NONE
/*******************************************************************************
Function Name : ECT_Init
Engineer : r62780
Date : Sep/15/2010
Parameters : NONE
Modifies : NONE
Returns : NONE
Notes : Sets ECT for required functionality
Issues : NONE
*******************************************************************************/
static void ECT_Init(void)
{
TSCR2 = 0x03; // prescaller = 8
//--- channel 0 setup ---------------
TCTL1 = 0x00; // timer disconnected form output pin logic
TIOS = 0x01; // channels 0 is output compare
//-----------------------------------
TIE_C0I = 0; // disable interrupt from channel 0
//-----------------------------------
TSCR1 = 0xF0; // enable timer,stop in wait,stop in freeze, fast flag clear
}
/*******************************************************************************
Function Name : Delay
Engineer : r62780
Date : Sep/15/2010
Parameters : UWORD interval
Modifies : NONE
Returns : NONE
Notes : Performs hard delay
Issues : NONE
*******************************************************************************/
static void Delay(UWORD interval)
{
TC0 = TCNT + interval; // set new value and clear interrupt flag
// TC0 = t + interval * fbus / prescaler;
while(!TFLG1_C0F);
}
/*******************************************************************************
Function Name : main
Engineer : r62780
Date : Sep/15/2010
Parameters : NONE
Modifies : NONE
Returns : NONE
Notes : Demonstrate SW delay
Issues : NONE
*******************************************************************************/
void main(void)
{
for(;;)
{
//----------------------------------
ECT_Init();
//----------------------------------
DDRB=0xFF; // an output port to visualize period
//----------------------------------
for(;;)
{
Delay(10000); // true delay is value 500*prescaller/busclk=
// = 10000*8/8MHz = 10000us = 10ms
PORTB = ~PORTB;
}
}
}
/*******************************************************************************
The Answer to the Ultimate Question of Life, the Universe, and Everything is:.......... 42.
(Douglas Adams - The Hitchhiker's Guide to the Galaxy)
*******************************************************************************/
Here is an example for similar MCU with a lot of description:
/*******************************************************************************
* Freescale Semiconductor Inc.
* (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
* ALL RIGHTS RESERVED.
********************************************************************************
Services performed by FREESCALE in this matter are performed AS IS and without
any warranty. CUSTOMER retains the final decision relative to the total design
and functionality of the end product. FREESCALE neither guarantees nor will be
held liable by CUSTOMER for the success of this project.
FREESCALE DISCLAIMS ALL WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY INCLUDING,
BUT NOT LIMITED TO, IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
A PARTICULAR PURPOSE ON ANY HARDWARE, SOFTWARE OR ADVISE SUPPLIED TO THE PROJECT
BY FREESCALE, AND OR NAY PRODUCT RESULTING FROM FREESCALE SERVICES. IN NO EVENT
SHALL FREESCALE BE LIABLE FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
THIS AGREEMENT.
CUSTOMER agrees to hold FREESCALE harmless against any and all claims demands or
actions by anyone on account of any damage, or injury, whether commercial,
contractual, or tortuous, rising directly or indirectly as a result of an advise
or assistance supplied CUSTOMER in connection with product, services or goods
supplied under this Agreement.
********************************************************************************
* File main.c
* Owner r62780
* Version 1.0
* Date Sep-15-2010
* Classification General Business Information
* Brief FLASH E/W
********************************************************************************
* Detailed Description:
*
* - the SW software demonstrates one of the possibilities how to generate SW
delay by means of ECT.
* - Generates only one impulse given length after the delay of given length
* - The hard delay, which means the CPU does not work on another task, is presented.
* It waits in loop.
*
* - Theory:
*
* interval = TCx * dt = TCx * (prescaler/fbus)
* TCx = interval / (prescaller/fbus) = interval * fbus / prescaler
* There are two ways for TCx and prescaler setup:
* 1) prescaler = constant (min and max interval is given)
* 2) prescaler and TCx are considered as a variables to calculate required interval
* interval = f(TCx, prescaler)
* this solution is more difficult
*
*
* This example solves item 1.
* - Prescaler = const = 8.
* - fbus = 8MHz
* - TCx = interval / (prescaller/fbus) = interval * fbus / prescaler
*
* Max interval can be calculated as interval[us]=65535*prescaler/fbus[MHz]
* and setup precision is dt=prescaler/fbus
*
* for fbus = 8MHz
* If prescaler = 1 then max interval is 8191.8 us; dt = 0.125 us
* If prescaler = 2 then max interval is 16383,7 us; dt = 0.250 us
* If prescaler = 4 then max interval is 32767,5 us; dt = 0.500 us
* If prescaler = 8 then max interval is 65535 us; dt = 1.000 us
* If prescaler = 16 then max interval is 131070 us; dt = 2.000 us
* If prescaler = 32 then max interval is 262140 us; dt = 4.000 us
* If prescaler = 64 then max interval is 524280 us; dt = 8.000 us
* If prescaler = 128 then max interval is 1048560 us; dt = 16.000 us
*
* - tested on: HCS12X STARTER KIT
* - OSCCLK = 16MHz, BUSCLK = 8MHz
* - Reference to documentation: MC9S12XDP512V2 Rev.2.17
********************************************************************************
Revision History:
Version Date Author Description of Changes
1.0 Sep-15-2010 R62780 Initial version
*******************************************************************************/
#include <hidef.h> // common defines and macros
#pragma LINK_INFO DERIVATIVE "mc9s12xdp512"
#include "map.h"
/*******************************************************************************
* Local function prototypes
*******************************************************************************/
static void Delay(UWORD interval);
static void ECT_Init(void);
/*******************************************************************************
* Local variables
*******************************************************************************/
// NONE
/*******************************************************************************
Function Name : ECT_Init
Engineer : r62780
Date : Sep/15/2010
Parameters : NONE
Modifies : NONE
Returns : NONE
Notes : Sets ECT for required functionality
Issues : NONE
*******************************************************************************/
static void ECT_Init(void)
{
TSCR2 = 0x03; // prescaller = 8
//--- channel 0 setup ---------------
TCTL1 = 0x00; // timer disconnected form output pin logic
TIOS = 0x01; // channels 0 is output compare
//-----------------------------------
TIE_C0I = 0; // disable interrupt from channel 0
//-----------------------------------
TSCR1 = 0xF0; // enable timer,stop in wait,stop in freeze, fast flag clear
}
/*******************************************************************************
Function Name : Delay
Engineer : r62780
Date : Sep/15/2010
Parameters : UWORD interval
Modifies : NONE
Returns : NONE
Notes : Performs hard delay
Issues : NONE
*******************************************************************************/
static void Delay(UWORD interval)
{
TC0 = TCNT + interval; // set new value and clear interrupt flag
// TC0 = t + interval * fbus / prescaler;
while(!TFLG1_C0F);
}
/*******************************************************************************
Function Name : main
Engineer : r62780
Date : Sep/15/2010
Parameters : NONE
Modifies : NONE
Returns : NONE
Notes : Demonstrate SW delay
Issues : NONE
*******************************************************************************/
void main(void)
{
for(;;)
{
//----------------------------------
ECT_Init();
//----------------------------------
DDRB=0xFF; // an output port to visualize period
//----------------------------------
for(;;)
{
Delay(10000); // true delay is value 500*prescaller/busclk=
// = 10000*8/8MHz = 10000us = 10ms
PORTB = ~PORTB;
}
}
}
/*******************************************************************************
The Answer to the Ultimate Question of Life, the Universe, and Everything is:.......... 42.
(Douglas Adams - The Hitchhiker's Guide to the Galaxy)
*******************************************************************************/