0

PIC32 で UART1 を使用してデータを送受信しようとしていますが、そのポートから何も出力されません。PIC32 イーサネット キットと 8Mhz クリスタルを使用しています。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 の正確な初期化シーケンスです ulWantedBaud は希望のボーレートです (通常は 115200 ですが、設定によっては 9600 になることもあります)

あまりにも複雑なため、割り込みハンドラを投稿することはできませんが、シリアル ポート 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 に答える