英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流

 找回密码
 立即注册
搜索
电子烟方案单片机单片机开发深圳单片机开发
单片机方案国产单片机8位单片机电子烟方案开发
查看: 5048|回复: 5
打印 上一主题 下一主题

MICROCHIP_PICC常用优化技巧

[复制链接]
1#
发表于 2009-4-19 01:25:07 | 显示全部楼层
优化提示2: 基于字节Byte的循环Loops

这里有两块代码,它们做的完全是同样的事情。但是其中一个要完成得快25%,并且使用更小的RAM空间,你能挑出是哪一个吗?

unsigned char i;

for(i=0;i<250;i++) do_func(); //executes do_func() 250 times, in 3.25ms

for(i=250;i!=0;i--) do_func(); //executes do_func() 250 times, in 2.5ms

要找出这个,我们来看一下产生的汇编代码

for(i=0;i<250;i++) do_func();

//executes 250 times in 3251 cy

1617 01B8 clrf 0x38

1618 260F call 0x60F

1619 0AB8 incf 0x38

161A 3008 movlw 0xFA

161B 0238 subwf 0x38,W

161C 1C03 btfss 0x3,0x0

161D 2E18 goto 0x618  

for(i=250;i!=0;i--) do_func();

//executes 250 times in 2502 cy

1621 3008 movlw 0xFA

1622 00B8 movwf 0x38

1623 260F call 0x60F

1624 0BB8 decfsz 0x38

1625 2E23 goto 0x623   

结论2:

如果可能,让你的循环递减到零。检查一个ram变量是否为零会更快一些。

但是,注意在递增循环里,do_func()要早一个时钟周期被调用。如果你希望进入函数的速度快些,可选择递增循环。
回复 支持 反对

使用道具 举报

2#
发表于 2009-4-19 01:25:25 | 显示全部楼层
优化提示3: Integer

Timeout Loops

如果你想查询一个端口,或者在timeout“时间到”之前执行一个函数一定的次数,你需要一个定时循环timeout loop.

unsigned int timeout;

#define hibyte(x) ((unsigned char)(x>>8))

#define lobyte(x) ((unsigned char)(x&0xff))

//the optimizer takes care of using the hi/lo correct byte of integer

·Loops to avoid with timeouts: 320000 to 380000 cycles for 20000 iterations.

for(timeout=0;timeout<20000;timeout++) do_func(); //380011 cycles

for(timeout=20000;timeout!=0;timeout--) do_func(); //320011 cycles |

· Best loop for a timeout: 295000 cycles for 20000 iterations.

//we want to execute do_func() approx. 20000 times before timing out
timeout=(20000/0x100)*0x100; //keeps lobyte(timeout)==0, which speeds up assignments

for(;hibyte(timeout)!=0;timeout--) do_func(); //295704 cycles

Notice the features of the loop shown above.

1. 它在每个循环里只测试整型数的高位字节。

2. 它检查这个字节是否到零,所以很快。

3. 当初始化timeout变量时,它又一个好处是:汇编代码清零一个ram变量只要一条指令,而赋值则需要两条指令。

结论3:

·尽可能地采用递减到零的循环,因为检查ram变量是否为零更简单。

· 在延时循环里只查询整型数的高位字节,这要快一些。  给整型数赋值时,对ram变量清零要比赋一个数值要快一些。
回复 支持 反对

使用道具 举报

3#
发表于 2009-4-19 01:26:14 | 显示全部楼层
优化提示4: 使用内部定时器的Timeout定时循环

当然,使用芯片片内定时器并检查中断的方式是最快的定时循环。它通常会比用延时循环快70%左右。

//set up tmr0 to set flag T0IF high when it rolls over

while(RA0==0 && !T0IF); //wait until port goes high

结论4:

·尽可能地使用内建的定时器及中断标志。
优化提示5: Case 语句

慢而且效率低

c=getch();

switch(c)

{

case 'A':
{
do something;
break;
}
case 'H':
{
do something;
break;
}
case 'Z':
{
do something;
break;
}
}

getch 快且高效率
c=getch();
switch(c)
{
case 0:
{
do something;
break;
}
case 1:
{
do something;
break;
}
case 2:
{
do something;
break;
}
}
Hi-Tech C的优化器会尽可能地把switch语句变成计算偏移的goto
回复 支持 反对

使用道具 举报

4#
发表于 2009-4-19 01:26:51 | 显示全部楼层
结论5:
·尽可能地在case语句里使用连续的数字。
优化提示6: Hi-Tech C里的除法

如果你使用Hi-Tech C,在你程序的任何位置有任何数学除法的运算,就将会使用到bank0里13到23个字节的空间,以及一些EPROM/Flash程序空间。
尽管变量不在bank0,这一样会发生。
Occurrence
Any mathematical division at all in the entire program using a variable of type 'long', even if all variables do not reside in bank0.
RAM usage
23 bytes in bank0

ROM/flash usage
large, it has to include ldiv routines
Fix/Explanation
Use combinations of bit shifts ie: x=x*6 is replaced by x1=x;x2=x;x=x1<<2 + x2<<1

Occurrence
Any mathematical division at all in the entire program using a variable of type 'unsigned int', even if all variables do not reside in bank0.
RAM usage
13 bytes in bank0
ROM/flash usage
large,it has to include ldiv routines
Fix/Explanation
Use combinations of bit shifts

Occurrence
Any mathematical division involving a divisor that is a power of 2, ie: x=x/64;
RAM usage
none
ROM/flash usage
low
Fix/Explanation
Use combinations of bit shifts
Occurrence
Any mathematical division involving a divisor that is not a power of 2, ie: x=x/65;
RAM usage
none
ROM/flash usage
high
Fix/Explanation
make your divisors a power of 2, ie: 2^5=32.
回复 支持 反对

使用道具 举报

5#
发表于 2009-4-19 01:27:05 | 显示全部楼层
结论6:
如果可能,尽量让C编译器使用简单的移位来做除法,且除数是2的幂。除数是2的幂,例如,256=2^8,可以被C编译器优化为移位操作。

如果你在程序里没有使用任何除法运算,则可以节省bank0里的23个字节和一部分程序空间-ldiv()程序。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|公司首页|Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 ( 粤ICP备09008620号 )

GMT+8, 2024-5-12 15:22 , Processed in 0.051115 second(s), 21 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表