0

我正在尝试使用 PIC32 上的 UART1 来发送和接收数据,但该端口没有任何输出。我正在使用带有 8Mhz 晶体的 PIC32 以太网套件。对于在 CI 中使用 MPLAB IDE v2.15 和 XC32 v 1.32 进行编程,我现在没有可用的范围,所以我试图用非常有限的万用表进行测试。当我将仪表连接到 UART1 TX 引脚时,可以看到一旦执行 initU1 例程,引脚从 0V 变为 3.3V,这似乎正在工作,但是当应该传输位时,我在 LCD 上看不到任何东西(LK204-25),我也连接了万用表,没有看到电压有任何变化,也没有显示任何频率。波特率设置为 9600。我正在使用的代码如下所示,请告诉我应该更改哪些内容才能使其正常工作。


/* 
 * File:   main.c
 * Author: Acer
 *
 * Created on August 27, 2014, 11:57 PM
 */

#include <stdio.h>
#include <stdlib.h>
//#include <p32xxxx.h>
#include <proc/p32mx795f512l.h>
#include <plib.h>

/*
 * 
 */

#define CTS1 _RD14
#define RTS1 _RD15
#define TRTS1 TRISDbits.TRISD15

#define BRATE1 368 //2083 //9600 @80MHz, BREGH=1
#define U_ENABLE1 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX1 0x0400 //Enable TX, Clear all flags

#define CTS2 _RF12
#define RTS2 _RF13
#define TRTS2 TRISFbits.TRISF13

#define BRATE2 2082 //9600 @80MHz, BREGH=1
#define U_ENABLE2 0x8008 //BREGH=1, 1 stop, no parity
#define U_TX2 0x0400 //Enable TX, Clear all flags

char Port1Buffer[100], Port2Buffer[100];
int i1=0, i2=0;
int nextRun1=0, nextRun2=0;

void __attribute__ ((interrupt(ipl1),vector(27)))
U1RXInterruptHandler(void)//UART 1 RX interrupt routine
{
    if(mU1RXGetIntFlag())
    {
        // Clear the RX interrupt Flag
        mU1RXClearIntFlag();
        // Echo what we just received
        //putcUART1(ReadUART1());
            Port1Buffer[i1]=ReadUART1();
            i1++;
            if(i1==100){
                i1=0;
                nextRun1=1;
            }
        // Toggle LED to indicate UART activity
            mPORTDToggleBits(BIT_0);
    }
    // We don't care about TX interrupt
    if ( mU1TXGetIntFlag() )
    {
        mU1TXClearIntFlag();
    }

}

void __attribute__ ((interrupt(ipl2),vector(41)))
U2RXInterruptHandler(void)//UART 2 RX interrupt routine
{
    if(mU2RXGetIntFlag())
    {
        // Clear the RX interrupt Flag
        mU2RXClearIntFlag();
        // Echo what we just received
        //putcUART2(ReadUART2());
            Port2Buffer[i2]=ReadUART2();
            i2++;
            if(i2==100){
                i2=0;
                nextRun2=1;
            }

        // Toggle LED to indicate UART activity
            mPORTDToggleBits(BIT_0);
    }
    // We don't care about TX interrupt
    if ( mU2TXGetIntFlag() )
    {
        mU2TXClearIntFlag();
    }

}

void initU1(void)
{
    U1BRG = BRATE1; //Initialize baud rate generator
    U1MODE = U_ENABLE1; //Initialize UART module
    U1STA = U_TX1; //Enable Transmitter
    TRTS1 = 0; //Make RTS an output pin
    RTS1 = 1; //Set RTS default status (not ready)
    //ConfigIntUART1(UART_INT_PR1|UART_RX_INT_EN);
}

void initU2(void)
{
    U2BRG = BRATE2; //Initialize baud rate generator
    U2MODE = U_ENABLE2; //Initialize UART module
    U2STA = U_TX2; //Enable Transmitter
    TRTS2 = 0; //Make RTS an output pin
    RTS2 = 1; //Set RTS default status (not ready)
    ConfigIntUART2(UART_INT_PR2|UART_RX_INT_EN);
}

void putU1(char c)
{
//    while(CTS1);
    while(U1STAbits.UTXBF);
    U1TXREG = c;
}

void putU2(char c)
{
    while(CTS2);
    while(U2STAbits.UTXBF);
    U2TXREG = c;
}

void DelayRoutine(int delint){
    int delcount=0;
    while(delcount < delint){
        delcount++;
    }
}
void main(void) {
    DelayRoutine(0xFFFF);
//    mU1SetIntPriority(1);
//    mU2SetIntPriority(2);
    INTEnableSystemMultiVectoredInt();
    //mU1IntEnable( 1);
    //mU2IntEnable( 1);
    int y1=0, y2=0;
    initU1();
    initU2();
//    putU1(0x41);
    putU1(0xFE);
    putU1(0x58);
    while(1)
    {
        if(y1<i1 || nextRun1==1){
            putU1(Port1Buffer[y1]);
            y1++;
            if(y1==100){
                y1=0;
                nextRun1=0;
            }
        }
/*        if(y2<i2 || nextRun2==1){
            putU1(Port2Buffer[y2]);
            y2++;
            if(y2==100){
                y2=0;
                nextRun2=0;
            }
        }
*/    }
    //U2CTS=1;
    //return (EXIT_SUCCESS);
}
4

1 回答 1

0
void init_serial_port( U32 ulWantedBaud )
{
    unsigned short usBRG;
    /* Configure the UART and interrupts. */
    usBRG = (unsigned short)(( (float)40000000/ ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
    OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
    ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_RX_INT_EN );
    U2STAbits.UTXISEL = 0x00;
}

只需将我的“2”替换为 1,这就是我在 PIC32MX745F512L 上的 UART 的确切初始化序列

我不能发布我的中断处理程序 b/c 它太复杂了,但是使用串行端口 API 的复杂性要低得多。另外,我建议使用一个像这样的简单逻辑分析仪(当然用于 TTL 端)。

旁注:如果您使用 API,他们会为您处理 TRIS 和 LAT。

编辑:添加编译指示

#pragma config DEBUG    = OFF           // Background Debugger disabled
#pragma config FPLLMUL = MUL_20         // PLL Multiplier: Multiply by 20
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider:  Divide by 2
#pragma config FPLLODIV = DIV_1         // PLL Output Divider: Divide by 1
#pragma config FWDTEN = OFF             // WD timer: OFF
#pragma config POSCMOD = HS             // Primary Oscillator Mode: High Speed xtal
#pragma config FNOSC = PRIPLL           // Oscillator Selection: Primary oscillator  w/ PLL
#pragma config FPBDIV = DIV_1           // Peripheral Bus Clock: Divide by 1 //Note: Application    FPBDIV = DIV_2
#pragma config BWP = OFF                // Boot write protect: OFF
#pragma config ICESEL = ICS_PGx2        // ICE pins configured on PGx2, Boot write protect OFF.
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config CP = OFF
#pragma config PWP = OFF
#pragma config UPLLEN = ON
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
#pragma config FSRSSEL = PRIORITY_7
#pragma config FMIIEN = OFF
#pragma config FVBUSONIO = OFF          // VBUSON is controlled by the port
#pragma config FETHIO = ON            // external PHY in RMII/alternate configuration
#pragma config FUSBIDIO = OFF           // USB ID controlled by PORT function
于 2014-10-14T13:57:10.683 回答