2016-12-15 5 views
-1

これまではこのコードを書いていましたが、アレイ(スキャナー)を読み込むのではなく、porthのスイッチを読んでいると言われました。アレイを読み込んでCのルックアップテーブルを使って結果を表示する

最初の問題はこれです:original problem

コード:

#include <stdio.h> 
int main(void) { 
    /******************* Declare the port addresses **********************/ 
    unsigned char *DDRA = (unsigned char *)0x0002; 
    unsigned char *PORTB = (unsigned char *)0x0001; 
    unsigned char *DDRB = (unsigned char *)0x0003; 
    unsigned char *PTH = (unsigned char *)0x0260; 
    unsigned char *DDRH = (unsigned char *)0x0262; 
    unsigned char *PERH = (unsigned char *)0x0264; 

    /******************* Declare functions*******************************/ 
    unsigned char threshold; 
    unsigned char read; 
    unsigned char index; 
    /************************** Scanner Data ******************************/ 

    unsigned char scanner[255] = { 50, 
            4, 9, 14, 18, 23, 26, 29, 30, 31, 32, 
            34, 37, 41, 47, 54, 63, 71, 80, 87, 92, 
            94, 94, 92, 89, 85, 83, 83, 86, 93, 102, 
            115, 128, 141, 153, 161, 164, 164, 160, 152, 144, 
            137, 132, 132, 136, 146, 161, 178, 196, 213, 226 
            }; 

    /******************* Set up I/O ports ********************************/ 
    *DDRH = 0x00;     /* make Port H an input port */ 
    *PERH = 0xFF;     /* enable Port H */ 
    *DDRA = 0xFF;     /* make Port A an output port */ 
    *DDRB = 0xFF;     /* make Port B an output port */ 

    /******************* Main loop ***************************************/ 

    *PORTB = 255; // This clears the display on PORTB // 
    threshold = *PTH; // Make PORTH equal to threshold // 
    read = scanner[0]; 
    index = 1; 

    do { 
     *PORTB = pattern(scanner[index]/32); 
     wait (1); 
    } while ((index<=scanner[0]) && (read<threshold)); 

    if (index < threshold) { 
     printf("threshold reached at reading %d with a current value %d.", index, scanner[index]); 
    } else { 
     printf("Threshold not reached after %d readings.", scanner[255]); 
    } 
    return 0 ; 
} 
/******************* Pattern Function **********************************/ 

void pattern(char threshold) { 
    unsigned char LedTable [8] = {0b00000001, 
            0b00000011, 
            0b00000111, 
            0b00001111, 
            0b00011111, 
            0b00111111, 
            0b01111111, 
            0b11111111 
           }; 
    unsigned char index; 
    unsigned char display; 
    unsigned char sw_on; 
    unsigned char sw_off; 

    index = threshold; 
    sw_on = ((unsigned char)LedTable[index]); 
    sw_off = threshold & 0b11111111; 

    if (sw_off & 0b00010000) { 
     display = sw_on | 0b10000000 ; 
    } else { 
     display = sw_on & 0b01111111; 
    } 
} 

/********************** Wait Function **********************************/ 

void wait(int seconds) { 
    unsigned int i; 
    unsigned int j; 
    unsigned int k; 

    for (k = 0; k <= seconds; k++) { 
     for (i = 0; i <= 50; i++) { 
      for (j = 0; j <= 2000; j++); 
     } 
    } 
} 

すべてのヘルプははるかに高く評価されるだろう。私はこれをシミュレータで実行しようとしていますが、コードを正しく実行するためにコードをs19ファイルに変換する正しいソフトウェアを持っていないので、現在私が次の週に働いているのでダウンロードできません。しかし、私はすべてのエラーをクリアするためにQTを使用しました。

+0

'index = 1;' do {...}(index <= scanner [0] ..); 'は常に' index'をインクリメントしないのでtrueをテストします。 (他の問題もあるかもしれませんが、それはショーストッパーです) –

