1
完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
所有的好日子,我一直被要求制造一个系统,它使用一个与PIC微控制器接口的GPS模块来解析GPS“GPRMC消息”,以加快速度,首先在结中,然后转换为MPH,但是我不确定我的代码是否正确地做了,因为我对GPS有点新的,而且还没有。之前将数组转换为整数。我也不确定如果PUTB上的弱牵引会干扰来自GPS的读数,B端口上的其他引脚将被连接到地上,以设置电路提供以下输出的速度(PIC在速度低于一组时打开电子开关)。下面是我的代码;
以上来自于百度翻译 以下为原文 Good Day all, I have been tasked with fabricating a system which uses a GPS module interfaced with a PIC microcontroller to parse the GPS' GPRMC message to aquire the speed, first in knots and then convert to MPH, but am unsure if my code is doing it correctly as I am kind of new to GPS and haven't converted an array to an integer before. I am also unsure if the weak pull ups on PORTB will interfere with the reading from the GPS, the other pins on port B will be linked to ground to set the speed that the circuit provides an output below (the PIC turns on an electronic switch when the speed is below the one set). below is my code; /******************************************************************************/ /* Project: PIC18F1320 GPS Speed Monitor 1a decode $GPRMC message */ /* Author: Ben Thomas */ /* Date: 12/07/17 */ /* Pin Assignments; */ /* RB4(p10) - RX */ /* RB1(p9) - TX */ /* */ /* */ /******************************************************************************/ #include #include #include #include /******************************************************************************/ /* User Global Variable Declaration */ /******************************************************************************/ #define FREQ 12000000 #define baud 4800 #define spbrg_value (((FREQ/64)/baud)-1) unsigned char rx_data(); unsigned char data,value=0; unsigned int i=0,pos; unsigned char longi_data[12]; unsigned char lati_data[12]; unsigned char knot_data[12]; const int lengthA; uint8_t knot; uint8_t mph; /* i.e. uint8_t /****PROTOTYPES****/ void initialise(void) { SPBRG = spbrg_value; //set baud rate RCSTAbits.SPEN = 1; //activate serial port RCSTAbits.CREN = 1; // enable continuous reception INTCON2bits.RBPU = 0; //enable weak pull-ups on port B //set PORTB bits for speed setting TRISBbits.RB0 = 1; TRISBbits.RB2 = 1; TRISBbits.RB3 = 1; TRISBbits.RB7 = 1; TRISBbits.RB5 = 1; TRISBbits.RB6 = 1; TRISBbits.RB4 = 1; //set RB1 and RB4 for USART TRISBbits.RB1 = 1; } void GPRMCdecode(void) { data = rx_data(); //check the strip '$GPRMC' if(data =='$') { data = rx_data(); if(data == 'G') { data = rx_data(); if(data == 'P') { data = rx_data(); if(data == 'R') { data = rx_data(); if(data == 'M') { data = rx_data(); if(data == 'C') { data = rx_data(); if(data == ',') { data = rx_data(); while(data != ',') //skip time data = rx_data(); if(data == 'A') { data = rx_data(); if(data == ',') { for(i=0;data!='N'||'S';i++) { data=rx_data(); lati_data=data; //store the latitude data } } data = rx_data(); if(data == ',') { for(i=0;data!='E'||'W';i++) { data=rx_data(); longi_data=data; //store longitude data } } data = rx_data(); if(data == ',') { for(i=0;data!='.';i++) { data=rx_data(); knot_data=data; } data=rx_data(); knot_data[i+1]=data; //store speed in knots } } } } } } } } } } void DECODESpeed(void) { knot = 0; const int lengthA = sizeof(knot_data) / sizeof(*knot_data); for(i=0;i knot += knot_data; if(i int digits = (int)ceil(log10((double)knot_data[i+1])); knot *= (int)pow(10, digits); //turn knot from an array to an integer? } } mph = knot * 1.15078; } /******************************************************************************/ /* Main Program */ /******************************************************************************/ void main(void) { /* Initialize I/O and Peripherals for application */ initialise(); //Setspeed(); /* TODO while(1) { GPRMCdecode(); DECODESpeed(); } } unsigned char rx_data(void) { while(PIR1bits.RCIF==0); //wait until RCIF gets low (EUSART buffer is empty) return RCREG; } |
|
相关推荐
2个回答
|
|
是的,还有更好的方法来读取UART的字符串,至少你可以工作。而且,在RX线上没有弱的拔出不应该是一个问题——不管使用LATX寄存器还是不使用PORTX,都要小心地写入其他引脚。
以上来自于百度翻译 以下为原文 Yeah, there are better ways to read a string from the UART Still, yours may work, at least to begin with. And no a weak pullup at RX line should not be an issue - beware of writing to the other pins using LATx registers and not PORTx anyway. You must also check for UART errors anytime. |
|
|
|
你没有描述你有什么问题。你期待什么?什么是工作,什么是不工作?什么是你的GPS信息间隔和什么是你的UART速度。你将无法做任何合理的错误处理这种方式。如果你的代码已经工作了,你可能会用你的第一个版本做一些评估,但是我建议完全改写它,因为你应该明确地分开代码。用于接收一个GPS消息和任何代码解析该文本行,然后使用它进行计算和动作。通常,您将使用一个中断驱动的RX函数,它将一行GPS数据收集到一个缓冲区中(您可以在像$,Cr,LF之类的字符上触发),并设置一个标志,当您处于Trrutt函数认为行是完整的,主while循环将在包含一条消息的缓冲区上运行。在处理消息时,不能接受进来的新字符(在RX中断中)。或者,你可以实现双缓冲,但例如1赫兹,我认为没有必要这么做。但是看看你的代码,我建议不要重新发明轮子。尝试找到一些代码来进行基本的接收,甚至可以解析,然后添加你的真实的特定代码。还决定,你是否真的想要/需要浮点计算。
以上来自于百度翻译 以下为原文 You do not describe what problem you have. What do you expect ? What is working, what is not working ? What is your GPS message interval and what is your UART speed. You won't be able to do any reasonable error processing this way. And errors will always occur. Also it will become difficult to adjust this type of code for any future change in requirements. If your code already works, you might do some evaluation with your first version, but I would recommend to rewrite it completely since you should definitely separate the code for receiving ONE GPS message and any code parsing that line of text, then using it for calculations and actions. Typically you would use an interrupt driven RX function, which collects one line of GPS data into a buffer (you could trigger on characters like $, CR, LF ) and set a flag, when your interrupt function considers the line to be complete. The main while loop then would operate on this buffer containing one message. While processing the message you cannot accept new characters coming in (in your RX interrupt). Alternatively you could implement double buffering but e.g. for 1 Hz I think there is no need to do so. But looking at your code, I would recommend not to reinvent the wheel. Try to find some code which does the basic receiving, may be even parsing, and then add your real specific code. Also decide, whether you really want/need floating point calculations. |
|
|
|
只有小组成员才能发言,加入小组>>
5255 浏览 9 评论
2038 浏览 8 评论
1958 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3219 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2268 浏览 5 评论
794浏览 1评论
689浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
618浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
687浏览 0评论
587浏览 0评论
小黑屋| 手机版| Archiver| 德赢Vwin官网 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-30 20:00 , Processed in 1.909448 second(s), Total 80, Slave 64 queries .
Powered by 德赢Vwin官网 网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
德赢Vwin官网 观察
版权所有 © 湖南华秋数字科技有限公司
德赢Vwin官网 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号