0

私はPIC18、LCDなどを使用してクロックを実装するプロジェクトに取り組んでおり、mikroCを使用してこのプロジェクトを実装しています。

しかし、私はCが苦手で、ある点で行き詰まっています。時計には、ユーザーが時間、アラーム、アラーム音などを設定できるようにするいくつかのオプションを備えたメニューがあります。メニューには次のものがあります。

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

時計には 、OKRIGHT、 の 3 つの押しボタンがありLEFTます。クロックの電源がオンになるとSet Time、デフォルトとして LCD に表示されます。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   ");

これは 1 つの動きであり、PIC の RAM が限られている間、他の動きを行うには大きなコードが必要です。それで、この問題を解決するのを手伝ってくれませんか?

4

3 に答える 3

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);
    }
}

それはテストされていないので(私の頭の中を除いて、通常はかなり良いです)、それを出発点と見なす必要があります。

于 2011-06-02T16:28:55.100 に答える
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);
}

非常に小さなデバイスでは、不必要なメモリの移動を避けることが重要になる場合があります

于 2011-06-02T18:17:44.157 に答える
2

ほとんどの LCD 自体がスクロールをサポートしています。したがって、LCD 用の C ライブラリは、データをスクロールするための関数を提供します。私は PIC18 C ライブラリを使用しており、2 つの機能を提供しています。

void lcd_scroll_left(char n) LCD 画面を n 位置左にスクロールします。

void lcd_scroll_right(char n) LCD 画面を右に n 位置スクロールします。

使用しなければならない関数名を見つけるために、使用しているライブラリのドキュメントを参照できます。

LCD用Microchip PIC18 Cライブラリ

于 2011-06-02T21:53:11.123 に答える