serial communication-- bizarre

Thread Starter

pawankumar

Joined Oct 28, 2009
42
Hi friends,

I am facing a strange issue. I am trying to serially communicate between two microcontrollers(LPC2148) and in vain.

1)I ve used one board for transmitting 'A' continuously @9600,8bits,No pairity,1stop bit. It works well with P.C-Hyperterminal.

2)I ve used another board to receive the data sent by 1 and display it in LCD. it never works.

I ve tried both ways: connecting Rxd,Txd,Gnd appropriately, and using Max 232 on both sides.

My doubt is : does hyperterminal recieve data differently?

I ve called a function rec, which waits and receives a byte of data, which is returned.

for your reference:

unsigned char rec()
{
unsigned char p;
while ((U0LSR&0x01)!=1);
p=U0RBR;
return p;
}

I ve also included my C code. don't mind it, I am a shabby fellow
Rich (BB code):
#include<lpc214x.h>
#define enhi IO0SET=(1<<18)
#define rshi IO0SET=(1<<16)
#define rwlo IO0CLR=(1<<17)

#define enlo IO0CLR=(1<<18)
#define rslo IO0CLR=(1<<16)
void init()							  //UART0 initialization
{
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
}
unsigned char rec()				//recieves a byte of data
{
unsigned char p;
while ((U0LSR&0x01)!=1);
p=U0RBR;
return p;
}
void delay(int msec)
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<60000;j++);
}
void cmd(unsigned int text)				 //LCD command sending fn
{
unsigned int cmdhi=(text&0xF0)<<15;
unsigned int cmdlow=(text&0x0F)<<19;
rwlo;
rslo;
IO0PIN&=0xFF07FFFF;
IO0PIN|=cmdhi;
enhi;
delay(1);
enlo;
//delay(20);
IO0PIN&=0xFF07FFFF;
IO0PIN|=cmdlow;
enhi;
delay(1);
enlo;
}
void data(unsigned int dat)					//LCD data sending fn
{
unsigned int dathi=(dat&0xF0)<<15;
unsigned int datlow=(dat&0x0F)<<19;

rwlo;
rshi;
//delay(20);
IO0PIN&=0xFF87FFFF;
IO0PIN|=dathi;
enhi;
delay(1);
enlo;
//delay(20);
IO0PIN&=0xFF87FFFF;
IO0PIN|=datlow;
enhi;
delay(1);
enlo;
}
void word(unsigned char *word)
{
while(*word!='\0')
{
data(*word);
word++;
}
}
int main()
{
PINSEL0=0x00000005;		
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
IO0DIR|=(1<<16)|(1<<17)|(1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22);
init();
cmd(0x20);
cmd(0x28);
cmd(0x0C);
cmd(0x06);
cmd(0x01);
cmd(0x80);
while(1)
{
data(rec());
}
}
but do help me. Am equally dumb.
 

Bosparra

Joined Feb 17, 2010
79
Chances are, that the problem is with the receiving side. Hyperterminal does not do anything different. I am not familiar with the microcontroller you are using, but some of the things to check are:
1. RX and TX must be swapped i.e. the one's TX must go to the other one's RX.
2. Check the pre scaler value for the baud rate you are using.
3. Try toggle an LED when ANY data is received, this will eliminate issues with the LCD interface.
 

t06afre

Joined May 11, 2009
5,934
If things work well with hyperterm. Then something is wrong on the receiving board. Hard to say what is wrong. Without knowing more. Have you crossed the signals like this
GND--------GND
RxD--------TxD
TxD--------RxD
 

Thread Starter

pawankumar

Joined Oct 28, 2009
42
@bosparra : Except that prescaler thing, which i ve started googling, everything else is familiar to me and I tried blinking an LED for every alternate byte received. It remains dead stopped.

@t06afre : exactly , that's how I ve connected. I did the same using Max 232 on both sides and directly. but in vain.
@MrChips: yes, both of 12Mhz .
 

MrChips

Joined Oct 2, 2009
30,706
Rich (BB code):
unsigned int dathi=(dat&0xF0)<<15;
unsigned int datlow=(dat&0x0F)<<19;
What is the size of int?

You should not use int. Use short or long or create new data types such as uint16, uint32.
 
Last edited:

stirling

Joined Mar 11, 2010
52
I don't know this MCU and it would help if you indented and properly commented your code. However for starters and as a shot to nothing you might want to check that your delay is actually doing something. Your compiler may be optimizing it out of existance (loop(s) with no "side effect"/no compiler directive).

