-1

I am working on a project that requires UART communication between the dsPIC and an xbee module. When I run the code that I have, it compiles and programs but doesnt send anything over the UART line. I am just trying to get something to send over that signal line and then will work it up from there. Below is my main code as well as my UART1.c both were initially created by MCC within MPLABx. Thank you all in advance.

#include "mcc_generated_files/system.h"
#include "mcc_generated_files/uart1.h"
#include "mcc_generated_files/pin_manager.h"
#include "mcc_generated_files/clock.h"
#include "mcc_generated_files/dma.h"
#include "mcc_generated_files/ecan1.h"
#include "mcc_generated_files/interrupt_manager.h"
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/reset.h"
#include "mcc_generated_files/reset_types.h"
#include "mcc_generated_files/system_types.h"
#include "mcc_generated_files/traps.h"
#include "mcc_generated_files/watchdog.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"

int count = 1, i;
char string[30];

int main (void)
{
    UART1_Initialize();

    while(1)
    {
    printf(string,"c");   //
        for(i = 0;i<sizeof(string);i++)
        {
            UART1_Write(string[i]);
        }
        count = count + 1;
    }

} 

UART1.c

#include "uart1.h"
#include <stdbool.h>
#include <stdint.h>

/**
  Section: UART1 APIs
*/

void UART1_Initialize(void)
{
/**    
     Set the UART1 module to the options selected in the user interface.
     Make sure to set LAT bit corresponding to TxPin as high before UART 
initialization
*/
    // 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 UARTEN 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 = 40000000 Hz; BRG 1041; 
    U1BRG = 0x411;

    U1MODEbits.UARTEN = 1;  // enabling UARTEN bit
    U1STAbits.UTXEN = 1; 
}

uint8_t UART1_Read(void)
{
    while(!(U1STAbits.URXDA == 1))
    {

    }

    if ((U1STAbits.OERR == 1))
    {
        U1STAbits.OERR = 0;
    }

    return U1RXREG;
}

void UART1_Write(uint8_t txData)
{
    while(U1STAbits.UTXBF == 1)
    {

    }

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

uint16_t UART1_StatusGet (void)
{
    return U1STA;
}

int __attribute__((__section__(".libc.write"))) write(int handle, void 
*buffer, unsigned int len) {
    int i;
    while(U1STAbits.TRMT == 0);  
    for (i = len; i; --i)
    {
        while(U1STAbits.TRMT == 0);
        U1TXREG = *(char*)buffer++;        
    }
    return(len);
}
Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
dfetz
  • 27
  • 1

1 Answers1

1

Edit: I forgot that C zero-initializes global arrays, this.means that your string variable will consist of only NULL characters.

Your call to printf also looks suspicious: printf(string, "c");. You're trying to print string and fill in the first variable in the string with "c", but string was never initialized?

How do you know nothing is getting sent, do you have a logic analyzer hooked up to the UART pins? If so, that might be useful to add.

shmoo6000
  • 495
  • 4
  • 22
  • I do have a logic analyzer connected and I am getting the correct signals on MCLR, PGED, and PGEC but my UART channel just sits high. That being said I will try your idea. The only thing that I will ask is what do I have to add other than “char string[30]”? Thanks again in advance. – dfetz Apr 13 '19 at 21:58