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) { } }
nobody?
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?
"What I want to do is to print a sentence to the terminal."
A simple sence - like, "Hello, world!", perhaps...
View all questions in Keil forum