我正在尝试使用UART模块在dsPIC33FJ64GP802与终端和PC之间建立基本的串行通信。但是,UART不会发送多次字符。我正在使用MPLAB-X(V 5.05)。我还使用快照调试器对dsPic进行编程。我试图找出我的代码中的错误。我尝试更改dsPic本身以及蓝牙模块。它不起作用。我不知道我是否缺少一些东西。
// FBS
#pragma config BWRP = WRPROTECT_OFF // Boot Segment Write Protect (Boot Segment may be written)
#pragma config BSS = NO_FLASH // Boot Segment Program Flash Code Protection (No Boot program Flash segment)
#pragma config RBS = NO_RAM // Boot Segment RAM Protection (No Boot RAM)
// FSS
#pragma config SWRP = WRPROTECT_OFF // Secure Segment Program Write Protect (Secure segment may be written)
#pragma config SSS = NO_FLASH // Secure Segment Program Flash Code Protection (No Secure Segment)
#pragma config RSS = NO_RAM // Secure Segment Data RAM Protection (No Secure RAM)
// FGS
#pragma config GWRP = OFF // General Code Segment Write Protect (User program memory is not write-protected)
#pragma config GSS = OFF // General Segment Code Protection (User program memory is not code-protected)
// FOSCSEL
#pragma config FNOSC = LPRCDIVN // Oscillator Mode (Internal Fast RC (FRC) with divide by N)
#pragma config IESO = ON // Internal External Switch Over Mode (Start-up device with FRC, then automatically switch to user-selected oscillator source when ready)
// FOSC
#pragma config POSCMD = NONE // Primary Oscillator Source (Primary Oscillator Disabled)
#pragma config OSCIOFNC = OFF // OSC2 Pin Function (OSC2 pin has clock out function)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow Only One Re-configuration)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor (Both Clock Switching and Fail-Safe Clock Monitor are disabled)
// FWDT
#pragma config WDTPOST = PS32768 // Watchdog Timer Postscaler (1:32,768)
#pragma config WDTPRE = PR128 // WDT Prescaler (1:128)
#pragma config WINDIS = OFF // Watchdog Timer Window (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = ON // Watchdog Timer Enable (Watchdog timer always enabled)
// FPOR
#pragma config FPWRT = PWR128 // POR Timer Value (128ms)
#pragma config ALTI2C = OFF // Alternate I2C pins (I2C mapped to SDA1/SCL1 pins)
// FICD
#pragma config ICS = PGD1 // Comm Channel Select (Communicate on PGC1/EMUC1 and PGD1/EMUD1)
#pragma config JTAGEN = OFF // JTAG Port Enable (JTAG is Disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <stdbool.h>
#include "stdio.h"
#include <string.h>
//delay in milliseconds
void delay_milliseconds(long double milliSecond)
{
milliSecond = (milliSecond * 147);
long double i;
for(i = 0; i<milliSecond;i++);
}
/*
Main application
*/
void UART1_setup(void)
{
U1BRG = 25;
U1MODE = 0;
U1MODEbits.UEN = 0;
U1MODEbits.UARTEN = 1; //enable the module
U1STAbits.UTXEN = 1; //enable transmissiond
U1MODEbits.BRGH = 0;
U1STAbits.UTXISEL1 = 1;
U1STAbits.UTXISEL0 = 0;
U1STAbits.UTXBF = 0;
U1STAbits.URXDA = 1;
U1STAbits.OERR = 0;
U1STAbits.FERR = 0;
U1STAbits.PERR = 0;
INTCON1bits.NSTDIS = 0;
IFS0bits.U1RXIF = 0;
IEC0bits.U1RXIE = 1;
IPC2bits.U1RXIP = 7;
}
char UART1_Read(void)
{
while(!(U1STAbits.URXDA == 1))
{
}
if ((U1STAbits.OERR == 1))
{
U1STAbits.OERR = 0;
}
return U1RXREG;
}
void UART1_Write(char txData)
{
while(U1STAbits.UTXBF == 1)
{
}
U1TXREG = txData; // Write the data byte to the USART.
}
void printString(char *data)
{
int len = strlen(data);
int i =0;
for( i = 0; i<len; i++)
{
char txData = *(data + i);
UART1_Write(txData);
}
}
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
//Clear receive interrupt flag
IFS0bits.U1RXIF = 0;
}
uint8_t data[2];
void pinSetup(void)
{
//set directions
TRISBbits.TRISB11 = 0; //TX
TRISBbits.TRISB10 = 1; //Rx
//***************************************************************************&&
__builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS
RPINR18bits.U1RXR = 0x000B; //RB11->UART1:U1RX
RPOR5bits.RP10R = 0x0003; //RB10->UART1:U1TX
//RPINR0bits.INT1R = 0x0005; //RB5->EXT_INT:INT1
__builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS
//////////////////**********************************************************************&&&&&&&&
}
int main(void)
{
pinSetup();
I2C1_init();
UART1_setup();
while(true)
{
UART1_Write('A');
}
}
答案 0 :(得分:0)
有时在初次启动时,UART RX线自发传输,导致接收缓冲区溢出并设置其相关位。这将禁用UART模块,直到清除溢出位。每次要发送字符时,尝试清除接收缓冲区溢出位,看看是否有帮助
答案 1 :(得分:0)
strlen ()
函数返回字符串中的字符数,直到空终止符为止。但是,当您编写'A'
时,您只有一个字符,而不是带有终止符的完整字符串。因此,请尝试"ABC"
,例如