This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC 2138 simple problem

Hi all,

I have a small starter problem with my LPC2138

I'm using Flash Magic to make the hex file and program it to the arm controller.

I managed to make a serial connection with the terminal on Flash magic, so I can send characters to it.

What I want to do is to print a sentence to the terminal.

this is how I print a character:

void    outchar0(char k)
        {
        unsigned long a;                        //local variable
        U0THR=k;                                        //ascii code to buffer transmitter
                while((a=U0LSR&0x41)!=0)
                {
                }
        }

Parents
  • Since it is a simple problem to make the chip send text out on the serial port, don't you think that the sample code available with the Keil tools or from the chip manufacturers site would be good to look at?

    How is the comment "//local variable" improving the program? Don't you think that a C programmer is able to directly see that the variable is local, without first examining the comment?

    Wouldn't it be better to check if the UART is ready for more data before putting a character in the transmit holding register?

    Why make use of the magic constant 0x41? Isn't it better to create a named constant, so that a reader knows exactly what the statement is waiting for?

    while((a=U0LSR&0x41)!=0)
    


    And what happens if your loop looks for two bits (0x40 and 0x01) and repeats while any one of them is set? You stripped your code, so we don't know. But your loop repeats while U0LSR returns 0x01 or 0x40 or 0x41. So what exactly do you want to repeat while you have a character available, or you have an error in the RX FIFO? And what happens if there is a gap between two received characters? Or the first character hasn't already arrived at the time when you start sending your only character?

    And have you considered:

    while ((a = U0LSR) & 0x41) { ... }
    


    Remember that a read of U0LSR will clear a number of bits in the register, so your code will not allow your variable a to pick up an overrun, a parity error, a framing error, ... since you masked two bits before the assign and not just as the terminating condition for your loop. All it will pick up is the RXFE, i.e. if there is an error anywhere in the receive FIFO. Don't you have an interest in knowing the error status for the current RX character?

Reply
  • Since it is a simple problem to make the chip send text out on the serial port, don't you think that the sample code available with the Keil tools or from the chip manufacturers site would be good to look at?

    How is the comment "//local variable" improving the program? Don't you think that a C programmer is able to directly see that the variable is local, without first examining the comment?

    Wouldn't it be better to check if the UART is ready for more data before putting a character in the transmit holding register?

    Why make use of the magic constant 0x41? Isn't it better to create a named constant, so that a reader knows exactly what the statement is waiting for?

    while((a=U0LSR&0x41)!=0)
    


    And what happens if your loop looks for two bits (0x40 and 0x01) and repeats while any one of them is set? You stripped your code, so we don't know. But your loop repeats while U0LSR returns 0x01 or 0x40 or 0x41. So what exactly do you want to repeat while you have a character available, or you have an error in the RX FIFO? And what happens if there is a gap between two received characters? Or the first character hasn't already arrived at the time when you start sending your only character?

    And have you considered:

    while ((a = U0LSR) & 0x41) { ... }
    


    Remember that a read of U0LSR will clear a number of bits in the register, so your code will not allow your variable a to pick up an overrun, a parity error, a framing error, ... since you masked two bits before the assign and not just as the terminating condition for your loop. All it will pick up is the RXFE, i.e. if there is an error anywhere in the receive FIFO. Don't you have an interest in knowing the error status for the current RX character?

Children
  • "What I want to do is to print a sentence to the terminal."

    A simple sence - like, "Hello, world!", perhaps...

  • thanks for the replies.

    I'm not that good in programming the arm controller with Keil, I just started.

    so if you ask me why I use the magic constant 0x41 then its because I extracted it from a democode found on the internet.

    I appreciate the long answer from Per Westermark, but for now I don't understand much of it.
    It will take me some time to figure al things out.

    I already found how to print a sense by using sendstr()

    
    void sendstr (char *p) {                   /* Write string */
      while (*p) {
        putchar (*p++);
      }
    }
    
    int putchar (int ch)      /* Write character to Serial Port    */
            {
            if (ch == '\n')
            {
        while (!(U0LSR & 0x20));
        U0THR = CR;                          /* output CR */
            }
            while (!(U0LSR & 0x20));
            return (U0THR = ch);
            }
    

    this works for sendstr ("1000"); sendstr(" test3 ");

    but now I want to print a value.

    like : value = 1000; sendvalue(value);

    is there an easy solution for this?
    thanks in advance!

  • It actually sounds like you don't have much (any?) prior programming experience with any tools for any target - is that right?

    If so, then you really need to start from scratch by learning the 'C' programming language; it is a very widely used language, so there is a vast library of books - both real and "virtual" - to help you.

    You could start here: http://www.keil.com/books/genbooks.asp
    Or here: publications.gbdirect.co.uk/.../

    but just asking random questions in a forum is probably not a very effective way to learn the basics of the language.

    Having learned the 'C' language, you could then move on to applying it to ARM microcontrollers:
    http://www.keil.com/books/armbooks.asp

    "I extracted it from a democode found on the internet."

    "demo code" on the internet (and elsewhere) does tend to assume that you know the programming language in which it's written...

  • Once you have learned the basics of the 'C' programming language, yes - there is.

    The not-so-easy part, of course, is the learning of the language that is needed in order to make the solution "easy"!

    It's a bit like saying, "is it easy to drive to London?"
    As driving goes, it's not particularly difficult - but it would be hard if you didn't already know how to drive!

  • is there an easy solution for this?

    Is there any reason why you can't use the standard library?

    #include <stdio.h>
    
    printf("%d"\n",value);
    

  • That's what I meant by saying that there is an easy answer, but it does require learning 'C' first!