[mw_shl_code=c,true]
#include "msp430g2553.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 Uart_Init(void)
{
UCA0CTL1|=UCSWRST;
UCA0CTL0&=~UC7BIT;//字符长度为8
UCA0CTL1 |= UCSSEL_2; // CLK = mCLK=1Mhz
UCA0BR0 = 208; // 4800
UCA0BR1 = 0x00; //
UCA0MCTL = UCBRS1+ UCBRS0; // Modulation UCBRSx = 3
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
P1SEL|=BIT1+BIT2; //将P1.1 P1.2设为第二功能
P1SEL2|=BIT1+BIT2;
// _BIS_SR(LPM3_bits + GIE);
TA0CCTL0|= CCIE; //设置捕获/比较控制寄存器,CCIE=0x0010,使能捕获比较中断
TA0CTL = TASSEL_2+MC_1; // 设置定时器A控制寄存器,TA0CTL = MC_1;
TA0CCR0 =10000; //设置捕获/比较寄存器,初始值为12000,对于ACLK时钟频率为12khz的频率,相当于1s
_EINT();//开总中断
}
/*void TimerA_init(void)
{
CCTL0|= CCIE; //设置捕获/比较控制寄存器,CCIE=0x0010,使能捕获比较中断
CCR0 =7200; //设置捕获/比较寄存器,初始值为12000,对于ACLK时钟频率为12khz的频率,相当于1s
TA0CTL = TASSEL_2+MC_1; // 设置定时器A控制寄存器,TA0CTL = MC_1;
}*/
#pragma vector=TIMER0_A0_VECTOR//固定的格式
__interrupt void Timer_A (void) //定时器A的CC0中断处理程序 必须是没有返回值的
{
change_page++; //换页
if (change_page == 10)
change_page = 0;
// _EINT();
}
#pragma vector=USCIAB0RX_VECTOR
__interrupt void usart0_rx(void)
{
char ch;
IE2&=~UCA0RXIE;
if((IFG2&UCA0RXIE) )
{
//a=UCAORXBUF;
//i++;C
ch=UCA0RXBUF;
P1OUT ^= 0x01;
if ((ch == '$') && (gps_flag == 0)) //如果收到字符'$',便开始接收
{
rev_start = 1;
rev_stop = 0;
}
if (rev_start == 1) //标志位为1,开始接收
{
rev_buf[num++] = ch; //字符存到数组中
if (ch == '\n') //如果接收到换行
{
rev_buf[num] ='\0';
rev_start = 0;
rev_stop = 1;
gps_flag = 1;
num = 0;
}
}
}
IFG2&=~UCA0RXIFG;
IE2 |=UCA0RXIE;
}
void delay11(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=20;y>0;y--);
}
/****************************************
主函数
****************************************/
void main(void)
{
P1DIR = 0XFF 1OUT = 0XFF;
P2DIR = 0XFF 2OUT = 0XFF;
WDTCTL = WDTPW + WDTHOLD;
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
uchar error_num = 0;
Uart_Init(); //初始化串口
lcdinit(); //初始化LCD
GPS_Init(); //初始化GPS
rev_stop=0;
_EINT();
while(1)
{
if (rev_stop==1) //如果接收完一行
{
TA0CCTL0|= CCIE;//开启定时器0
if(change_page%2==1)//换页
{
if (GPS_RMC_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)) //解析GPGGA
{
GPS_DisplayOne(); //显示第一页信息
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;
}
}
}
}
}
[/mw_shl_code]
老是运行到if (rev_stop==1) //如果接收完一行 就不在运行,是GPRMC信号,lcd翻页显示
|