如何将无符号长整型转换为字符,在C++中显示在LCD上 [英] How to Convert unsigned long int to char to show on LCD in C

查看:11
本文介绍了如何将无符号长整型转换为字符,在C++中显示在LCD上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个变量应该转换为字符才能在LCD上显示,我的问题是当我用sprintf将这个整数转换为字符时,它显示了错误的数字,每个超过4个长度的数字都显示错误,它只正确地显示长度低于4的数字。

我的微控制器是ATmega16aIDE是CodeVisionAVR,语言是C

unsigned long int username;
char show[20];

unsigned long int ScanKey(void)
{
    unsigned long int num = 0;
    _lcd_write_data(0x0F);
    PORTC .2 = 1;
    while (1)
    {
        PORTD .0 = 0;
        PORTD .1 = 1;
        PORTD .2 = 1;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0;
            lcd_putchar('1');
            num = (num * 10) + 1;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0);
            lcd_putchar('4');
            num = (num * 10) + 4;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0);
            lcd_putchar('7');
            num = (num * 10) + 7;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0);
            _lcd_write_data(0x10);
            lcd_putchar(' ');
            _lcd_write_data(0x10);
            num /= 10;
        }

        PORTD .0 = 1;
        PORTD .1 = 0;
        PORTD .2 = 1;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0);
            lcd_putchar('2');
            num = (num * 10) + 2;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0);
            lcd_putchar('5');
            num = (num * 10) + 5;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0)
                ;
            lcd_putchar('8');
            num = (num * 10) + 8;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0);
            lcd_putchar('0');
            num = (num * 10) + 0;
        }

        PORTD .0 = 1;
        PORTD .1 = 1;
        PORTD .2 = 0;
        delay_ms(5);
        if (PIND .3 == 0)
        {
            while (PIND .3 == 0);
            lcd_putchar('3');
            num = (num * 10) + 3;
        }
        else if (PIND .4 == 0)
        {
            while (PIND .4 == 0);
            lcd_putchar('6');
            num = (num * 10) + 6;
        }
        else if (PIND .5 == 0)
        {
            while (PIND .5 == 0);
            lcd_putchar('9');
            num = (num * 10) + 9;
        }
        else if (PIND .6 == 0)
        {
            while (PIND .6 == 0);
            break;
        }
    }
    PORTC .2 = 0;
    _lcd_write_data(0x0C);

    return num;
}

lcd_clear();
lcd_putsf("Enter Username:");
lcd_gotoxy(0, 1);
sprintf(show, "%lu", username);
lcd_puts(show);

这不是我的全部代码,这是我有问题的部分。

我输入此56321

输出如下

https://files.fm/f/qpwrmfs6h这是视频。

和我尝试的%ul和<[2-2],%ul都有相同的问题 和%lu输出如下所示

我的英语不是很好,很难添加细节

推荐答案

  1. 请勿在8位环境中使用printfscanf函数。它们是贪婪的资源,不太适合这种微型计算机。
  2. printfscanf实现受到限制,不支持许多格式。
  3. 在8位环境中,32位数字运算非常昂贵(尤其是除法)-尽可能使用16位
#define MASK32ULONG 1000000000UL
#define MASK16UINT  10000

char *toDecimal(char *buff, unsigned long val)
{
    unsigned long mask = MASK32ULONG;
    unsigned long remain;
    char *wrk = buff;

    do
    {
        int digit = val / mask;

        if(digit || !val) *wrk++ = '0' + digit;
        val %= mask;
        mask /= 10;
    }while(val);
    *wrk = 0;
    return buff;
}

这篇关于如何将无符号长整型转换为字符,在C++中显示在LCD上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