单片机的接收端是连接的GPS信号, 发送端连接的是GPRS的接收。
单片机就是把收到的经纬度找出来传给GPRS,用UDP发送。
只要不连GPS, 发送的数据就完全正确。
连了GPS之后,数据就乱了,GPS是一直在发送,速度很快
#include <reg52.h>
#include <intrins.h>
#include <stdlib.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
uchar runFly = 1;
uchar temp;
uchar fir[5] = {0};
uchar dataJW[38] = {0};
uchar dataOK[20] = {0};
uchar nowPoint_fir = 0;
uchar nowPoint_data = 0;
uchar mode = 0;
uchar canSend = 0;
// GPRS模块联网AT+CIPSTART="UDP","IP","PORT" 语句
unsigned char code AT_CIPSTART[] = {"AT+CIPSTART="UDP","113.250.157.114","22223""};
// 定长发送语句 AT+CIPSEND=20
unsigned char code AT_CIPSEND_20[]={"AT+CIPSEND=20"};
void delay_1ms(uint x)
{
uint i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
//向串口发送一个数据
void send_uart(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
// 模块UDP联网指令AT_CIPSTART 发送
void send_AT_CIPSTART()
{
uchar i;
for(i=0;AT_CIPSTART
!=' ';i++)
{
send_uart(AT_CIPSTART);
}
send_uart(0x0d);
send_uart(0x0a);
}
// 发送指令AT_CIPSEND_20 的发送
void send_AT_CIPSEND_20()
{
uchar i;
for(i=0;AT_CIPSEND_20!=' ';i++)
{
send_uart(AT_CIPSEND_20);
}
send_uart(0x0d);
send_uart(0x0a);
}
// 发送经纬度数组 WD[]
void send_WD()
{
uchar i;
for(i=0; i<20; i++)
{
send_uart(dataOK);
}
send_uart(0x0d);
send_uart(0x0a);
}
void uart_init()
{
TMOD = 0x20;
TH1 = 0xfa;
TL1 = 0xfa;
SCON=0x40; //接受数据
PCON = 0x00;
EA = 0;
ES = 0;
TR1 = 1;
}
char cmp(uchar *a, uchar *b)
{
uchar i = 0;
char result = 0;
while(a != ' ' && b != ' ')
{
if(a != b)
{
result = 1;
return result;
}
i++;
}
return result;
}
/*********************************************************/
/* */
/* 主程序 */
/* */
/*********************************************************/
main()
{
uart_init();
while(1)
{
if(runFly)
{
ES = 0;
delay_1ms(50000);
send_AT_CIPSTART();
delay_1ms(50000);
delay_1ms(50000);
send_AT_CIPSTART();
delay_1ms(50000);
delay_1ms(50000);
ES = 1;
}
}
}
//检测数据是否符合发送条件
uchar isSend()
{
if(dataJW[12] == 'A')
{
return 1;
}
return 0;
}
void getOK()
{
uchar j = 0;
for(j = 0; j < 9; j++)
{
dataOK[j] = dataJW[j+14];
}
for(j = 9; j < 20; j++)
{
dataOK[j] = dataJW[j+16];
}
}
//串口接收中断
void analysis(uchar dat)
{
if(dat == '$')
{
mode = 1;
nowPoint_fir = 0;
return ;
}
switch(mode)
{
case 0: break;
case 1: fir[nowPoint_fir] = dat; nowPoint_fir++; break;
case 2: dataJW[nowPoint_data] = dat; nowPoint_data++; break;
default: break;
}
if(nowPoint_fir == 5)
{
nowPoint_fir = 0;
if(cmp(fir, "GPRMC") == 0)
{
mode = 2;
}
else
{
mode = 0;
}
}
if(nowPoint_data == 38)
{
nowPoint_data = 0;
if(isSend() == 1)
{
send_AT_CIPSEND_20();
delay_1ms(50000);
getOK();
send_WD();
}
else
{
mode = 0;
}
}
}
void receive() interrupt 4
{
ES = 0;
if(RI == 1)
{
temp = SBUF;
analysis(temp);
RI = 0;
}
ES = 1;
}
此帖出自小平头技术问答
一周热门 更多>