/*
**********************************************************************
* 本地宏定义
**********************************************************************
*/
#define MATRIX_PORT P0 // 点阵LED负极端口
/*
**********************************************************************
* 本地全局变量
**********************************************************************
*/
sbit M1 = P1^0; // M1接在P1.0
sbit gBuz = P1^5; // P1.5通过ULN2003放大功率后驱动BUZZER
sbit SCK = P3^6; // SCK上升沿移位
sbit RCK = P3^5; // RCK上升沿串行输出寄存器锁存
sbit SER = P3^4; // SER引脚送字节数据进去
code u8 init_1_8266[] ="AT+CIPMUX=1\r\n";//设置8266为多连接模式(建立TCP server的前提)
code u8 init_2_8266[] ="AT+CIPSERVER=1\r\n";//建立 TCP server
code u8 init_3_8266[] ="AT+CIPSEND=0,5\r\n";//8266朝手机发送多少字节
u8 flag = 0;//串口接收数据标志位,当8266初始化完后被改写
u8 Buffer[40] = {0}; //从串口接收的数据
u8 i = 0;//Buffer的index
// 点阵列选值,数组每个元素的值选中其中一列
code u8 gLineCode[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
code u8 gZhu[] = {0x73,0xD6,0x58,0xFF,0x58,0x54,0x52,0x12}; // 汉字 朱
void main()
{
u16 temp = 0;
u8 temps[6] = {0};
temps[2] = '.';
M1 = 0;
UartInit();//串口初始化
delay1ms(1000);
UartSendBytes(init_1_8266);//设置8266为多连接模式(建立TCP server的前提)
delay1ms(1000);
UartSendBytes(init_2_8266);//建立 TCP server
delay1ms(1000);
P2 = 0x55;//表示可以wifi连接8266了
flag = 1;//开启串口接收数据
while (1)
{
switch (Buffer[9]) //根据手机输入的键值不同,执行不同的内容
{
case '1':
P2 = 0x0;//点亮所有灯
break;
case '2':
P2 = 0xff;//熄灭所有灯
break;
case '3':
M1 = 1;//马达转
break;
case '4':
M1 = 0;//马达停
break;
case '5':
BuzzerOnOff();//蜂鸣器叫
break;
case '6':
//蜂鸣器静
break;
case '7':
MatrixDisplay(gZhu);//点阵亮
break;
case '8':
//点阵灭
break;
case '9'://发送18b20温度
temp = TempDisplayTest();
temps[0] = temp /1000 + '0';
temp %= 1000;
temps[1] = temp/100 + '0';
temp %= 100;
temps[3] = temp/10 + '0';
temp %= 10;
temps[4] = temp + '0';
UartSendBytes(init_3_8266);//确定发送数据长度(5个字节)
delay1ms(10);
UartSendBytes(temps);//发送温度数据
Buffer[9] = 'x';
P2 = 0x55;
break;
default:
break;
}
}
}
/********************************************************************
* 名称 : UartIsr()
* 功能 : 串口中断处理程序
* 输入 : 无
* 输出 : 无
***********************************************************************/
void UartIsr(void) interrupt 4
{
EA = 0;
if (RI == 1) //当硬件接收到一个数据时,RI会置位
{
RI = 0;
if(flag == 1) //初始化完8266后,此处才为真
{
Buffer[i] = SBUF;//将接收到的字符放入Buffer
i++;
if(SBUF == 0x0a)//遇到换行就回到Buffer的头部开始存
{
i = 0;
}
}
}
EA = 1;
}
/********************************************************************
* 名称 : UartInit()
* 功能 : 串口初始化,注意波特率是2400
* 输入 : 无
* 输出 : 无
***********************************************************************/
void UartInit(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xF3;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
代码中使用到的AT指令
1、初始化配置
2、字符串发送之前的AT配置命令
AT 数据手册