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);
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
I already posted my code and the startup.s file. When i am using simulation of the code and debug , the code hangs at BX R0
; Enter the C code IMPORT __main LDR R0, =__main BX R0
when i click the stop button the code goes to the SWI_Handler B SWI_Handler and code hangs at this point continuously. Please anyone help me out
"I already posted my code and the startup.s file. When i am using simulation of the code and debug , the code hangs at BX R0"
Well ... it's highly unlikely that the BX R0 will be jumping to the SWI handler, so you need to single step into the BX R0 and see where that takes you.
I am doing single step only and the code always wait at BX R0 continuously. After clicking stop code execution , it is going to SWI_Handler B SWI_Handler and the code hangs there
"I am doing single step only and the code always wait at BX R0 continuously"
I would think you're doing it while the source code window has focus. Enable the disassembly window, give that the focus and step into it there.
Even though i will single step in disassemble, the code hangs in 3 lines continuously. I think i cannot know the exact bug when i use disassemble window also
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