Take the 2-minute tour ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I have a MAX232 IC wired to a PIC24FJ64GB110 on pin32 (TX) and pin33 (Rx), Clock = 16 MH. This is the code:

_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( IESO_ON & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRI & PLLDIV_DIV2 & IOL1WAY_ON)
_CONFIG3( WPCFG_WPCFGDIS & WPDIS_WPDIS) 

void InitUART1(void) 
{
    // configure U2MODE
    U1MODEbits.UARTEN = 0;  // Bit15 TX, RX DISABLED, ENABLE at end of func
    U1MODEbits.USIDL = 0;   // Bit13 Continue in Idle
    U1MODEbits.IREN = 0;    // Bit12 No IR translation
    U1MODEbits.RTSMD = 0;   // Bit11 Simplex Mode
    U1MODEbits.UEN = 0;     // Bits8,9 TX,RX enabled, CTS,RTS not
    U1MODEbits.WAKE = 0;    // Bit7 No Wake up (since we don't sleep here)
    U1MODEbits.LPBACK = 0;  // Bit6 No Loop Back
    U1MODEbits.ABAUD = 0;   // Bit5 No Autobaud (would require sending '55')
    U1MODEbits.RXINV = 0;   // Bit4 IdleState = 1
    U1MODEbits.BRGH = 0;    // Bit3 16 clocks per bit period
    U1MODEbits.PDSEL = 0;   // Bits1,2 8bit, No Parity
    U1MODEbits.STSEL = 0;   // Bit0 One Stop Bit

    U1BRG = 9;  // baud rate

    // Load all values in for U1STA SFR
    U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
    U1STAbits.UTXINV = 0;   //Bit14 N/A, IRDA config
    U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
    U1STAbits.UTXBRK = 0;   //Bit11 Disabled
    U1STAbits.UTXEN = 0;    //Bit10 TX pins controlled by periph
    U1STAbits.UTXBF = 0;    //Bit9 *Read Only Bit*
    U1STAbits.TRMT = 0;     //Bit8 *Read Only bit*
    U1STAbits.URXISEL = 0;  //Bits6,7 Int. on character recieved
    U1STAbits.ADDEN = 0;    //Bit5 Address Detect Disabled
    U1STAbits.RIDLE = 0;    //Bit4 *Read Only Bit*
    U1STAbits.PERR = 0;     //Bit3 *Read Only Bit*
    U1STAbits.FERR = 0;     //Bit2 *Read Only Bit*
    U1STAbits.OERR = 0;     //Bit1 *Read Only Bit*
    U1STAbits.URXDA = 0;    //Bit0 *Read Only Bit*

    IFS0bits.U1TXIF = 0;    // Clear the Transmit Interrupt Flag
    IEC0bits.U1TXIE = 1;    // Enable Transmit Interrupts
    IFS0bits.U1RXIF = 0;    // Clear the Recieve Interrupt Flag
    IEC0bits.U1RXIE = 1;    // Enable Recieve Interrupts

    U1MODEbits.UARTEN = 1;  // And turn the peripheral on

    U1STAbits.UTXEN = 1;
}

void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
{
}

void __attribute__((interrupt, no_auto_psv)) _U1TXInterrupt(void)
{
}

void main()
{
    RPINR18bits.U1RXR = 8;      //RP8 --> U1Rx
    RPOR4bits.RP9R = 3;
    InitUART1()

    while (1)
    {
        putcUART1(0x64);
    }
}

I did not receive anything from it. what I am doing wrong here?

share|improve this question
    
Have you looked at the raw output of the PIC on an oscilloscope to see that a) it is outputting the right data, and b) working at the right speed? Transmitting "U" repeatedly will give you the RS232 clock speed. –  Majenko Oct 17 '11 at 8:40

1 Answer 1

up vote 1 down vote accepted

With your code as shown, you should have transmitted one character and then hung.

You are setting up interrupt service routines for the UART transmit and receive interrupts, but you never clear the interrupts except in the initialization. As a minimum, you need to put:

IFS0bits.U1RXIF = 0;

inside the receive interrupt handler and

IFS0bits.U1TXIF = 0;

inside the transmit interrupt handler.

In your example, you really don't need the interrupt routines at all, you could eliminate them as long as you set IEC0bits.U1TXIE and IEC0bits.U1RXIE to 0 instead of 1.

I'm not sure whether putcUART1 waits for the transmit buffer to be empty; you may want to stick a call to BusyUART1

while(BusyUART1());

just before the putcUART1 call.

share|improve this answer
    
Hi Many thank! I tried everything you suggested. It works perfectly, except the baud rate. I am using a 16 Mh external clock. U1BRG = (1600000 / (16*9600))- 1 = 9.41666, so take 9. error = 0.41666 / 9.41666 = 4.4%. I am sending out 0x64, instead repeatly I got 0x49, 0x92 –  Wei Oct 17 '11 at 10:24
    
Hi I missed a 0 for 16 Mh. The result should be 103, not 9. It still does not work with U1BRG = 103, but works at U1BRG = 26. 103/26 = 4. somewhere it takes a factor of 4. cheers –  Wei Oct 17 '11 at 10:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.