2

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

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

下面是用于uart

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

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

我可以将数据传输到电脑,但 PIC 没有从电脑接收任何字符。提前致谢

这是我的代码。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;

 } 
4

2 回答 2

0

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

请检查与外围设备及其引脚的连接。如果该引脚有额外的连接,请将其移除并重试。

于 2018-06-20T08:07:03.350 回答
0

RB2 应该是一个输入。

TRISB = 0x04;
于 2018-06-20T08:26:37.940 回答