2013-12-16 30 views
-1

我試圖將我的十六進制輸出左移一位,以便我可以在7段LCD上顯示9位以上的數字。嘗試移位十六進制值,請幫助

在C上編程,我使用的軟件是NIOS II,這樣我可以直接在DE0板上重新編程。

這個項目的目的是每按一次'button1'就增加一個LCD的值。我成功地完成了這個任務,但是當然在9之後,它需要向左移動並從1重新開始,取代它來自的位置。我已經做了一些研究,但沒有運氣。任何幫助表示讚賞。謝謝。

代碼如下:

#include "sys/alt_stdio.h" //for the alt_putstr function below. Outputs to Eclipse console 
#include "altera_avalon_pio_regs.h" //for the I/O functions in the while loop below 
#include "sys/alt_timestamp.h" //see Nios II Software Developer’s Handbook, Timestamp Driver 
#include "system.h" 

#define setHeaderOuts HEADEROUTPUTS_BASE+0x10 //HEADEROUTPUTS_BASE is defined in system.h of the _bsp file. It refers to the base address in the Qsys design 
               //the hex offset (in this case 0x10, which is 16 in decimal) gives the number of bytes of offset 
               //each register is 32 bits, or 4 bytes 
               //so to shift to register 4, which is the outset register, we need 4 * (4 bytes) = 16 bytes 
#define clearHeaderOuts HEADEROUTPUTS_BASE+0x14 //to shift to register 5 (the 'outclear' register) we need to shift by 5 * (4 bytes) = 20 bytes, (=0x14 bytes) 
               // offset of 5 corresponds to the 'outclear' register of the PIO. 


int main(void) 
{ 
    alt_putstr("This is the ELEE1062 version of the NIOS processor"); 
    int buttons = 0; //the buttons on the DE0 
    //int switches = 0; //the switches on the DE0 
    int count = 0; //general purpose counter 
    int hexd = 0; 

    while(1) 
    { 
     buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons 

     while((buttons & 0x01) == 1) // i.e. while pushbutton 1 is not pressed 
     { 
      buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons 
     } 

     count=count+1; 

     IOWR_ALTERA_AVALON_PIO_DATA(DE0_LEDS_BASE,count); //display the value of count in binary, using the green LEDs 

     while((buttons & 0x01) == 0) //i.e. while pushbutton 1 is pressed 
     { 
      buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons 

     } 

     if (count==0) 
     { 
      hexd=0x000000c0; 
     } 

     else if (count==1) 
     { 
      hexd=0xf9; 
     } 

     else if (count==2) 
     { 
      hexd=0xa4; 
     } 

     else if (count==3) 
     { 
      hexd=0xb0; 
     } 

     else if (count==4) 
     { 
      hexd=0x99; 
     } 

     else if (count==5) 
     { 
      hexd=0x92; 
     } 

     else if (count==6) 
     { 
      hexd=0x82; 
     } 

     else if (count==7) 
     { 
      hexd=0xd8; 
     } 

     else if (count==8) 
     { 
      hexd=0x80; 
     } 

     else if (count==9) 
     { 
      hexd=0x90; 
     } 

     else if (count>9) 
     { 
      hexd= hexd & ~(1<<count); 
     } 


     //count=alt_timestamp_start(); //start the timer. Timer increments each clock cycle. Clock for ELEE1062_NIOS is 50MHz 
     //buttons=IORD_ALTERA_AVALON_PIO_DATA(PUSHBUTTONS1_2_BASE); //read the value of the pushbuttons 
     //switches=IORD_ALTERA_AVALON_PIO_DATA(DE0SWITCHES_BASE); //read the value of the switches 
     IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,hexd); //DE0 7 segment displays all off --notice that a logic '1' turns the segment off 
     IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,hexd); //DE0 7 segment displays all on 
     IOWR_ALTERA_AVALON_PIO_DATA(DE0_LEDS_BASE,0x000); //all off --for the green LEDs, a logic '0' turns the LED off 
     IOWR_ALTERA_AVALON_PIO_DATA(DE0_LEDS_BASE,0xfff); //all on 
     IOWR_ALTERA_AVALON_PIO_DATA(clearHeaderOuts,0x01); //turn off the first pin of the output port 
     IOWR_ALTERA_AVALON_PIO_DATA(setHeaderOuts,0x01); //turn on the first pin of the output port 
     //IOWR_ALTERA_AVALON_PIO_DATA(SSEG_BASE,switches); //light up the 7 segment display segments corresponding to how the DE0 switches are set 
     IOWR_ALTERA_AVALON_PIO_DATA(DE0_LEDS_BASE,buttons); //light up the green LEDs corresponding to which DE0 buttons are pushed 
     //count=alt_timestamp(); //record the value of the timer, and store in the 'count' variable 


    } 
} 
+0

