完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我需要创建一个计时器。在每一个值中,我都保持小时和分钟。但是在某些情况下,我需要把它们分开,只取一个小时或只有一个整数作为整数。现在我只需要把第一个字节作为整数,使用上面的代码,我取的值是219。之后,我只需要把值保存到第一字节。错误的值。我应该把它转换成什么吗?
以上来自于百度翻译 以下为原文 I need to create a timeRegister. In each value i'm keeping Hour and Minitues. But i need in some cases to separate them and take Only Hour or only minitues as integer. typedef union { unsigned int Value; unsigned char bytes[2]; }SetValues; extern volatile SetValues TimeRegister[42]; TimeRegister[1].Value=1243; //Lets say that is 12:43 HH:mm DATAEE_WriteByte(1,TimeRegister[1].bytes[0]); // save lower byte DATAEE_WriteByte(1,TimeRegister[1].bytes[1]); Now i need to take only first byte which is 12 as integer int test=TimeRegister[1].bytes[0]; sprintf(txt,"%d",test); LCDPutStr(txt,8); Using code above i'm taking a value like 219. Also after that i need to save the value only to first byte DATAEE_WriteByte(1,TimeRegister[1].bytes[0]); I'm taking wrong values.Should i convert it to something? |
|
相关推荐
16个回答
|
|
1243=0x04dB,其中高位字节0x04=4,下字节0xdB=219。
以上来自于百度翻译 以下为原文 1243 = 0x04DB where the upper byte 0x04 = 4 and the lower byte 0xDB = 219. |
|
|
|
我认为你从根本上误解了数字是如何存储的。你说你用“1234”表示12:34.但是,十进制值1234是十六进制值0x4d2SO,0x04存储在上字节,0xd2在较低的字节中。将字节分开处理为十进制给出4和210。您需要确切地决定如何存储它,以及如何操作它。如果它来自RTC,那么它将以BCD格式。您可以使用它,但是12:34将被存储为0x1234,而不是十进制1234。如果您想要的话。只要保持小时为十进制值,每个字节中有一个,那么你就必须在顶部字节中存储12,而在底部字节中存储34,在十六进制中,这将是0x0C和0x22。如果你试图访问整个LAS,一个16位整数0x0c22=3106十进制。我不能告诉你哪一个是最好的,直到你解释你想要做什么。
以上来自于百度翻译 以下为原文 I think you're fundamentally misunderstanding how numbers are stored. You say you are using "1234" to mean 12:34. However, decimal value 1234 is hex value 0x4D2 so, 0x04 is stored in the upper byte, and 0xD2 is in the lower byte. treating the bytes separately as decimal gives 4 and 210. You need to decide exactly how you want it stored, and how you are going to manipulate it. If it's coming from an RTC, then it will be in BCD format. You can work with that, but then 12:34 would be stored as 0x1234, not decimal 1234. If you want to just keep the hours an minutes as decimal values, one in each byte, then you would have to store 12 in the top byte, and 34 in the bottom byte. In hex, that would be 0x0C and 0x22. If you try to access the whole las a 16 bit integer 0x0C22 = 3106 decimal. I can't tell you which was is best until you explain what you are trying to do. |
|
|
|
节省时间和分钟的最好方法是什么?我需要创建一个打开和关闭系统的时间表。阅读我的RTC是与此分离的。我将BCD转换成一个例程中的DEC。我要做的是创建一个小时和分钟,当我的RTC被捕获的时候,我的系统将自动启动。
以上来自于百度翻译 以下为原文 Which is the best way for saving hour and minutes? i need to create a schedule for opening and closing my system. Reading my rtc is separated with this. I'm converting Bcd to dec inside a routine. This i'm trying to do is to create an hour and minutes, and when my rtc is capture this time my system will start automatically. |
|
|
|
究竟如何?在不显示代码的情况下,你没有准确地描述它所做的事情。你实际上把十进制值存储在一个字节中,而十进制值存储在另一个字节中吗?如果是这样的话,那就是你的“计时员(1),价值=1243”,这是错误的,因为它不是用相同的方式存储数字。你所显示的代码只是试图把它写入一些EEPROM。你会把这些值转移到另一个代码中的一些RTC报警寄存器吗?
以上来自于百度翻译 以下为原文 How exactly? Without showing that code, you have not accurately described what it does. Do you actually store the decimal value for hours into one byte, and the decimal value for minutes into the other byte? If so, then it's just your "TimeRegister[1].Value=1243" that is faulty, because it is not storing the number the same way. How? The code you showed just tries to write it to some EEPROM. Are you going to transfer the values to some RTC alarm registers in another bit of code? |
|
|
|
我只能读取EEPROM的前两个字节作为整数吗?我需要取int N1=12和int n2=43。其余的我知道我将如何处理它。
以上来自于百度翻译 以下为原文 Can i read only first two bytes from eeprom as integer? i need to take int n1=12 and int n2=43. Rest of it i know how i will handle it. |
|
|
|
我不确定你所说的“整数”是什么,PIC里面的所有东西都是二进制的,只是我们人类更喜欢读十六进制或十进制。为什么你需要一个带有两字节数组的int(16位)变量共享空间?我认为你实际上是正确地处理了两个字节到EEPROM,这就是“其余的”,你错了。
以上来自于百度翻译 以下为原文 I'm uncertain what you mean by "as integer". Everything inside the PIC is done in binary, it's just we humans who prefer to read hex or decimal. Why exactly do you need that union with an int (16 bit) variable sharing space with a two byte array? I think you're actually handling the two bytes to the EEPROM correctly, it's the "rest of it" that you're getting wrong. |
|
|
|
我需要打印小时和分钟。另外两个将它们转换成数字。值219如何被转换成43?我需要转换器程序吗?从二进位到小数?我的问题是写还是读?
以上来自于百度翻译 以下为原文 i need to printf hour and minutes. also two convert them into numbers. How the value 219 will be convert into 43? Do i need a converter routine? From binary to decimal? My problem is in write or in read? |
|
|
|
你的问题在于理解数字是如何存储的。你为什么不把时间和分钟分开存储为两个字节呢?正如Qub所问的,为什么需要将这两个字节存储为16位int?
以上来自于百度翻译 以下为原文 Your problem is in understanding how numbers are stored. Why don't you just store the hour and minute separately as two bytes? As Qub has asked, why do you need to store these two bytes as a 16-bit int? |
|
|
|
+ 1到1和0的注释。你误解了数字是如何存储的,并认为它是在一些BCD格式中,前两个数字在一个字节中,而第二个在第二个字节中,这是完全错误的。小时是在较低的字节,而分钟在上字节,正如你的打印例程预期,大多数人会用另一种方式,用较大的单位(小时)在上字节。
以上来自于百度翻译 以下为原文 +1 to 1and0's comments. You are misunderstanding how numbers are stored, and thinking it's in some BCD format where the first two digits are in one byte, and the second two in the second byte, which is totally wrong. As I already said, the problem in your test is this line TimeRegister[1].Value=1243; //Lets say that is 12:43 HH:mm Change it to TimeRegister[1].bytes[0]=12; TimeRegister[1].bytes[1]=43; That's assuming hours are in the lower byte, and minutes are in the upper byte, as your print routine expects, Most people would do it the other way, with the bigger unit (hours) in the upper byte. |
|
|
|
解决了!很抱歉,这很容易。N1=TimeReistor(0)。值/ 100;N2=TimeReistist[0 ]。值% 100;
以上来自于百度翻译 以下为原文 Solved it! sorry it was easy at all. n1=TimeRegister[0].Value/100; n2=TimeRegister[0].Value%100; |
|
|
|
是的,如果你用“1243”的小数值初始化“值”字段,你所提出的代码意味着你想要一个字节中的小时,另一个字节中的分钟数。你根本不想要这个吗?
以上来自于百度翻译 以下为原文 Yes, you can do that if you initialise the "Value" field with a decimal value like "1243", The code you presented implied you wanted the hour in one byte, and the minutes in the other byte. Do you not want that at all? |
|
|
|
这将采取PIC设备使用闪存和周期时间来计算除法和模,而如果时间和分钟分别存储,就不需要任何这样的计算。不管怎么说,这是你的密码。
以上来自于百度翻译 以下为原文 That will take the PIC device to use flash memory and cycle time to compute the division and modulo, whereas if the hour and minute are stored separately there would be no need for any such computation. Anyway, it's your code. |
|
|
|
@ Qub,我猜OP必须从RTC读取小时和分钟作为四BCD数字,在这里他使用BCD到二进制例程并将结果存储为16位int;
以上来自于百度翻译 以下为原文 @Qub, I guess OP must be reading hour and minute from the RTC as four BCD digits, where he used a BCD to binary routine and stored the result as a 16-bit int. ;) |
|
|
|
可能是的,令人沮丧的是,所有重要的细节都在未显示的代码中。
以上来自于百度翻译 以下为原文 Probably yes. It's frustrating that all the important details are in code that is not shown... |
|
|
|
通常情况下,真正的问题往往是在别处。
以上来自于百度翻译 以下为原文 It always is, as often the real problem is elsewhere. |
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
4844 浏览 9 评论
1846 浏览 8 评论
1762 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
2971 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2074 浏览 5 评论
472浏览 1评论
1123浏览 1评论
313浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
375浏览 0评论
270浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-5-14 17:26 , Processed in 1.493753 second(s), Total 108, Slave 92 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号