+0

ok do whileループで32分周を保持しながら、これをやりなおすにはどうすればいいですか? – Owenb11

+0

あなたはあなたの課題を達成できましたか? – CristiFati

答えて

0

質問が明確ではありませんでしたが、私はそれを持っていると思う...

ノート

  • 私はscannerはダミーデータが含まれており、デバイスを模擬するために使用されていると仮定。
  • 私はあなたのコード内の特定の物事が動作しません、VStudio 10(64 上のWin10)を使用しています:バイナリフォーマットされた数値(例えば0b00000001

    • ので、私が持っていましたそれらを(16進で)変換する。
    • いくつかの割り当て(たとえば、unsigned char *DDRA = (unsigned char *)0x0002;は2番目のメモリバイトを逆参照しようとしていますが、これは許可されていません)。 void pattern(char threshold)patternは何も返さないながら、*PORTB = pattern(scanner[index]/32);

      1. 割り当て:
  • はしかし、私はまた、何のコンパイラが好きなんだろうといくつかの問題を発見しました。

  • 参照patternおよびwaitmainが宣言されていない場合。

    • コードはコンパイルすらない:

    だから、私は2つの質問がありますか?

  • どのコンパイラを使用していますか?

私の環境(特に2進数の問題)をコンパイルするためにコードを修正しなければならないと言いましたが、そのトリックを行うコードスニペット全体を投稿するつもりはありませんが、既存のコード(質問のコードスニペットのため、あなたの質問にあるものであなたのマシン/デバイス上のコードを更新する必要があります)の何を置き換える。

  • 問題の場合1。(私は上記のこと):関数はunsigned charを返すべきである:

    • 関数の定義:void pattern(char threshold)unsigned char pattern(char threshold)に変更されなければなりません。
    • 何かを返す必要があります(私はそれがdisplayと仮定します)ので、関数の中括弧(})の前にreturn display;を追加してください。
  • 問題について

    、次のいずれか

    フォワード
    • 宣言(下記のコードの2行を追加)waitpatternmainの宣言(int main(void) {)前:

      void wait(int seconds);
      unsigned char pattern(char threshold);

    • mainの前に、waitpattern機能体を移動します。

  • 第三私は質問が第四LedTable要素が先頭に0bマーカーを行方不明になったということでした編集されたときに、私は固定の問題(マイナー、私はタイプミスを想定)。 (あなたが言ったように - またはより良い:あなたが言われた通り)

は今、論理的な問題(およびいくつかの他は)do/whileループに依存しています。で

do { 
    *PORTB = pattern(scanner[index]/32); 
    wait (1); 
} while ((index<=scanner[0]) && (read<threshold)); 

while (((index <= read) && (scanner[index] < threshold))) { 
    *PORTB = pattern(scanner[index]/32); 
    //printf("pattern(scanner[%d]/32): %02X - %d\n", index, pattern(scanner[index]/32), scanner[index]); 
    wait(1); 
    index++; 
}; 

説明:だから、私はそのループを交換することをお勧めdo/whilewhileにループを変更することにより、

  • 、私が生じる可能性があるいくつかのコーナーケースを回避しています配列の最初または最後の要素のいずれかを含む。
  • index++;これは@ DavidC.Rankinが発見したエラーです:indexをインクリメントしないと、ループに入る前に同じ値(ループに入る前に割り当てられた値)になり、ループは終了しません。
  • ループ条件(私は元のコードの右から左へとそれらの上に行くつもりです):
    • readthresholdを比較または配列インデックスは意味がありません。
    • と比較するとからreadの代わりにscanner[0]-これは任意です 2は同じ値です。より効率的なのでこのフォームを選択しました。

最後の変化である:それは配列インデックスを比較しない点をしないように(すなわち、その配列中にあるかもしれない)値(whileループを出た後)if (index < read) {によってif (index < threshold) {を交換 - 同じことを、上記。

関連する問題