2011-06-02 50 views
0

我正在使用PIC18,LCD等實現一個時鐘的項目,我正在使用mikroC來實現此項目。你能幫我做這個算法「將字符串向右或向左移動」

但是,我不擅長C,而且我被困在某個點。時鐘有幾個選項的菜單,使用戶設定的時間,報警,報警聲等菜單有以下幾點:

1. Set Time 
2. Add Alarm 
3. Select Alarm 
4. Add New Tone 
5. Select Tone 
6. EXIT 

時鐘有3個按鍵,OKRIGHTLEFT。當時鍾開機時,它將在LCD上顯示Set Time作爲默認值。我想添加一個功能,當我按RIGHT按鈕時,它應該顯示Add Alarm,但逐漸不是直接。我在菜單中有6個項目,所以我必須逐步移動12次(右側6個,左側6個)。我曾經嘗試這樣做如下:

Lcd_Out(2, 2, " set Time "); 
Delay_ms(50); 
Lcd_Out(2, 2, " set Time "); 
Delay_ms(50); 
Lcd_Out(2, 2, " set Time  "); 
Delay_ms(50); 
Lcd_Out(2, 2, " et Time  "); 
Delay_ms(50); 
Lcd_Out(2, 2, " t Time  "); 
Delay_ms(50); 
Lcd_Out(2, 2, " Time   "); 
Delay_ms(50); 
Lcd_Out(2, 2, " ime   "); 
Delay_ms(50); 
Lcd_Out(2, 2, " me   "); 
Delay_ms(50); 
Lcd_Out(2, 2, " e   "); 
Delay_ms(50); 
Lcd_Out(2, 2, "    "); 
Delay_ms(50); 
Lcd_Out(2, 2, "    "); 
Delay_ms(50); 
Lcd_Out(2, 2, "    "); 
Delay_ms(50); 
Lcd_Out(2, 2, "    "); 
Delay_ms(50); 
Lcd_Out(2, 2, "   A "); 
Delay_ms(50); 
Lcd_Out(2, 2, "   Ad "); 
Delay_ms(50); 
Lcd_Out(2, 2, "   Add "); 
Delay_ms(50); 
Lcd_Out(2, 2, "   Add "); 
Delay_ms(50); 
Lcd_Out(2, 2, "  Add A "); 
Delay_ms(50); 
Lcd_Out(2, 2, "  Add Al "); 
Delay_ms(50); 
Lcd_Out(2, 2, "  Add Ala "); 
Delay_ms(50); 
Lcd_Out(2, 2, "  Add Alar "); 
Delay_ms(50); 
Lcd_Out(2, 2, " Add Alarm "); 
Delay_ms(50); 
Lcd_Out(2, 2, " Add Alarm "); 
Delay_ms(50); 
Lcd_Out(2, 2, " Add Alarm "); 

這是一個運動,做我需要大量的代碼,而PIC的RAM是有限的人。那麼,你們能幫我解決這個問題嗎?

+8

第一件事第一:**你需要一個循環**。這立即解決了您現在擁有的冗餘代碼。 – 2011-06-02 16:23:10

+0

@Cody Gray:問題是,如何將字符串向右或向左移動? – 2011-06-02 16:29:00

+0

我該如何開始這個問題的賞金? – 2011-06-02 18:03:11

回答

3

這裏是避免所有memcpy,str*cpy狂歡的變體。

#define STRSZ 14 

char str[STRZ*2+1] = " set Time  Add Alarm "; /* The buffer must be writable */ 

for (i = 0; i <= STR_SZ; i++) {   // Loop 
    char save_ch = str[i + STRZ];   // Save the character at the end 
    str[i + STRZ] = 0;      // Terminate the string 
    Lcd_Out (2, 2, str + i);     
    str[i + STRZ] = save_ch;     // Restore buffer 
    Delay_ms (50); 
} 

編輯:右移

for (i = STR_SZ; i >= 0; i--) {   // Loop 
    char save_ch = str[i + STRZ];   // Save the character at the end 
    str[i + STRZ] = 0;      // Terminate the string 
    Lcd_Out (2, 2, str + i);     
    str[i + STRZ] = save_ch;     // Restore buffer 
    Delay_ms (50); 
} 

在非常小的設備,以避免不必要的內存移動可能是至關重要的

+0

它的工作原理!真的非常感謝......你能寫出另一部分轉移到正確的地方嗎? – 2011-06-02 18:32:33

6

你需要的東西是這樣的:

#define STRSZ 14 
//   <------------><------------> 
char *str = " set Time  Add Alarm "; // Two 14-char strings. 
char disp[STRSZ+1];       // Buffer for holding display string. 
for (i = 0; i <= STR_SZ; i++) {    // Starting character to use. 
    memcpy (disp, &(str[i]), STR_SZ);   // Copy the relevant bit. 
    disp[STR_SZ] = '\0';      // And null-terminate. 
    Lcd_Out (2, 2, disp);      // Display it then wait. 
    Delay_ms (50); 
} 

對於轉移的其他方式,只需使用:

for (i = STR_SZ; i >= 0; i--) { 
    // blah blah blah 
} 

如果你正在尋找一個更完整的示例,請嘗試:

#define STR_SZ 14 
// PreCond: from and to MUST be 14-character strings. EXACTLY! 
// Pass in from and to strings and 1 to go left, 0 to go right. 

void transition (char *from, char *to, int goLeft) { 
    // Space for transition and display strings. 
    char str[STR_SZ * 2 + 1]; 
    char disp[STR_SZ + 1]; 

    // Transition variables. 
    int pos, start, end, incr; 

    // Check preconditions. 
    if (strlen (from) != STR_SZ) return; 
    if (strlen (to) != STR_SZ) return; 

    // Different values for each direction. 
    if (goLeft) { 
     start = 0; end = STR_SZ + 1; incr = 1; 
     strcpy (str, from); strcat (str, to); 
    } else { 
     start = STR_SZ; end = -1; incr = -1; 
     strcpy (str, to); strcat (str, from); 
    } 

    // Do the transitions. 
    for (pos = start; pos != end; pos += incr) { 
     // Copy string portion to display then delay. 
     memcpy (disp, &(str[i]), STR_SZ); 
     disp[STR_SZ] = '\0'; 
     Lcd_Out (2, 2, disp); 
     Delay_ms (50); 
    } 
} 

這是未經測試(除了在我的頭上,這是通常漂亮g ood),所以你應該認爲它是一個起點。

+0

我試過這個,但不好。 memcpy究竟做了什麼? – 2011-06-02 16:45:40

+0

它複製內存。那個特定的人會從'str'的​​'i'位置開始複製14個字符,'i'從0到14(左移)或14到0(右移)。它將其複製到「disp」,以便將其發送到顯示器。 – paxdiablo 2011-06-02 16:47:23

+0

它只是在一段時間後用'to'替換'from'而沒有任何移位:\ – 2011-06-02 17:39:04

2

大多數LCD本身支持滾動。所以LCD的C庫提供滾動數據的功能。我已經使用了PIC18 C庫,它提供了兩個功能

void lcd_scroll_left(char n)將LCD屏幕向左滾動n個位置。

void lcd_scroll_right(char n)在右側n個位置滾動LCD屏幕。

您可以查閱您正在使用的庫的文檔,以查找您要使用的函數名稱。

Microchip PIC18 C library for LCDs

相關問題