dsPIC33EP128MC202 UART接收器无法工作

时间:2018-01-11 05:27:26

标签: embedded uart dspic

我为UART通信编写了代码。 TX工作正常,但RX不工作。我搜索了很多但没找到解决办法。

我正在以时间间隔将字符x传送到PC,我能够看到数据。但是当传输数据时,pic没有收到任何东西。

以下是用于uart

的引脚
//PGEC1/AN4/C1IN1+/RPI34/RB2   for receiver
 RPINR18bits.U1RXR = 34;  

//RP36/RB4   for transmitting data
RPOR1bits.RP36R = 1; 

我能够将数据传输到PC,但PIC没有从pc接收任何查询器。 提前致谢

这是我的代码。 DSPIC33EP128MC202

    /*
 * File:   newmainXC16.c
 * Author:
 *
 * Created on 27 December, 2017, 4:21 PM
 */

// FICD
#pragma config ICS = PGD3               // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
#pragma config JTAGEN = OFF             // JTAG Enable bit (JTAG is disabled)

// FPOR
#pragma config ALTI2C1 = OFF            // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF            // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25           // Watchdog Window Select bits (WDT Window is 25% of WDT period)

// FWDT
#pragma config WDTPOST = PS32768        // Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128           // Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON              // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)

// FOSC
#pragma config POSCMD = NONE            // Primary Oscillator Mode Select bits (Primary Oscillator disabled)
#pragma config OSCIOFNC = ON           // OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = ON             // Peripheral pin select configuration (Allow only one reconfiguration)
#pragma config FCKSM = CSDCMD           // Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor are disabled)

// FOSCSEL
#pragma config FNOSC = FRCPLL           // Oscillator Source Selection (Fast RC Oscillator with divide-by-N with PLL module (FRCPLL) )
#pragma config PWMLOCK = ON             // PWM Lock Enable bit (Certain PWM registers may only be written after key sequence)
#pragma config IESO = ON                // Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then switch to user-selected oscillator source)

// FGS
#pragma config GWRP = OFF               // General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF                // General Segment Code-Protect bit (General Segment Code protect is Disabled)


#include "xc.h"
#include <stdint.h>
#include <p33EP128MC202.h>


#define FP 60000000
#define BAUDRATE 115200
#define BRGVAL ((FP/BAUDRATE)/16)-1
#define DELAY_100uS asm volatile ("REPEAT, #5400"); Nop(); // 100uS delay


int main(void)
{
    uint8_t c;
    int i,j,a,b;

    // Configure Oscillator to operate the device at 60Mhz
    // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
    // Fosc= 8M*60/(2*2)=120Mhz for 8M input clock  == 8*60/4 = 120/2 = 60

 CLKDIVbits.PLLPOST=0;       // PLLPOST (N1) 0=/2 
 while(!OSCCONbits.LOCK);    // wait for PLL ready
    PLLFBD = 58;                        // M=60
    CLKDIVbits.PLLPOST = 0;             // N1=2
    CLKDIVbits.PLLPRE = 0;              // N2=2
    OSCTUN = 0;                         // Tune FRC oscillator, if FRC is used

    /*Initialize the Ports */
    TRISA = 0x00;
    LATA = 0x0000;
    PORTA = 0x0000;
    ANSELAbits.ANSA1 = 0;
    ANSELAbits.ANSA0 = 0;


    __builtin_write_OSCCONL(OSCCON & ~(1<<6));
    RPINR18bits.U1RXR = 34;
    RPOR1bits.RP36R = 1;
    __builtin_write_OSCCONL(OSCCON | (1<<6));

    U1MODEbits.STSEL = 0; 
    U1MODEbits.PDSEL = 0; 
    U1MODEbits.ABAUD = 0; 
    U1MODEbits.BRGH = 0;
    U1BRG = 30;
    U1MODEbits.UARTEN = 1; 
    IEC0bits.U1TXIE = 0;

    U1STAbits.UTXEN = 1;
    U1STAbits.URXISEL = 0; 



    DELAY_100uS;
    DELAY_100uS;
    DELAY_100uS;
    U1TXREG = 'X';

    char ReceivedChar;

  while(1){

    //If data is received send data to TX  
    if(U1STAbits.URXDA == 1) {
        ReceivedChar = U1RXREG + 2; 
        U1TXREG = 10;
        U1TXREG = 13;
    }
    for(i=0;i<1000;i++){
          a = a + 1;
          for(j=0;j<2000;j++){
              b = b  + 1;
          }
    }
    //LED Blink code for programme check
    if(c == 0){
          c = 1;
    }else{
          c = 0;
    }
    LATAbits.LATA0 = c; 
    LATAbits.LATA1 = c; 
    U1TXREG = 'x';
    U1TXREG = 10;
    U1TXREG = 13;
  }

}


 void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt( void ) 
 { 
    IFS0bits.U1TXIF = 0; // Clear TX Interrupt flag 
 } 
 void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt( void ) 
 { 
     IFS0bits.U1RXIF = 0; // Clear RX Interrupt flag 
 //cntr++; 
    U1TXREG='a'; 
    U1TXREG = 10;
      U1TXREG = 13;

 } 

2 个答案:

答案 0 :(得分:0)

我查看了代码,对我来说很好。

请检查与外围设备及其引脚的连接。如果该针脚有多余的连接,请将其删除,然后重试。

答案 1 :(得分:0)

RB2应该是输入。

TRISB = 0x04;