我不乐意啊 发表于 2013-3-16 10:27:41

有关CC2530读出AM2302的问题,等待大侠的帮忙!

我根据51C语言的程序写的,在CC2530中运行,得到的数据不管温度还是湿度都是53.5!!!!!!!!!!!! 谁有用过AM2302的请帮忙一下!我的代码贴出来,大家看看。

#include <ioCC2530.h>
#include <string.h>
#include "UART.H"

#define uint unsigned int
#define uchar unsigned char
#define Data P0_5

uint wen,shi;
uchar wbai,wshi,wge,sbai,sshi,sge,s,crc,i,j;
uint ww,ss,cc;

void Delay_us() //1 us延时
{
    asm("NOP");
    asm("NOP");
    asm("NOP");
    asm("NOP");
    asm("NOP");   
    asm("NOP");
    asm("NOP");
    asm("NOP");   
    asm("NOP");
    asm("NOP");
   
}

void Delay_10us() //10 us延时
{
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();   
}

void Delay_ms(uint Time)//n ms延时
{
unsigned char i;
while(Time--)
{
    for(i=0;i<100;i++)
   Delay_10us();
}
}
void Data_IN(void)
{
P0SEL &= ~0x20;   //定义为普通IO口
P0DIR &= ~0x20;   //定义为输入
P0INP &= ~0x20;   //打开上拉
}
void Data_OUT(void)
{
P0SEL &= ~0x20;   //定义为普通IO口
P0DIR |=0x20;   //义为输出
P0INP &= ~0x20;   //打开上拉
}
void cton()
{
   wen=0;
   shi=0;
   for(j=0;j<16;j++)
    {
          wen=wen|(ww<<(15-j));
    }
           for(j=0;j<16;j++)
    {
          shi=shi|(ss<<(15-j));
    }
   wbai=wen%1000/100;
   wshi=wen%100/10;
   wge=wen%10;
   sbai=shi%1000/100;
   sshi=shi%100/10;
   sge=shi%10;

}
       

uint read_Sbit()
{
   Data_IN();
   while(!Data)
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   Delay_us();
   if(0==Data)
   {
          return 0;
   }
   while(Data);

          return 1;
}
void read_Data()
{
Data_OUT();
Data=0;
Delay_ms(20);
Data=1;
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Delay_us();
Data_IN();
    while(!Data)
    {

    }

    while(Data)
    {
               
    }
                   
      for(j=0;j<16;j++)
      {
                  //shi=shi|((read_Sbit()<<j)&0xffff) ;
                  ss=read_Sbit();

      }

          for(j=0;j<16;j++)
      {
                  //wen=wen|((read_Sbit()<<j)&0xffff) ;
                  ww=read_Sbit();
      }

          for(j=0;j<7;j++)
      {
                  //crc=crc|((read_Sbit()<<j)&0xff) ;
                  cc=read_Sbit();

      }

}
/***************************
          主函数
***************************/
void main(void)
{
      Delay_ms(1000);//让设备稳定
      InitUart();    //串口初始化
        while(1)
        {         

         read_Data();
       cton();
       UartSend_OneChar(wbai+0x30);
         UartSend_OneChar(wshi+0x30);
         UartSend_OneChar(wge+0x30);
         UartSend_OneChar(0x0d);
         UartSend_OneChar(0x0a);
       UartSend_OneChar(sbai+0x30);
         UartSend_OneChar(sshi+0x30);
         UartSend_OneChar(sge+0x30);
         UartSend_OneChar(0x0d);
         UartSend_OneChar(0x0a);
       UartSend_OneChar(0x0d);
         UartSend_OneChar(0x0a);
         
         Delay_ms(1000);//延时,使周期性1S读取1次
       
      }
}

jjj2012 发表于 2013-3-16 10:37:55

这个不错,楼主V587,楼主给力;
但是我不懂,上面就是意见
页: [1]
查看完整版本: 有关CC2530读出AM2302的问题,等待大侠的帮忙!