0

Chip : dspic33ev256gm102

Compiler : XC16

IDE : MPLAB X v6.05

I am having a small problem with my uart to be honest and do not know where it is exactly. I configured my uart1 in a very simple way and tried a serial port with TeraTerm.

The things is transmitting/receiving worked only once and then not anymore. I don't know what can I change in the configuration. TeraTerm does not seem to recognize the dsPIC after the first successful attempt. Any help would be very much appreciated.

// main.c
int main(void)
{
    uint8_t displayContent[64];
    uint8_t inputChar;
    uint16_t i;
    uint16_t returnStatus = 0xFFFF;
    uint16_t intArray[2];
    double relHumidity = 0.0;
    double temperature = 0.0;
    double dewPoint = 0.0;
    unsigned short adc1_raw = 0;
    
    
    // initialize the device
    SYSTEM_Initialize();
    UART1_Initialize();
    
 
    if (0) // ATTENTION: turn off FRC postscaler when using this!
    {
        U1STAbits.UTXEN = 0;     // disable UART1 transmit
        U1MODEbits.UARTEN = 0;   // disable UART1
        
        
        // Desired instruction clock frequency (FCY) = 60 MHz = 60000000 Hz
        // Configure PLL prescaler, PLL postscaler, PLL divisor
        PLLFBD=63; // M=65
        CLKDIVbits.PLLPOST=0; // N2=2
        CLKDIVbits.PLLPRE=0; // N1=2
        // Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
        __builtin_write_OSCCONH(0x01);
        __builtin_write_OSCCONL(OSCCON | 0x01);
        // Wait for Clock switch to occur
       // while (OSCCONbits.OSWEN);
        
        // DOZE-mode disabled ==> peripheral clock (FP) = instruction clock (FCY)
        // See 70000582e - UART.pdf, page 12, with BRGH = 1:
        // 9600 = Desired_Baud_Rate = FP / (4 * (U1BRG + 1))
        // U1BRG = ((FP / Desired_Baud_Rate) / 4) - 1
        //       = ((60000000 / 9600) / 4) - 1
        //       = 1562 = 0x61A (rounded)
        U1BRG = 0x61A;
        U1MODEbits.UARTEN = 1; // enable UART1
        U1STAbits.UTXEN = 1; // enable UART1 transmit
    }
    
    u8x8_d_ssd1306_128x32_univision_init_seq();
    u8x8_turn_on();
    clearDisplay(displayContent);
    for (i=0; i<10; i++) wait100kcy();
 
      while (1) {
        
        if (UART1_IsRxReady())
        {
            inputChar = UART1_Read();
            switch (inputChar)
            {
                case 'd':
                    u8x8_turn_on();
                    break;
                case 'f':
                    u8x8_turn_off();
                    break;
                case 'l':
                    LATBbits.LATB7 = !LATBbits.LATB7;
                    break;
                case 'w':
                    printf("%s\r\n", "Hello World ");
                    break;
                case 't':
                    if (returnStatus == 0)
                    {
                        printf("%s %0.1f %0.1f %0.1f\r\n", "DHT22:", relHumidity, temperature, dewPoint);
                    }
                    else
                    {
                        printf("%s %d\r\n", "DHT22 -- ERROR:", returnStatus);
                    }
                    break;
            }
        }
    }
void UART1_Initialize(void)
{
    IEC0bits.U1TXIE = 0;
    IEC0bits.U1RXIE = 0;
 
 
    // STSEL 1; IREN disabled; PDSEL 8N; UARTEN enabled; RTSMD disabled; USIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; URXINV disabled; UEN TX_RX; 
    // Data Bits = 8; Parity = None; Stop Bits = 1;
    U1MODE = (0x8008 & ~(1<<15));  // disabling UART ON bit
    // UTXISEL0 TX_ONE_CHAR; UTXINV disabled; OERR NO_ERROR_cleared; URXISEL RX_ONE_CHAR; UTXBRK COMPLETED; UTXEN disabled; ADDEN disabled; 
    U1STA = 0x00;
    // BaudRate = 9600; Frequency = 1842500 Hz; BRG 47; 
    U1BRG = 0x2F;
    
    txHead = txQueue;
    txTail = txQueue;
    rxHead = rxQueue;
    rxTail = rxQueue;
   
    rxOverflowed = 0;
 
 
    UART1_SetTxInterruptHandler(UART1_Transmit_ISR);
 
 
    UART1_SetRxInterruptHandler(UART1_Receive_ISR);
 
 
    IEC0bits.U1RXIE = 1;
    
    //Make sure to set LAT bit corresponding to TxPin as high before UART initialization
    U1MODEbits.UARTEN = 1;   // enabling UART ON bit
    U1STAbits.UTXEN = 1;
}
  • That all looks a bit confusing. One thing I've noticed is that you have enabled the Interrupt Vecotr for the UART (U1RXIE=1) but you don't have an Interrupt Handler defined? It might be setting the IF flag to 1 and waiting for you to clear it. Look at adding an Interrupt Handler. – Mike Mar 29 '23 at 14:31

0 Answers0