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

📄 timer4.c

📁 Zigbee无线收发控制芯片JN5121C语言例程
💻 C
字号:
/****************************************************************************
 *
 * MODULE:             Timer2.c
 *
 * COMPONENT:          $RCSfile: $
 *
 * VERSION:            $Name: $
 *
 * REVISION:           $Revision: $
 *
 * DATED:              $Date: 30/06/06  $
 *
 * STATUS:             $State: $
 *
 * AUTHOR:             Gordon MacNee
 *
 * DESCRIPTION
 * code segment to demonstrate the operation of the timers
 * in Capture mode
 * We generate a pulse train with a M/S ratio set at compile time
 * with timer1 and feed this into timer0
 * When timer0 sees a H to L transition it will generate an interrupt
 * and the period of this high is sent to UART0
 * at 19K2 8 N 1
 *
 * CHANGE HISTORY:
 *
 * $Log: $
 *
 *
 * LAST MODIFIED BY:   $Author: pc1 $
 *                     $Modtime: $
 *
 *
 ****************************************************************************
 *
 *  (c) Copyright 2006 JENNIC Ltd
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/

#include <jendefs.h>
#include <AppHardwareApi.h>
#include <AppQueueApi.h>

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/

PRIVATE void vTimerConfig(void);
PRIVATE void vTimer0ISR(uint32 u32DeviceId, uint32 u32ItemBitmap);

PRIVATE void vUART_Init(void);
PRIVATE void vDebug(char *pcMessage);
PRIVATE void vDisplayHex(uint32 u32Data, int iSize);


/****************************************************************************
 *
 * NAME: vTimerConfig
 *
 * DESCRIPTION:
 * Running Timer0 in Capture Mode with no prescalar
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * Notes: ensure that high pulse is never wider than (1/16MHz)*(2^PRESCALAR)*2^16
  ****************************************************************************/
PRIVATE void vTimerConfig(void)
{
/* set up timer 0 for capture */
    vAHI_TimerEnable(E_AHI_TIMER_0,
                     0x00,      // no prescalar
                     FALSE,     // no interrupt when L-H
                     TRUE,      // interrupt when H-L
                     FALSE);    // no output
    vAHI_TimerClockSelect(E_AHI_TIMER_0,
                          FALSE,    //use internal 16MHz clock
                          FALSE);   //for PWM only

/* set up timer 1 for PWM */
    vAHI_TimerEnable(E_AHI_TIMER_1,
                     0x00,
                     FALSE,
                     FALSE,
                     TRUE);
    vAHI_TimerClockSelect(E_AHI_TIMER_1,
                          FALSE,
                          TRUE);
    vAHI_TimerStartRepeat(E_AHI_TIMER_1,
                          0xc000,       // SPACE period
                          0xffff);      // period

    /* register Timer0 interrupt */
    vAHI_Timer0RegisterCallback(vTimer0ISR);
}


/****************************************************************************
 *
 * NAME: vTimer0ISR
 *
 * DESCRIPTION: routine to handle interrupt from timer0
 *
 *
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PRIVATE void vTimer0ISR(uint32 u32DeviceId, uint32 u32ItemBitmap)
{
    uint16 u16Hi;   /* stores the tmr0 count when input goes high */
    uint16 u16Lo;   /* stores the tmr0 count when input goes low */
    int32 i32Data;  /* stores the difference between u16Hi and u16Lo */

    vAHI_TimerReadCapture(E_AHI_TIMER_0,
                          &u16Hi,
                          &u16Lo);

    vDebug("\n\rint ");

    if(u16Lo > u16Hi)   /* have we rolled over */
    {
        i32Data = u16Lo - u16Hi ;
    }
    else
    {
        i32Data = 0x10000 + u16Lo - u16Hi ;
    }

    vDisplayHex(i32Data,4);

    vAHI_TickTimerIntPendClr();             // ensure that interrupt flag is cleared
    vAHI_TimerStartCapture(E_AHI_TIMER_0);  // restart capture
}



/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * Entry point for a power on reset or wake from sleep mode.
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* Initialise stack and hardware interfaces.  */
    (void)u32AppApiInit(NULL,NULL,NULL,NULL,NULL,NULL,NULL);    // to init peripheral interrupts
    (void)u32AHI_Init();                                        // to init hardware peripherals

    /* Initialise the serial port */
    vUART_Init();

    vTimerConfig();

    vDebug("\n\rready to start\n\r");
    vAHI_TimerStartCapture(E_AHI_TIMER_0);

    while(1)
    {
    }
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for a wake from sleep mode with the memory contents held. We
 * are not using this mode and so should never get here.
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * None.
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    AppColdStart();
}


/****************************************************************************
 *
 * NAME: vUART_Init, vDebug, vDisplayHex
 *
 * DESCRIPTION:
 *
 * PARAMETERS:      Name            RW  Usage
 * None.
 *
 * RETURNS:
 * None.
 *
 * NOTES:
 * used for simple debug using UART0
 ****************************************************************************/
PUBLIC void vUART_Init(void)
{
    /* Enable UART 0: 19200-8-N-1 */
    vAHI_UartEnable(E_AHI_UART_0);
    vAHI_UartReset(E_AHI_UART_0, TRUE, TRUE);
    vAHI_UartReset(E_AHI_UART_0, FALSE, FALSE);
    vAHI_UartSetClockDivisor(E_AHI_UART_0, E_AHI_UART_RATE_19200);
}


PRIVATE void vDebug(char *pcMessage)
{
    while (*pcMessage)
    {
        while ((u8AHI_UartReadLineStatus(0) & 0x20) == 0);
        vAHI_UartWriteData(0, *pcMessage);
        pcMessage++;
    }
}

PRIVATE void vDisplayHex(uint32 u32Data, int iSize)
{
    char acValue[9];
    char *pcString = acValue;
    uint8 u8Nybble;
    int i, j;

    j = 0;
    for (i = (iSize << 2) - 4; i >= 0; i -= 4)
    {
        u8Nybble = (uint8)((u32Data >> i) & 0x0f);
        u8Nybble += 0x30;
        if (u8Nybble > 0x39)
            u8Nybble += 7;

        *pcString = u8Nybble;
        pcString++;
    }
    *pcString = '\0';

    vDebug(acValue);
}

/****************************************************************************/
/***        END OF FILE                                                   ***/
/****************************************************************************/

⌨️ 快捷键说明

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