UART ft232r FTDI to PIC 16f1829 question

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
russpatterson
Posts: 84
Joined: Wed Oct 21, 2009 10:45 am
Location: Carlsbad, CA
Contact:

UART ft232r FTDI to PIC 16f1829 question

Post by russpatterson » Wed Apr 04, 2018 10:43 am

Hello,

I'm able to connect my Mac to serial devices (e.g. blemate2) using the ft232r, however I'm having issues getting my PIC 16f1829 to connect to the Mac via the ft232r. When I send a byte from either end, I see a response on the other end but it's the wrong data. I thought it was a baud rate issue but I've read everything in the data sheet and tried multiple permutations and still no luck.

Here's my hardware connection setup. My PIC is running at 5V, FT232r switch is in the VCC position (as opposed to 3.3v). I have the grounds on both connected (no VCC connection should these be connected?), and both the tx-rx's connected.

Here's my code:

Code: Select all

void UART_setup()
{
	// select TX and RX pins
	TRISB7 = 0; // TX Pin set as output
	TRISB5 = 1; // RX Pin set at input
	TXCKSEL = 0; // TX on RB7
	RXDTSEL = 0; // RX on RB5
	ANSB5 = 0; // clear analog	
		
	// set baud rate
	//SPBRG = ((_XTAL_FREQ/16)/9600) - 1;
	SPBRG = 12; //51;
	BRGH = 0;
	BRG16 = 0;
	
	// enable Asynchonous serial port
	SYNC = 0; // Asynchronous
	SPEN = 1; // Enable serial port pins
	
	// prepare for transmission and reception
	TXEN = 1; // enable transmission
	CREN = 1; // enable reception

	// select 8bit mode
	TX9 = 0;
	RX9 = 0;
}

char UART_get_char()
{
	if(OERR) // check for Error
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF); // hold the program till RX buffer is free
	
	return RCREG;
}	

void UART_send_char(char bt)
{
	while(!TXIF); // hold the program till TX buffer is free
	TXREG = bt; // load the transmitter with the value
}			

void UART_send_string(char* st_pt)
{
	while(*st_pt) // if there is a char
	{
		UART_send_char(*st_pt++);
	}
}	


void do_BLE_frame()
{
	//UART_send_char('A');
	//UART_send_char('B');
	
	char get_value = UART_get_char();
	if(get_value == '1')
	{
		blinkRate = 115;
	}	
	else if(get_value == '0')
	{
		blinkRate = 115/2;
	}	
}


Any help is appreciated!

Thanks!

-Russ

User avatar
DanV
Posts: 334
Joined: Sat Apr 25, 2015 9:29 am

Re: UART ft232r FTDI to PIC 16f1829 question

Post by DanV » Wed Apr 04, 2018 11:52 am

You should explain what means "it's the wrong data".
Does that mean if one side says "hello" the other end sees "hi"?

use this baud rate calculator to set the baud rate:
http://www.nicksoft.info/el/calc/?ac=sp ... ateError=1

russpatterson
Posts: 84
Joined: Wed Oct 21, 2009 10:45 am
Location: Carlsbad, CA
Contact:

Re: UART ft232r FTDI to PIC 16f1829 question

Post by russpatterson » Wed Apr 04, 2018 12:22 pm

Thanks for the reply. I'm running at 8Mhz so from that calculator I get 25 for SPBRG, for BRGH = 0 and RBG16 = 1.

Code: Select all

 #define _XTAL_FREQ 8000000
//
	SPBRG = 25;
	BRGH = 0;
	BRG16 = 1;  
When I send an 'A' from the terminal program on the Mac I get 0x78 in RCREG. When I send a 'B' I get 0x80 in RCREG. I would expect 0x41 and 0x42.

When I send 'A' and 'B' repeatedly from the PIC this is what I see in on the Mac: Image https://ibb.co/ieLkSH

Thanks!

User avatar
DanV
Posts: 334
Joined: Sat Apr 25, 2015 9:29 am

Re: UART ft232r FTDI to PIC 16f1829 question

Post by DanV » Thu Apr 05, 2018 7:40 am

Those settings appear to be for 19200.
I think you should be using SPBRG = 51; when 8Mhz and 9600.

SPRBG = 25 when 4MHz and 9600.

russpatterson
Posts: 84
Joined: Wed Oct 21, 2009 10:45 am
Location: Carlsbad, CA
Contact:

Re: UART ft232r FTDI to PIC 16f1829 question

Post by russpatterson » Thu Apr 05, 2018 10:16 am

I found the error. The clock was set at 16Mhz not 8. Works like a charm. Thanks for the help.

Post Reply