Also data() takes an unsigned int yet rec() returns an unsigned char - sloppy at best so I'd take a stab that your problem is your software.

EDIT: Yes Mr. Chips I wondered that but I think this is a 32 bit chip.
 

MrChips

Joined Oct 2, 2009
30,706
Rich (BB code):
IO0DIR|=(1<<16)|(1<<17)|(1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22);
This is a bit nasty code.

@Crathes, this is an example where a 32-bit machine is a nuisance.
 

t06afre

Joined May 11, 2009
5,934
What you can do is to modify your working program. To echo any char sent from the hyperterm program. If you get that correct. You will master both receive and transmit.
 

John P

Joined Oct 14, 2008
2,025
I don't like the look of the line

while ((U0LSR&0x01)!=1);

Are you absolutely certain sure that the flag will be set eventually? If it's not, you've got an endless wait there. I'd be more comfortable with a routine that polled the flag and kept the processor active, then grabbed the incoming character if and when the flag was seen.
 

Thread Starter

pawankumar

Joined Oct 28, 2009
42
I am also trying to write a code which is interrupt based. I ve Wondered how people often appreciate datasheets.I truly miss you 8051
 

Thread Starter

pawankumar

Joined Oct 28, 2009
42
I don't know how to poll an interrupt when i receive something. I have tried a code and obviously, its wrong. please help me with a simple code to do this : transmit a char continuously, when there is something received, store it in a variable and keep transmitting the variable.

Ok, you may laugh for a while after seeing the code.
Rich (BB code):
#include<lpc214x.h>
unsigned char a;
void u0init()
{
U0IER=0x01;
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
}
void interrupt()
{
a=U0RBR;
}
void u0tx(unsigned char data)
{ 
U0THR=data;
while(!(U0LSR&0x60));

}



int main()
{
PINSEL0=0x00000005;
PINSEL1=0x00000000;
PINSEL2=0x00000000;
u0init();
while(1)
{
u0tx('l');
u0tx(a);
}
}
 

Thread Starter

pawankumar

Joined Oct 28, 2009
42
Am sorry stirling, i ve included comments for the code. this problem is rather generic : I am able to receive a character sent by P.C hyperterm into my LPC controller but not from any other device. : i tried with GPS module and another ARM board.
Rich (BB code):
#include<lpc214x.h>
unsigned char a;
void u0init()          //initialization @9600 baud, 8bits,1stop bit,none pairity
{
U0IER=0x01;
U0LCR=0x83;
U0DLL=0x61;
U0DLM=0x00;
U0LCR=0x03;
}
void interrupt()       //this is my own illogical stuff
{
a=U0RBR;
}
void u0tx(unsigned char data)   //transmits a byte through UART0
{ 
U0THR=data;
while(!(U0LSR&0x60));

}



int main()
{
PINSEL0=0x00000005;    //select pins P0.0 and 0.1 to work as txd0 and rxd0
PINSEL1=0x00000000;
PINSEL2=0x00000000;
u0init();
while(1)
{
u0tx('l');     //transmits 'l' continiously
u0tx(a);      //transmits the value stored in a
}
}
 

stirling

Joined Mar 11, 2010
52
pawankumar, I'm now lost as to what you are and are not able to do. You said in your first post...

Hi friends,

I am facing a strange issue. I am trying to serially communicate between two microcontrollers(LPC2148) and in vain.

1)I ve used one board for transmitting 'A' continuously @9600,8bits,No pairity,1stop bit. It works well with P.C-Hyperterminal.

But now you say...

Am sorry stirling, i ve included comments for the code. this problem is rather generic : I am able to receive a character sent by P.C hyperterm into my LPC controller but not from any other device. : i tried with GPS module and another ARM board.
:confused:

I've just googled "uart example code for LPC2148" and it comes back with loads of stuff. Best I can suggest is you read a few and see where you get. Sorry :(
 

PaulEE

Joined Dec 23, 2011
474


Hyperterminal also asks whether or not you're controlling flow of bytes with hardware, software, or none.

Make sure your microcontroller is hooked up right (image) and you check out whether all your other hardware is configured as null modem wiring.

If you receive bytes in one case, that means there is a difference between this case and others, not your code...which points to hardware. Perhaps your hardware DOES have flow rate controlled via hardware or software. In this case, you will need a code change, or hardware modifications.
 
Top