gpt4 book ai didi

c - 尝试让 UART 在 dsPIC33EV256GM102 上工作

转载 作者:行者123 更新时间:2023-11-30 19:02:36 29 4
gpt4 key购买 nike

我正在开发一个项目,需要在 dsPIC 和 xbee 模块之间进行 UART 通信。当我运行已有的代码时,它会进行编译和编程,但不会通过 UART 线路发送任何内容。我只是想通过信号线发送一些东西,然后从那里开始处理。下面是我的主要代码以及 UART1.c,它们最初都是由 MCC 在 MPLABx 中创建的。提前谢谢大家。

#include "mcc_generated_files/system.h"
#include "mcc_generated_files/uart1.h"
#include "mcc_generated_files/pin_manager.h"
#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/reset.h"
#include "mcc_generated_files/reset_types.h"
#include "mcc_generated_files/system_types.h"
#include "mcc_generated_files/traps.h"
#include "mcc_generated_files/watchdog.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

int count = 1, i;
char string[30];

int main (void)
{
UART1_Initialize();

while(1)
{
printf(string,"c"); //
for(i = 0;i<sizeof(string);i++)
{
UART1_Write(string[i]);
}
count = count + 1;
}

}

UART1.c

#include "uart1.h"
#include <stdbool.h>
#include <stdint.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 = 9600; Frequency = 40000000 Hz; BRG 1041;
U1BRG = 0x411;

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 == 1)
{

}

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);
}

最佳答案

编辑:我忘记了 C zero-initializes global arrays ,这意味着您的 string 变量将仅包含 NULL 字符。

您对 printf 的调用看起来也很可疑:printf(string, "c");。您正在尝试打印字符串并用“c”填充字符串中的第一个变量,但字符串从未初始化?

您如何知道没有发送任何内容,您是否有连接到 UART 引脚的逻辑分析仪?如果是这样,添加它可能会有用。

关于c - 尝试让 UART 在 dsPIC33EV256GM102 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639948/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com