0

我正在尝试使用 UART 模块在 dsPIC33FJ64GP802 与终端和 PC 之间设置基本的串行通信。但是,UART 不会传输超过一次的字符。我正在使用 MPLAB-X (V 5.05)。我还使用 snap 调试器对 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');        
    }
}
4

2 回答 2

0

strlen ()函数返回字符串中直到空终止符的字符数。但是当您编写时,'A'您只有一个字符,而不是带有终止符的完整字符串。所以尝试"ABC"例如

于 2019-03-04T07:53:26.903 回答
0

有时在初始启动时,UART RX 线会自发转换,导致接收缓冲区溢出并设置相关位。这将禁用 UART 模块,直到溢出位被清除。每次要发送字符时尝试清除接收缓冲区溢出位,看看是否有帮助

于 2019-02-28T21:49:36.380 回答