Search code examples
cembeddeduartpic

Getting strange output from PIC16F over UART


I was trying a basic program to print hello world using PIC16F15325. I configured PIC and made its library using MPLAB Code Configurator. The function "EUSART1_Write" is as follows:

void EUSART1_Write(uint8_t txData)
{
    while(0 == PIR3bits.TX1IF)
    {
    }

    TX1REG = txData;    // Write the data byte to the USART.
}

and code I wrote for doing "hello world" is this:

#include "mcc_generated_files/mcc.h"
void main(void)
{

    // initialize the device
    SYSTEM_Initialize();
    EUSART1_Initialize();
    char n1[] = "Hello world";
    uint8_t i = 0;

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        // Add your application code
        while(n1[i] != "\0")
        {
            EUSART1_Write(n1[i]);
            DELAY_milliseconds(20);
            i++;
        }
        DELAY_milliseconds(1000);
    }
}

I connected ground of FTDI cable to ground of PIC, Receiver of FTDI to transmitter of PIC, Transmitter of FTDI to receiver of PIC. baud rate-9600, Parity none, stop bit-1. This setting is same for PIC and terminal emulator "TeraTerm". rest I connected MCLR as recommended in datasheet. However my output is as follows:

UART Output from PIC on TeraTerm

Please help me to figure it out.


Solution

  • The condition n1[i] != "\0" is wrong. "\0" is an array. It is converted to an address of the first element and it may not be equal to one of the element of n1. You should compare with a character line n1[i] != '\0' instead.