Following is my program. I am trying to read characters at different baud rate and if no character is received within 10 seconds I want to break out of the loop and similarly if any character other than alphabets is received, I again want to break out of the loop? How can I do that. Please guide me.
char read() { while(1) { delay_ms(10000); break;
if((uart_RxChar() >= 65 && uart_RxChar() <= 90) || (uart_RxChar() >= 97 && uart_RxChar() >= 122)) { uart_TxChar('k'); Glow_GreenLED(); } else { Glow_RedLED(); break; } } }
1. Learn how to insert code in post. 2. uart_RxChar() >= 122 <- should be uart_RxChar() <= 122 3. I understand that "uart_RxChar()" read character from UART. But - you read it 4 times. After first comparsion (uart_RxChar() >= 65) character (if was received) will be cleared. So you have to create local variable to get it works:
char data; ... in loop data = uart_RxChar(); if((data >= 65 && data <= 90) || (data >= 97 && data <= 122)) {
4. 65, 90 - what's this? Of course I know but you'll forget in less than week. Why you don'y use 'A', 'Z', etc?
Think about the loop more logically so things occur in parallel and aren't serialized. ie Loop 10000 times waiting 1ms, checking the availability as it occurs, not after 10 seconds has expired. Better yet use some time stamp that is independent of the loop itself, and check for characters as they arrive, at high baud rates this may exceed one per millisecond, and the goal is to not miss something by ignoring things for an arbitrary period.
Each time you call the function it likely pulls another character, not the same one, read it once into a variable, then range check the variable.
The uart_Rx() receives a character and stores it in a local variable and at the end return that local variable to the function. I have used the timer to control the execution time. The new problem which I am facing is that my UART is not able to change baud rates as soon as a break is appeared in the loop. Following is my code to read character. Please advise if I am doing anything wrong.
char read() {
flag0=0; timer0_init(); timer_count =0; uart_rxChar();
while(1) {
if(timer_count<300) {
if(uart_RxChar()>=97 && uart_RxChar()<=122) {
uart_TxChar('k');
if(timer_count>=299) {
LPC_GPIO2->FIOCLR=1<<13; Glow_GreenLED(); delay_ms(100); flag0=1;
}
else {
Glow_RedLED(); LPC_GPIO2->FIOCLR = 1<<8; break;
else if(timer_count>300) {
LPC_GPIO2->FIOCLR = 1<<8; Glow_RedLED(); delay_ms(100); LPC_GPIO2->FIOCLR = 1<<13; break;
} } }
//UART Receiving Function
char uart_RxChar() {
while(util_IsBitCleared(LPC_UART0->LSR, SBIT_RBR)); //Wait till data is received char data = LPC_UART0->RBR; return data;
As was previously brought to your attention
if(uart_RxChar()>=97 && uart_RxChar()<=122) // Reads up to TWO characters
You also need to pay attention to the instructions for posting source code.
To spell it out: www.danlhenry.com/.../keil_code.png