调用方式
/**************************************************************************//**
* @file main.c
* @version V3.00
* @brief DS18B20 Driver Sample Code
*
* [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2013~2015 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include "stdio.h"
#include "NuMicro.h"
#define PLL_CLOCK 192000000
int16_t DS18B20_ReadTemperature(void);
void SYS_Init(void)
{
/* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
/* Enable HXT clock (external XTAL 12MHz) */
CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
/* Wait for HXT clock ready */
CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Set PCLK0/PCLK1 to HCLK/2 */
CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_LXT);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Select UART module clock source as HXT and UART module clock divider as 1 */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
SystemCoreClockUpdate();
}
void UART0_Init()
{
/* Configure UART0 and set UART0 baud rate */
UART_Open(UART0, 115200);
}
int32_t main(void)
{
float t;
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
/* Init UART0 for printf */
UART0_Init();
printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
printf("+-------------------------------------------------+\n");
printf("| PH.0(Output) and PG.15(Input) Sample Code |\n");
printf("+-------------------------------------------------+\n\n");
/* Configure PH.0 as Output mode and PG.15 as Input mode then close it */
GPIO_SetMode(PH, BIT0, GPIO_MODE_OUTPUT);
GPIO_SetMode(PG, BIT15, GPIO_MODE_INPUT);
GPIO_SetMode(PB, BIT2, GPIO_MODE_QUASI);
while(1)
{
PH0=1;
CLK_SysTickDelay(1000000);
PH0=0;
CLK_SysTickDelay(1000000);
t = DS18B20_ReadTemperature() * 0.0625;
printf("\n%f\n", t);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
CLK_SysTickDelay(1000000);
}
}
|