S12ZVL-TIM-FrequencyMeasurement-V1_0-CW106-TIC-EXAMPLE

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S12ZVL-TIM-FrequencyMeasurement-V1_0-CW106-TIC-EXAMPLE

S12ZVL-TIM-FrequencyMeasurement-V1_0-CW106-TIC-EXAMPLE

 *******************************************************************************
 * File              main.c
 * Owner             LaMa-TIC-RPR
 * Version           1.1
 * Date              Jun/17/2016
 * Classification    General Business Information
 * Brief             S12Z Frequency Measurement at PT0 (IOC0_0) by TIM module
 *                   in input capture mode
 *******************************************************************************
 * Detailed Description:
 * The code measures frequency at PT0
 * It demonstrates
 * - It measures frequency at PT0 by means of TIM module IOC0_0.
 * - The measurement is sensitive on rising edges so duty cycle has no affect
 *   to the measurement
 * - Constants:
 *           BUSCLOCK
 *           MAX_OVERFLOWS
 *           TIMER_PRESCALLER
 *      must be defined
 * - MAX_OVERFLOWS determines minimum measurable frequency.
 *   It is max. allowed number of overflows. When overflows counter reaches
 *   this value it the flag FREQUENCY_TOO_LOW is set. The message
 *   PERIOD BETWEEN TWO EDGES IS TO WIDE is then written to a variable "str"
 *   This value gives time interval:
 *     T = TIMER_PRESCALER * MAX_OVERFLOWS * 65536 / BUSCLOCK    
 *     For example:
 *      - TIMER_PRESCALER = 1
 *      - MAX_OVERFLOWS   = 1000
 *      - BUSCLOCK        = 16,000,000Hz
 *      T = 1 * 1000 * 65536 / 16,000,000 = 4,096s  => f=0,244140625Hz
 * - TIMER_PRESCALER = { 1,2,4,8,16,32,64,128} - smaller values give more
 *   precise values but it generates timer overflow more frequently
 * - the physical representation of the measured frequency is stored in the
 *   string "str"
 *
 * - The measurement precision is df which can be expressed as:
 *
 *    df = BUSCLK / ((n^2 + n) * PRESCALER); n = measured number of TCNT periods
 *             
 *   For example: BUSCLK = 16MHz, n = 399, PRESCALER = 1
 *   
 *   
 *             df = 16,000,000 / ((399^2 + 399) * 1) = 100.25Hz
 *             
 *             so, count 399 means f=BUSCL/399=16,000,000/399 = 40,100.25Hz
 *             so, count 400 means f=BUSCL/400=16,000,000/399 = 40,000.00Hz
 *                 
 * So this method is not suitable for large frequencies as can be seen in
 * following table (percentual error we can get is df/f):
 * n   f[Hz]      df[Hz]                        n   f[Hz] df[Hz]
 * 1   8000000   5333333,3           101 1553,0 30,1
 * 2   2666666,6 1333333,3           102 1522,9 29,2
 * 3   1333333,3  533333,3           103 1493,6 28,4
 * 4    800000    266666,6             104 1465,2 27,6
 * 5    533333,3  152380,9            105 1437,5 26,8
 * 6    380952,3   95238,0             106 1410,6 26,1
 * 7    285714,2   63492,0             107 1384,5 25,4
 * 8    222222,2   44444,4             108 1359,1 24,7
 * 9    177777,7   32323,2             109 1334,4 24,0
 * 10   145454,5   24242,4            110 1310,4 23,4
 * 11   121212,1   18648,0            111 1287,0 22,7
 * 12   102564,1   14652,0            112 1264,2 22,1
 * 13    87912,0   11721,6             113 1242,0 21,6
 * 14    76190,4    9523,8             114 1220,4 21,0
 * 15    66666,6    7843,1             115 1199,40,502
 * 16    58823,5    6535,9             116 1178,8 19,9
 * 17    52287,5    5503,9             117 1158,9 19,4
 * 18    46783,6    4678,3             118 1139,4 18,9
 * 19    42105,2    4010,0             119 1120,4 18,5
 * 20    38095,2    3463,2             120 1101,9 18,0
 * 21    34632,0    3011,4             121 1083,8 17,6
 * 22    31620,5    2635,0             122 1066,2 17,1
 * 23    28985,5    2318,8             123 1049,0 16,7
 * 24    26666,6    2051,2             124 1032,2 16,3
 * 25    24615,3    1823,3             125 1015,8 15,9
 *
 * - PCB setup:
 *   - J35 is disconnected because measurement is done on PT0
 *   - pulse generator is connected to the J35 pin 1. (PT0)
 *  
 * - Reference to documentation: MC9S12ZVLMR1.pdf Rev. 1.02
 * - Tested on TRK-S12ZVL
 * - MCU MC9S12ZVL32 0N22G
 * - OSCCLK = 4MHz
 * - BUSCLK = 16MHz (set by PLL)
 *
 * The info about frequency and count is transmitted over the SCI0 Tx which is
 * routed (MODRR) to PS1 pin
 *
 *******************************************************************************
 Revision History:
 Version Date          Author            Description of Changes
 1.0     Jun/17/2016   LaMa-TIC-RPR      Initial version
 ******************************************************************************/

Labels (1)
Attachments
No ratings
Version history
Last update:
‎09-06-2016 04:07 AM
Updated by: