C 从GPS模块读取UART接收缓冲器时,程序挂起
这是在我为我的capstone项目设计的定制PCB上,我仍然对此感到困惑,因为不幸的是,我没有很多UART的经验。我确信这就是问题所在,我只发现讨论主要围绕arduino用户等,而不是C编程/UART通信 我的程序使其进入并读取U3STAbits.URXDA true(缓冲区已满),但它不会继续超过该点,并且在尝试读取U3RXREG后不会转到该行 我使用的是PIC24FJ512GA606微控制器和MPLAB X IDE,我没有任何外设库 我试图与之通信的GPS模块是Antenova M20048,当我在模式寄存器下启用UARTEN时,备用引脚变高 到目前为止,我的模块没有收到任何响应,我没有办法在家里探测它,看看它是否真的有响应,缓冲区被标记为已满的事实给了我希望它一切正常,如果有人有任何想法,也许我只是遗漏了一些东西C 从GPS模块读取UART接收缓冲器时,程序挂起,c,gps,microcontroller,uart,pic,C,Gps,Microcontroller,Uart,Pic,这是在我为我的capstone项目设计的定制PCB上,我仍然对此感到困惑,因为不幸的是,我没有很多UART的经验。我确信这就是问题所在,我只发现讨论主要围绕arduino用户等,而不是C编程/UART通信 我的程序使其进入并读取U3STAbits.URXDA true(缓冲区已满),但它不会继续超过该点,并且在尝试读取U3RXREG后不会转到该行 我使用的是PIC24FJ512GA606微控制器和MPLAB X IDE,我没有任何外设库 我试图与之通信的GPS模块是Antenova M20048
#define FCY 8000000 //required for __delay_us(time) function
#include "elen116header.h"
#include <libpic30.h>
#include <stdint.h>
#include <math.h>
#include <string.h>
int main(void)
{
UART2_Initialize();
UART3_Initialize();
__delay_ms(250);
__delay_ms(250);
printf("LoRa Tranciever Unit Online\n") ;
char c='0'; //declare a character
char data[80] = {'\0'}; //declare a character array to store an entire message
int count = 80; //declare an integer to be used as an index variable
__delay_ms(250);
__delay_ms(250);
//transmit the command to GPS
strcpy(data,"$PMTK414*33\r\n");
printf(" Sending: ");
int i;
count = 80;
for(i = 0; i < count; i++)
{
c = data[i];
if(c == '\0' )
{
memset(data, '\0', sizeof(char) * 80);//clear data
i = count;
}
else
{
UART3_Write(c);
printf("%c", c);
}
}
printf("\n\n Reading: ");
//transmission of command is finished, now read in characters
while(1) //forever read in and store the last NMEA message
{
if(U3STAbits.OERR) //check to see if there's an overrun error
U3STAbits.OERR = 0;//clear to allow messages to be received
if(U3STAbits.URXDA) //check if data is ready to be read
{
printf("read attempt ");
c = U3RXREG; //get a character from the receive buffer
printf("%c", c);
while(!(U3STAbits.URXDA == 1));
if(c == '$') //c == $, hex=0x0024
{
count = 0; //for indexing the character array
}
if(count < 80)
{
data[count] = c;//store the character into the data array
count++; //increment the index
}
}
}
return 0;
}
#定义uu delay u us(时间)功能所需的FCY 8000000//
#包括“elen116头.h”
#包括
#包括
#包括
#包括
内部主(空)
{
UART2_初始化();
UART3_初始化();
__延迟时间(250);
__延迟时间(250);
printf(“联机LoRa分期付款单位”);
char c='0';//声明一个字符
char data[80]={'\0'};//声明一个字符数组来存储整个消息
int count=80;//声明一个用作索引变量的整数
__延迟时间(250);
__延迟时间(250);
//将命令发送到GPS
strcpy(数据,“$PMTK414*33\r\n”);
printf(“发送:”);
int i;
计数=80;
对于(i=0;i
以及具有一些配置位和UART配置的报头
// PIC24FJ512GA606 Configuration Bit Settings
// 'C' source line config statements
// FSEC
#pragma config BWRP = OFF // Boot Segment Write-Protect bit (Boot Segment may be written)
#pragma config BSS = DISABLED // Boot Segment Code-Protect Level bits (No Protection (other than BWRP))
#pragma config BSEN = OFF // Boot Segment Control bit (No Boot Segment)
#pragma config GWRP = OFF // General Segment Write-Protect bit (General Segment may be written)
#pragma config GSS = DISABLED // General Segment Code-Protect Level bits (No Protection (other than GWRP))
#pragma config CWRP = OFF // Configuration Segment Write-Protect bit (Configuration Segment may be written)
#pragma config CSS = DISABLED // Configuration Segment Code-Protect Level bits (No Protection (other than CWRP))
#pragma config AIVTDIS = OFF // Alternate Interrupt Vector Table bit (Disabled AIVT)
// FBSLIM
#pragma config BSLIM = 0x1FFF // Boot Segment Flash Page Address Limit bits (Boot Segment Flash page address limit)
// FSIGN
// FOSCSEL
#pragma config FNOSC = FRC // Oscillator Source Selection (Internal Fast RC (FRC))
#pragma config PLLMODE = DISABLED // PLL Mode Selection (No PLL used; PLLEN bit is not available)
#pragma config IESO = ON // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
#pragma config OSCIOFCN = OFF // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config SOSCSEL = ON // SOSC Power Selection Configuration bits (SOSC is used in crystal (SOSCI/SOSCO) mode)
#pragma config PLLSS = PLL_PRI // PLL Secondary Selection Configuration bit (PLL is fed by the Primary oscillator)
#pragma config IOL1WAY = OFF // Peripheral pin select configuration bit (Allow only one reconfiguration)
#pragma config FCKSM = CSDCMD // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)
// FWDT
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler bits (1:32,768)
#pragma config FWPSA = PR128 // Watchdog Timer Prescaler bit (1:128)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bits (WDT Enabled)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config WDTWIN = WIN25 // Watchdog Timer Window Select bits (WDT Window is 25% of WDT period)
#pragma config WDTCMX = WDTCLK // WDT MUX Source Select bits (WDT clock source is determined by the WDTCLK Configuration bits)
#pragma config WDTCLK = LPRC // WDT Clock Source Select bits (WDT uses LPRC)
// FPOR
#pragma config BOREN = ON // Brown Out Enable bit (Brown Out Enable Bit)
#pragma config LPCFG = OFF // Low power regulator control (No Retention Sleep)
#pragma config DNVPEN = ENABLE // Downside Voltage Protection Enable bit (Downside protection enabled using ZPBOR when BOR is inactive)
// FICD
#pragma config ICS = PGD2 // ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF // JTAG Enable bit (JTAG is disabled)
#pragma config BTSWP = OFF // BOOTSWP Disable (BOOTSWP instruction disabled)
// FDEVOPT1
#pragma config ALTCMPI = DISABLE // Alternate Comparator Input Enable bit (C1INC, C2INC, and C3INC are on their standard pin locations)
#pragma config TMPRPIN = OFF // Tamper Pin Enable bit (TMPRN pin function is disabled)
#pragma config SOSCHP = ON // SOSC High Power Enable bit (valid only when SOSCSEL = 1 (Enable SOSC high power mode (default))
#pragma config ALTVREF = ALTREFEN // Alternate Voltage Reference Location Enable bit (VREF+ and CVREF+ on RA10, VREF- and CVREF- on RA9)
// #pragma config statements should precede project file includes.
/************************* DO NOT Alter any of the above code *******/
/************************* START of Your Program *********************/
/***** Directives and Define AREA************/
#include <xc.h>
#include <stdio.h>
///////////////////////////////////////
// UART2
///////////////////////////////////////
void UART2_Initialize(void)
{
/****************************************************************************
* Set the PPS
***************************************************************************/
__builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS
RPOR11bits.RP23R = 0x0005; //RP23/RD1->UART2:U2TX
RPINR19bits.U2RXR = 0x0018; //RP24/RD2->UART2:U2RX
__builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
/**
Set the UART2 module to the options selected in the user interface.
Make sure to set LAT bit corresponding to TxPin as high before UART initialization
*/
// STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX;
// Data Bits = 8; Parity = None; Stop Bits = 1;
U2MODE = (0x8008 & ~(1<<15)); // disabling UARTEN bit
// UTXISEL0 TX_ONE_CHAR; UTXINV disabled; URXEN disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled;
U2STA = 0x00;
// BaudRate = 9600; Frequency = 8000000 Hz; U2BRG 0x67;
U2BRG = 0x67;//0x33;
// BaudRate = 57600; Frequency = 8000000 Hz; U2BRG 0x16;
//U2BRG = 0x16;//0x33;
// BaudRate = 115200; Frequency = 8000000 Hz; U2BRG 0x16;
//U2BRG = 0x07;//0x33;
// ADMADDR 0; ADMMASK 0;
U2ADMD = 0x00;
UART2_Enable(); // enabling UARTEN bit
}
uint8_t UART2_Read(void)
{
while(!(U2STAbits.URXDA == 1))
{
}
if ((U2STAbits.OERR == 1))
{
U2STAbits.OERR = 0;
}
return U2RXREG;
}
void UART2_Write(uint8_t txData)
{
while(U2STAbits.UTXBF == 1)
{
}
U2TXREG = txData; // Write the data byte to the USART.
}
uint16_t UART2_StatusGet (void)
{
return U2STA;
}
void UART2_Enable(void)
{
U2MODEbits.UARTEN = 1;
U2STAbits.UTXEN = 1;
}
void UART2_Disable(void)
{
U2MODEbits.UARTEN = 0;
U2STAbits.UTXEN = 0;
}
///////////////////////////////////////
// UART3
///////////////////////////////////////
void UART3_Initialize(void)
{
/****************************************************************************
* Set the PPS
***************************************************************************/
__builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS
RPOR4bits.RP9R = 0x0013; //RP9/RB9>UART3:U3TX
RPINR17bits.U3RXR = 0x0008; //RP8/RB8->UART3:U3RX
__builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
ANSBbits.ANSB10 = 0;
TRISBbits.TRISB10 = 0;
/**
Set the UART3 module to the options selected in the user interface.
Make sure to set LAT bit corresponding to TxPin as high before UART initialization
*/
// STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH disabled; URXINV disabled; UEN TX_RX;
// Data Bits = 8; Parity = None; Stop Bits = 1;
U3MODE = (0x8000 & ~(1 << 15)); // disabling UARTEN bit //
// UTXISEL0 TX_ONE_CHAR; UTXINV disabled; URXEN disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled;
U3STA = 0x00;
// BaudRate = 9600; Frequency = 8000000 Hz; U2BRG 25;
U3BRG = 25;//0x33;
// BaudRate = 57600; Frequency = 8000000 Hz; U2BRG 0x16;
//U3BRG = 0x16;//0x33;
// BaudRate = 115200; Frequency = 8000000 Hz; U2BRG 0x16;
//U3BRG = 0x07;//0x33;
// ADMADDR 0; ADMMASK 0;
U3ADMD = 0x00;
UART3_Enable(); // enabling UARTEN bit
}
uint8_t UART3_Read(void)
{
// while(!(U3STAbits.URXDA == 1)) //These are used in the program instead
//{}
// if ((U3STAbits.OERR == 1))
// {
// U3STAbits.OERR = 0;
// }
return U3RXREG;
}
void UART3_Write(uint8_t txData)
{
while(U3STAbits.UTXBF == 1)
{
}
U3TXREG = txData; // Write the data byte to the USART.
}
uint16_t UART3_StatusGet (void)
{
return U3STA;
}
void UART3_Enable(void)
{
U3MODEbits.UARTEN = 1;
U3STAbits.UTXEN = 1;
U3STAbits.URXEN = 1;
PORTBbits.RB10 = 1; //disable standby
}
void UART3_Disable(void)
{
U3MODEbits.UARTEN = 0;
U3STAbits.UTXEN = 0;
U3STAbits.URXEN = 0;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//helper function for std output functions
int __attribute__((__section__(".libc.write"))) write(int handle, void *buffer, unsigned int len) {
int i;
//UART2_Initialize();
while(U2STAbits.TRMT == 0);
for (i = len; i; --i)
{
while(U2STAbits.TRMT == 0);
U2TXREG = *(char*)buffer++;
}
return(len);
}
//helper function for std intput functions
int __attribute__((__section__(".libc.read"))) read(int handle, void *buffer, unsigned int len) {
int i;
//UART2_Initialize();
while(U2STAbits.URXDA == 0);
for (i = len; i; --i)
{
while(U2STAbits.URXDA == 0);
*(char*)buffer++=U2RXREG;
}
return(len);
}
//PIC24FJ512GA606配置位设置
//“C”源代码行配置语句
//FSEC
#pragma config BWRP=OFF//引导段写保护位(可以写入引导段)
#pragma config BSS=禁用//引导段代码保护级别位(无保护(BWRP除外))
#pragma config BSEN=OFF//引导段控制位(无引导段)
#pragma config GWRP=OFF//通用段写保护位(可以写入通用段)
#pragma config GSS=禁用//通用段代码保护级别位(无保护(GWRP除外))
#pragma config CWRP=OFF//配置段写保护位(配置段可能被写入)
#pragma config CSS=禁用//配置段代码保护级别位(无保护(CWRP除外))
#pragma config AIVTDIS=OFF//备用中断向量表位(禁用AIVT)
//FBSLIM
#pragma config BSLIM=0x1ff//引导段闪存页地址限制位(引导段闪存页地址限制)
//F标志
//福塞尔
#pragma config FNOSC=FRC//振荡器源选择(内部快速RC(FRC))
#pragma config PLLMODE=禁用//PLL模式选择(未使用PLL;PLLEN位不可用)
#pragma config IESO=ON//双速振荡器启动启用位(使用FRC启动设备,然后切换到用户选择的振荡器源)
//FOSC
#pragma config POSCMD=NONE//主振荡器模式选择位(主振荡器禁用)
#pragma config OSCIOFCN=OFF//OSC2引脚功能位(OSC2为时钟输出)
#pragma config SOSCSEL=ON//SOSC电源选择配置位(SOSC用于晶体(SOSCI/SOSCO)模式)
#pragma config PLLSS=PLL_PRI//PLL二次选择配置位(PLL由主振荡器馈电)
#pragma config IOL1WAY=OFF//外围引脚选择配置位(仅允许一次重新配置)
#pragma config FCKSM=CSDCMD//时钟切换模式位(时钟切换和故障安全时钟监视器均被禁用)
//FWDT
#pragma-config-WDTPS=PS32768//看门狗定时器后置定标器位(1:32768)
#pragma config FWPSA=PR128//看门狗定时器预分频器位(1:128)
#pragma config FWDTEN=OFF//看门狗定时器启用位(WDT启用)
#pragma config WINDIS=OFF//看门狗定时器窗口启用位(非窗口模式下的看门狗定时器)
#pragma config WDTWIN=WIN25//看门狗定时器窗口选择位(WDT窗口为WDT周期的25%)
#pragma config WDTCMX=WDTCLK//WDT MUX源选择位(WDT时钟源由WDTCLK配置位确定)
#pragma config WDTCLK=LPRC//WDT时钟源选择位(WDT使用LPRC)
//FPOR
#pragma config BOREN=ON//Brown Out启用位(Brown Out启用位)
#pragma config LPCFG=关闭//低功率调节器控制(无保留睡眠)
#pragma config DNVPEN=启用