I want to interface MCB2140 with a pressure sensor which will give input for every 1 second.. I connected UART0 to pressure sensor output, UART1 to serial port of PC. The pressure sensor gives the integer value as output.The UART0 will receive data from sensor and should display in hyper terminal for every 1 minute..I wrote the following code..The code gets hanged in startup file at BX R0...Please let me know any other changes required #include <LPC21xx.H> void init_serial (void); int main(void) { init_serial();
while(1) { int getc_serial0; int putc_serial1; } } void init_serial (void) {
PINSEL0 = 0x00050005;
U0LCR = 0x00000083;
U0DLL = 98;
U0LCR = 0x00000003;
U1LCR = 0x00000083;
U1DLL = 98;
U1LCR = 0x00000003;
}
int getc_serial0(void)
{
while (!(U0LSR & 0x01));
return U0RBR;
} int putc_serial1(void)
while (!(U1LSR & 0x20));
U1THR=U0RBR;
return (U1THR);
What three lines?
And no register that counts down in these three lines?
As suggested by the IB Shy, i enabled the disassemble window and did single step. Below code is the taken from disassemble window.
0x00000190 E2522010 SUBS R2,R2,#0x00000010 0x00000194 28A10078 STMCSIA R1!,{R3-R6} 0x00000198 8AFFFFFC BHI 0x00000190
It starts from 1st line and then it goes to 2nd line and 3rd line. From 3rd line it will again come to 1st line and this is continuous loop
"It starts from 1st line and then it goes to 2nd line and 3rd line. From 3rd line it will again come to 1st line and this is continuous loop"
And what's R2 doing during this continuous loop?
Put a breakpoint on the following instruction - Does it get to that?
Same problem, after using breakpoints also the code moves over three lines
"Same problem, after using breakpoints also the code moves over three lines"
What do you mean "same problem"?
So you've gone past the three lines of code. Did you then single step further to see what happens?
I have kept a breakpoint at 0x00000190 E2522010 SUBS R2,R2,#0x00000010 , then single step. The code moves over the 3 lines continuously in a loop
How many times did you step the three lines?
And more importantly - didn't you set a breakpoint on the first instruction _after_ the three-line loop?
Westermark, i did not understand what you adviced to do
So, it seems I need to repeat what I asked before:
"And what's R2 doing during this continuous loop?"
That was a hint for you - To see if you'd figure out what the loop was doing.
You need to put a breakpoint at 0x0000019C (i.e., on the following instruction) to see if it gets there. If it does, then you're past your continuous loop and you can single step further.
Till now i did not got to know, what is the need of changing the startup code?. Is there any bug in my application code or startup file.
If you have a loop:
int i; for (i = 0; i < 100; i++) { mem[i] = 0; }
and you single-step the above loop, it will seem like it is an infinite loop. But code that repeats are not automatically infinite.
If you notice that the loop counter is ticking, it would be obvious that it isn't an infinite loop.
Then there would be two alternatives available: 1) The loop ends when it has run the configured number of turns - best caught with a breakpoint on the first statement _after_ the loop.
2) The loop ends because it performs an invalid operation or invalid access. Best caugt by having breakpoints on the exception handlers and then backtrack where it came from and how far it got in the loop before the failure.
3) The loop may be intended to be infinite but actually be infinite because the loop body happens to have a side effect on the loop counter resulting in it never being able to reach the termination value.
It doesn't seem like you are trying to put any breakpoints after the loop and on the exception handlers. So if you just single-step, it can take a potentially very long time to get through the loop. And if you only have your next breakpoint inside main() and the loop can't end because of access problems, you will not find out.
Debugging is all about subdividing problems into smaller sub-problems to find out what is actually happening and when what is happening starts to deviate from what is expected to happen.
You are receiving a lot of hints what to look for, but you don't seem to try.
Thank you westermark for your kind advice..I am trying hard after your suggestions from 1st day of your guidance.Thanks for all..I will try to debug
A successful debugging experience to fix a certain problem is important to a beginner. But I think it might be too early to learn how to debug for the OP. I say so, because I think my skill level is closer to the OP.
To the OP: Maybe it would be better to start with something it does work; and try to study how it works. Then, you will get some knowledge about startup code, C main(), exception handler, UART, and how to trace a working well program. Then re-start to learn how to debug.
To the OP:
I quickly re-read the whole thread, I think you could start with a working well UART example project for your LPC2148.
1. Find one working well UART example project for your LPC2148. 2. Trace (Debug) it on your real hardware, to see how it works.
Please let me know if any bug is there in the code.
#include<lpc214x.h> #include<stdio.h> void init_serial(void) { PINSEL0 = 0x00050005; /* Enabling RxD0 and TxD0, RxD1 and TxD1 */ U0LCR = 0x00000083; /* 8 bits, no Parity, 1 Stop bit * for UART0*/ U0DLL = 98; U0FCR=0x01; /* To enable the Tx and Rx FIFO*/ U0IER=0; /*Disable all interrupts*/ U0LCR = 0x00000003; /* DLAB = 0 for UART0*/ U1LCR = 0x00000083; /* 8 bits, no Parity, 1 Stop bit for UART1*/ U1DLL = 98; U1LCR = 0x00000003; /* DLAB = 0 * for UART1*/ U1FCR=0x81; /*Enable the FIFO registers*/ U1IER=0; /*Disable all interrupts*/ } char receive() { if (U0LSR & 0x01) /* If U0LSR 1st bit contains valid data then return value of U0RBR*/ { if(U0RBR >=48 || U0RBR <=58) /*If data received is between 0 to 9 then only return that value to the fucntion receive()*/ return (U0RBR); } return '#'; /* If other than 0 to 9 data is recieved return #*/ } void transmit() { if(!(U1LSR & 0x20) && receive() != '#' ) /* Checking whether U1LSR 5th bit contains valid data or not and data is between 0 to 9*/ { U1THR=receive(); /* copying the receive() to U1THR*/ printf("%d",U1THR); /*print U1THR */ } else printf("z"); /*Print z */ } main(void) { init_serial(); /* Initailization of UART1 & UART2*/ while(1) /* Infinite loop*/ { receive(); /* function for receiving data from sensor */ transmit(); /* function for transmitting the data to PC hyperterminal*/ } }
"Please let me know if any bug is there in the code."
So, haven't you either tested the code and found that it seems to work? Or tested the code and found that it doesn't work, but after spent time debugging it failed to locate the problem?
No - your code is incorrect. When you return with information about what testing you have done and what problems you have seen, I will consider helping with some further notes about the code. But just some hints: - do you think you should read out data from the UART multiple times? - do you think you should have a receive() that returns a value and then sometimes call receive() without using the return value()? - do you think you should have a main loop that calls receive() and transmit() and also have transmit() call receive()? - does the user manual for the chip say that you can read from the transmitter holding register?
See comment in my first post (the second in this thread): "What have you done to find out what the problem may be?"
And you still don't seem to have considered posting code with good indentation - proper indentation makes the code way easier to read. And note that web forms have big problems with code indented with tab characters or that mixes tabs and spaces for identing.