找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2524|回复: 0
收起左侧

51单片机WIFI通信发射机源码

[复制链接]
ID:349440 发表于 2018-6-11 14:26 | 显示全部楼层 |阅读模式


单片机源程序如下:
  1. #include "reg51.h"
  2. #include "intrins.h"
  3. #include "timer0.h"

  4. typedef unsigned char BYTE;
  5. typedef unsigned int WORD;

  6. #define FOSC 11059200L      //System frequency  系统频率
  7. #define BAUD 9600           //UART baudrate     UART波特率

  8. /*Define UART parity mode*/
  9. #define NONE_PARITY     0   //None parity       无奇偶校验
  10. #define ODD_PARITY      1   //Odd parity        奇校验
  11. #define EVEN_PARITY     2   //Even parity       偶校验
  12. #define MARK_PARITY     3   //Mark parity       校验位始终为1
  13. #define SPACE_PARITY    4   //Space parity      校验位始终为0

  14. #define PARITYBIT NONE_PARITY   //Testing even parity      偶校验测试
  15.         
  16. bit busy;

  17. void SendData(BYTE dat);
  18. void SendString(char *s);
  19. void Delay500ms()                //@11.0592MHz
  20. {
  21.         unsigned char i, j, k;

  22.         _nop_();
  23.         i = 4;
  24.         j = 129;
  25.         k = 119;
  26.         do
  27.         {
  28.                 do
  29.                 {
  30.                         while (--k);
  31.                 } while (--j);
  32.         } while (--i);
  33. }
  34. void main()
  35. {
  36. #if (PARITYBIT == NONE_PARITY)
  37.     SCON = 0x50;            //8-bit variable UART(8位变量的UART)
  38. #elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
  39.     SCON = 0xda;            //9-bit variable UART, parity bit initial to 1(9位变量的UART,奇偶位初始值为1)
  40. #elif (PARITYBIT == SPACE_PARITY)
  41.     SCON = 0xd2;            //9-bit variable UART, parity bit initial to 0(9位变量的UART,奇偶位初始值为0)
  42. #endif

  43.     TMOD = 0x20;            //Set Timer1 as 8-bit auto reload mode(设置定时器18位自动重载模式)
  44.     TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule(设置自动加载值)
  45.     TR1 = 1;                //Timer1 start run(打开定时器1)
  46.     ES = 1;                 //Enable UART interrupt(开UART中断)
  47.     EA = 1;                 //Open master interrupt switch(开主中断开关)

  48.     while(1)
  49.         {
  50.                 Delay500ms();
  51.                 SendString("1");  
  52.         }
  53. }

  54. /*----------------------------
  55. UART interrupt service routine(中断服务程序)
  56. ----------------------------*/
  57. void Uart_Isr() interrupt 4 using 1
  58. {
  59.     if (RI)
  60.     {
  61.         RI = 0;             //Clear receive interrupt flag(清除接收中断标志)
  62. //        P0 = SBUF;          //P0 show UART data
  63.     }
  64.     if (TI)
  65.     {
  66.         TI = 0;             //Clear transmit interrupt flag(清除传输中断标志)
  67.         busy = 0;           //Clear transmit busy flag(清除传输繁忙标志)
  68.     }
  69. }

  70. /*----------------------------
  71. Send a byte data to UART     将字节数据发送到UATR
  72. Input: dat (data to be sent)   输入:dat(要发送的数据)
  73. Output:None                    输出:无
  74. ----------------------------*/
  75. void SendData(BYTE dat)
  76. {
  77.     while (busy);           //Wait for the completion of the previous data is sent  等待前一个数据完成发送
  78.     ACC = dat;              //Calculate the even parity bit P (PSW.0)        计算奇偶校验位P(PSW.0)
  79.     if (P)                  //Set the parity bit according to P              根据P设置奇偶校验位
  80.     {
  81. #if (PARITYBIT == ODD_PARITY)
  82.         TB8 = 0;            //Set parity bit to 0               将奇偶校验位设置为0
  83. #elif (PARITYBIT == EVEN_PARITY)
  84.         TB8 = 1;            //Set parity bit to 1               将奇偶校验位设置为1
  85. #endif
  86.     }
  87.     else
  88.     {
  89. #if (PARITYBIT == ODD_PARITY)
  90.         TB8 = 1;            //Set parity bit to 1               将奇偶校验位设置为1
  91. #elif (PARITYBIT == EVEN_PARITY)
  92.         TB8 = 0;            //Set parity bit to 0               将奇偶校验位设置为0
  93. #endif
  94.     }
  95.     busy = 1;
  96.     SBUF = ACC;             //Send data to UART buffer          发送数据到UART缓冲区
  97. }

  98. /*----------------------------
  99. Send a string to UART            发送字符串到UART
  100. Input: s (address of string)     输入:s(字符串抵地址)
  101. Output:None                      输出:无
  102. ----------------------------*/
  103. void SendString(char *s)
  104. {
  105.     while (*s)              //Check the end of the string         检查字符串结尾
  106.     {
  107.         SendData(*s++);     //Send current char and increment string ptr   发送当前字符和字符串指针增量
  108.     }
  109. }

复制代码

所有资料51hei提供下载:
发射机.zip (29.7 KB, 下载次数: 21)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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