找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2466|回复: 2
打印 上一主题 下一主题
收起左侧

基于STC89C52的NRF24L01无线温度传输系统程序设计

[复制链接]
跳转到指定楼层
楼主
ID:655517 发表于 2019-12-3 21:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于STC89C52单片机,使用DS18B20数字温度传感器,通过NRF24L01无线模块传输温度数据的系统设计。

单片机源程序如下:
  1. /*
  2. STA
  3. */


  4. #include "reg52.h"

  5. #include "lcd1602.h"
  6. #include <stdio.h>
  7. #include"temp.h"
  8. #include "intrins.h"

  9. typedef unsigned char BYTE;
  10. typedef unsigned int WORD;

  11. #define FOSC 11059200L      //System frequency
  12. #define BAUD 9600           //UART baudrate

  13. /*Define UART parity mode*/
  14. #define NONE_PARITY     0   //None parity
  15. #define ODD_PARITY      1   //Odd parity
  16. #define EVEN_PARITY     2   //Even parity
  17. #define MARK_PARITY     3   //Mark parity
  18. #define SPACE_PARITY    4   //Space parity

  19. #define PARITYBIT NONE_PARITY   //Testing even parity


  20. bit busy;
  21. char cStr[50] ;

  22. char Rec,xx=0;
  23. char Re[10];
  24. void SendData(BYTE dat);
  25. void SendString(char *s);
  26. void Delay2000ms(void);
  27. void Delay200ms(void);
  28. void USART_Init(void)
  29. {
  30.         #if (PARITYBIT == NONE_PARITY)
  31.             SCON = 0x50;            //8-bit variable UART
  32.         #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  33.             SCON = 0xda;            //9-bit variable UART, parity bit initial to 1
  34.         #elif (PARITYBIT == SPACE_PARITY)
  35.             SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0
  36.         #endif
  37.        
  38.     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode
  39.     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
  40.     TR1 = 1;                //Timer1 start run
  41.     ES = 1;                 //Enable UART interrupt
  42.     EA = 1;                 //Open master interrupt switch       
  43. }
  44. /*

  45.          SendString("AT+CWMODE=3\r\n");
  46.          Delay2000ms();
  47.          SendString("AT+RST\r\n");
  48.          Delay2000ms();
  49.          SendString("AAT+CIPMUX=1\r\n");
  50.          Delay2000ms();
  51.          SendString("AT+CIPSERVER=1,8089\r\n");
  52.          Delay2000ms();

  53. */


  54. void main()
  55. {
  56.        
  57.         unsigned char i = 1;
  58.         int temp = 0;
  59.         float tp;
  60.         unsigned char aa[10];
  61.         USART_Init();
  62.         LCD1602_Init();

  63.        
  64.         Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();Delay2000ms();
  65.         sprintf(cStr,"AT+CIPSTART=\"%s\",\"%s\",%s\r\n","TCP","192.168.4.1","8089");  //端口号字符串
  66.         SendString(cStr);
  67.         Delay2000ms();
  68.         SendString("AT+CIPMODE=1\r\n");
  69.          Delay2000ms();
  70.          SendString("AT+CIPSEND\r\n");
  71.          Delay2000ms();
  72.     while(1)
  73.         {
  74.                         temp = DS18B20_1_GetTemp();
  75.                     tp=temp;                                                               //如果温度是正的那么,那么正数的原码就是补码它本身
  76.                
  77.                 temp=tp*0.0625*100+0.5;       

  78. //                sprintf(aa,"%d\r\n",i);
  79.                 sprintf(aa,"%d\r\n",temp);
  80.                 SendString(aa);
  81. //                SendString(aa);
  82.                 Delay200ms();
  83.                 i++;
  84.                 if(i>50) i=1;
  85.         }
  86. }

  87. /*----------------------------
  88. UART interrupt service routine
  89. ----------------------------*/
  90. void Uart_Isr() interrupt 4
  91. {
  92.     if (RI)
  93.     {
  94.         RI = 0;             //Clear receive interrupt flag
  95.         Re[xx] = SBUF;          //P0 show UART data
  96.             xx++;
  97.                 if((Re[xx-1]=='\n'&&Re[xx-2]=='\r')|xx == 10)
  98.                 {
  99.                 LCD1602_DispString(1,0,"           ");
  100.                         xx=0;
  101.                         LCD1602_DispString(1,0,Re);
  102.                 }
  103.     }
  104.     if (TI)
  105.     {
  106.         TI = 0;             //Clear transmit interrupt flag
  107.         busy = 0;           //Clear transmit busy flag
  108.     }
  109. }

  110. /*----------------------------
  111. Send a byte data to UART
  112. Input: dat (data to be sent)
  113. Output:None
  114. ----------------------------*/
  115. void SendData(BYTE dat)
  116. {
  117.     while (busy);           //Wait for the completion of the previous data is sent
  118.     ACC = dat;              //Calculate the even parity bit P (PSW.0)
  119.     if (P)                  //Set the parity bit according to P
  120.     {
  121. #if (PARITYBIT == ODD_PARITY)
  122.         TB8 = 0;            //Set parity bit to 0
  123. #elif (PARITYBIT == EVEN_PARITY)
  124.         TB8 = 1;            //Set parity bit to 1
  125. #endif
  126.     }
  127.     else
  128.     {
  129. #if (PARITYBIT == ODD_PARITY)
  130.         TB8 = 1;            //Set parity bit to 1
  131. #elif (PARITYBIT == EVEN_PARITY)
  132.         TB8 = 0;            //Set parity bit to 0
  133. #endif
  134.     }
  135.     busy = 1;
  136.     SBUF = ACC;             //Send data to UART buffer
  137. }

  138. /*----------------------------
  139. Send a string to UART
  140. Input: s (address of string)
  141. Output:None
  142. ----------------------------*/
  143. void SendString(char *s)
  144. {
  145.     while (*s)              //Check the end of the string
  146.     {
  147.         SendData(*s++);     //Send current char and increment string ptr
  148.     }
  149. }
  150. void Delay2000ms()                //@11.0592MHz
  151. {
  152.         unsigned char i, j, k;

  153.         _nop_();
  154.         i = 15;
  155.         j = 2;
  156.         k = 235;
  157.         do
  158.         {
  159.                 do
  160.                 {
  161.                         while (--k);
  162.                 } while (--j);
  163.         } while (--i);
  164. }
  165. void Delay200ms()                //@11.0592MHz
  166. {
  167.         unsigned char i, j, k;

  168.         _nop_();
  169.         i = 2;
  170.         j = 103;
  171.         k = 147;
  172.         do
  173.         {
  174.                 do
  175.                 {
  176.                         while (--k);
  177.                 } while (--j);
  178.         } while (--i);
  179. }
复制代码

所有资料51hei提供下载:
基于STC89C52的无线温度传输系统.rar (100.79 KB, 下载次数: 98)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:306996 发表于 2019-12-9 15:16 来自手机 | 只看该作者
感谢楼主的分享
回复

使用道具 举报

板凳
ID:361216 发表于 2021-8-16 15:57 | 只看该作者
不错不错,顶一个,正常准备做一个这样的东西来用用。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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