你能清楚你遇到麻煩你的計劃的一部分,(我認爲這是表達'hexd = hexd&〜(1 <<計數)',但我不知道)?當你處於這個狀態時,更詳細地解釋發生了什麼問題,比如你期望發生什麼以及你看到什麼。我想這裏的大多數人不知道你操縱的LED設備的細節。 –

+0

感謝您的回覆,我需要的程序是每次按'button1'時將值遞增1,一旦它在LCD上傳遞值9,它需要顯示10,一直到9999。我遇到的問題是我無法通過9.理想情況下,我想創建一個循環來做到這一點,但它很棘手。 – NoobProgrammer

+0

使用hexd = hexd&〜(1 << count)來嘗試並將數字向左移動,但當然不起作用。 – NoobProgrammer

回答

0

以下函數將返回一個由4個字節組成的u32值,並顯示數字顯示的LED值。請注意,我不知道您的顯示器需要輸入數字的順序,所以您可能需要以字節形式切換返回值或其他內容。此外,該功能將爲LED顯示屏提供前導零 - 您可能需要修改顯示屏上空白處顯示的內容。

typedef unsigned int u32; 

static char led_digits[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xd8, 0x80, 0x90 }; 

u32 four_digits(int x) 
{ 
    unsigned char c[4]; 
    int i; 

    for (i = 0; i < 4; ++i) 
    { 
     int digit = x % 10; 
     x = x/10; 
     c[i] = led_digits[digit]; 
    } 

    return (u32)(c[3] << 24) | (u32)(c[2] << 16) | (u32)(c[1] << 8) | (u32)c[0]; 
} 
+0

你是男人! – NoobProgrammer

+0

爲明顯的作業問題提供完整的代碼有什麼意義? – Andrey

+0

此外,您根本不需要數組,只需將循環中的數字左移即可。 – Andrey

1

僅僅移位將不工作。 9 -> 10(你可以稱之爲轉移)但19 -> 20呢?由於它顯然是作業或其他形式的學習,所以我不會爲你編寫代碼。你的最終目標是代表7段LED顯示屏上的數字。從中思考。所以作爲輸入你有二進制數(count)和輸出應該是引腳信號。你的任務是將一個轉換爲另一個。 Led基本上使用十進制小數運算,因此您首先需要將二進制轉換爲十進制數字序列,然後將它們轉換爲引腳信號(您已有此代碼)。要使用全部四位數字,您需要將您的號碼轉換爲格式0x11223344,其中number表示led位置。 0xF9A4B099是1234(如果我沒有弄錯)。

+0

我一直在研究什麼是十進制數字,我沒有運氣。就0xF9A4B099而言,它代表1234,但是我試圖解決的問題是,如何使用循環讓程序增加1,而不必以十六進制格式輸出每個數字? – NoobProgrammer

+0

@ user2192435你正試圖解決錯誤的問題,你只需按原樣增加你的號碼,然後你需要將二進制數字轉換爲十進制數字(你應該知道什麼是十進制數字,這是人類用來寫數字的)。 – Andrey