【发布时间】:2019-09-13 05:06:32
【问题描述】:
我创建了一个系统,可以从车辆读取 CANBUS 数据并将其无线传输到“基站”。到目前为止,我的代码的工作原理是它通过数据线将东西发送到 xbee,xbee 将它发送到接收模块,除了它唯一发送的是 FF 和 FE,无论我要求它发送什么.非常感谢任何帮助。
main.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
*/
UART1.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
【问题讨论】:
-
在调试器中单步执行代码告诉您什么?为什么你的帖子中有注释掉的代码?
-
当我运行它时,它不会给我任何错误或任何东西。有注释掉的代码是因为我一直在尝试其中的一些新东西,并且在我知道一切正常之前我不想进行任何“永久性”更改。
-
您将一个 字符串 传递给
UART1_Write和"A"。它[可能]正在写入字符串的地址的第一个字节(可能是(例如)0xFF123455),因此是FF。尝试(例如)UART1_Write('A') -
即使我将其更改为 UART1_Write('A') 它仍然只发送一次 FE,即使它应该连续发送它,因为它在一段时间内(1)对吧?
-
您没有将 uint8 传递给您的写入函数。将 U1TXREG = 'A" 硬编码到写入函数中,然后 ='B' 等,看看它是否像您期望的那样变化。您可以在逻辑分析仪上交换引脚,这会给您带来框架问题或错误边缘。如果 U1TXREG = 'A' 没有发送 'A' 的 ascii 值,那么你的硬件设置是错误的,如果它确实发送了 'A' 它是你的小测试程序。
标签: c uart xbee microchip dspic