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);
If the program hangs in the startup file long before reaching main(), then I don't see the need to post your C code.
What have you done to find out what the problem may be?
Have you selected the correct processor, so you got the correct startup file?
Are the memory settings correct, so the program doesn't try to access memory that doesn't exist?
Any watchdog enabled that needs to be kicked before the startup code manages to reach your main loop?
Does the startup code initializes any interrupts without having the interrupt vectors mapping to real code?
Have you tried breakpoints for the exception handlers?
Is the program even correctly downloaded to your device?
Is your device properly working? Tested with another program?
Thanks westermark for your reply. When i run the code in simulation mode the code is running perfectly ( i.e code is executed and waits for the data in UART0. But when i execute the code in board the code hangs at BX R0.. I selected the correct processor and startup file. I don't know which memory i have to select. I have selected LPC2000IAP2 512kB. I did not enabled any watch dog timer. I already programmed other simple programs in the same processor and it is working fine. Please see whether any i did any mistake in the code i posted in forum....
"Please see whether any i did any mistake in the code i posted in forum...."
But if it hangs in the startup file, it never reaches main(). So it will not matter if you have any bugs in the code that can't be accessed until you reach main().
Your question is a bit like: When I turn the ignition key, nothing happens. So can you please check if I have adjusted the driver seat correctly.
The startup file requires that your project file is correctly configured, so that there exists flash where you are going to download the binary and so that there exists RAM where the startup file tries to prepare a stack, an optional heap and where the startup file tries to initialize all your variables.
The main exception to the above is if you have modified your startup file, or if you are using C++ and have global objects with constructors - these constructors will be called before you reach main() and can get into troubles with failed memory allocations, pointer errors, buffer overruns, division by zero etc.
By the way: Your code does not work in the simulator either.
Your "code" - you forgot to read the instructions on how to post source code (directly above the message input box) - does not do anything:
nt main(void) { init_serial(); while(1) { int getc_serial0; <= what do you think this line does? int putc_serial1; <= and this? } }
Your while loop needs to call the functions.
So - please remind us. Exactly how well did your program actually work in the simulator?
Remember that the simulator may not fail in similar ways for incorrect project configurations that doesn't match the real processor. And you only mentioned a selected flash-programming algorithm for LPC chips. But that is not enough to have a proper project configuration file...
Thanks westermark for your kind suggestions. I have changed the program..The program will run once only, i removed while(1) loop.
#include <LPC21xx.H> init_serial() { PINSEL0 = 0x00050005; U0LCR = 0x00000083; U0DLL = 98; U0LCR = 0x00000003; U1LCR = 0x00000083; U1DLL = 98; U1LCR = 0x00000003; } getc_serial0() { while (!(U0LSR & 0x01)); return U0RBR; } putc_serial1() { while (!(U1LSR & 0x20)); U1THR=U0RBR; return (U1THR); } main() { init_serial(); getc_serial0(); putc_serial1(); }
And then what will happen...?!
"i removed while(1) loop"
Why?
What do you think that will achieve?
main() { init_serial(); getc_serial0(); putc_serial1(); }
Do you not believe in indentation when you write source code?
Do you not think this is easier to read:
main() { init_serial(); getc_serial0(); // What is the point of this call? putc_serial1(); // What is the point of this call? }
What will happen when main() returns...?
Wait a minute. Your original error description was "The code gets hanged in startup file at BX R0".
Do you not mean that it hangs on startup, but that it does enter main() and then after a while fails? Always (!) write clear enough error descriptions that people who read them can understand what problems you are having.
If the program do reach main(), but then the obvious question is if you have tried to step through your code to getIn that case - haven't you checked how many source lines you can step through before you get an exception? Haven't you tried my previous advice: "Have you tried breakpoints for the exception handlers?"
A main loop without a loop is obviously an epic failure. There is no command line or GUI desktop to get back to when your application ends. So don't let your programs end until you build large enough embedded systems that you really do have a GUI program manager or a command line (Linux, Win CE, ...)
Please let me know any problem is there in the code which i posted earlier. I don't know what is going on in the code. I am new to the embedded field. Please rather than asking me can anyone exactly tell the reason why it is not working.
It doesn't matter how new you are to embedded.
1) You just have to learn to debug. Therea are no alternatives, since people on web forums will not copy/paste code just to debug for you.
2) You have received multiple comments about your strange plays with your get/put functions. In the first code you didn't do any calls at all. In the second sets of code you don't do what you should - return a byte from getc() and send a byte to putc(). You somehow thinks putc() should copy data between two SFR which is the wrong thing to do.
3) Being new to something means a large need to read. What have you been reading? The datasheet for your processor? Don't assume that a transmit holding register (used for writing data to a serial port) is a register that can also be read. Exactly what do you think it will return? If the receive and transmit registers where actually the same thing - wouldn't the processor just have a "data register" then? And for a processor that can cache multiple outgoing bytes - which byte should the transmit holding register then return? And what if the transmitter is empty - what should the transmit holding register then return?
Lots of questions in my text? Yes. Why? Because you need to see for yourself that you are making assumptions. To assume is sometimes spelled ass-u-me because assumptions is the best way to do something wrong. Assumption is just a "finer" word for "guessing". Datasheets, user manuals, application notes, sample code packages etc are there so that developers will not have to guess.
Remember that I - and everyone else on this forum - are regularly new to different processors. But we can't then write random code lines and ask on a forum why the lines doesn't work. We have to read until we find text sections that tells us what is needed to get data from a serial port. Or for sending data to a serial port. Or asking if data is available. Or the transmitter have room for more data. Or how to set the correct baudrate.
Experience just means we better knows what terms to scan for in the manuals. And that we are quicker at "guessing" where to put breakpoints in the code - or trace output - to compare what really happens with what was intended to happen.
"Please let me know any problem is there in the code which i posted earlier"
Plenty of problems have been pointed out to you already.
"I don't know what is going on in the code. I am new to the embedded field"
So, before just charging into writing code, don't you think you should spend some time studying & learning first?
If you wanted to learn to swim, would you just jump off a ship in the middle of the ocean...?
There are book suggestions here: http://www.keil.com/books/
Seminars here: http://www.keil.com/events/
Training providers here: http://www.keil.com/events/links.asp
etc, etc,...
But a discussion forum such as this cannot give you a full training course in embedded development!
"Please rather than asking me can anyone exactly tell the reason why it is not working"
But you need to lay the basic foundations before you will understand the explanations!
You have two choices here:
1. Spend time (possiby also money) to learn about embedded development;
2. Pay someone to do this for you...
This is the code which i wrote for using 2 UARTs at a time.
#include <LPC21xx.H> #include<stdio.h> void init_serial() { PINSEL0 = 0x00050005; /* Enabling RxD0 and TxD0, RxD1 and TxD1 */ U0LCR = 0x00000083; /* 8 bits, no Parity, 1 Stop bit * for UART0*/ U0DLL = 98; 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*/ } int getchar1() { if (U0LSR & 0x01) /* If U0LSR 1st bit contains valid data then return value of U0RBR*/ { return (U0RBR); } else { return 0; /*Else return 0 to the fucntion*/ } } void putchar1() { while(!(U1LSR & 0x20)); /* If U1LSR 5th bit contains valid data the copy the value of getchar1() value to U1THR*/ U1THR=getchar1(); printf("%d",U1THR); } int main() { init_serial(); /* Initailization of UART1 & UART2*/ while(1) /* Infinite loop*/ { getchar1(); /* function for receiving data from sensor */ putchar1(); /* function for transmitting the data to PC hyperterminal*/ } }
The following is the startup.s file where the program hangs at SWI_Handler B SWI_Handler
Vectors LDR PC, Reset_Addr LDR PC, Undef_Addr LDR PC, SWI_Addr LDR PC, PAbt_Addr LDR PC, DAbt_Addr NOP ; Reserved Vector LDR PC, IRQ_Addr LDR PC, [PC, #-0x0FF0] ; Vector from VicVectAddr LDR PC, FIQ_Addr Reset_Addr DCD Reset_Handler Undef_Addr DCD Undef_Handler SWI_Addr DCD SWI_Handler PAbt_Addr DCD PAbt_Handler DAbt_Addr DCD DAbt_Handler DCD 0 ; Reserved Address IRQ_Addr DCD IRQ_Handler FIQ_Addr DCD FIQ_Handler Undef_Handler B Undef_Handler SWI_Handler B SWI_Handler PAbt_Handler B PAbt_Handler
Maybe you need a retarget.c file...?
I tried to add retarget.c file form already available keil examples folder. But it show error
Hello chiranjeevi vivek,
The reason why your program hangs in the startup file at the SWI_Handler is that you are using SWI functions without providing a SWI handler which processes the SWIs.
An example for an SWI handler can be found in .\Keil\ARM\Examples\SWI.
An other reason could be that your program is a RTX application and you do not provide the correct startup file.
RTX uses SWI functions and the RTX kernel provides a SWI handler. To use this SWI handler you need a slightly different startup file:
Vectors LDR PC, Reset_Addr LDR PC, Undef_Addr LDR PC, SWI_Addr LDR PC, PAbt_Addr LDR PC, DAbt_Addr NOP ; Reserved Vector ; LDR PC, IRQ_Addr LDR PC, [PC, #-0x0FF0] ; Vector from VicVectAddr LDR PC, FIQ_Addr Reset_Addr DCD Reset_Handler Undef_Addr DCD Undef_Handler SWI_Addr DCD SWI_Handler PAbt_Addr DCD PAbt_Handler DAbt_Addr DCD DAbt_Handler DCD 0 ; Reserved Address IRQ_Addr DCD IRQ_Handler FIQ_Addr DCD FIQ_Handler IMPORT SWI_Handler Undef_Handler B Undef_Handler ;SWI_Handler B SWI_Handler ; Part of RTL PAbt_Handler B PAbt_Handler DAbt_Handler B DAbt_Handler IRQ_Handler B IRQ_Handler FIQ_Handler B FIQ_Handler
Best Regards, Martin Guenther