【问题标题】:Trying To Shift Hex Values, Help Please尝试转换十六进制值,请帮助
【发布时间】:2013-12-16 16:55:23
【问题描述】:

我正在尝试将我的十六进制输出向左移动一位,以便我可以在 7 段液晶显示器上显示 9 以上的数字。

在 C 上编程,我使用的软件是 NIOS II,所以我可以直接在 DE0 板上重新编程。

该项目的目的是在每次按下“button1”时将 LCD 的值增加一。我已经成功地做到了这一点,但是当然在 9 点之后它需要向左移动并从 1 重新开始,用 0 替换它来自的位置。我做了一些研究,但没有任何运气所以任何帮助表示赞赏。谢谢。

代码如下:

#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


    }
}

【问题讨论】:

  • 你能说清楚你程序的哪一部分有问题吗(我认为是hexd= hexd &amp; ~(1&lt;&lt;count),但我不确定)?当你在做的时候,更详细地解释出了什么问题,比如你期望发生什么以及你所看到的。我想这里的大多数人都不知道您正在操作的 LED 设备的详细信息。
  • 感谢您的回复,我需要程序做的是每次按下“button1”时将值增加 1,一旦它在 LCD 上通过值 9,它需要显示 10,一直到 9999。我遇到的问题是我无法超过 9。理想情况下,我想创建一个循环来执行此操作,但它非常棘手。
  • 使用了 hexd= hexd & ~(1
  • 我想知道在研究什么。怎么转?您正在尝试解决一个与您最初的目标无关的目标。

标签: c binary hex shift nios


【解决方案1】:

仅仅移动是行不通的。 9 -&gt; 10(你可以称之为转变)但是19 -&gt; 20呢?由于显然是作业或其他形式的学习,我不会为您编写代码。您的最终目标是在 7 段 LED 显示屏上表示数字。从中思考。因此,作为输入,您有二进制数(count),输出应该是 LED 引脚信号。你的任务是将一个转换成另一个。 Led 基本上使用十进制基数运行,因此您首先需要将二进制转换为一系列十进制数字,然后将它们转换为引脚信号(您已经拥有此代码)。要使用所有四位数字,您需要将您的数字转换为格式 0x11223344,其中数字表示 LED 位置。 0xF9A4B099 是 1234(如果我没记错的话)。

【讨论】:

  • 我一直在研究什么是十进制数字,我没有运气。就 0xF9A4B099 而言,它确实代表 1234,但是我要解决的问题是,如何使用循环使程序增加 1,而不必以十六进制格式输入每个数字?
  • @user2192435 您正在尝试解决错误的问题,您只需按原样递增您的数字,然后您需要将二进制数转换为十进制数字(你应该知道什么是十进制数字,它是什么人类用来写数字)。
【解决方案2】:

以下函数将返回一个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];
}

【讨论】:

  • 为明显的作业问题提供完整代码的意义何在?
  • 另外,您根本不需要数组,只需将循环中的数字左移即可。
  • @Andrey 和 Jongware:我并没有想到这是一个家庭作业问题。我并不总是费心去想一个问题背后的潜台词——有时我只是发布一个答案。另外,请记住,即使我已经意识到作业的角度,关于如何处理作业问题也有很多意见:meta.stackexchange.com/questions/10811/… 最后,我不确定我是否会将这个答案称为完整的解决方案作业,只是问题的一个方面。
  • @MichaelBurr 我非常了解这个元问题,当然这取决于你为这个问题选择什么方法。我只是表达了我的观点,为了教育目的,给出直接的答案是完全没有意义的。给男人一根钓鱼竿和所有这些东西。也许只有我一个人,但我能以 90% 的命中率发现家庭作业问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-05
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-07-26
  • 2011-08-04
相关资源
最近更新 更多