我的程序通过7个不同的通道读取ADC值。我有一个定时器设置,按顺序读取每个通道。我想无限地运行这个计时器(但现在我将它设置为一小时)并且能够通过按键(例如q)退出计时器。我必须同时插入一个if和while循环来查找getchar()== q,但是每个循环都会暂停程序直到我实际按下q然后它会读取下一行并重复。如果有人能帮助我在按键上退出计时器,我将不胜感激。感谢
static void ch0
{
send_receive_LTC1859 (0x00, 0x00, 0, spifd);
}
//Here CH 1-6 go (saving space for readers)
static void ch7
{
send_receive_LTC1859 (0xF0, 0x08, 0, spifd);
printf("\n");
//Look for quit key
}
uint32 accel_testing (){
spifd = fopen("spi2:", 0);
printf("Accel names\n");
MQX_TICK_STRUCT ticks;
MQX_TICK_STRUCT dticks;
_timer_id read_ch0;
_timer_id read_ch2;
_timer_id read_ch3;
_timer_id read_ch4;
_timer_id read_ch5;
_timer_id read_ch6;
_timer_id read_ch7;
_timer_create_component(TIMER_TASK_PRIORITY, TIMER_STACK_SIZE);
_time_init_ticks(&dticks, 0);
_time_add_msec_to_ticks(&dticks,500);
_time_get_elapsed_ticks(&ticks);
read_ch0 = _timer_start_periodic_at_ticks(ch0, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch2 = _timer_start_periodic_at_ticks(ch2, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch3 = _timer_start_periodic_at_ticks(ch3, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch4 = _timer_start_periodic_at_ticks(ch4, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch5 = _timer_start_periodic_at_ticks(ch5, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch6 = _timer_start_periodic_at_ticks(ch6, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
read_ch7 = _timer_start_periodic_at_ticks(ch7, 0,
TIMER_ELAPSED_TIME_MODE, &ticks, &dticks);
_time_add_msec_to_ticks(&ticks, 5);
_time_delay(3600000); // wait 1 hour
printf("\nThe task is finished!\n");
_timer_cancel(read_ch0);
_timer_cancel(read_ch2);
_timer_cancel(read_ch3);
_timer_cancel(read_ch4);
_timer_cancel(read_ch5);
_timer_cancel(read_ch6);
_timer_cancel(read_ch7);
return 0;
}
答案 0 :(得分:0)
如果你正在使用linux,那么select()
会有所帮助。
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
int
input_timeout (int filedes, unsigned int seconds)
{
fd_set set;
struct timeval timeout;
/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_SET (filedes, &set);
/* Initialize the timeout data structure. */
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
/* select returns 0 if timeout, 1 if input available, -1 if error. */
return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
&set, NULL, NULL,
&timeout));
}
int
main (void)
{
fprintf (stdin, "select returned %d.\n",
input_timeout (STDIN_FILENO, 5));
return 0;
}