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;
}