0
votes

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.

2
Read the warnings and everything will be clear. DO not ignore them!!!0___________
It's always a good idea to use for loops when possible. for(uint8_t i=0; n1[i] != '\0'; i++)Lundin

2 Answers

3
votes

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.

2
votes

You have two errors in your code:

  1. As MikeCAT already stated, the condition n[i] != "\0" is wrong. The compiler should have warned you about comparing an integer (n[i] promoted from char) and a pointer ("\0" is an array of char, here converted to the address of its first element). If not, please get used to always raise the warning level to the maximum. But you want to check for the end of the string: n[i] != '\0'.

  2. You don't (re)set i to zero after sending all characters of the string. You can do this before the inner loop, which I would prefer because it makes your intention clear, or after the inner loop, which is OK because you initialized it at its definition.