c - lpc2148 RTC打印问题

标签 c

在此处输入代码

void DateTimeConversion (void)
{
  unsigned char TempDay,TempMonth,TempYear,i;
  unsigned char TempHour,TempMinute,TempSecond;
  TempDay=0;TempMonth=0;TempYear=0;TempHour=0;TempHour=0;TempSecond=0;
  col=1;
  for(i=0;i<10;i++)
  {
    sprintf(MyStr,"%c",(unsigned int)StoreUserID[i]);
    ClcdGoto(col,2);ClcdPutS_P(MyStr);
    col++;
  }

  TempDay=((unsigned int)(StoreUserID[6] * 10) + (unsigned int)(StoreUserID[7] * 1));
  TempMonth=((unsigned int)(StoreUserID[8] * 10) + (unsigned int)(StoreUserID[9] * 1));
  TempYear=((unsigned int)(StoreUserID[4] * 10) + (unsigned int)(StoreUserID[5] * 1));
  TempHour   = ((unsigned int)(StoreUserID[0] *10) + (unsigned int)(StoreUserID[1] * 1));
  TempMinute = ((unsigned int)(StoreUserID[2] *10) + (unsigned int)(StoreUserID[3] * 1));
  TempSecond = ((unsigned int)(StoreUserID[4] *10) + (unsigned int)(StoreUserID[5] * 1));
}

我使用 LPC 2148 作为 RTC。
描述:

  1. 我使用单键读取同一列上的多个值(0-9)(使用 2 行 LCD 显示屏)。

  2. 读取的值存储在StoreUserId数组中(因为col++数组也增加了)

  3. 调用上述函数来保存 SEC、MINUTE、HOUR 的值。

  4. 打印 StoreUserid 以交叉检查输入的值是否正确。

  5. 但是转换后(检查乘以 *10)TempSecond、TempMinute、TempHour 在转换后显示随机值,但没有得到问题在哪里?

最佳答案

您的问题几乎肯定在这里:

{sprintf(MyStr,"%c",(unsigned int)StoreUserID[i]);

我无法知道您如何声明 StoreUserID 数组(作为 char?作为 int?)以及为什么您始终如一转换为unsigned int(对于这样的小正数,它没有意义),但有以下两个最可能的更正:

{sprintf(MyStr,"%c", StoreUserID[i]);      // for character representation in StoreUserID[]

或者 - 我认为这就是你的情况 -

{sprintf(MyStr,"%c",StoreUserID[i] + '0'); // for numerical one - the conversion is needed

说明:

数字0具有字符表示'0',它是某个数字(在ASCII中是48)。
数字 1 具有字符表示 '1',它是下一个数字(在 ASCII 中为 49)。
...等等。
因此,您需要添加值'0'以从裸数字中获取字符表示

关于c - lpc2148 RTC打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43398841/

相关文章:

c - C 中存储的数据被损坏

c - 套接字,使用 SOCK_DGRAM、epoll 进行客户端服务器通信

c - 是否可以打印日志文件的特定部分?

c - 如何在 OMX.broadcom.video_decode 组件中设置视频帧宽度和视频帧高度?

c - char * 的空终止

c - 使用 malloc、结构和可能参数的问题

c - 如何仅使用 32 位整数类型计算(a 乘以 b)除以 c,即使 a 乘以 b 不适合这种类型

c - 'struct x' 在参数列表中声明

c - C 上的 typedef 问题

c - c中整数除法运算的方向