本帖最后由 ddllxxrr 于 2015-4-7 20:58 编辑
这个RTT比昨天的RTC简单点。其原理也是先设RTC中断,然后,在中断里更新显示。
用户可以设一个数字,当程序运行到这个数字时,就显示一行报警的字样
以下是调有的显示函数
static void refresh_display(void)
{
printf("%c[2J\r", 27);
printf("Time: %u\n\r", (unsigned int)rtt_read_timer_value(RTT));
/* Display alarm */
if (g_uc_alarmed) {
puts("!!! ALARM !!!\r");
}
/* Main menu */
if (g_uc_state == STATE_MAIN_MENU) {
puts("Menu:\n\r"
" r - Reset timer\n\r"
" s - Set alarm\r");
if (g_uc_alarmed) {
puts(" c - Clear alarm notification\r");
}
puts("\n\rChoice? ");
} else {
if (g_uc_state == STATE_SET_ALARM) {
puts("Enter alarm time: ");
if (g_ul_new_alarm != 0) {
printf("%u", (unsigned)g_ul_new_alarm);
}
}
}
}
以下是中断函数
void RTT_Handler(void)
{
uint32_t ul_status;
/* Get RTT status */
ul_status = rtt_get_status(RTT);
/* Time has changed, refresh display */
if ((ul_status & RTT_SR_RTTINC) == RTT_SR_RTTINC) {
refresh_display();
}
/* Alarm */
if ((ul_status & RTT_SR_ALMS) == RTT_SR_ALMS) {
g_uc_alarmed = 1;
refresh_display();
}
}
而主函数就是检测按键的输入:
/* User input loop */
while (1) {
/* Wait for user input */
scanf("%c", (char *)&c);
/* Main menu mode */
if (g_uc_state == STATE_MAIN_MENU) {
/* Reset timer */
if (c == 'r') {
configure_rtt();
refresh_display();
} else if (c == 's') { /* Set alarm */
g_uc_state = STATE_SET_ALARM;
g_ul_new_alarm = 0;
refresh_display();
} else { /* Clear alarm */
if ((c == 'c') && g_uc_alarmed) {
g_uc_alarmed = 0;
refresh_display();
}
}
} else if (g_uc_state == STATE_SET_ALARM) { /* Set alarm mode */
/* Number */
if ((c >= '0') && (c <= '9')) {
g_ul_new_alarm = g_ul_new_alarm * 10 + c - '0';
refresh_display();
} else if (c == ASCII_BS) {
printf("%c", c);
g_ul_new_alarm /= 10;
refresh_display();
} else if (c == ASCII_CR) {
/* Avoid newAlarm = 0 case */
if (g_ul_new_alarm != 0) {
rtt_write_alarm_time(RTT, g_ul_new_alarm);
}
g_uc_state = STATE_MAIN_MENU;
refresh_display();
}
}
以下是运行截图:
|