我用過 DHT11 , 讀取資料的方法一樣 , 我把sample code 貼給你參考一下 , 我用的是 NANO102
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Unlock protected registers */
SYS_UnlockReg();
/* Enable External XTAL (4~24 MHz) */
// CLK->PWRCTL &= ~CLK_PWRCTL_HXT_EN_Msk;
// CLK->PWRCTL |= (0x1 << CLK_PWRCTL_HXT_EN_Pos); // HXT Enabled
/* Waiting for 12MHz clock ready */
// CLK_WaitClockReady( CLK_CLKSTATUS_HXT_STB_Msk);
/* Switch HCLK clock source to XTAL */
CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLK_S_Msk;
CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_HIRC;
/* Enable IP clock */
CLK->APBCLK |= CLK_APBCLK_UART0_EN; // UART0 Clock Enable
/* Select IP clock source */
CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk;
CLK->CLKSEL1 |= (0x0 << CLK_CLKSEL1_UART_S_Pos);// Clock source from external 12 MHz or 32 KHz crystal clock
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD and TXD */
SYS->PB_H_MFP &= ~(SYS_PB_H_MFP_PB13_MFP_Msk | SYS_PB_H_MFP_PB14_MFP_Msk);
SYS->PB_H_MFP |= (SYS_PB_H_MFP_PB13_MFP_UART0_RX | SYS_PB_H_MFP_PB14_MFP_UART0_TX);
/* Lock protected registers */
SYS_LockReg();
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
UART_Open(UART0, 115200);
}
void TMR0_IRQHandler(void)
{
time_DHT11[i] = TIMER_GetCaptureData(TIMER0);
i++;
TIMER_ClearCaptureIntFlag(TIMER0);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{
int32_t i32Err,uu;
/* Init System, IP clock and multi-function I/O */
SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.
PD11 = 0 ;
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1000000);
TIMER_SET_PRESCALE_VALUE(TIMER0, 11);
// Set compare value as large as possible, so don't need to worry about counter overrun too frequently.
TIMER_SET_CMP_VALUE(TIMER0, 0xFFFFFF);
TIMER_EnableCapture(TIMER0, TIMER_CAPTURE_TRIGGER_COUNTING_MODE, TIMER_CAPTURE_RISING_THEN_FALLING_EDGE);
GPIO_SetMode(PD,BIT11,GPIO_PMD_OUTPUT);
GPIO_ENABLE_PULL_UP(PD,BIT11);
PD11 = 0 ;
CLK_SysTickDelay (18000); //18ms
PD11 = 1;
SYS->PD_H_MFP = (SYS->PD_H_MFP & ~SYS_PD_H_MFP_PD11_MFP_Msk) | SYS_PD_H_MFP_PD11_MFP_TMR0_CAP;
// Start Timer 0
TIMER_Start(TIMER0);
i = 0;
// Enable timer interrupt
TIMER_EnableCaptureInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
while(i<40);
for (uu=0;uu<40;uu++)
printf ( "%x ",time_DHT11[uu]);
printf("\n");
while(1);
|