<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic input capture example code for S12ZVH? in S12 / MagniV Microcontrollers</title>
    <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759685#M15205</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying the measure the frequency from the IOC1 or IOC0. Is there any example code or project for this purpose?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 04 Jan 2018 08:25:33 GMT</pubDate>
    <dc:creator>yunuskara</dc:creator>
    <dc:date>2018-01-04T08:25:33Z</dc:date>
    <item>
      <title>input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759685#M15205</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying the measure the frequency from the IOC1 or IOC0. Is there any example code or project for this purpose?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jan 2018 08:25:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759685#M15205</guid>
      <dc:creator>yunuskara</dc:creator>
      <dc:date>2018-01-04T08:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759686#M15206</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You may refer to the&amp;nbsp;document below.&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.nxp.com/docs/DOC-332554"&gt;S12ZVL-TIM-FrequencyMeasurement-V1_0-CW106-TIC-EXAMPLE&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jan 2018 13:46:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759686#M15206</guid>
      <dc:creator>danielmartynek</dc:creator>
      <dc:date>2018-01-04T13:46:43Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759687#M15207</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;Hello Daniel. Firstly, thank you very much for your replay.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I tried to import the code to my project. But I couldnt make it work. I cant see an interrupt on TIM0_Channel0_ISR(). &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Can you pleae have a look on my code? I tested the code on TRK-S12ZVH128 board.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;My main file is as follows.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/****************************************************************************** &lt;BR /&gt;* S12ZVH128 Cluster Reference Design * &lt;BR /&gt;* * &lt;BR /&gt;* Created on: 05/03/2012 * &lt;BR /&gt;* Author: B34981 * &lt;BR /&gt;* Version: 0.2 * &lt;BR /&gt;* * &lt;BR /&gt;* This Reference Design resembles a Low-End cluster with the following * &lt;BR /&gt;* features: * &lt;BR /&gt;* - 160 Segment LCD * &lt;BR /&gt;* - 4 Stepper motors with Return-to-Zero detection * &lt;BR /&gt;* - Real Time Counter on LCD * &lt;BR /&gt;* - Dynamic backlighting (dimmable from Analog Input) * &lt;BR /&gt;* - CAN Communications * &lt;BR /&gt;* - Simple Sound Generation * &lt;BR /&gt;* * &lt;BR /&gt;******************************************************************************/ &lt;BR /&gt;#include "derivative.h" /* include peripheral declarations */&lt;BR /&gt;int kk = 0, ii = 0;&lt;BR /&gt;#define BUSCLOCK 4000000UL // Hz&lt;BR /&gt;#define TIMER_PRESCALER 1 // 1,2,4,8,16,32,64,128&lt;BR /&gt;#define MAX_OVERFLOWS 1000L // max. allowed number of TIM overflows&lt;BR /&gt;#define NEW_VALUE_PREPARED Flags.bit.f0 // info for sending data&lt;BR /&gt;#define FREQUENCY_TOO_LOW Flags.bit.f1 // mesured frequency is lower thannot measurablelower than&lt;BR /&gt;#define FIRST_EDGE 1&lt;BR /&gt;#define SECOND_EDGE 0&lt;BR /&gt;#define STOP_MEASUREMENT 3&lt;/P&gt;&lt;P&gt;//----- DEPENDENT DEFINITIONS -----&lt;BR /&gt;#if TIMER_PRESCALER==1&lt;BR /&gt;#define PRESCALER 0&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==2&lt;BR /&gt;#define PRESCALER 1&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==4&lt;BR /&gt;#define PRESCALER 2&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==8&lt;BR /&gt;#define PRESCALER 3&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==16&lt;BR /&gt;#define PRESCALER 4&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==32&lt;BR /&gt;#define PRESCALER 5&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==64&lt;BR /&gt;#define PRESCALER 6&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==128&lt;BR /&gt;#define PRESCALER 7&lt;BR /&gt;#endif&lt;/P&gt;&lt;P&gt;#define SET_INTERRUPT_PRIORITY(vec_off, priority) \&lt;BR /&gt; INT_CFADDR= (vec_off/4) &amp;amp; 0xF8; \&lt;BR /&gt; INT_CFDATA_ARR[(vec_off/4) &amp;amp; 0x03]= (priority);&lt;/P&gt;&lt;P&gt;#define TIM0_Channel0_VEC_OFF 0x1CC&lt;BR /&gt;#define TIM0_Overflow_VEC_OFF 0x1AC&lt;/P&gt;&lt;P&gt;//interrupt void TIM0_Channel0_ISR(void); // 0x1CC&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/*******************************************************************************&lt;BR /&gt; * Local types&lt;BR /&gt; ******************************************************************************/&lt;BR /&gt;typedef unsigned char UBYTE;&lt;BR /&gt;typedef char SBYTE;&lt;BR /&gt;typedef unsigned int UWORD;&lt;BR /&gt;typedef int SWORD;&lt;BR /&gt;typedef unsigned long ULONG;&lt;BR /&gt;typedef long SLONG;&lt;BR /&gt;typedef union Flags&lt;BR /&gt;{&lt;BR /&gt; UBYTE byte;&lt;BR /&gt; struct&lt;BR /&gt; {&lt;BR /&gt; UBYTE f0 :1;&lt;BR /&gt; UBYTE f1 :1;&lt;BR /&gt; UBYTE f2 :1;&lt;BR /&gt; UBYTE f3 :1;&lt;BR /&gt; UBYTE f4 :1;&lt;BR /&gt; UBYTE f5 :1;&lt;BR /&gt; UBYTE f6 :1;&lt;BR /&gt; UBYTE f7 :1;&lt;BR /&gt; } bit;&lt;BR /&gt;} FLAGS;&lt;BR /&gt;/*******************************************************************************&lt;BR /&gt; * Local function prototypes&lt;BR /&gt; ******************************************************************************/&lt;BR /&gt;static void CPMU_Init( void );&lt;BR /&gt;static void Delay( ULONG delay );&lt;BR /&gt;static void TIM0_Init( void ); // timer module initialization&lt;BR /&gt;static void SendValue( void );&lt;BR /&gt;void SCI0_Init(void);&lt;BR /&gt;void SCI0_Send_String(SBYTE *ptr);&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;BR /&gt; * Local variables&lt;BR /&gt; ******************************************************************************/&lt;/P&gt;&lt;P&gt;static ULONG period; // calcualted period between two rising edges&lt;BR /&gt;static UWORD ch0ovfCnt; // the number of main timer overflows between &lt;BR /&gt;// rising edges&lt;BR /&gt;static UWORD tFirstEdge = 0; // value of the timer when period starts&lt;BR /&gt;static UWORD tSecondEdge= 0; // value of the timer when period finishes&lt;/P&gt;&lt;P&gt;static UBYTE mPhase = STOP_MEASUREMENT;&lt;/P&gt;&lt;P&gt;volatile FLAGS Flags; // general purpose flags&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;interrupt VectorNumber_Vtim0ch0 void TIM0_Channel0_ISR( void )&lt;BR /&gt;{&lt;BR /&gt; unsigned int ttt, tof;&lt;/P&gt;&lt;P&gt;PTP_PTP0 = 1;&lt;BR /&gt; PTP_PTP0 = 0;&lt;/P&gt;&lt;P&gt;tof = TIM0TFLG2_TOF; // check whether additional TOF appeared&lt;BR /&gt; ttt = TIM0TC0; // get TCNT value&lt;/P&gt;&lt;P&gt;if( mPhase == FIRST_EDGE )&lt;BR /&gt; {&lt;BR /&gt; // what happens if ovfs happened while going to this interrupt?...solution&lt;BR /&gt; if( tof )&lt;BR /&gt; { // a new period has already started&lt;BR /&gt; if( ttt &amp;lt; 32768 ) TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt; TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt; }&lt;BR /&gt; ch0ovfCnt = 0; // reset overflows counter&lt;BR /&gt; //--------------------------&lt;BR /&gt; tFirstEdge = ttt; // get first edge value&lt;BR /&gt; mPhase = SECOND_EDGE; // set configuration to get second edge&lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; return; // go back to wait for second edge&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; // what happens if ovfs happened while going to this interrupt?...solution&lt;BR /&gt; if( tof )&lt;BR /&gt; { // a new period has already started&lt;BR /&gt; if( ttt &amp;lt; 32768 ) ch0ovfCnt++; &lt;BR /&gt; }&lt;BR /&gt; //--------------------------&lt;/P&gt;&lt;P&gt;tSecondEdge = ttt; // read new captured value and clear flag&lt;BR /&gt; // stop measurement&lt;BR /&gt; TIM0TSCR2_TOI = 0; // disable ovf interrupt&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; // Ch0 period calculation&lt;BR /&gt; if( !ch0ovfCnt ) // Did edges appear within one timer period?&lt;BR /&gt; {&lt;BR /&gt; period = ( ULONG ) ( tSecondEdge - tFirstEdge );&lt;BR /&gt; }&lt;BR /&gt; else // some number of overflows has appeared between two edges&lt;BR /&gt; {&lt;BR /&gt; // time given by overflows period=....&lt;BR /&gt; period = ( ULONG ) ( ~tFirstEdge + 1 ); // time in the first period&lt;BR /&gt; period += ( ULONG ) ( tSecondEdge ); // time in the last period&lt;BR /&gt; period += ( ULONG ) (( ULONG ) ( ch0ovfCnt - 1 ) * ( ULONG ) ( 65536 ));&lt;BR /&gt; }&lt;BR /&gt; NEW_VALUE_PREPARED = TRUE; // information for main routine &lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;interrupt VectorNumber_Vtim0ovf void TIM0_Overflow_ISR( void )&lt;BR /&gt;{&lt;BR /&gt; ch0ovfCnt++; // count overflows&lt;/P&gt;&lt;P&gt;if( ch0ovfCnt &amp;gt; MAX_OVERFLOWS )&lt;BR /&gt; {&lt;BR /&gt; ch0ovfCnt = 0;&lt;BR /&gt; FREQUENCY_TOO_LOW = TRUE;&lt;BR /&gt; // stop measurement&lt;BR /&gt; TIM0TSCR2_TOI = 0; // disable ovf interrupt&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; NEW_VALUE_PREPARED = TRUE; // information for main routine &lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; FREQUENCY_TOO_LOW = FALSE;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;static void TIM0_Init( void )&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;PERT_PERT0 = 1; // an internal pull resitor enable&lt;BR /&gt; PPST_PPST0 = 0; // an internal pull up connected&lt;/P&gt;&lt;P&gt;TIM0TSCR2 = PRESCALER; // tim disabled; prsc=1&lt;BR /&gt; TIM0TCTL4 = 0B00000001; // channel 0 inp. capture on rising edge&lt;BR /&gt; TIM0TSCR2_TOI = 0; // stop ovf interrupt counter&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TSCR1 = 0B01100000; // enable timer, stop in freeze mode&lt;BR /&gt; // stop in wait mode&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;static void SendValue( void )&lt;BR /&gt;{&lt;BR /&gt; if( FREQUENCY_TOO_LOW ) // Have overflows counter reached maximum value?&lt;BR /&gt; {&lt;BR /&gt; ODO_Mode(000); // print zero to LCD if period is too low&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; // calculate time between two edges&lt;BR /&gt; //f = period / BUSCLOCK;&lt;BR /&gt; ODO_Mode(period);&lt;BR /&gt; }&lt;BR /&gt; //----------------------------&lt;BR /&gt; //----------------------------&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // clear flag = value has been sent&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;void main(void) {&lt;BR /&gt; &lt;BR /&gt; EnableInterrupts;&lt;BR /&gt; &lt;BR /&gt; CPMUCOP_CR = 0x0; /* Disable COP */&lt;/P&gt;&lt;P&gt;PLL_Init(); /* Initialise the PLL to increase operating frequency */&lt;BR /&gt; GPIO_Init(); /* Configures inputs, outputs, pull-ups and KBI interrupts*/&lt;BR /&gt; LCD_Init(); /* Configures the LCD controller for 4x40 operation */&lt;BR /&gt; TIM1_Init(); /* Configures one TIM periodic interrupt for stepper control */&lt;BR /&gt; //Scheduler_Init(); /* Execute the Scheduler... never returns */&lt;BR /&gt; &lt;BR /&gt; //=== GPIO INIT ================ for input capture&lt;BR /&gt; DDRT_DDRT0 = 0; // PT0 input port&lt;BR /&gt; PERT_PERT0 = 1; // Pull resistor enabled at PT0&lt;BR /&gt; PPST_PPST0 = 1; // Pull down selected at PT0&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; //=== TIM0 INIT ================&lt;BR /&gt; FREQUENCY_TOO_LOW = TRUE; // set flag&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // set flag&lt;BR /&gt; SET_INTERRUPT_PRIORITY(TIM0_Channel0_VEC_OFF, 7); // max priority&lt;BR /&gt; SET_INTERRUPT_PRIORITY(TIM0_Overflow_VEC_OFF, 6); // less priority than IC0&lt;BR /&gt; TIM0_Init(); // timer and channels initialization&lt;BR /&gt; //=== ENABLE INTERRUPTS ========&lt;BR /&gt; TIM0TSCR2_TOI = 1; // start ovf interrupt counter&lt;BR /&gt; TIM0TIE_C0I = 1; // enable Ch0 interrupt &lt;BR /&gt; EnableInterrupts;&lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt; for( ;; )&lt;BR /&gt; {&lt;BR /&gt; // start measurement&lt;BR /&gt; FREQUENCY_TOO_LOW = FALSE; // clear flag&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // set flag&lt;BR /&gt; ch0ovfCnt = 0; // clear number of ovfs interrupts&lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; TIM0TFLG2 = 0B10000000; // clear interrupt TOV flag&lt;BR /&gt; mPhase = FIRST_EDGE; // prepare processing of a first edge&lt;BR /&gt; TIM0TSCR1_TEN = 1; // run timer&lt;BR /&gt; TIM0TIE_C0I = 1; // enable Ch0 interrupt &lt;BR /&gt; TIM0TSCR2_TOI = 1; // enable ovf interrupt &lt;BR /&gt; &lt;BR /&gt; while( !NEW_VALUE_PREPARED ) // wait for measurement &lt;BR /&gt; {&lt;BR /&gt; }&lt;BR /&gt; SendValue(); // send measured value&lt;BR /&gt; &lt;BR /&gt; asm nop; // only for debug purposes for brkpnt&lt;BR /&gt; &lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jan 2018 08:32:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759687#M15207</guid>
      <dc:creator>yunuskara</dc:creator>
      <dc:date>2018-01-09T08:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759688#M15208</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code is not complete, PLL_Init and GPIO_Init functions are called but not defined.&lt;/P&gt;&lt;P&gt;You want to use PT0 as input (Timer1, ch0) so you call&amp;nbsp;&lt;SPAN style="color: #51626f; background-color: #ffffff;"&gt;TIM1_Init() but only&amp;nbsp;&lt;SPAN&gt;TIM0_Init() is defined.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff;"&gt;&lt;SPAN&gt;Also, you have interrupt routines only for Timer 0, not for Timer1.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff;"&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #51626f; background-color: #ffffff;"&gt;&lt;SPAN&gt;Daniel&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jan 2018 14:33:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759688#M15208</guid>
      <dc:creator>danielmartynek</dc:creator>
      <dc:date>2018-01-09T14:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759689#M15209</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;Hi again,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="background-color: #ffffff; color: #51626f;"&gt;PLL_Init and GPIO_Init were defined in another c file. I moved them to main.c. Also added timer1.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="background-color: #ffffff; color: #51626f;"&gt;Can you please have a look on this revised file. Thank you very much.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;/****************************************************************************** &lt;BR /&gt;* S12ZVH128 Cluster Reference Design * &lt;BR /&gt;* * &lt;BR /&gt;* Created on: 05/03/2012 * &lt;BR /&gt;* Author: B34981 * &lt;BR /&gt;* Version: 0.2 * &lt;BR /&gt;* * &lt;BR /&gt;* This Reference Design resembles a Low-End cluster with the following * &lt;BR /&gt;* features: * &lt;BR /&gt;* - 160 Segment LCD * &lt;BR /&gt;* - 4 Stepper motors with Return-to-Zero detection * &lt;BR /&gt;* - Real Time Counter on LCD * &lt;BR /&gt;* - Dynamic backlighting (dimmable from Analog Input) * &lt;BR /&gt;* - CAN Communications * &lt;BR /&gt;* - Simple Sound Generation * &lt;BR /&gt;* * &lt;BR /&gt;******************************************************************************/ &lt;BR /&gt;#include "derivative.h" /* include peripheral declarations */&lt;BR /&gt;int kk = 0, ii = 0;&lt;/P&gt;&lt;P&gt;UINT16 u16_testPer = 1200;&lt;/P&gt;&lt;P&gt;#define BUSCLOCK 16000000UL // Hz&lt;BR /&gt;#define TIMER_PRESCALER 1 // 1,2,4,8,16,32,64,128&lt;BR /&gt;#define MAX_OVERFLOWS 1000L // max. allowed number of TIM overflows&lt;BR /&gt;#define NEW_VALUE_PREPARED Flags.bit.f0 // info for sending data&lt;BR /&gt;#define FREQUENCY_TOO_LOW Flags.bit.f1 // mesured frequency is lower thannot measurablelower than&lt;BR /&gt;#define FIRST_EDGE 1&lt;BR /&gt;#define SECOND_EDGE 0&lt;BR /&gt;#define STOP_MEASUREMENT 3&lt;/P&gt;&lt;P&gt;//----- DEPENDENT DEFINITIONS -----&lt;BR /&gt;#if TIMER_PRESCALER==1&lt;BR /&gt;#define PRESCALER 0&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==2&lt;BR /&gt;#define PRESCALER 1&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==4&lt;BR /&gt;#define PRESCALER 2&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==8&lt;BR /&gt;#define PRESCALER 3&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==16&lt;BR /&gt;#define PRESCALER 4&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==32&lt;BR /&gt;#define PRESCALER 5&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==64&lt;BR /&gt;#define PRESCALER 6&lt;BR /&gt;#endif&lt;BR /&gt;#if TIMER_PRESCALER==128&lt;BR /&gt;#define PRESCALER 7&lt;BR /&gt;#endif&lt;/P&gt;&lt;P&gt;#define SET_INTERRUPT_PRIORITY(vec_off, priority) \&lt;BR /&gt; INT_CFADDR= (vec_off/4) &amp;amp; 0xF8; \&lt;BR /&gt; INT_CFDATA_ARR[(vec_off/4) &amp;amp; 0x03]= (priority);&lt;/P&gt;&lt;P&gt;#define TIM0_Channel0_VEC_OFF 0x1CC&lt;BR /&gt;#define TIM0_Overflow_VEC_OFF 0x1AC&lt;/P&gt;&lt;P&gt;//interrupt void TIM0_Channel0_ISR(void); // 0x1CC&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;/*******************************************************************************&lt;BR /&gt; * Local types&lt;BR /&gt; ******************************************************************************/&lt;BR /&gt;typedef unsigned char UBYTE;&lt;BR /&gt;typedef char SBYTE;&lt;BR /&gt;typedef unsigned int UWORD;&lt;BR /&gt;typedef int SWORD;&lt;BR /&gt;typedef unsigned long ULONG;&lt;BR /&gt;typedef long SLONG;&lt;BR /&gt;typedef union Flags&lt;BR /&gt;{&lt;BR /&gt; UBYTE byte;&lt;BR /&gt; struct&lt;BR /&gt; {&lt;BR /&gt; UBYTE f0 :1;&lt;BR /&gt; UBYTE f1 :1;&lt;BR /&gt; UBYTE f2 :1;&lt;BR /&gt; UBYTE f3 :1;&lt;BR /&gt; UBYTE f4 :1;&lt;BR /&gt; UBYTE f5 :1;&lt;BR /&gt; UBYTE f6 :1;&lt;BR /&gt; UBYTE f7 :1;&lt;BR /&gt; } bit;&lt;BR /&gt;} FLAGS;&lt;BR /&gt;/*******************************************************************************&lt;BR /&gt; * Local function prototypes&lt;BR /&gt; ******************************************************************************/&lt;BR /&gt;static void CPMU_Init( void );&lt;BR /&gt;static void Delay( ULONG delay );&lt;BR /&gt;static void TIM0_Init( void ); // timer module initialization&lt;BR /&gt;static void SendValue( void );&lt;BR /&gt;void SCI0_Init(void);&lt;BR /&gt;void SCI0_Send_String(SBYTE *ptr);&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;BR /&gt; * Local variables&lt;BR /&gt; ******************************************************************************/&lt;/P&gt;&lt;P&gt;static ULONG period; // calcualted period between two rising edges&lt;BR /&gt;static UWORD ch0ovfCnt; // the number of main timer overflows between &lt;BR /&gt;// rising edges&lt;BR /&gt;static UWORD tFirstEdge = 0; // value of the timer when period starts&lt;BR /&gt;static UWORD tSecondEdge= 0; // value of the timer when period finishes&lt;/P&gt;&lt;P&gt;static UBYTE mPhase = STOP_MEASUREMENT;&lt;/P&gt;&lt;P&gt;volatile FLAGS Flags; // general purpose flags&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;interrupt VectorNumber_Vtim0ch0 void TIM0_Channel0_ISR( void )&lt;BR /&gt;{&lt;BR /&gt; unsigned int ttt, tof;&lt;/P&gt;&lt;P&gt;PTP_PTP0 = 1;&lt;BR /&gt; PTP_PTP0 = 0;&lt;/P&gt;&lt;P&gt;tof = TIM0TFLG2_TOF; // check whether additional TOF appeared&lt;BR /&gt; ttt = TIM0TC0; // get TCNT value&lt;/P&gt;&lt;P&gt;if( mPhase == FIRST_EDGE )&lt;BR /&gt; {&lt;BR /&gt; // what happens if ovfs happened while going to this interrupt?...solution&lt;BR /&gt; if( tof )&lt;BR /&gt; { // a new period has already started&lt;BR /&gt; if( ttt &amp;lt; 32768 ) TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt; TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt; }&lt;BR /&gt; ch0ovfCnt = 0; // reset overflows counter&lt;BR /&gt; //--------------------------&lt;BR /&gt; tFirstEdge = ttt; // get first edge value&lt;BR /&gt; mPhase = SECOND_EDGE; // set configuration to get second edge&lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; return; // go back to wait for second edge&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; // what happens if ovfs happened while going to this interrupt?...solution&lt;BR /&gt; if( tof )&lt;BR /&gt; { // a new period has already started&lt;BR /&gt; if( ttt &amp;lt; 32768 ) ch0ovfCnt++; &lt;BR /&gt; }&lt;BR /&gt; //--------------------------&lt;/P&gt;&lt;P&gt;tSecondEdge = ttt; // read new captured value and clear flag&lt;BR /&gt; // stop measurement&lt;BR /&gt; TIM0TSCR2_TOI = 0; // disable ovf interrupt&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; // Ch0 period calculation&lt;BR /&gt; if( !ch0ovfCnt ) // Did edges appear within one timer period?&lt;BR /&gt; {&lt;BR /&gt; period = ( ULONG ) ( tSecondEdge - tFirstEdge );&lt;BR /&gt; }&lt;BR /&gt; else // some number of overflows has appeared between two edges&lt;BR /&gt; {&lt;BR /&gt; // time given by overflows period=....&lt;BR /&gt; period = ( ULONG ) ( ~tFirstEdge + 1 ); // time in the first period&lt;BR /&gt; period += ( ULONG ) ( tSecondEdge ); // time in the last period&lt;BR /&gt; period += ( ULONG ) (( ULONG ) ( ch0ovfCnt - 1 ) * ( ULONG ) ( 65536 ));&lt;BR /&gt; }&lt;BR /&gt; NEW_VALUE_PREPARED = TRUE; // information for main routine &lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;interrupt VectorNumber_Vtim0ovf void TIM0_Overflow_ISR( void )&lt;BR /&gt;{&lt;BR /&gt; ch0ovfCnt++; // count overflows&lt;/P&gt;&lt;P&gt;if( ch0ovfCnt &amp;gt; MAX_OVERFLOWS )&lt;BR /&gt; {&lt;BR /&gt; ch0ovfCnt = 0;&lt;BR /&gt; FREQUENCY_TOO_LOW = TRUE;&lt;BR /&gt; // stop measurement&lt;BR /&gt; TIM0TSCR2_TOI = 0; // disable ovf interrupt&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; NEW_VALUE_PREPARED = TRUE; // information for main routine &lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; FREQUENCY_TOO_LOW = FALSE;&lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;TIM0TFLG2 = 0B10000000; // clear ovf interrupt flag&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;static void TIM0_Init( void )&lt;BR /&gt;{&lt;/P&gt;&lt;P&gt;PERT_PERT0 = 1; // an internal pull resitor enable&lt;BR /&gt; PPST_PPST0 = 0; // an internal pull up connected&lt;/P&gt;&lt;P&gt;TIM0TSCR2 = PRESCALER; // tim disabled; prsc=1&lt;BR /&gt; TIM0TCTL4 = 0B00000001; // channel 0 inp. capture on rising edge&lt;BR /&gt; TIM0TSCR2_TOI = 0; // stop ovf interrupt counter&lt;BR /&gt; TIM0TIE_C0I = 0; // disable Ch0 interrupt &lt;BR /&gt; TIM0TSCR1 = 0B01100000; // enable timer, stop in freeze mode&lt;BR /&gt; // stop in wait mode&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;static void SendValue( void )&lt;BR /&gt;{&lt;BR /&gt; if( FREQUENCY_TOO_LOW ) // Have overflows counter reached maximum value?&lt;BR /&gt; {&lt;BR /&gt; ODO_Mode(000); // print zero to LCD if period is too low&lt;BR /&gt; }&lt;BR /&gt; else&lt;BR /&gt; {&lt;BR /&gt; // calculate time between two edges&lt;BR /&gt; //f = period / BUSCLOCK;&lt;BR /&gt; ODO_Mode(period);&lt;BR /&gt; }&lt;BR /&gt; //----------------------------&lt;BR /&gt; //----------------------------&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // clear flag = value has been sent&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;/*******************************************************************************&lt;BR /&gt;* @brief PLL_Init - Clock configurations (32 MHz from 8 MHz OSC)&lt;BR /&gt;* @param none&lt;BR /&gt;* @return none&lt;BR /&gt;********************************************************************************/ &lt;BR /&gt;void PLL_Init(void)&lt;BR /&gt;{ &lt;BR /&gt; &lt;BR /&gt; //WD Disable...&lt;BR /&gt; &lt;BR /&gt; CPMUCLKS_PLLSEL = 0x1; // FBUS = FPLL/2. FBUS = 32MHz, &lt;BR /&gt; CPMUREFDIV_REFDIV = 0x1; // FREF = FOSC/(REFDIV+1) = 8/(1+1) = 4MHZ &lt;BR /&gt; CPMUREFDIV_REFFRQ = 0x1; // Reference clock between 2MHZ and 6MHZ. &lt;BR /&gt; CPMUSYNR_SYNDIV = 0x7; // FVCO = 2xFREFx(SYNDIV+1) = FVCO = 2x4x(7+1) = 64MHZ &lt;BR /&gt; CPMUSYNR_VCOFRQ = 0x1; // FVCO is between 48MHZ and 80MHZ &lt;BR /&gt; CPMUPOSTDIV_POSTDIV = 0x0; // FPLL = FVCO/(POSTDIV+1). FPLL = 64MHZ/(0+1) FPLL = 64MHz &lt;BR /&gt; CPMUOSC_OSCE = 0x1; // External oscillator enable. 8MHZ. FREF=FOSC/(REFDIV+1) &lt;BR /&gt; &lt;BR /&gt; while(!CPMUIFLG_LOCK){} // Wait for LOCK. &lt;BR /&gt; &lt;BR /&gt; CPMUIFLG = 0xFF; // clear CMPMU int flags - not needed but good practice &lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;void TIM1_Init(){&lt;BR /&gt; &lt;BR /&gt; TIM1TIOS_IOS1 = 1; // TIMCH0 as output compare&lt;BR /&gt; TIM1TIE_C1I = 1; // TIMCH0 interrupt enable&lt;BR /&gt; TIM1TFLG1_C1F = 1; // Clear interrupt flag&lt;BR /&gt; &lt;BR /&gt; TIM1TC1 = u16_testPer; // Interrupt period&lt;BR /&gt; &lt;BR /&gt; TIM1TSCR2_PR = 0; // Clock Prescaler FBUS/2^PR &lt;BR /&gt; TIM1TSCR1_PRNT = 1; // Precision Timer enable&lt;BR /&gt; TIM1TSCR1_TEN = 1; // TIM enable&lt;BR /&gt; &lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;void main(void) {&lt;BR /&gt; &lt;BR /&gt; //EnableInterrupts;&lt;BR /&gt; &lt;BR /&gt; CPMUCOP_CR = 0x0; /* Disable COP */&lt;/P&gt;&lt;P&gt;PLL_Init(); /* Initialise the PLL to increase operating frequency */&lt;BR /&gt; LCD_Init(); /* Configures the LCD controller for 4x40 operation */&lt;BR /&gt; TIM1_Init(); /* Configures one TIM periodic interrupt for stepper control */&lt;BR /&gt; //Scheduler_Init(); /* Execute the Scheduler... never returns */&lt;BR /&gt; &lt;BR /&gt; //=== GPIO INIT ================ for input capture&lt;BR /&gt; DDRT_DDRT0 = 0; // PT0 input port&lt;BR /&gt; PERT_PERT0 = 1; // Pull resistor enabled at PT0&lt;BR /&gt; PPST_PPST0 = 1; // Pull down selected at PT0&lt;/P&gt;&lt;P&gt;&lt;BR /&gt; //=== TIM0 INIT ================&lt;BR /&gt; FREQUENCY_TOO_LOW = TRUE; // set flag&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // set flag&lt;BR /&gt; SET_INTERRUPT_PRIORITY(TIM0_Channel0_VEC_OFF, 7); // max priority&lt;BR /&gt; SET_INTERRUPT_PRIORITY(TIM0_Overflow_VEC_OFF, 6); // less priority than IC0&lt;BR /&gt; TIM0_Init(); // timer and channels initialization&lt;BR /&gt; //=== ENABLE INTERRUPTS ========&lt;BR /&gt; TIM0TSCR2_TOI = 1; // start ovf interrupt counter&lt;BR /&gt; TIM0TIE_C0I = 1; // enable Ch0 interrupt &lt;BR /&gt; EnableInterrupts;&lt;BR /&gt; &lt;BR /&gt; &lt;BR /&gt; for( ;; )&lt;BR /&gt; {&lt;BR /&gt; // start measurement&lt;BR /&gt; FREQUENCY_TOO_LOW = FALSE; // clear flag&lt;BR /&gt; NEW_VALUE_PREPARED = FALSE; // set flag&lt;BR /&gt; ch0ovfCnt = 0; // clear number of ovfs interrupts&lt;BR /&gt; TIM0TFLG1 = 0B00000001; // clear interrupt flag from PT0&lt;BR /&gt; TIM0TFLG2 = 0B10000000; // clear interrupt TOV flag&lt;BR /&gt; mPhase = FIRST_EDGE; // prepare processing of a first edge&lt;BR /&gt; TIM0TSCR1_TEN = 1; // run timer&lt;BR /&gt; TIM0TIE_C0I = 1; // enable Ch0 interrupt &lt;BR /&gt; TIM0TSCR2_TOI = 1; // enable ovf interrupt &lt;BR /&gt; while( !NEW_VALUE_PREPARED ) // wait for measurement &lt;BR /&gt; {&lt;BR /&gt; }&lt;BR /&gt; SendValue(); // send measured value&lt;BR /&gt; &lt;BR /&gt; asm nop; // only for debug purposes for brkpnt&lt;BR /&gt; &lt;BR /&gt; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jan 2018 15:19:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759689#M15209</guid>
      <dc:creator>yunuskara</dc:creator>
      <dc:date>2018-01-09T15:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759690#M15210</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;IOC0_0 is a function of PT0 on S12ZVL but&amp;nbsp;PU0&amp;nbsp;on S12ZVH.&lt;/P&gt;&lt;P&gt;Do you measure the signal at PU0?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 09 Jan 2018 16:44:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759690#M15210</guid>
      <dc:creator>danielmartynek</dc:creator>
      <dc:date>2018-01-09T16:44:40Z</dc:date>
    </item>
    <item>
      <title>Re: input capture example code for S12ZVH?</title>
      <link>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759691#M15211</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you very much Daniel, that was my problem. I configured IOC1_0 and measure the signal at PT0. Thanks.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 10 Jan 2018 14:22:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S12-MagniV-Microcontrollers/input-capture-example-code-for-S12ZVH/m-p/759691#M15211</guid>
      <dc:creator>yunuskaramavus</dc:creator>
      <dc:date>2018-01-10T14:22:56Z</dc:date>
    </item>
  </channel>
</rss>

