uint16 累加_如何把一个uint16整数分解成两个字节并传输?

本文讨论了如何将uint16_t类型的数值高效地分解为两个字节,通过使用union实现,涉及C语言和嵌入式编程中的字节操作。讨论中提到了不同方法的指令数量和可移植性,并给出了代码示例。
摘要由CSDN通过智能技术生成

以上来自于百度翻译

以下为原文

Yeah, the second is possibly less portable , but can do the job efficiently in this case

2019-6-28 07:12:13

评论

提交评论

以上来自于百度翻译

以下为原文

Thakns!

i need clean asms,so i will take the second.

2019-6-28 07:28:20

评论

提交评论

以上来自于百度翻译

以下为原文

this declaration cost 0 instructions, possibly less portable ;

union timer {

uint16_t i2cTimer;

uint8_t  val[2];

}  i2ct  ;

2019-6-28 07:43:39

评论

提交评论

以上来自于百度翻译

以下为原文

yes,better idea to take advantage of union,clean in c  too...

2019-6-28 07:58:38

评论

提交评论

以上来自于百度翻译

以下为原文

That's also true

2019-6-28 08:06:55

评论

提交评论

以上来自于百度翻译

以下为原文

Just curious. Could you post the disassembly for this line? I don't see how it can take 10 instructions, even for Free mode.

2019-6-28 08:14:18

评论

提交评论

以上来自于百度翻译

以下为原文

*(uint16_t*)val = i2cTimer; // assuming val is word-aligned edit: doesn't matter for XC8

If you're building a buffer, just copy i2cTimer diectly to where it should be in the buffer.

2019-6-28 08:34:14

评论

提交评论

以上来自于百度翻译

以下为原文

@NorthGuy:Thanks! all of your advice have been of great help!

2019-6-28 08:51:51

评论

提交评论

以上来自于百度翻译

以下为原文

Hi,1and0,below was asm lines i tried to restore,really confused,please kindly check if something was wrong:

5417 ;RSN-1601A_Functions.c: 126: ToSendDataBuffer[8] = i2cTimer;

5418 015B 0023 movlb 3 ; select bank3

5419 015C 085B movf _i2cTimer^(0+384),w

5420 015D 0022 movlb 2 ; select bank2

5421 015E 00A8 movwf 40

5422

5423 ;RSN-1601A_Functions.c: 127: ToSendDataBuffer[9] = i2cTimer>>8;

5424 015F 0023 movlb 3 ; select bank3

5425 0160 085C movf (_i2cTimer+1)^(0+384),w

5426 0161 0022 movlb 2 ; select bank2

5427 0162 00E3 movwf (??_CmdService^(0+256)+1)//_CmdService is the function name which contain the codes

5428 0163 0023 movlb 3 ; select bank3

5429 0164 085B movf _i2cTimer^(0+384),w

5430 0165 0022 movlb 2 ; select bank2

5431 0166 00E2 movwf ??_CmdService^(0+256)

5432 0167 0863 movf (??_CmdService^(0+256)+1),w

5433 0168 00E2 movwf ??_CmdService^(0+256)

5434 0169 01E3 clrf (??_CmdService^(0+256)+1)

5435 016A 0862 movf ??_CmdService^(0+256),w

5436 016B 00A9 movwf 41

5437

5438 ;RSN-1601A_Functions.c: 128: break;//i do above in a "case:",so "break" is here

5439 016C 0008 return

2019-6-28 09:10:59

评论

提交评论

以上来自于百度翻译

以下为原文

Use pointer conversion,only 4 instructions:

5417 ;RSN-1601A_Functions.c: 126: ToSendDataBuffer[8] = i2cTimer;

5418 015B 0023 movlb 3 ; select bank3

5419 015C 085B movf _i2cTimer^(0+384),w

5420 015D 0022 movlb 2 ; select bank2

5421 015E 00A8 movwf 40

5422

5423 ;RSN-1601A_Functions.c: 127: ToSendDataBuffer[9] = ((uint8_t *)&i2cTimer)[1];

5424 015F 0023 movlb 3 ; select bank3

5425 0160 085C movf (_i2cTimer^(0+384)+1),w

5426 0161 0022 movlb 2 ; select bank2

5427 0162 00A9 movwf 41

5428

5429 ;RSN-1601A_Functions.c: 128: break;

