为什么使用dspic33ep512mu810芯片uart 2的此代码不起作用

时间:2019-01-11 12:27:09

标签: uart dspic

在MPLAB X IDE v5.10中,我使用的是dspic33ep512mu810微控制器。

我有以下C代码:

#include "xc.h"

_FOSCSEL(FNOSC_FRCPLL) //INT OSC with PLL (always keep this setting)
_FOSC(OSCIOFNC_OFF & POSCMD_NONE) //disable external OSC
_FWDT(FWDTEN_OFF) //watchdog timer off
_FICD(JTAGEN_OFF & 0b11); //JTAG debugging off

void UART2TX(char c) {
    if (U2STAbits.UTXEN == 0)
        U2STAbits.UTXEN = 1; //enable UART TX

    while (U2STAbits.UTXBF == 1); //if buffer is full, wait
    U2TXREG = c;
}

int main(void) {

    //setup internal clock for 80MHz/40MIPS
    //7.37/2=3.685*43=158.455/2=79.2275
    CLKDIVbits.PLLPRE = 0; // PLLPRE (N2) 0=/2 
    PLLFBD = 41; //pll multiplier (M) = +2
    CLKDIVbits.PLLPOST = 0; // PLLPOST (N1) 0=/2
    while (!OSCCONbits.LOCK); //wait for PLL ready

    _U2TXIF = 0;
    _U2TXIE = 0;
    _U2RXIF = 0;
    _U2RXIE = 0;
    //setup UART
    U2BRG = 85; //86@80mhz, 85@79.xxx=115200
    U2MODE = 0; //clear mode register
    U2MODEbits.BRGH = 1; //use high percison baud generator
    U2STA = 0; //clear status register

    //DSPIC33EP512MU810T-I/PT, RP96 as TX pin
    RPOR7bits.RP96R = 3; //

    while (1) {
        UART2TX('H'); 
    }
 }

我正在尝试通过UART2以115200的波特率发送'H',但是它不起作用。

1 个答案:

答案 0 :(得分:0)

您必须切换RF0(RP96)aus输出:

TRISFbits.TRISF0 = 0;     //make F0 an onput

您必须将RFO切换为数字:

ANSELFbits.ANSF0 = 0;      //make F0 digital