/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V1.00
* $Revision: 3 $
* $Date: 15/08/18 11:54a $
* [url=home.php?mod=space&uid=247401]@brief[/url] NUC200 Series LED Controller Sample Code
*
* @note
* Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "NUC230_240.h"
#include "NuEdu-Basic01.h"
void Timer_initial(void)
{
/* Step 1. Enable and Select Timer clock source */
CLK_EnableModuleClock(TMR0_MODULE);
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
/* Step 2. Select Operation mode */
TIMER0->TCSR=TIMER_PERIODIC_MODE; //Select periodic mode for operation mode
/* Step 3. Select Time out period = (Period of timer clock input) * (8-bit Prescale + 1) * (24-bit TCMP)*/
TIMER0->TCSR|=11; // Set Prescale [0~255]
TIMER0->TCMPR = 16777215; // Set TICR(TCMP) [0~16777215]
TIMER0->TCSR|=TIMER_TCSR_TDR_EN_Msk; // Enable TDR function
TIMER_Start(TIMER0); //Enable Timer0
}
unsigned int DHT11_RESET(void)
{
unsigned int old_time,poll_time;
/* The Quasi-bidirection mode could be used as input with pull up enable */
GPIO_SetMode(PA, BIT0, GPIO_PMD_QUASI);
PA0=1;
PA0=0;
CLK_SysTickDelay(18000);//delay 18ms
PA0=1;
poll_time=TIMER0->TDR;
//wait ack
while(1)
{
if (PA0 == 0)
{
old_time=TIMER0->TDR;
break;
}
if(TIMER0->TDR-poll_time>1000)
return 1;//DHT11 time out, no ack.
}
while(1)
{
if (PA0 == 1)
{
//printf("0x%x\n\r",TIMER0->TDR-old_time);
break;
}
}
return 0;//pass
}
unsigned int temp1,temp2,temp3,temp4;
unsigned int DHT11_Read(void)
{
int i=0;
unsigned int old_time;
Timer_initial();
if(DHT11_RESET()==0)
{
while(PA0==1);
temp1=0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
while(PA0==0);
old_time=TIMER0->TDR;
//GPIOC->DOUT=0;
while(PA0==1);
//GPIOC->DOUT=1;
if(TIMER0->TDR-old_time>40)
temp1=temp1|0x01;
}
temp2=0;
for(i=0;i<8;i++)
{
temp2=temp2<<1;
while(PA0==0);
old_time=TIMER0->TDR;
//GPIOC->DOUT=0;
while(PA0==1);
//GPIOC->DOUT=1;
if(TIMER0->TDR-old_time>40)
temp2=temp2|0x01;
}
temp3=0;
for(i=0;i<8;i++)
{
temp3=temp3<<1;
while(PA0==0);
old_time=TIMER0->TDR;
//GPIOC->DOUT=0;
while(PA0==1);
//GPIOC->DOUT=1;
if(TIMER0->TDR-old_time>40)
temp3=temp3|0x01;
}
temp4=0;
for(i=0;i<8;i++)
{
temp4=temp4<<1;
while(PA0==0);
old_time=TIMER0->TDR;
//GPIOC->DOUT=0;
while(PA0==1);
//GPIOC->DOUT=1;
if(TIMER0->TDR-old_time>40)
temp4=temp4|0x01;
}
return 0;
}
else
return 1;
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
SYS_Init();
UART0_Init();
if(DHT11_Read()==0)
{
printf("humidity: %d.%d\n\r",temp1, temp2);
printf("temperature: %d.%d\n\r",temp3, temp4);
}
while(1);
}
|