5430 0163 0008 return

2019-6-28 09:16:40

评论

提交评论

以上来自于百度翻译

以下为原文

Union Version:only cost 2 instructions!!!(XC8,free mode,what happeded with you)

typedef union

{ uint16_t  valUint16;

uint8_t    byte[2];

} union_uint16_t;

extern union_uint16_t  i2cTimer;

//

ASM lines:

5419 ;RSN-1601A_Functions.c: 126: ToSendDataBuffer[8] = i2cTimer.byte[0];

5420 015B 086E movf _i2cTimer^(0+256),w

5421 015C 00A8 movwf 40

5422

5423 ;RSN-1601A_Functions.c: 127: ToSendDataBuffer[9] = i2cTimer.byte[1];//cost 2 instructions!!!

5424 015D 086F movf (_i2cTimer^(0+256)+1),w

5425 015E 00A9 movwf 41

5426

5427 ;RSN-1601A_Functions.c: 128: break;

5428 015F 0008 return

2019-6-28 09:24:29

评论

提交评论

以上来自于百度翻译

以下为原文

The comparisons are not representative and depend mostly on the way the compiler allocates variables. If they're in different banks it takes 8 instructions to move 2 bytes. If they're in the same bank - 5 instructions (4 if the bank is already selected). Otherwise, it's all the same.

2019-6-28 09:41:57

评论

提交评论

以上来自于百度翻译

以下为原文

WTF...even Free mode cannot be that stupid! Lines 5427 to 5435 are totally unnecessary. That disassembly does this:

uint16_t CmdService;

CmdService = i2cTimer;

CmdService >>= 8;

ToSendDataBuffer[9] = CmdService;

2019-6-28 09:56:44

评论

提交评论

以上来自于百度翻译

以下为原文

Hi 1and0,

i just built a simple prj of xc8 under free mode to test this,by simulator,the last line did not work at all,i am afraid it is a bug of XC8 ver1.38,you could also test it on you side.

#include

#include

void main(void) {

uint16_t u16t=0xABCD;

uint8_t val[2];

val[0]= u16t;//it works,val[0] is 0xCD

val[1]= (u16t>>8) ;//vars got lost......

return;

}

asm lines:

217 ;psect for function _main

218 07EF _main:

219

220 ;main.c: 12: uint16_t u16t=0xABCD;

221

222 ;incstack = 0

223 ; Regs used in _main: [wreg+status,2]

224 07EF 30CD movlw 205

225 07F0 00F3 movwf main@u16t

226 07F1 30AB movlw 171

227 07F2 00F4 movwf main@u16t+1

228

229 ;main.c: 13: uint8_t val[2];

230 ;main.c: 14: val[0]= u16t;

231 07F3 0873 movf main@u16t,w

232 07F4 00F0 movwf ??_main

233 07F5 0870 movf ??_main,w

234 07F6 00F1 movwf main@val

235

236 ;main.c: 15: val[1]= (u16t>>8) ;

237 07F7 0874 movf main@u16t+1,w

238 07F8 00F0 movwf ??_main

239 07F9 0870 movf ??_main,w

240 07FA 00F2 movwf main@val+1

241

242 ;main.c: 17: return;

2019-6-28 10:15:35

评论

提交评论

以上来自于百度翻译

以下为原文

Hi 1and0,is that possible to know result on you side? MPLAB x blue-screened my PC several times while compiling these days,maybe i need to re install them.

2019-6-28 10:27:02

评论

提交评论

以上来自于百度翻译

以下为原文

That is an invalid test.

If your variable is never used after being written (as you have done here),

and if you don't make your variable "volatile",

then the compiler is free to discard the writing code altogether as part of its optimisation.

2019-6-28 10:34:40

评论

提交评论

以上来自于百度翻译

以下为原文

in fact,if i just add a while(1); at the end,things are in order...

2019-6-28 10:50:52

评论

提交评论

以上来自于百度翻译

以下为原文

No, it looks like it is still there at line 240.

2019-6-28 11:07:31

评论

提交评论

以上来自于百度翻译

以下为原文

sorry,i just check the output result when debugging after adding the while(1);without review asm lines;

does that mean something wrong with XC8? or need update.

2019-6-28 11:16:55

评论

提交评论

只有小组成员才能发言,加入小组>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值