serial communication-- bizarre

Discussion in 'The Projects Forum' started by pawankumar, Feb 1, 2012.

  1. pawankumar

    Thread Starter Member

    Oct 28, 2009
    42
    3
    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
    Code ( (Unknown Language)):
    1.  
    2. #include<lpc214x.h>
    3. #define enhi IO0SET=(1<<18)
    4. #define rshi IO0SET=(1<<16)
    5. #define rwlo IO0CLR=(1<<17)
    6.  
    7. #define enlo IO0CLR=(1<<18)
    8. #define rslo IO0CLR=(1<<16)
    9. void init()                           //UART0 initialization
    10. {
    11. U0LCR=0x83;
    12. U0DLL=0x61;
    13. U0DLM=0x00;
    14. U0LCR=0x03;
    15. }
    16. unsigned char rec()             //recieves a byte of data
    17. {
    18. unsigned char p;
    19. while ((U0LSR&0x01)!=1);
    20. p=U0RBR;
    21. return p;
    22. }
    23. void delay(int msec)
    24. {
    25. int i,j;
    26. for(i=0;i<msec;i++)
    27. for(j=0;j<60000;j++);
    28. }
    29. void cmd(unsigned int text)              //LCD command sending fn
    30. {
    31. unsigned int cmdhi=(text&0xF0)<<15;
    32. unsigned int cmdlow=(text&0x0F)<<19;
    33. rwlo;
    34. rslo;
    35. IO0PIN&=0xFF07FFFF;
    36. IO0PIN|=cmdhi;
    37. enhi;
    38. delay(1);
    39. enlo;
    40. //delay(20);
    41. IO0PIN&=0xFF07FFFF;
    42. IO0PIN|=cmdlow;
    43. enhi;
    44. delay(1);
    45. enlo;
    46. }
    47. void data(unsigned int dat)                 //LCD data sending fn
    48. {
    49. unsigned int dathi=(dat&0xF0)<<15;
    50. unsigned int datlow=(dat&0x0F)<<19;
    51.  
    52. rwlo;
    53. rshi;
    54. //delay(20);
    55. IO0PIN&=0xFF87FFFF;
    56. IO0PIN|=dathi;
    57. enhi;
    58. delay(1);
    59. enlo;
    60. //delay(20);
    61. IO0PIN&=0xFF87FFFF;
    62. IO0PIN|=datlow;
    63. enhi;
    64. delay(1);
    65. enlo;
    66. }
    67. void word(unsigned char *word)
    68. {
    69. while(*word!='\0')
    70. {
    71. data(*word);
    72. word++;
    73. }
    74. }
    75. int main()
    76. {
    77. PINSEL0=0x00000005;    
    78. PINSEL1 = 0x00000000;
    79. PINSEL2 = 0x00000000;
    80. IO0DIR|=(1<<16)|(1<<17)|(1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22);
    81. init();
    82. cmd(0x20);
    83. cmd(0x28);
    84. cmd(0x0C);
    85. cmd(0x06);
    86. cmd(0x01);
    87. cmd(0x80);
    88. while(1)
    89. {
    90. data(rec());
    91. }
    92. }
    93.  
    but do help me. Am equally dumb.
     
  2. Bosparra

    Member

    Feb 17, 2010
    79
    3
    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.
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Are you using xtals on both MCUs?
     
  5. pawankumar

    Thread Starter Member

    Oct 28, 2009
    42
    3
    @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 .
     
  6. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Code ( (Unknown Language)):
    1. unsigned int dathi=(dat&0xF0)<<15;
    2. 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: Feb 1, 2012
  7. stirling

    Member

    Mar 11, 2010
    52
    2
    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.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Code ( (Unknown Language)):
    1. 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.
     
  9. stirling

    Member

    Mar 11, 2010
    52
    2
    who's Crathes? :D
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  12. stirling

    Member

    Mar 11, 2010
    52
    2
    @MrChips Ah right - had me confused for a moment there (easilly done).
     
  13. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
    pawankumar likes this.
  14. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Good point. His code could be hanging on the get go.
     
  15. pawankumar

    Thread Starter Member

    Oct 28, 2009
    42
    3
    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
     
  16. pawankumar

    Thread Starter Member

    Oct 28, 2009
    42
    3
    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.
    Code ( (Unknown Language)):
    1. #include<lpc214x.h>
    2. unsigned char a;
    3. void u0init()
    4. {
    5. U0IER=0x01;
    6. U0LCR=0x83;
    7. U0DLL=0x61;
    8. U0DLM=0x00;
    9. U0LCR=0x03;
    10. }
    11. void interrupt()
    12. {
    13. a=U0RBR;
    14. }
    15. void u0tx(unsigned char data)
    16. {
    17. U0THR=data;
    18. while(!(U0LSR&0x60));
    19.  
    20. }
    21.  
    22.  
    23.  
    24. int main()
    25. {
    26. PINSEL0=0x00000005;
    27. PINSEL1=0x00000000;
    28. PINSEL2=0x00000000;
    29. u0init();
    30. while(1)
    31. {
    32. u0tx('l');
    33. u0tx(a);
    34. }
    35. }
    36.  
     
  17. stirling

    Member

    Mar 11, 2010
    52
    2
    Still no indentation - still no comments! Why do you expect US to try to figure out your code. Try to help US to help YOU.
     
  18. pawankumar

    Thread Starter Member

    Oct 28, 2009
    42
    3
    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.
    Code ( (Unknown Language)):
    1.  
    2. #include<lpc214x.h>
    3. unsigned char a;
    4. void u0init()          //initialization @9600 baud, 8bits,1stop bit,none pairity
    5. {
    6. U0IER=0x01;
    7. U0LCR=0x83;
    8. U0DLL=0x61;
    9. U0DLM=0x00;
    10. U0LCR=0x03;
    11. }
    12. void interrupt()       //this is my own illogical stuff
    13. {
    14. a=U0RBR;
    15. }
    16. void u0tx(unsigned char data)   //transmits a byte through UART0
    17. {
    18. U0THR=data;
    19. while(!(U0LSR&0x60));
    20.  
    21. }
    22.  
    23.  
    24.  
    25. int main()
    26. {
    27. PINSEL0=0x00000005;    //select pins P0.0 and 0.1 to work as txd0 and rxd0
    28. PINSEL1=0x00000000;
    29. PINSEL2=0x00000000;
    30. u0init();
    31. while(1)
    32. {
    33. u0tx('l');     //transmits 'l' continiously
    34. u0tx(a);      //transmits the value stored in a
    35. }
    36. }
    37.  
     
  19. stirling

    Member

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


    But now you say...

    :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 :(
     
  20. PaulEE

    Member

    Dec 23, 2011
    423
    32
    [​IMG]

    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.
     
Loading...