Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Unable to echo RX data on PC via Realterm using PIC18F4520 UART

Status
Not open for further replies.
I've changed the microcontroller around to see if there was a problem with the hardware but that doesn't seem to be it (meaning I got a new PIC18f4520 but it still doesnt show anything on the terminal).

Anyone have any other ideas??

The thing I'm not able to understand is that if its working for others it should work for me... Frustrating!!!

My last thought is that the interrupt code is wrong? I had to mess around with some of the linker script code to get this line to work -

#pragma code high_vector=0x08

Are there other ways to set interrupts? Maybe something im doing wrong with the interrupt code...
 
Last edited:
Hi, Mike and PICman, try out this code, its as simple as you can get, when you send an S, it sends hello world(it is interrupt based).

Code:
//Baud Rate of 19200

#include<p18f4520.h>
#include<usart.h>
#pragma config OSC=HSPLL,WDT=OFF,LVP=OFF
void Senddata(void);
void chk_isr(void);
void hi_prioriint(void)
{
	_asm
	GOTO chk_isr
	_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
	unsigned char startcode;
	if(PIR1bits.RCIF==1)
	{	
		unsigned char startcode='S';
		unsigned char startread;
		startread=ReadUSART();
			if(startcode==startread)
			{
			Senddata();
			}
	}
}
void main(void)
{	
   	ADCON1=0x0F;
	OpenUSART(USART_TX_INT_OFF &
			  USART_RX_INT_ON &
			  USART_ASYNCH_MODE & 
			  USART_EIGHT_BIT &
			  USART_CONT_RX &
			  USART_BRGH_LOW,
			  12);
	INTCONbits.PEIE=1;
	INTCONbits.GIEH=1;
	putrsUSART ("\nPlease type a S to Start");
	while(1);
}	
void Senddata(void)
{
	putrsUSART("\nHello World\n");
}



If it does not work, please answer this question,

Have you ever performed a receive with your board before?

If not try this code, manual receive:

Code:
//Baud Rate of 19200
//Turns on RB0 when receives any byte

#include<p18f4520.h>
#include<usart.h>
#pragma config OSC=HSPLL,WDT=OFF,LVP=OFF
void main(void)
{	
   	ADCON1=0x0F;
	TRISBbits.TRISB0=0;
	LATBbits.LATB0=0;
	OpenUSART(USART_TX_INT_OFF &
			  USART_RX_INT_OFF &
			  USART_ASYNCH_MODE & 
			  USART_EIGHT_BIT &
			  USART_CONT_RX &
			  USART_BRGH_LOW,
			  12);
	INTCONbits.PEIE=1;
	INTCONbits.GIEH=1;
	putrsUSART ("\nPlease enter any alphabet ");
	while(1)
	{
		if(PIR1bits.RCIF==1)
			{
				LATBbits.LATB0=1;
			}
	}
				
}


If none of these work, try setting up the hardware on a breadboard. Use the internal oscillator to make it easy.
 
Last edited:
Syed,

Appreciate the effort! But its still not working.

If I put the hardware on a breadboard how would I go about programming it? Currently Im using an MPLAB ICD2 which connects to my PICDEM board using a lan cable type thing. That obvioulsy cannot be used with a breadboard. So do I program it first and then put it on the breadboard?
 
Syed,

Appreciate the effort! But its still not working.

If I put the hardware on a breadboard how would I go about programming it? Currently Im using an MPLAB ICD2 which connects to my PICDEM board using a lan cable type thing. That obvioulsy cannot be used with a breadboard. So do I program it first and then put it on the breadboard?

Both the codes are not working?

What about performing a receive using your board? Have you ever done that?
 
Does the 18F4520 have an internal oscillator? I'm sure I used one before and I had to use an external crystal (not had to, but the one I used didn't have an INT OSC).

Silly question, you have pulled MCLR high haven't you? I can not see any fuse settings for turning it off?

Also, if you try it with the programmer in place you will need to disconnect it or "release from reset".

