[Help] IC18F45K22 EUSART serial communication(transmit only)

Thread Starter

VoltVolt

Joined Jan 25, 2013
12
hi everyone, just want to ask is there any problem from the below codes?
I just want to print out "helloword" by using serial communication
I'm using PIC18F45K22 and MPLABX IDE compiler...
The codes can be smoothly run but it cannot be print out...
Btw, i don't have interrupt, it is just a test code....

Rich (BB code):
#include <htc.h>
#include <string.h>
char sendBuffer[11];
char sendPosition=0;
void sendUartString(const char string[10]);
void init()
{
// Asynchronous Transmission Setup
// 1. Initialize the SPBRGHx:SPBRGx register pair and the BRGH and BRG16 bits to achieve the desired baud rate
        SPBRGH2=0X01;
        SPBRG2=0XA0;
	TXSTA2bits.BRGH=1;
	BAUDCON2bits.BRG16=1;
// 2. Set the RXx/DTx and TXx/CKx TRIS controls to ?1?.
	TRISB = 0b11000000;	//set TX2 and RX2 as output

// 3. Enable the asynchronous serial port by clearing the SYNC bit and setting the SPEN bit.
	ANSELB = 0;	//disable ANSEL
	SYNC2 = 0;	//asynchronous mode
	SPEN2 = 1;	//enable EUSART

// 4. If 9-bit transmission is desired, set the TX9 control bit. A set ninth data bit will indicate that the eight Least Significant data bits are an address when the receiver is set for address detection.
// 5. Set the CKTXP control bit if inverted transmit data polarity is desired.
// 6. Enable the transmission by setting the TXEN control bit. This will cause the TXxIF interrupt bit to be set.
	TXEN2 = 1;
// 7. If interrupts are desired, set the TXxIE interrupt enable bit. An interrupt will occur immediately provided that the GIE/GIEH and PEIE/GIEL bits of the INTCON register are also set.

// 8. If 9-bit transmission is selected, the ninth bit should be loaded into the TX9D data bit.
// 9. Load 8-bit data into the TXREGx register. This will start the transmission.
}
void main()
{
	init();
	//now you want to send something, so first load a string into the sendBuffer
        while(1)
        {
		//sendUartString("helloworld");
        }
}


void sendUartString(const char string[10])
{
	strcpy(sendBuffer,string); //please check the correct command,copy string[] to sendBuffer[]

	sendPosition=0; //start sending first char
	while((sendPosition!= strlen(sendBuffer)) && sendPosition<10 )
	{
		if(TX2IF==1)//if Transmit register is empty
		{
			TX2REG=sendBuffer[sendPosition]; //send one more char
			sendPosition++;
		}
	}
}
 

t06afre

Joined May 11, 2009
5,934
How is your setup between your PIC and the unit that receive your data. Describe it with so many details that we who are not sitting besides you on your workbench can understand it.
By the way have debugged your code. The MPLAB simulator may be a big help here
 

Thread Starter

VoltVolt

Joined Jan 25, 2013
12
I'm using cytron UC00B USB to serial port converter...
I connect the RX and TX from PIC to TX and RX from serial port respectively...
and I only connect the 5 ICSP headers(MCLR,Vdd,Vss,PGC,PGD)...
That's all my circuit...
 

t06afre

Joined May 11, 2009
5,934
Ah.. that may be error. Then a signal is going out in one end (TX) it evidently has to go to the receive (RX) on the other end. The most simple setup is
RX-TX
TX-RX
GND-GND
In order to get this working. You have to turn off hardware and software handshaking in your PC program
 

MaxHeadRoom

Joined Jul 18, 2013
20,676
Are you receiving on a PC Could it be your modem program?
What program are you using?
Does anything at all come through?
I have found USART use on a PIC very simple to set up, but I have only used the assembly examples from Picmicro.
 

Thread Starter

VoltVolt

Joined Jan 25, 2013
12
receiving on a PC
I am using MPLABX IDE

Wow...
I found assembly more hard to write...
My school using assembly to teach intel processor 808x...
I got that kind of phobia on write assemble haha...
not confident at all....
 

MaxHeadRoom

Joined Jul 18, 2013
20,676
If your port software has identified the USB port as a simulated RS232 you could try one of the free RS232 modem software such as RS232 HEX COM
http://www.rs232pro.com/
It has an auto search for ports and if it recognizes your USB port as valid, then this program should work.
Max.
 

ErnieM

Joined Apr 24, 2011
8,041
What hardware do you have on your PIC? The signals on the PIC pins are the correct data, but not the correct levels for RS232.
 

MaxHeadRoom

Joined Jul 18, 2013
20,676
Good catch, I thought the OP was using a USB only, if using a hardware convertor to RS232, then the PIC end requires a MAX232 or equivalent IC.
Max.
 

t06afre

Joined May 11, 2009
5,934
Good catch, I thought the OP was using a USB only, if using a hardware convertor to RS232, then the PIC end requires a MAX232 or equivalent IC.
Max.
As fas as I can see. The USB dongle that the OP use. Are made for directly interfacing with TTL levels. 3.3 or 5 volt depending on the settings
 

THE_RB

Joined Feb 11, 2008
5,438
Yes I did connect in this way:
RX-TX
TX-RX
GND-GND
Vcc-Vdd

But somehow still couldn't work... :(
Try connecting TX-TX and RX-RX.

Put a 220 ohm series resistor between them, like this; TX-220-TX.

Some modules say "TX" as that is the pin you connect to the micro's TX pin.
 

MaxHeadRoom

Joined Jul 18, 2013
20,676
The manual appears to make it quite clear as to the hook up, and it is now clear that the RS232 levels are not needed with this unit.
What terminal program is being used? Hyperterminal or ?
Have you tried the HEX COM program? If so set it to 8 bit receive and HEX, this way you should see anything that is coming over the link.
Max.
 

THE_RB

Joined Feb 11, 2008
5,438
Thanks Max.

One easy test is to hook the module's TX to its RX pin (PIC circuit disconnected), then send a byte from hyperterminal and see that it is echoed back to hyperterminal.
 
Top