我試圖通過輸入UART事件處理程序捕獲輸入數字,並用該數字的一些乘法打印回來。它打印字符串很好,但打印完成後程序不再對任何輸入做出反應。可以打印alfabet或顯示錯誤消息,但是當我使用printf函數時,終端停止響應輸入,並且光標位於下一行的中間位置。Printf停止工作的終端
這是C代碼:
#include "mss_uart.h"
#include <stdio.h>
#define RX_BUFF_SIZE 64
#define MSS_UART_57600_BAUD 57600
uint8_t g_rx_buff[RX_BUFF_SIZE];
uint8_t g_rx_idx;
void uart0_rx_handler(mss_uart_instance_t * this_uart)
{
MSS_UART_get_rx(&g_mss_uart0, &g_rx_buff[g_rx_idx], sizeof(g_rx_buff));
if(g_rx_buff[g_rx_idx] > 96 && g_rx_buff[g_rx_idx] < 123)
{
uint8_t message[55] = "De letter was: x, de uppercase letter van : x is y.\n\r";
message[15] = g_rx_buff[g_rx_idx];
message[44] = g_rx_buff[g_rx_idx];
message[49] = g_rx_buff[g_rx_idx] - 32;
MSS_UART_polled_tx(&g_mss_uart0, message, sizeof(message));
}
else if(g_rx_buff[g_rx_idx] > 64 && g_rx_buff[g_rx_idx] < 91)
{
uint8_t message[55] = "De letter was: x, de lowercase letter van : x is y.\n\r";
message[15] = g_rx_buff[g_rx_idx];
message[44] = g_rx_buff[g_rx_idx];
message[49] = g_rx_buff[g_rx_idx] + 32;
MSS_UART_polled_tx(&g_mss_uart0, message, sizeof(message));
}
else if(g_rx_buff[g_rx_idx] > 47 && g_rx_buff[g_rx_idx] < 58)
{
int number = g_rx_buff[g_rx_idx] - '0';
int number2 = number * number;
int number3 = number2 * number;
int number4 = number3 * number;
printf("Getallenreeks: %d, %d, %d, %d.\n\r", number, number2, number3, number4);
}
else
{
uint8_t message[10] = "Error.\n\r";
MSS_UART_polled_tx(&g_mss_uart0, message, sizeof(message));
}
}
int main(void)
{
MSS_UART_init
(
&g_mss_uart0,
MSS_UART_57600_BAUD,
MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
);
MSS_UART_set_rx_handler(&g_mss_uart0, uart0_rx_handler, MSS_UART_FIFO_SINGLE_BYTE );
while (1)
{}
return(0);
}
空隙uart0_rx_handler是intterupt處理程序和MSS_UART_get_rx放輸入在g_rx_buff [g_rx_idx]。
我試着用相同的號碼打印號碼MSS_UART_polled_tx函數我用於字符,但沒有運氣。它打印錯誤ASCII值:
if(g_rx_buff[g_rx_idx] > 47 && g_rx_buff[g_rx_idx] < 58)
{
int number = g_rx_buff[g_rx_idx] - '0';
int number2 = number * number;
int number3 = number2 * number;
int number4 = number3 * number;
uint8_t message[15] = "Getallenreeks: ";
uint8_t komma[2] = ", ";
uint8_t end[5] = ".\n\r";
char numberstring2[2];
char numberstring3[3];
char numberstring4[4];
sprintf(numberstring2, "%d", number2);
sprintf(numberstring3, "%d", number3);
sprintf(numberstring4, "%d", number4);
uint8_t messagenumber[1];
uint8_t messagenumber2[1];
uint8_t messagenumber3[1];
uint8_t messagenumber4[1];
messagenumber[0] = '0' + number;
messagenumber2[0] = '0' + number2;
messagenumber3[0] = '0' + number3;
messagenumber4[0] = '0' + number4;
http://imageshack.us/photo/my-images/843/testlan.jpg/(&g_mss_uart0, message, sizeof(message));
MSS_UART_polled_tx(&g_mss_uart0, messagegetal, sizeof(messagenumber));
MSS_UART_polled_tx(&g_mss_uart0, komma, sizeof(komma));
MSS_UART_polled_tx(&g_mss_uart0, messagegetal2, sizeof(messagenumber2));
MSS_UART_polled_tx(&g_mss_uart0, komma, sizeof(komma));
MSS_UART_polled_tx(&g_mss_uart0, messagegetal3, sizeof(messagenumber3));
MSS_UART_polled_tx(&g_mss_uart0, komma, sizeof(komma));
MSS_UART_polled_tx(&g_mss_uart0, messagegetal4, sizeof(messagenumber4));
MSS_UART_polled_tx(&g_mss_uart0, end, sizeof(end));
}
代碼輸出例如:http://imageshack.us/photo/my-images/843/testlan.jpg/ 左終端示出使用的printf功能,右末端示出了用於數字使用MSS_UART_polled_tx功能(在第二碼塊中所示)。
考慮使用「0」和「9」(以及「> =」和「<=」)而不是「47」和「58」。 – pmg 2012-03-21 10:48:10
我沒有看到g_rx_idx在這個片段中增加,而你似乎在消耗一個字節。也許你的循環緩衝區管理代碼卡在某處(也許在這裏)? – wildplasser 2012-03-21 10:50:10
你應該使用'isdigit()'來檢測數字,而不是硬編碼的編碼值。如果你沒有'isdigit()',使用@ pmg的建議。 – unwind 2012-03-21 12:11:08