Tangshm作者
6个月5天前IP:北京
949827
18楼

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];

}

}

}

}

}

评论
文号 / 949827

千古风流
名片发私信
学术分 1
总主题 17 帖总回复 202 楼拥有证书:学者 机友
注册于 2024/11/16 16:33:04最后登录 2026/10/28 20:03:44
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:北京

个人简介

研究神经科学的火箭工程师

笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的