如图所示,单片机载入程序后,没有按照程序执行,蜂鸣器轻微响起,LED全亮,数码管第一个和最后一个数字亮起。
我刚开始以为是IO口复用引起的,修改了一下IO口结果也一样。
我觉得可能是串口有问题,求各位专家指点!
main.c
#include <REGX52.H>
#include<intrins.h>
#include "Delay.h"
#include "ADC0832.h"
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include "uart.h"
#define FOSC_110592M
#define uchar unsigned char
#define uint unsigned int
#define false 0
#define true 1
unsigned int AD_X;
unsigned int AD_Y;
//Unicode
char Unicode_latitude[10*latitude_Length];
char Unicode_N_S[10*N_S_Length];
char Unicode_longitude[10*longitude_Length];
char Unicode_E_W[10*E_W_Length];
void errorLog(int num)//解析错误
{
while (1)
{
Uart1Sends("ERROR");
Uart1Send(num+0x30);
Uart1Sends("\r\n");
}
}
void parseGpsBuffer()//获得GPS数据
{
char *subString;
char *subStringNext;
char i = 0;
if (Save_Data.isGetData)
{
Save_Data.isGetData = false;
//Uart1Sends("**************\r\n");
//Uart1Sends(Save_Data.GPS_Buffer);
for (i = 0 ; i <= 6 ; i++)
{
if (i == 0)
{
if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
errorLog(1); //解析错误
}
else
{
subString++;
if ((subStringNext = strstr(subString, ",")) != NULL)
{
char usefullBuffer[2];
switch(i)
{
case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break; //获取UTC时间
case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break; //获取UTC时间
case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break; //获取纬度信息
case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break; //获取N/S
case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break; //获取经度信息
case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break; //获取E/W
default:break;
}
subString = subStringNext;
Save_Data.isParseData = true;
if(usefullBuffer[0] == 'A')
Save_Data.isUsefull = true;
else if(usefullBuffer[0] == 'V')
Save_Data.isUsefull = false;
}
else
{
errorLog(2); //解析错误
}
}
}
}
}
/***
void printGpsBuffer()//输出GPS数据
{
if (Save_Data.isParseData)
{
Save_Data.isParseData = false;
Uart1Sends("Save_Data.UTCTime = ");
Uart1Sends(Save_Data.UTCTime);
Uart1Sends("\r\n");
if(Save_Data.isUsefull)
{
Save_Data.isUsefull = false;
Uart1Sends("Save_Data.latitude = ");
Uart1Sends(Save_Data.latitude);
Uart1Sends("\r\n");
Uart1Sends("Save_Data.N_S = ");
Uart1Sends(Save_Data.N_S);
Uart1Sends("\r\n");
Uart1Sends("Save_Data.longitude = ");
Uart1Sends(Save_Data.longitude);
Uart1Sends("\r\n");
Uart1Sends("Save_Data.E_W = ");
Uart1Sends(Save_Data.E_W);
Uart1Sends("\r\n");
}
else
{
Uart1Sends("GPS DATA is not usefull!\r\n");
}
}
}
***/
//ASCLL-Unicode
void ASCII_TO_Unicode(char *ASCII,char *Unicode)
{
int length;
int i = 0;
int j = 0;
memset(Unicode,'\0',sizeof(Unicode));
length = strlen(ASCII);
for(i=0;i<length;i++)
{
Unicode[j++] = '0';
Unicode[j++] = '0';
Unicode[j++] = (ASCII[i] / 16) + 0x30;
Unicode[j++] = (ASCII[i] % 16) + 0x30;
}
}
void main()
{
Uart_Init();
Delay(10000);
clrStruct();
while(1)
{
AD_X=ADC0832(1);
AD_Y=ADC0832(0);
parseGpsBuffer();
ASCII_TO_Unicode(Save_Data.latitude,Unicode_latitude);
//ASCII_TO_Unicode(Save_Data.N_S,Unicode_N_S);
ASCII_TO_Unicode(Save_Data.longitude,Unicode_longitude);
//ASCII_TO_Unicode(Save_Data.E_W,Unicode_E_W);
//printGpsBuffer();
if((AD_X<=47 || AD_X>=126) || (AD_Y>=141 || AD_Y<=57))
{
//----------------为什么是下面这些AT指令呢,请看群共享文件SIM900A重要的短信指令文件------------
Uart1Sends("AT\r\n");
Delay(2000);//延时大约2秒
//Uart1Sends("AT+CSCS=\"UCS2\"\r\n");
//Delay(2000);//延时大约2秒
Uart1Sends("AT+CMGF=1\r\n");
Delay(2000);//延时2秒
Uart1Sends("AT+CSCA?\n");
Delay(2000);//延时2秒
Uart1Sends("AT+CSMP=17,167,0,25\r\n");
Delay(2000);//延时2秒
//Uart1Sends("AT+CMGS=\"00310039003100360037003500340036003700320034\"\r\n");//此处修改为对方的电话号,电话话的UNICON编码,可使用我们配套的
//资料包中的 中文转UNICON编码工具来获取。特别注意:那个软件
//获得的unicon 编码待有空格,实际中要去掉,否则模块会报错
Uart1Sends("AT+CMGS=\"19167546724\"\r\n");
Delay(2000);//延时2秒
Uart1Sends("4F7F7528800553EF80FD64545012FF0C8BF7524D5F8063075B9A573070B9655152A9FF01");//修改短信内容,短信内容可为中文、英文和数字,但都是对应的unicon 编码。
Uart1Sends(Unicode_latitude);
Uart1Sends("004E0053");
Uart1Sends(Unicode_longitude);
Uart1Sends("00450057");
/***
Uart1Sends("使用者可能摔倒,请前往");
Uart1Sends(Save_Data.N_S);
Uart1Sends(Save_Data.latitude);
Uart1Sends(Save_Data.E_W);
Uart1Sends(Save_Data.longitude);
Uart1Sends("进行救助!");
***/
Uart1Send(0x1a);
Delay(8000);//延时10秒
}
Delay(100);
}
}
uart.c
#include "uart.h"
//以下是开机后发送到手机的内容,发送的号码在程序中修改。
unsigned int rec_data_len_uart=0; //标记Buffer_Uart0接收数组
unsigned char idata Buffer_Uart0_Rec[25]={0}; //Uart0中断接收数组
//注意,无论接收到信号还是发送完信号,都会进中断服务程序的
char idata gpsRxBuffer[gpsRxBufferLength];
unsigned char RX_Count = 0;
_SaveData Save_Data;
void Uart_Init()
{
SCON = 0X50; //UART方式1;8位UART
REN = 1; //允许串行口接收数据
PCON = 0x00; //SMOD=0;波特率不加倍
TMOD = 0x20; //T1方式2,用于产生波特率
TH1 = 0xFD; //装初值
TL1 = 0xFD;
TR1 = 1; //启动定时器1
EA = 1; //打开全局中断控制
ES = 1; //打开串行口中断
}
void Uart1Sends(unsigned char *p) //·¢?í×?·?′?
{
while(*p)
{
SBUF=*(p++);
while(TI==0)
{
};
TI=0;
}
}
void Uart1Send(unsigned char c) //·¢?íò???×?·?
{
TI=0;
SBUF=c;
while(TI==0);
TI=0;
}
/***
void Serial_interrupt() interrupt 4
{
unsigned char temp_rec_data_uart0;
temp_rec_data_uart0 = SBUF;//读取接收数据
RI=0;//接收中断信号清零,表示将继续接收
Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0; //接收数据
rec_data_len_uart++;
if(rec_data_len_uart>24)
{
rec_data_len_uart=0; //从头开始接收数据
}
}
***/
/*串行通讯中断,收发完成将进入该中断*/
void RECEIVE_DATA(void) interrupt 4
{
unsigned char temp = 0;
char i = 0;
ES=0;
temp = SBUF;
RI = 0;
if(temp == '$')
{
RX_Count = 0;
}
if(RX_Count <= 5)
{
gpsRxBuffer[RX_Count++] = temp;
}
else if(gpsRxBuffer[0] == '$' &gpsRxBuffer[4] == 'M' && gpsRxBuffer[5] == 'C') //è·?¨ê?·?ê?μ?"GPRMC/GNRMC"?aò???êy?Y
{
gpsRxBuffer[RX_Count++] = temp;
if(temp == '\n')
{
memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); //????
memcpy(Save_Data.GPS_Buffer, gpsRxBuffer, RX_Count); //±£′?êy?Y
Save_Data.isGetData = true;
RX_Count = 0;
memset(gpsRxBuffer, 0, gpsRxBufferLength); //????
}
if(RX_Count >= 75)
{
RX_Count = 75;
gpsRxBuffer[RX_Count] = '\0';//ìí?ó?áê?·?
}
}
ES=1;
}
void clrStruct()
{
Save_Data.isGetData = false;
Save_Data.isParseData = false;
Save_Data.isUsefull = false;
memset(Save_Data.GPS_Buffer, 0, GPS_Buffer_Length); //????
memset(Save_Data.UTCTime, 0, UTCTime_Length);
memset(Save_Data.latitude, 0, latitude_Length);
memset(Save_Data.N_S, 0, N_S_Length);
memset(Save_Data.longitude, 0, longitude_Length);
memset(Save_Data.E_W, 0, E_W_Length);
}
uart.h
#ifndef __GPS_H__
#define __GPS_H__
#include <reg52.h>
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define false 0
#define true 1
//定义数组长度
#define GPS_Buffer_Length 80
#define UTCTime_Length 11
#define latitude_Length 11
#define N_S_Length 2
#define longitude_Length 12
#define E_W_Length 2
typedef struct SaveData
{
char GPS_Buffer[GPS_Buffer_Length];
char isGetData; //是否获取到GPS数据
char isParseData; //是否解析完成
char UTCTime[UTCTime_Length]; //UTC时间
char latitude[latitude_Length]; //纬度
char N_S[N_S_Length]; //N/S
char longitude[longitude_Length]; //经度
char E_W[E_W_Length]; //E/W
char isUsefull; //定位信息是否有效
} xdata _SaveData;
//函数或者变量声明
extern void Uart_Init();
extern void UartPrintf(unsigned char *p);
extern void UartPrintASCII(unsigned char c);
extern void clrStruct();
#define gpsRxBufferLength 76
extern char idata gpsRxBuffer[gpsRxBufferLength];
extern unsigned char RX_Count;
extern _SaveData Save_Data;
#endif