- 打卡等级:常住居民II
- 打卡总天数:87
- 最近打卡:2025-04-30 10:02:32
已绑定手机
高级会员
- 积分
- 878
|
本帖最后由 欢迎哈哈哈3 于 2024-11-15 15:42 编辑
问大家个问题,我用串口2与ESP8266发送数据到手机APP上,用定时器T0作为定时,coun为定时时间,count%7==0,每7秒发送,count计时到60秒,但为什么每发送一次,count会自动会回到初始值呢,液晶显示程序太多了,就只上传了一部分{:5_358:},液晶能够正常显示数字,下面是部分主要程序
主程序:
#include "main.h"
#include "stc8.h"
#include "lcd1602.h"
#include "ESP8266.h"
#include "uart2.h"
#include "string.h"
#define MAIN_Fosc 11059200UL
#define Timer0_Reload (65536UL -(MAIN_Fosc/240))
unsigned char buf_flag[]={0};
unsigned char flg=0,count=0;
unsigned char dataBuf[]={0};
void setinoutport()
{
P0M1=0X00;P0M0=0X00;
P1M1=0X00;P1M0=0X02;
P2M1=0X00;P2M0=0X00;
P3M1=0X00;P3M0=0X02;
P0PU=0XFF;
}
void LCD_Initial(void)//液晶初始化
{
LCD1602_WriteCmd(0x38);
LCD1602_WriteCmd(0x0C);
LCD1602_WriteCmd(0x01);
LCD1602_WriteCmd(0x06);
}
void esp8266int()//ESP8266初始化
{
PrintString("AT\r\n",4);
PrintString("ATE0\r\n",6);
PrintString("AT+CWMODE=2\r\n",13);
PrintString("AT+CWSAP=\"ESP8266\",\"666666666\",6,4\r\n",34);
PrintString("AT+RST\r\n",8);
PrintString("AT+CIPMUX=1\r\n",13);
PrintString("AT+CIPSERVER=1,8090\r\n",21);
}
void tim0int()
{
AUXR &= 0x7F;
TMOD = 0xF0;
TL0 = Timer0_Reload%256;
TH0 = Timer0_Reload/256;
TF0=0;
ET0 = 1;
EA = 1; //´ò¿ª×ÜÖжÏ
TR0 = 1;
}
void Print(u8 x,u8 y, u8 dat)//液晶显示程序
{
u8 address;
if(y==0)
{
address=0x80+x;
}
else
{
address=0xC0+x;
}
LCD1602_WriteCmd(address);
LCD1602_WriteData(dat);
}
main()
{
setinoutport();
LCD_Initial();
uart1int();
uart2int();
esp8266int();
tim0int();
for(;;)
{
Print(14,1,count/10+'0');
Print(15,1, count%10+'0');//液晶显示count计数值
if(count%7)
{
sprintf(dataBuf,"Temp:%3.1fC\r\n",32.5);
PrintString("AT+CIPSEND=0,12\r\n",17); //·¢ËÍÊý¾Ý
DelayMs(500);
PrintString(dataBuf,12);//串口2发送数据到ESP8266
DelayMs(500);
}
Print(6,1, flg1+'0');
Print(1,1, 3+'0');
}
}
void fxt0(void) interrupt 1
{
static unsigned char B_ms;
B_ms ++; //50ms++
if(B_ms>20)
{
B_ms=0;
count++;
if(count>60) count=1;
}
}
串口子程序
#include "stc8.h"
#include "main.h"
#include "string.h"
extern unsigned char RX_num=0,RX_i=0;
extern unsigned char RX_buffer[]={0};
unsigned char tbuf=100,i=0;
bit busy2,busy1;
sbit LED2 = P1^6;
extern unsigned char flg1=6;
void DelayUs2x(unsigned char t)
{
while(--t);
}
void uart1int(void)
{
SCON = 0x50; //8???,?????
AUXR |= 0x01; //??1?????2???????
AUXR |= 0x04; //?????1T??
T2L = 0xE0; //???????
T2H = 0xFE; //???????
AUXR |= 0x10; //???2????
ES=1;
EA=1;
}
void uart2int(void) // 9600bps@11.0592MHz
{
S2CON = 0x50;
AUXR |= 0x04;
AUXR |= 0x01;
T2L = 0xE0;
T2H = 0xFE;
AUXR |= 0x10;
IE2 = 0x01;
EA = 1;
}
void Uart1_SendChar(unsigned char Udat)
{
SBUF=Udat;
while(!TI);
TI=0;
}
void Uart1_Send_Data(u8 *Buf , u8 len)
{
unsigned char i;
for(i=0;i<len;i++)
{
Uart1_SendChar(*(Buf+i));
}
}
void Uart1_Isr() interrupt 4
{
if(RI)
{
RI=0;
}
}
void PrintString(unsigned char *puts,unsigned char tt)
{
unsigned char NUM=0;
for (; NUM<tt; puts++)
{
S2BUF = *puts;
NUM++;
busy2 = 1;
while(busy2);
}
}
void Uart2() interrupt 8
{
IE2&=0Xfe;
if (S2CON & S2RI)
{
S2CON =0x50;
RX_buffer[RX_num] = S2BUF;
if(RX_buffer[0]=='+')
{
RX_num++;
if(RX_buffer[RX_num-1]=='-')
{
if(RX_buffer[RX_num-2]=='1') {LED2=1;flg1=1;RX_num=0;}
if(RX_buffer[RX_num-2]=='2') {LED2=0;flg1=2;RX_num=0; }
}
}
else {RX_num=0;}
if(RX_num>tbuf)
RX_num = 0;
}
if (S2CON & S2TI)
{
S2CON =0x50;
busy2 = 0;
}
IE2|=0X01;
}
|
|