It is unusual for it to not work at all! Even with the incorrect baud rate you will just get funny charachters, or some illegal data.

Have you tried a application like the Moxa Datascope or TerraTerm in HEX mode? This will show you if you are receiving anything at all.

Also, check your serial port by using a loop back (short pins 2 and 3 the 9 way D-Type).

You can also loop back the micro I presume the code above will put the LED on if any data is received?

You can use an LCD or something to see if the data is comming through correctly.

Hope this helps somewhat?

Regards

Wilksey
 
@syed - I've never used this board for receive. This my first time trying anything like this.

@Wilksey - it does have an internal oscillator, i havent tried that yet.

umm this is the first im hearing of an MCLR. how do i set it to high?

I do 'release it from reset' haha.. as for getting something, ur right that i would except that the code has a gotData if statement, which i think it is not going into because my interrupts are not working! if i remove that if statement i get a constant stream of 0's on the terminal. any thoughts as to why this is happening?

I've checked my serial port by shorting the tx rx pins and that works - also i can store data as variables in the PIC and passing these variables into TXREG. that works perfectly, its only when i try to get data from RCREG and store it in a variable and pass that to TXREG that i dont get anything.

I'm a little fuzzy on how to use LCD for this.. any links or tips on that are welcome..
 
If the LED program is not working then there is no need to do for the LCD(for checking receiving), the program clearly shows that a receive is not being made. Program the IC, make it on bread board, for MCLR check the data sheet, any resistor between 1-10k and MCLR,vcc will do, if you have a PICKIT2 then use its USART tool( will save hardware connection). This one uses the internal oscillator at 4MHz.

//Baud Rate of 4800

#include<p18f4520.h>
#include<usart.h>
#pragma config OSC=INTIO67,WDT=OFF,LVP=OFF
void Senddata(void);
void chk_isr(void);
void hi_prioriint(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
unsigned char startcode;
if(PIR1bits.RCIF==1)
{
unsigned char startcode='S';
unsigned char startread;
startread=ReadUSART();
if(startcode==startread)
{
Senddata();
}
}
}
void main(void)
{
OSCCON=0x64;
ADCON1=0x0F;
OpenUSART(USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,
12);
INTCONbits.PEIE=1;
INTCONbits.GIEH=1;
putrsUSART ("\nPlease type a S to Start");
while(1);
}
void Senddata(void)
{
putrsUSART("\nHello World\n");
}
 
Last edited:
The only time I have seen the UART send / receive funny characters is if the baud rate was wrong, but I would have though that the TX would be messed up too, so I would try the code above with the internal oscillator, I see you mentioned the PICDEM2 board, are you using the built in MAX232 chip for this? I presume it is wired to RC6/7 for the hardware UART?

You may also, as standard get 1 funny character through a terminal (maybe have to view the HEX representation) of the UART Init.


There isn't anything special to a simple UART rx / tx, my demo board consists of a 16F887, MAX232, couple of caps, 10k resistor on the MCLR to +5v, DB9 Connector for the serial, and a bit of wire wrap.

I use the internal 4MHz OSC at 9600bps and it works fine. So try the internal OSC, if not I would check the MAX232 chip is working correctly, you could try using a lower baud rate also, like 2400, 1200 etc.
 
Last edited:
i am using a max232 chip, and like u said the TX part is working correctly so i dont think theres an issue there..
i read up on the MCLR in the data sheet.. now im pretty new to this so it dint make a whole lot of sense.. but from what ur saying.. there needs to be a resistor in there somewhere? how exactly does that help??
 
All the necessary hardware is on your PICDEM 2 Board, i think that is where the problem lies, thats why i am advising to check the program i wrote on a standalone setup.
 
It's called a pull up resistor, pulls the line when it is floating to high.
Otherwise the PIC could go into a permanent reset cycle.

You could try a different demo board, or construct a new one as Syed has mentioned.
 
Status
Not open for further replies.

Latest threads

Back
Top