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

📄 timer3.c

📁 Zigbee无线收发控制芯片JN5121C语言例程
💻 C
字号:
/****************************************************************************
 *
 * MODULE:             Timer2.c
 *
 * COMPONENT:          $RCSfile: $
 *
 * VERSION:            $Name: $
 *
 * REVISION:           $Revision: $
 *
 * DATED:              $Date: 20/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
 * The software polls timer0 to see when a H to L transition
 * completes the capture then 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>

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


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


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

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

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

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

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

PRIVATE void vTimerConfig(void);

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,
                         FALSE,
                         FALSE,
                         FALSE);
        vAHI_TimerClockSelect(E_AHI_TIMER_0,
                              FALSE,
                              FALSE);



/* 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
    }


/****************************************************************************
 *
 * 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)
{
    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 */

    /* Initialise stack and hardware interfaces.  */
    (void)u32AHI_Init();

    /* Initialise the serial port and rx/tx queues */
    vUART_Init();
    vTimerConfig();

    vDebug("\n\rready to start\n\r");   // output message to UART0

    while(1)
    {
        vAHI_TimerStartCapture(E_AHI_TIMER_0);
        /* wait for capture complete */
        while((u8AHI_TimerFired(E_AHI_TIMER_0) & E_AHI_TIMER_INT_PERIOD) == FALSE)
        {
        }
        vAHI_TimerReadCapture(E_AHI_TIMER_0,
                              &u16Hi,
                              &u16Lo);
        if(u16Lo > u16Hi)   /* have we rolled over */
        {
            i32Data = u16Lo - u16Hi ;
        }
        else
        {
            i32Data = 0x10000 + u16Lo - u16Hi ;
        }

        vDebug("\n\r");         // new line and carriage return sent to UART0
        vDisplayHex(i32Data,4); // output to UART0 i32Data showing 4 digits
    }
}

/****************************************************************************
 *
 * 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
 ****************************************************************************/
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 + -