标题:
51单片机WIFI通信发射机源码
[打印本页]
作者:
green33
时间:
2018-6-11 14:26
标题:
51单片机WIFI通信发射机源码
单片机源程序如下:
#include "reg51.h"
#include "intrins.h"
#include "timer0.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //System frequency 系统频率
#define BAUD 9600 //UART baudrate UART波特率
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity 无奇偶校验
#define ODD_PARITY 1 //Odd parity 奇校验
#define EVEN_PARITY 2 //Even parity 偶校验
#define MARK_PARITY 3 //Mark parity 校验位始终为1
#define SPACE_PARITY 4 //Space parity 校验位始终为0
#define PARITYBIT NONE_PARITY //Testing even parity 偶校验测试
bit busy;
void SendData(BYTE dat);
void SendString(char *s);
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART(8位变量的UART)
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1(9位变量的UART,奇偶位初始值为1)
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0(9位变量的UART,奇偶位初始值为0)
#endif
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode(设置定时器18位自动重载模式)
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule(设置自动加载值)
TR1 = 1; //Timer1 start run(打开定时器1)
ES = 1; //Enable UART interrupt(开UART中断)
EA = 1; //Open master interrupt switch(开主中断开关)
while(1)
{
Delay500ms();
SendString("1");
}
}
/*----------------------------
UART interrupt service routine(中断服务程序)
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag(清除接收中断标志)
// P0 = SBUF; //P0 show UART data
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag(清除传输中断标志)
busy = 0; //Clear transmit busy flag(清除传输繁忙标志)
}
}
/*----------------------------
Send a byte data to UART 将字节数据发送到UATR
Input: dat (data to be sent) 输入:dat(要发送的数据)
Output:None 输出:无
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent 等待前一个数据完成发送
ACC = dat; //Calculate the even parity bit P (PSW.0) 计算奇偶校验位P(PSW.0)
if (P) //Set the parity bit according to P 根据P设置奇偶校验位
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0 将奇偶校验位设置为0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1 将奇偶校验位设置为1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1 将奇偶校验位设置为1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0 将奇偶校验位设置为0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer 发送数据到UART缓冲区
}
/*----------------------------
Send a string to UART 发送字符串到UART
Input: s (address of string) 输入:s(字符串抵地址)
Output:None 输出:无
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string 检查字符串结尾
{
SendData(*s++); //Send current char and increment string ptr 发送当前字符和字符串指针增量
}
}
复制代码
所有资料51hei提供下载:
发射机.zip
(29.7 KB, 下载次数: 21)
2018-6-11 14:25 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1