Motorola SC140 User Manual

Page of 28
16
Using the SC140 Enhanced OnCE Stopwatch Timer
Code 9.   Testing Code
#include <stdio.h>
#include "EOnCE_stopwatch.h"
#ifdef COMPILER_BETA_1_BUG    
extern long ECNT_VAL;
#else    
#include "EOnCE_registers.h"
#endif
void PLL_setup_300MHz()
{
    asm("move.l #$80030003,PCTL0");
    asm("move.l #$00010000,PCTL1");
void main(){
    unsigned long clock_ext,clock_val,clock_cycle,cycle_req;
    unsigned long time_sec;
    extern unsigned long CLOCK_SPEED;
    PLL_setup_300MHz();
    EOnCE_stopwatch_timer_init();              /* Setup to event detector 1 for any write to 
                                                  dummy variable. Setup EOnCE event counter 
                                                  to count if event 1 happens */
    EOnCE_LED_init();                          /* Setup LED to toggle in detection of 
                                                  event 1 */
        
    cycle_req = CLOCK_SPEED*10;                /* Calculate total clock cycles required 
                                                  by 10 sec */
    EOnCE_stopwatch_timer_start();             /* Event 1 happens, counter & LED on */
    
    do {
        READ_IOREG(ECNT_VAL,clock_cycle);      /* Read bit 31-0 counter value */
        clock_cycle = MAX_32_BIT - clock_cycle;/* Minus max value due to count down */ 
    } while (clock_cycle <= cycle_req);
    
    EOnCE_stopwatch_timer_stop(&clock_ext, &clock_val);  /* Stop timer, return bit 63-0 
counter value */
    EOnCE_LED_off();                            /* LED off */
    time_sec = Convert_clock2time(clock_ext, clock_val, EONCE_SECOND);
    printf("duration = %u sec\n", time_sec);
    cycle_req = CLOCK_SPEED*7.5;              /* Calculate total clock cycles required 
                                                  by 7.5 sec */
    EOnCE_stopwatch_timer_start();            /* Event 1 happens, counter & LED on */
    
    do {
        READ_IOREG(ECNT_VAL,clock_cycle);      /* Read bit 31-0 counter value */
        clock_cycle = MAX_32_BIT - clock_cycle;/* Minus max value due to count down */ 
    } while (clock_cycle <= cycle_req);
    
    EOnCE_stopwatch_timer_stop(&clock_ext, &clock_val);  /* Stop timer, return bit 63-0 
counter value */
    EOnCE_LED_off();                           /* LED off */
    time_sec = Convert_clock2time(clock_ext, clock_val, EONCE_MILLISECOND);
    printf("duration = %u ms\n", time_sec);
    
    return;
}