GPS串口通讯代码有更新,使用标准GPIO串口通讯函数uart及中断。单片机主频降至66.5MHz,在集成度高的情况下减少对GPS干扰。下面是读取日期时间及卫星数的例程:
#include <hardware/clocks.h>
#include "hardware/uart.h"
#include "hardware/irq.h"
uint16_t Yr;
uint8_t SA,Mt,Dy,Hr,Mn,Sd;
unsigned char GPS_buf[75];
void setup() {
set_sys_clock_khz(66500,true);
//使用Uart0串口及中断读GPS
uart_init(uart0, 115200); // 初始化 UART
gpio_set_function(12, GPIO_FUNC_UART); // 设置 GPIO 引脚为 UART 功能(例如 GP12 作为 TX,GP13作为 RX)
gpio_set_function(13, GPIO_FUNC_UART);
gpio_pull_up(13); // 确保 RX 引脚上拉,避免悬空输入
uart_set_irqs_enabled(uart0, true, false); // 启用 UART 中断,读数中断,写数不中断
irq_set_exclusive_handler(UART0_IRQ, GPS_Read); // 设置GPS读数中断处理函数
irq_set_enabled(UART0_IRQ, true); // 启用串口中断
delay(1000);
}
// GPS读数中断函数
void GPS_Read() {
while (uart_is_readable(uart0)) {
uint8_t ch = uart_getc(uart0); // 读取一个字
if (ch==0xB5){
ch = uart_getc(uart0); // 读取一个字
if (ch==0x62){
uint8_t msgClass = uart_getc(uart0);
uint8_t msgId = uart_getc(uart0);
uint8_t length = (uart_getc(uart0));
if (msgClass 0x01 && msgId 0x07) { // NAV-POSLLH消息
for(uint8_t i=0;i<73;i++){
GPS_buf[i] = uart_getc(uart0);
}
Yr = (GPS_buf[6]<<8)|GPS_buf[5];
Mt = GPS_buf[7];
Dy = GPS_buf[8];
Hr = GPS_buf[9]+8;
Mn = GPS_buf[10];
Sd = GPS_buf[11];
SA = GPS_buf[24];
}
}
}
}
}