String formats for control

I finally got back to my project, and have some newbie questions. I am using HiTech C, and was trying to locate the proper functions to use for my PIC18f4685. To format the int values into ASCII, can I use UTOA? And then use PUTS? I am new to using C with microcontrollers.

so would this code work? puts(#1P1500S333/r);

or could I use a printf? Do I need to use /r/n or ?? I see both mentioned.

Thanks for any help!

Why not just use printf?

I make up strings:

sprintf(str, "#0P%dT%d/r", pos1, time1);

Then send the stings. Or even use a format string:

[code]char const ServoStr1] = “#%02dP%d#%02dP%d”;

sprintf(buff, ServoStr1, RRHH, HipH_Pulse[RR_LEG], RRHV, HipV_Pulse[RR_LEG]);

PutString(buff); /* send to serial port */
[/code]

You can use /r/n if you want to, I do when I also want to echo the line to a terminal for viewing.

Spaces are optional. They make the strings easier to read, but aren’t required.

Alan KM6VV

Ah now I see, the sprintf() makes the string. Cool.

Is the PutString() function in the compiler? Or is that a function that you wrote yourself. I still have not looked through the documentation on how to send serial data. Is it as simple as writing the data to a register and then waiting for a flag to tell you it is ready for more characters?

I wrote a PutString() which calls a PutChar()

while(chr = *ptr) { PutChar(chr); ptr++; }

Which can call a non-interrupt routine:

void PutChar(unsigned char chr) { while(!TXIF); /* BLOCKS! */ TXREG = chr; }

Or an interrupt driven routine which buffers the data:

[code] void PutSerial(unsigned char chr)
{
static unsigned char ndx;

/* Wait while buffer is full as interrupt changes Front pointer. */
	ndx = (TxTailPtr + 1) & TX_BUFF_MASK;

/* One char is removed by interrupt routine while loop exits. */

	while(ndx == TxHeadPtr); /* BLOCKS if no room in BUFFER!!  */

	PEIE = 0;	/* don't let interrupt modify what we look at */

	if(TXIE == 0)	/* enable TX interrupt on first char to send */
		{
		TXREG = chr;
		TXIE = 1;	/* ENABLE interrupt to handle sending chars */
		}
	else
		{
		TxBuff[TxTailPtr++] = chr; /* char into XMIT buffer */
		TxTailPtr &= TX_BUFF_MASK; /* wrap the buffer */
		}

	PEIE = 1;	/* Let interrupt finish */
	}

[/code]

My receive routine uses a buffer also, and I can get either a char or a full string.

Writing to the TXREG sends a character, check TXIF first to see if transmit buffer is empty.

There are demos for char send/receive on the Microchip website, and shipped with the compilers.

Alan KM6VV