0
\$\begingroup\$

I am currently trying to port the functionality of one of my projects from an Arduino to a PIC µC. To begin I wanted to establish a UART communication to my computer but I cannot get it to work. I tried with an TTL to USB converter and now with a logic analyzer to see if anything happens on the wire but there is nothing. I read in the datasheet the tx pin should be RC6 and I set all registers mentioned in the datasheet or in many tutorials and tried to understand them but I seem to forget something. Has anyone of you an idea?

main.c

#define _XTAL_FREQ 16000000
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>

#include "uart.h"


int main(void)
{

    OSCCON = 0x7B;
    UART_Init(9600);

    while(1){              
        UART_Write('c');
        UART_Write('w');

    }
}

uart.h

char UART_Init(const long int baudrate)
{
  unsigned int x;
  x = _XTAL_FREQ/(64*baudrate)-1;               
  if(x>255){                                 
  x = _XTAL_FREQ/(16*baudrate)-1;               
  TX1STAbits.BRGH = 1;                          
  }
  if(x<256){                         
    TX1STAbits.TXEN = 1;                         
    TX1STAbits.SYNC = 0;                         
    RC1STAbits.SPEN = 1;                                                       
    SP1BRG = x;                               

    return 1;                             
  }
  return 0;                                     
}

void UART_Write(char data)
{
  while(!TX1STAbits.TRMT);
  TX1REG = data;
}
\$\endgroup\$
  • \$\begingroup\$ You might need pull-up or pull-down resistors on the UART line. Did you try analysing the ports with a scope? \$\endgroup\$ – Rick van Schijndel Sep 24 '17 at 21:29
  • \$\begingroup\$ I don't think that a standard UART connection needs any pull-up or pull-down resistors to operate. I observed the pin with my logic analyzer (like a cheap oscilloscope to keep track of the logic levels) and nothing happened. \$\endgroup\$ – moessi774 Sep 24 '17 at 21:36
  • 1
    \$\begingroup\$ Are you familiar with the configuration bits on PICs? If not, read about them (google "PIC configuration bits"). And find the configuration bits registers in the datasheet. Each PIC had it's own unique settings. In your case, it's most likely the watchdog timer is resetting the processor before it can send any data. \$\endgroup\$ – Dan Laks Sep 24 '17 at 21:44
  • 1
    \$\begingroup\$ PICs do not need pull-up or down resistors on the UART lines. \$\endgroup\$ – Dan Laks Sep 24 '17 at 21:45
  • 1
    \$\begingroup\$ The configuration bits transcend any one specific peripheral, so it would be tedious to describe how every individual configuration setting affects each peripheral in the datasheet. It's just part of the learning curve of using PICs. Every PIC uses them and you just get used to adding the code at the top of every new program. MPLAB provides an easy way to generate the necessary code. \$\endgroup\$ – Dan Laks Sep 24 '17 at 22:09
0
\$\begingroup\$

I think you've missed some setup register(s). I don't see BAUD1CON initialization for example.

PIC datasheets are quite good in that they summarize all the bits that can affect each peripheral. You should check that you've set each and every (non-shaded) bit or byte or word below exactly the way you want.

enter image description here

There's another similar set for asynchronous reception.


That's the proper and thorough way to do it. You can also try using MCC (Microchip Code Configurator) plugin in MPLAB-X which allows you to pick options with check boxes and so on, and generates configuration code. You can redirect STDIO to the UART.

Take care with this, I've often run into subtle bugs in this sort of aid.

\$\endgroup\$
  • \$\begingroup\$ I read what the SCKP and BRG16 bits in the BAUD1CON register do. But I think they are fine when they are clear. SCKP is just for polarity and BRG16 affects the baud rate but when my SPBRG calculation is right I don't need it to be set, right? I guess it should normally work like I did and without MCC for beginning. MCC or config bits are never mentioned in all UART tutorials with PICs thats what I don't understand. \$\endgroup\$ – moessi774 Sep 25 '17 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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