#include <AVR_PQ1A.H> //包含自定义常量头文件
#include <AVR interrupt.h>
uchar buf[500];
uint readCount=0;
uint writeCount=0;
uchar Time[6];
uchar Date[6];
uchar Status;
uchar Latitude[9];
uchar NSIndicator;
uchar Longitude[10];
uchar EWIndicator;
uchar Speed[4];
void Uart_init(uint baud)
{
baud=MCLK/16/baud-1;
UCSRB=0x00;
UCSRA=0x00;
UCSRC=(1<<URSEL)|(0<<UPM0)|(3<<UCSZ0); UBRRH="baud" UBRRL="baud;" 选择UCSRC,异步模式,禁止校验,1位停止位,8位数据位>>8;
UCSRB=(1<<TXEN)|(1<<RXEN)|(1<<RXCIE); if(writeCount ++writeCount; 将接收到的数据存入全局数组 buf[writeCount]="UDR;" 关闭接收中断 UCSRB&="~BIT(RXCIE);" { ISR(USART_RXC_vect) ******************************************** 无 : 返回值 数: 参 异步串口接收 能: 功 Uart_RX 函数名称: ******************************************* } Uart_sendB(*sendpt++); 字符串未结束则继续发送 while(*sendpt) *sendpt) Uart_sentstr(uchar void sendpt--发送的数组指针 异步串口发送一个字符串 Uart_sentstr 清除发送完毕状态位 UCSRA|="1<<TXC;" 等待发送完毕 while(!(UCSRA&(1<<TXC))); 发送数据 UDR="sendB;" 等待发送缓冲区为空 while(!(UCSRA&(1<<UDRE))); sendB) Uart_sendB(uchar sendB--发送的字节数据 异步串口发送一个字节 Uart_sendB 配置TX为输出(很重要) DDRD|="0X02;" 全局中断开放 sei(); 接收、发送使能,接收中断使能> 499)
writeCount = 0;
UCSRB|=BIT(RXCIE);
}
uchar Read_byte(void)
{
uchar temp;
while(readCount == writeCount)
{
Delayms(10);
}
temp = buf[readCount];
++readCount;
if(readCount > 499)
readCount = 0;
return temp;
}
void LCD1602_portini(void)
{
LCDa_CTRL_DDR |= BIT(LCDa_RS)|BIT(LCDa_RW)|BIT(LCDa_E);
LCDa_DATA_DDR |= 0xFF;
}
uchar LCD1602_readbyte(uchar DatCmd)
{
uchar dByte;
if (DatCmd == iCmd)
LCDa_CLR_RS;
else
LCDa_SET_RS;
LCDa_SET_RW;
LCDa_SET_E;
LCDa_DATA_DDR=0x00;
dByte=LCDa_DI;
Delayms(1);
LCDa_CLR_E;
LCDa_DATA_DDR|=0xff;
return dByte;
}
void LCD1602_sendbyte(uchar DatCmd, uchar dByte)
{
if (DatCmd == iCmd)
LCDa_CLR_RS;
else
LCDa_SET_RS;
LCDa_CLR_RW;
LCDa_SET_E;
LCDa_DO = dByte;
Delayms(1);
LCDa_CLR_E;
}
void LCD1602_sendstr(uchar *ptString)
{
while((*ptString)!='\0')
{
LCD1602_sendbyte(iDat, *ptString++);
}
}
void LCD1602_clear(void)
{
LCD1602_sendbyte(iCmd,LCDa_CLS);
Delayms(40);
}
uchar LCD1602_readBF(void)
{
uchar busy;
busy=LCD1602_readbyte(iCmd);
if(busy&0x80)
busy=1;
else
busy=0;
return busy;
}
void LCD1602_gotoXY(uchar Row, uchar Col)
{
switch (Row)
{
case 1:
LCD1602_sendbyte(iCmd, LCDa_L1 + Col); break;
case 2:
LCD1602_sendbyte(iCmd, LCDa_L2 + Col); break;
default:
break;
}
}
void LCD1602_initial(void)
{
Delayms(100);
LCD1602_portini();
LCD1602_sendbyte(iCmd, LCDa_FUNCTION);
while(LCD1602_readBF());
LCD1602_sendbyte(iCmd, LCDa_ON);
while(LCD1602_readBF());
LCD1602_clear();
while(LCD1602_readBF());
LCD1602_sendbyte(iCmd, LCDa_ENTRY);
}
void clear_gprmc(void)
{
uchar i;
for(i=0; i<6; i++)
{
Time[i] = '0';
Date[i] = '0';
}
for(i=0; i<9; i++)
{
Latitude[i] = '0';
}
for(i=0; i<10; i++)
{
Longitude[i] = '0';
}
for(i=0; i<4; i++)
{
Speed[i] = '0';
}
Status = '0';
NSIndicator = '0';
EWIndicator = '0';
}
void Parse_gprmc(void)
{
uchar i;
uchar temp;
clear_gprmc();
Read_byte();
temp = Read_byte();
if(',' != temp)
{
Time[0] = temp;
for(i=0; i<5; i++)
{
Time[i+1] = Read_byte();
}
Read_byte();
Read_byte();
Read_byte();
Read_byte();
}
Status = Read_byte();
Read_byte();
temp = Read_byte();
if(',' != temp)
{
Latitude[0] = temp;
for(i=0; i<3; i++)
{
Latitude[i+1] = Read_byte();
}
Read_byte();
for(i=0; i<5; i++)
{
Latitude[4+i] = Read_byte();
}
Read_byte();
}
temp = Read_byte();
if(',' != temp)
{
NSIndicator = temp;
Read_byte();
}
temp = Read_byte();
if(',' != temp)
{
Longitude[0] = temp;
for(i=0; i<4; i++)
{
Longitude[i+1] = Read_byte();
}
Read_byte();
for(i=0; i<5; i++)
{
Longitude[5+i] = Read_byte();
}
Read_byte();
}
temp = Read_byte();
if(',' != temp)
{
EWIndicator = temp;
Read_byte();
}
temp = Read_byte();
if(',' != temp)
{
Speed[0] = temp;
Read_byte();
for(i=0; i<3; i++)
{
Speed[1+i] = Read_byte();
}
Read_byte();
}
Read_byte();
temp = Read_byte();
if(',' != temp)
{
Date[0] = temp;
for(i=0; i<5; i++)
{
Date[i+1] = Read_byte();
}
}
}
void Display_gprmc(void)
{
uchar high,low,i;
high = Time[0];
low = Time[1];
low = low+8;
if(low > 57)
{
low = low - 10;
high = high + 1;
}
LCD1602_clear();
while(LCD1602_readBF());
LCD1602_gotoXY(1,0);
LCD1602_sendbyte(iDat,high);
LCD1602_sendbyte(iDat,low);
LCD1602_sendbyte(iDat,':');
LCD1602_sendbyte(iDat,Time[2]);
LCD1602_sendbyte(iDat,Time[3]);
LCD1602_sendbyte(iDat,':');
LCD1602_sendbyte(iDat,Time[4]);
LCD1602_sendbyte(iDat,Time[5]);
LCD1602_sendbyte(iDat,' ');
LCD1602_sendbyte(iDat,Status);
while(LCD1602_readBF());
LCD1602_gotoXY(2,0);
LCD1602_sendbyte(iDat,Date[4]);
LCD1602_sendbyte(iDat,Date[5]);
LCD1602_sendbyte(iDat,'.');
LCD1602_sendbyte(iDat,Date[2]);
LCD1602_sendbyte(iDat,Date[3]);
LCD1602_sendbyte(iDat,'.');
LCD1602_sendbyte(iDat,Date[0]);
LCD1602_sendbyte(iDat,Date[1]);
Delayms(50000);
LCD1602_clear();
while(LCD1602_readBF());
LCD1602_gotoXY(1,0);
LCD1602_sendbyte(iDat,Latitude[0]);
LCD1602_sendbyte(iDat,Latitude[1]);
LCD1602_sendbyte(iDat,'.');
for(i=0; i<7; i++)
{
LCD1602_sendbyte(iDat,Latitude[2+i]);
}
LCD1602_sendbyte(iDat,' ');
LCD1602_sendbyte(iDat,NSIndicator);
while(LCD1602_readBF());
LCD1602_gotoXY(2,0);
LCD1602_sendbyte(iDat,Longitude[0]);
LCD1602_sendbyte(iDat,Longitude[1]);
LCD1602_sendbyte(iDat,Longitude[2]);
LCD1602_sendbyte(iDat,'.');
for(i=0; i<7; i++)
{
LCD1602_sendbyte(iDat,Longitude[3+i]);
}
LCD1602_sendbyte(iDat,' ');
LCD1602_sendbyte(iDat,EWIndicator);
Delayms(50000);
}
void Read_gprmc(void)
{
uchar temp = 0;
temp = Read_byte();
if(temp == '$')
{
Read_byte();
Read_byte();
temp = Read_byte();
if(temp == 'R')
{
temp = Read_byte();
if(temp == 'M')
{
temp = Read_byte();
if(temp == 'C')
{
Parse_gprmc();
Display_gprmc();
}
}
}
}
}
int main(void)
{
Board_init( );
Uart_init(9600);
LCD1602_initial();
while(LCD1602_readBF());
LCD1602_gotoXY(1,0);
LCD1602_sendstr("AVR_PQ1A BOARD");
while(1)
{
Read_gprmc();
}
}