c - 无论我要求我的系统发送什么,它只发送 FF 和 FE

标签 c uart xbee microchip dspic

我创建了一个系统,可以从车辆读取 CANBUS 数据并将其无线传输到“基站”。到目前为止,我的代码的工作原理是它通过数据线将东西发送到 xbee,然后 xbee 将它发送到接收模块,除了它发送的唯一东西是 FF 和 FE,不管我要求它发送什么.非常感谢任何帮助。

主.c

/**
  Section: Included Files
*/
#include "mcc_generated_files/clock.h"
#include "mcc_generated_files/dma.h"
#include "mcc_generated_files/ecan1.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/reset.h"
#include "mcc_generated_files/reset_types.h"
#include "mcc_generated_files/system.h"
#include "mcc_generated_files/system_types.h"
#include "mcc_generated_files/traps.h"
#include "mcc_generated_files/uart1.h"
#include "mcc_generated_files/watchdog.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

void ms_delay (int N)
{
    T1CON = 0x08030;

    int delay = N * 62.5;   // 1 Milisecond => .001 / (256 * (1/16,000,000)) = 62.5
    TMR1 = 0;
    while (TMR1 < delay);
}

int main (void)
{
    PIN_MANAGER_Initialize();
    CLOCK_Initialize();
    INTERRUPT_Initialize();
    //DMA_Initialize();
    UART1_Initialize();
    ECAN1_Initialize();
    //INTERRUPT_GlobalEnable();
    while(1)
        {
        UART1_Write("A");
        ms_delay(2);
        UART1_Write("C");
        ms_delay(2);
        UART1_Write('S');
        ms_delay(2);
        }
}
/**
 End of File
*/

串口1.c

  Section: Included Files
*/
#include "uart1.h"

/**
  Section: UART1 APIs
*/

void UART1_Initialize(void)
{
/**    
     Set the UART1 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;
    U1MODE = (0x8008 & ~(1<<15));  // disabling UARTEN bit
    // UTXISEL0 TX_ONE_CHAR; UTXINV disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled; 
    U1STA = 0x00;
    // BaudRate = 2560.164; Frequency = 40000000 Hz; BRG 3905; 
    U1BRG = 0x19;

    U1MODEbits.UARTEN = 1;  // enabling UARTEN bit
    U1STAbits.UTXEN = 1; 
}

uint8_t UART1_Read(void)
{
    while(!(U1STAbits.URXDA == 1))
    {

    }

    if ((U1STAbits.OERR == 1))
    {
        U1STAbits.OERR = 0;
    }

    return U1RXREG;
}

void UART1_Write(uint8_t txData)
{
    while(U1STAbits.UTXBF)
    {

    }

    U1TXREG = txData;    // Write the data byte to the USART.
}

uint16_t UART1_StatusGet (void)
{
    return U1STA;
}

/*int __attribute__((__section__(".libc.write"))) write(int handle, void *buffer, unsigned int len) {
    int i;
    while(U1STAbits.TRMT == 0);  
    for (i = len; i; --i)
    {
        while(U1STAbits.TRMT == 0);
        U1TXREG = *(char*)buffer++;        
    }
    return(len);
}*/

预期结果:从一个模块向另一个模块重复发送 YES 实际结果:表示发送FF FE

最佳答案

最好的办法是将逻辑探头甚至示波器连接到 TX 和 RX 线路,这样您就可以查看实际发送的内容。您将能够查看位时间以查看您的波特率计算是否正确。您所做的任何其他事情都只是盲目猜测。

这个 Microchip 平台是否有中断或 DMA 驱动的 UART 驱动程序,您可以使用它来代替自己开发?您可以使用与 PC 的有线串行连接而不是 XBee(这会引入另一个可能的故障点)来测试您的驱动程序吗?

您的 UART1_Write() 正在阻塞,因此不需要延迟。

确保为 XBee 配置正确的波特率(我很确定 ATBD=3 用于 9600)。尝试发送序列 0x01、0x03、0x07 0x0F、0x1F、0x3F、0x7F、0xFF。这会生成一个 1 位后跟 0 位的序列,可能有助于解决波特率计算中的错误。

关于c - 无论我要求我的系统发送什么,它只发送 FF 和 FE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55820938/

相关文章:

c - LPC2148 串口中断

c - C 中的指针和内存混淆

C 二进制文件作为数组处理

c - 如何在 fork 的子进程期间在字符串上打印\n

xbee - 终端设备如何在 XBee(系列 1)NonBeacon(w/Coordinator)网络中相互通信

python - 如何在树莓派上使用 crontab 运行代码

python - 如何使用 python 库将数据字符串发送到 XBee?

c - 结构体指针不能正常工作?

C:在超时的阻塞套接字上等待 n 个字符

c - 如何用HAL_UART接收数据?