#include <msp430x14x.h>
#include "stdio.h"
#include "string.h"
#include "GPS.h"
#include "LCD.h"
#include "display.h"
char rev_buf[80]; //接收缓存
uchar rev_start = 0; //接收开始标志
uchar rev_stop = 0; //接收停止标志
uchar gps_flag = 0; //GPS处理标志
uchar change_page = 0; //换页显示标志
uchar num = 0;
extern GPS_INFO GPS; //在display.c中定义,使用时要加extern
void init_clock()
{
uchar i;
BCSCTL1 &= ~XT2OFF; //控制XT2振荡器开启(XT2OFF=0)
do
{
IFG1 &= ~OFIFG; //清除振荡器失效标志
i = 255;
while(i--); //延时等待至少50us
}while(IFG1&OFIFG); //判断XT2是否起振
IFG1&=~OFIFG;
BCSCTL2 |=SELM1+SELS;//选择主要时钟 MCLK:XTAL2, SMCLK:XTAL2,不分频
//清除振荡器错误中断标志,系统启动时不稳定造成的,必须要复位
}
void IO_init(void)
{
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0XFF;
}
void InitUART(void)
{
P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
ME1 |= URXE0; // Enable USART0 T/RXD
UCTL0 |= 0x10; // 8-bit character
UTCTL0 |=SSEL1; // UCLK = SMCLK
UBR00 = 0x03; // 32k/9600 - 3.41
UBR10 = 0x00; //
UMCTL0 = 0x4A; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
IE1 |= URXIE0; // 使能USART0的接收中断
}
void int_timer()
{
TACTL = TASSEL_2+TACLR+TAIE;
CCTL0 = CCIE;
CCR0 = 16384; //设定周期0.5S
// TACTL |=MC_2;
}
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
init_clock();
IO_init();
uchar error_num = 0;
InitUART(); //初始化串口
int_timer();
_EINT(); //打开全局中断
Lcd_Init(); //初始化LCD
GPS_Init(); //初始化GPS
rev_stop=0;
while(1)
{
if (rev_stop) //如果接收完一行
{
TACTL |= MC_1 ;
CCTL0 = CCIE; //使能CCR0中断
//CCR0 = 16384;
if (change_page % 2 == 1) //换页
{
if (GPS_GGA_Parse(rev_buf, &GPS)) //解析GPGGA
{
GPS_DisplayTwo(); //显示第二页信息
error_num = 0;
gps_flag = 0;
rev_stop = 0;
}
else
{
error_num++;
if (error_num >= 20) //如果数据无效超过20次
{
error_num = 20;
GPS_Init(); //返回初始化
}
gps_flag = 0;
rev_stop = 0;
}
}
else
{
if (GPS_RMC_Parse(rev_buf, &GPS)) //解析GPRMC
{
GPS_DisplayOne(); //显示GPS第一页信息
error_num = 0;
gps_flag = 0;
rev_stop = 0;
}
else
{
error_num++;
if (error_num >= 20) //如果数据无效超过20次
{
error_num = 20;
GPS_Init(); //返回初始化
}
gps_flag = 0;
rev_stop = 0;
}
}
}
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
static uchar count = 0;
CCR0 = 5000; //设定周期0.5S
count++;
if (count == 1) //2*5秒钟
{
count = 0;
change_page++; //换页
if (change_page == 10)
change_page = 0;
}
}
#pragma vector = UART0RX_VECTOR
__interrupt void UART0_RXISR(void)
{
uchar ch;
IE1 &= ~URXIE0;
while (!(IFG1 & URXIFG0))
{
ch = RXBUF0;
if ((ch == '$') && (gps_flag == 0)) //如果收到字符'$',便开始接收
{
rev_start = 1;
rev_stop = 0;
}
if (rev_start == 1) //标志位为1,开始接收
{
rev_buf[num++] = ch; //字符存到数组中
if (ch == '
') //如果接收到换行
{
rev_buf[num] = ' ';
rev_start = 0;
rev_stop = 1;
gps_flag = 1;
num = 0;
}
}
}
IFG1&=~URXIFG0;
IE1 |= URXIE0;
}
此帖出自
小平头技术问答
一周热门 更多>