新手求助DHT22传感器

[复制链接]
 楼主| PSD28335 发表于 2017-3-14 18:00 | 显示全部楼层 |阅读模式
  1. #include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
  2. #include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

  3. #define          RT1        GpioDataRegs.GPADAT.bit.GPIO5
  4. /*
  5. #define          LED2        GpioDataRegs.GPADAT.bit.GPIO1
  6. #define          LED3        GpioDataRegs.GPADAT.bit.GPIO2
  7. #define          LED4        GpioDataRegs.GPADAT.bit.GPIO3
  8. #define          LED5        GpioDataRegs.GPADAT.bit.GPIO4 */

  9. typedef unsigned char  U8;       /* defined for unsigned 8-bits integer variable           无符号8位整型变量  */
  10. typedef signed   char  S8;       /* defined for signed 8-bits integer variable                  有符号8位整型变量  */
  11. typedef unsigned int   U16;      /* defined for unsigned 16-bits integer variable           无符号16位整型变量 */
  12. typedef signed   int   S16;      /* defined for signed 16-bits integer variable           有符号16位整型变量 */
  13. typedef unsigned long  U32;      /* defined for unsigned 32-bits integer variable           无符号32位整型变量 */
  14. typedef signed   long  S32;      /* defined for signed 32-bits integer variable           有符号32位整型变量 */
  15. typedef float          F32;      /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
  16. typedef double         F64;      /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */
  17. //
  18. #define uchar unsigned char
  19. #define uint unsigned int
  20. // #define   Data_0_time    4

  21. //----------------------------------------------//
  22. //----------------------------------------------//
  23. U8  U8FLAG,k;
  24. U8  U8count,U8temp;
  25. U8  U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
  26. U8  U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
  27. U8  U8comdata;
  28. U8  outdata[5];  //定义发送的字节数
  29. U8  indata[5];
  30. U8  count, count_r=0;
  31. // U8 str[5]={"RS232"};
  32. U16 U16temp1,U16temp2,U8RH,U8T;
  33. /*SendData(U8 *a)
  34. {
  35.         outdata[0] = a[0];
  36.         outdata[1] = a[1];
  37.         outdata[2] = a[2];
  38.         outdata[3] = a[3];
  39.         outdata[4] = a[4];
  40.         count = 1;
  41.         SBUF=outdata[0];

  42. } */

  43.    void Delay(U16 j)
  44.     {      U8 i;
  45.             for(;j>0;j--)
  46.           {        
  47.                 for(i=0;i<27;i++);

  48.           }
  49.     }
  50.       void  Delay_10us(void)
  51.       {
  52.         U8 i;
  53.         i--;
  54.         i--;
  55.         i--;
  56.         i--;
  57.         i--;
  58.         i--;

  59.        }

  60. void configtestled(void);

  61.         void  COM(void)
  62.       {

  63.                 U8 i;
  64.                 U8comdata=0;
  65.                 GpioCtrlRegs.GPADIR.bit.GPIO5 = 0;  // GPIO0设置为输入
  66.        for(i=0;i<8;i++)
  67.             {

  68.                        U8FLAG=2;
  69.                    while((!RT1)&&U8FLAG++);
  70.                            Delay_10us();
  71.                                    Delay_10us();
  72.                                Delay_10us();
  73.                           U8temp=0;//26-28us,表示0
  74.              if(RT1)
  75.              {  U8temp=1;
  76.                     U8FLAG=2;}//超过28us依然为高电平,表示为1
  77.                  while((RT1)&&U8FLAG++);
  78.                    //超时则跳出for循环
  79.                     if(U8FLAG==0x0001)break;
  80.                    //判断数据位是0还是1

  81.                 // 如果高电平高过预定0高电平值则数据位为 1

  82.                    U8comdata<<=1;
  83.                       U8comdata|=U8temp;        //0
  84.              }//rof
  85.       

  86.         }

  87.         //--------------------------------
  88.         //-----湿度读取子程序 ------------
  89.         //--------------------------------
  90.         //----以下变量均为全局变量--------
  91.         //----温度高8位== U8T_data_H------
  92.         //----温度低8位== U8T_data_L------
  93.         //----湿度高8位== U8RH_data_H-----
  94.         //----湿度低8位== U8RH_data_L-----
  95.         //----校验 8位 == U8checkdata-----
  96.         //----调用相关子程序如下----------
  97.         //---- Delay();, Delay_10us();,COM();
  98.         //--------------------------------
  99.         void RH(void)
  100.                 {
  101.                  configtestled();
  102.                   //主机拉低500us
  103.                    RT1=0;
  104.                    Delay(5);
  105.                    RT1=1;
  106.                  //总线由上拉电阻拉高 主机延时20us
  107.                    Delay_10us();
  108.                    Delay_10us();
  109.                    Delay_10us();
  110.                    Delay_10us();
  111.                  //主机设为输入 判断从机响应信号
  112.                   
  113.                  // P2_0=1;

  114.                       GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0; // GPIO5复用为GPIO功能
  115.                       GpioCtrlRegs.GPADIR.bit.GPIO5 = 0;  // GPIO5设置为输入

  116.                  //判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
  117.                    if(!RT1)                 //T !
  118.                    {
  119.                    U8FLAG=2;
  120.                  //判断从机是否发出 80us 的低电平响应信号是否结束
  121.                    while((!RT1)&&U8FLAG++);
  122.                    U8FLAG=2;
  123.                  //判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
  124.                    while((RT1)&&U8FLAG++);
  125.                  //数据接收状态
  126.                    COM();
  127.                    U8RH_data_H_temp=U8comdata;
  128.                    COM();
  129.                    U8RH_data_L_temp=U8comdata;
  130.                    COM();
  131.                    U8T_data_H_temp=U8comdata;
  132.                    COM();
  133.                    U8T_data_L_temp=U8comdata;
  134.                    COM();
  135.                    U8checkdata_temp=U8comdata;
  136.                    RT1=1; //?????
  137.                  //数据校验
  138.                    U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
  139.                    if(U8temp==U8checkdata_temp)
  140.                    {
  141.                              U8RH_data_H=U8RH_data_H_temp;
  142.                              U8RH_data_L=U8RH_data_L_temp;
  143.                           U8T_data_H=U8T_data_H_temp;
  144.                              U8T_data_L=U8T_data_L_temp;
  145.                              U8checkdata=U8checkdata_temp;
  146.                    }//fi
  147.                    }//fi
  148.          U8RH=256*U8RH_data_H+U8RH_data_L;
  149.          U8T=U8T_data_H*256+U8T_data_L;
  150.                 }

  151. //----------------------------------------------
  152. //main()功能描述:  AT89C51  11.0592MHz         串口发
  153. //送温湿度数据,波特率 9600
  154. void main(void)
  155. {
  156. // Step 1. Initialize System Control:
  157. // PLL, WatchDog, enable Peripheral Clocks
  158. // This example function is found in the DSP2833x_SysCtrl.c file.
  159.    


  160. // Step 2. Initalize GPIO:
  161. // This example function is found in the DSP2833x_Gpio.c file and
  162. // illustrates how to set the GPIO to it's default state.
  163. // InitGpio();  // Skipped for this example
  164.    InitXintf16Gpio();        //zq
  165.    InitPieCtrl();
  166. // Step 3. Clear all interrupts and initialize PIE vector table:
  167. // Disable CPU interrupts
  168.    DINT;

  169. // Initialize the PIE control registers to their default state.
  170. // The default state is all PIE interrupts disabled and flags
  171. // are cleared.
  172. // This function is found in the DSP2833x_PieCtrl.c file.
  173. //  InitPieCtrl();

  174. // Disable CPU interrupts and clear all CPU interrupt flags:
  175.    IER = 0x0000;
  176.    IFR = 0x0000;

  177. // Initialize the PIE vector table with pointers to the shell Interrupt
  178. // Service Routines (ISR).
  179. // This will populate the entire table, even if the interrupt
  180. // is not used in this example.  This is useful for debug purposes.
  181. // The shell ISR routines are found in DSP2833x_DefaultIsr.c.
  182. // This function is found in DSP2833x_PieVect.c.
  183.    InitPieVectTable();
  184. //  configtestled();

  185.    //   LED1=1;
  186.   // DELAY_US(10);
  187.   //    LED2=1;
  188.   // DELAY_US(10);
  189.    //uchar str[6]={"RS232"};
  190.            /* 系统初始化 */
  191.   /*         TMOD = 0x20;          //定时器T1使用工作方式2
  192.            TH1 = 253;        // 设置初值
  193.            TL1 = 253;
  194.            TR1 = 1;          // 开始计时
  195.            SCON = 0x50;          //工作方式1,波特率9600bps,允许接收
  196.            ES = 1;
  197.            EA = 1;           // 打开所以中断
  198.            TI = 0;
  199.            RI = 0;
  200.            SendData(str) ;   //发送到串口
  201.            Delay(10);         //延时100US(12M晶振) */
  202.            while(1)
  203.            {

  204.               //------------------------
  205.               //调用温湿度读取子程序
  206.               RH();
  207.               //串口显示程序
  208.               //--------------------------
  209.          
  210.             /*str[0]=U8RH_data_H;
  211.               str[1]=U8RH_data_L;
  212.               str[2]=U8T_data_H;
  213.               str[3]=U8T_data_L;
  214.               str[4]=U8checkdata;

  215.           SendData(str) ;  //发送到串口
  216.               //读取模块数据周期不宜小于 2S
  217.               Delay(20000);*/
  218.            }//elihw

  219. }//MAIN
  220. /*
  221. void RSINTR() interrupt 4 using 2
  222. {
  223.         U8 InPut3;
  224.         if(TI==1) //发送中断
  225.         {
  226.                 TI=0;
  227.                 if(count!=5) //发送完5位数据
  228.                 {
  229.                         SBUF= outdata[count];
  230.                         count++;
  231.                 }
  232.         }

  233.         if(RI==1)         //接收中断
  234.         {
  235.                 InPut3=SBUF;
  236.                 indata[count_r]=InPut3;
  237.                 count_r++;
  238.                 RI=0;
  239.                 if (count_r==5)//接收完4位数据
  240.                 {
  241.                 //数据接收完毕处理。
  242.                         count_r=0;
  243.                 str[0]=indata[0];
  244.                  str[1]=indata[1];
  245.                    str[2]=indata[2];
  246.                          str[3]=indata[3];
  247.                                  str[4]=indata[4];
  248.                                  P0=0;
  249.                 }
  250.         }
  251. } */


  252. void configtestled (void)
  253. {
  254.    EALLOW;
  255.    GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0; // GPIO0复用为GPIO功能
  256.    GpioCtrlRegs.GPADIR.bit.GPIO5 = 1;  // GPIO0设置为输出
  257. /*
  258.    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO1 = GPIO1
  259.    GpioCtrlRegs.GPADIR.bit.GPIO1 = 1;
  260.    GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0; // GPIO0复用为GPIO功能
  261.    GpioCtrlRegs.GPADIR.bit.GPIO2 = 1;  // GPIO0设置为输出
  262.    GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 0; // GPIO1 = GPIO1
  263.    GpioCtrlRegs.GPADIR.bit.GPIO3 = 1;
  264.    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0; // GPIO1 = GPIO1
  265.    GpioCtrlRegs.GPADIR.bit.GPIO4 = 1;
  266.    EDIS;*/
  267. }
  268. //===========================================================================
  269. // No more.
  270. //===========================================================================

程序有问题,但刚开始学,本来是51单片机的程序,想转成dsp的
IIIINNH 发表于 2018-7-24 17:41 | 显示全部楼层
看看
zhangmangui 发表于 2018-7-24 22:39 | 显示全部楼层
和单片机一样    唯一注意的就是速度较快    注意延时量等是否够
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝
快速回复 返回顶部 返回列表