于GPS_CHECKTIAMER串口收发程序
时间:08-25
来源:互联网
点击:
单片机串口发送接收程序,下面是关于GPS_CHECKTIAMER,局部应用程序#define UART_R_LEN 60#define UART_T_LEN 34uchar idata trdata[UART_T_LEN]; //定义串口发送缓冲区uchar idata gpsdata[UART_R_LEN]; //定义GPS数据串口接收缓冲区uchar *inlast_t = trdata; //最后放进发送缓冲区的uchar *outlast_t = trdata; //最后从发送缓冲区出去的uchar *inlast_r = gpsdata; //最后进入接收缓冲区的uchar *outlast_r = gpsdata; //最后从接收缓冲区取走的bit t_empty,t_full,r_empty,r_full; //缓冲区的状态标志位bit new_time,t_finish; //接收完标志和发送完标志void serail(void) interrupt 4{if(TI) //如果发送中断置位{TI = 0;if(!t_empty) //如果发送缓冲区数据没有发完{SBUF = *outlast_t; //最后从发送缓冲区出去的字符送SBUFoutlast_t++; //最后发送字符指针地址增1t_full = 0; //发送区状态为未满if(outlast_t >= (trdata + UART_T_LEN)) //如果最后发送字符的地址超出outlast_t = trdata; //地址到顶部回到底部if(outlast_t == inlast_t) //如果最后发送字符的地址为最后进入发送的字符地址t_empty = 1;}else t_finish = 1;} if(RI) //如果接收中断置位{RI = 0;if(~r_full) //如果接收缓冲区未满{*inlast_r = SBUF; //最后进入接收缓冲区的字符送SBUFinlast_r++; //最后进入接收缓冲区的字符地址增1r_empty = 0; //接收缓冲区为非空if(inlast_r >= (gpsdata + UART_R_LEN)) //如果最后接收字符的地址超出inlast_r = gpsdata; //地址到顶部回到底部if(inlast_r == outlast_r)
//如果最后接收字符的地址等于最后从接收区取走的字符地址r_full = 1; //则接收区满}} }void loadmsg(uchar *msg, int num) //把字符串放入发送缓冲区准备发送{int i;for(i=0; i num; i++) {*inlast_t = *msg; //字符装入最后进入发送发送缓冲区的内容msg++; //字符串和缓冲地址同时增1inlast_t++;t_empty = 0; //未发送完if(inlast_t >= (trdata+UART_T_LEN))inlast_t = trdata; //地址到顶部回到底部if(inlast_t == outlast_t)t_full = 1; //发送缓冲区满}if(t_finish){TI = 1;t_finish = 0;}}uchar getbyte_r(void) //从接收缓冲区取一个字节{uchar c;while(r_empty); //当接收缓冲区等待接收ES = 0; //禁止串行中断c = *outlast_r; //最后从接收缓冲区取走的数据赋值给Cr_full = 0; //接收未满outlast_r++; //最后由接收缓冲区取走的数据地址增1if(outlast_r >= (gpsdata + UART_R_LEN)) //如果地址超出outlast_r = gpsdata; //地址回原处if(outlast_r == inlast_r)
//如果最后接收字符的地址等于最后从接收区取走的字符地址r_empty = 1; //接收缓冲区为空ES = 1; //恢复串行中断return c;}uchar r_state = 0;uchar r_byte = 0;uchar gps_chksum= 0;void r_string(void) // 接收字符串{uint *ptr;uchar temp;uchar tp[7];
//定义时标接收区,所存储的时标数据为接收缓冲区未作转换的时标,用来作全局时标的中转存储while(!r_empty !new_time) //若接收缓冲区非空,且接收未完成{temp = getbyte_r(); //每次while循环依次取入一个字节的接收数据switch(r_state)
//如果依次取入的数据符合表头“@@Hb”,r_state位置4,进入取时标及GPS状态字节{case 0:if(temp == 0x40) //@r_state = 1;break;case 1:if(temp == 0x40) //@ {r_state = 2;gps_chksum = 0;}else r_state = 0;break;case 2:if(temp == 0x48) //H{r_state = 3;gps_chksum = gps_chksum^temp;}else r_state = 0;break;case 3:if(temp == 0x62) //b{r_state = 4;r_byte = 0;gps_chksum = gps_chksum^temp;}else r_state = 0;break;case 4:if(r_byte == 47){ if(gps_chksum == temp)
//在完成有用数据采集后,经过数据转换,将标准字符存入全局时标缓冲中{ptr = (uint*)(tp+2); //经转换数据送时标缓冲区 gps_dt[0] = (*ptr)%100; //年gps_dt[1] = tp[0]; //月gps_dt[2] = tp[1]; //日gps_dt[3] = tp[4]; //时gps_dt[4] = tp[5]; //分gps_dt[5] = tp[6]; //秒new_time = 1; } r_state = 0; //用于判断的数据归零,以便下一次取数据}else{gps_chksum = gps_chksum^temp;if(r_byte 7)tp[r_byte] = temp;else if(r_byte == 38)gps_state = temp;r_byte++;} break;default:break;}}}
GPS CHECKTIAMER 串口收发 相关文章:
- 基于GPS和GPRS的小型追踪器设计方案(05-20)
- 基于GPSOne技术的个人定位终端(07-30)
- 2010年中国(成都)电子展胜利闭幕(08-15)
- 基于DSP的车载GPS/DR组合导航系统硬件设计(09-13)
- ARM-Linux平台下GPS信号的采集与处理研究(07-25)
- 基于MiniGUI的GPS导航定位系统设计(02-20)