简体   繁体   English

如何使用c和pic16f84a优化此代码?

[英]how to optimize this code using c and pic16f84a?

I'm using mikroC to program pic16f84a, and i have the following function 我正在使用mikroC编程pic16f84a,并且具有以下功能

volatile unsigned short d;  // global variable

void put_data(){
    RA3_bit = d & 1;
    d >>= 1;
    RA4_bit = d & 1;
    d >>= 1;
    PORTB.B0 = d & 1;
    d >>= 1;
    PORTB.B1 = d & 1;
    d >>= 1;
    PORTB.B2 = d & 1;
    d >>= 1;
    PORTB.B3 = d & 1;
    d >>= 1;
    PORTB.B4 = d & 1;
    d >>= 1;
    PORTB.B5 = d & 1;
}

this function, take each bit from d (8 bits) and output it to port pin RA3, RA4, RB0, ... , RB5. 此功能,从d(8位)中取每个位,并将其输出到端口引脚RA3,RA4,RB0,...,RB5。

how could I optimize this code,, and memory is my first concern. 我该如何优化此代码,而内存是我的首要考虑。

Update:: 更新::

from pic16f84a.h: 来自pic16f84a.h:

volatile unsigned char           PORTA               @ 0x005;
// bit and bitfield definitions
volatile bit RA0                 @ ((unsigned)&PORTA*8)+0;
volatile bit RA1                 @ ((unsigned)&PORTA*8)+1;
volatile bit RA2                 @ ((unsigned)&PORTA*8)+2;
volatile bit RA3                 @ ((unsigned)&PORTA*8)+3;
volatile bit RA4                 @ ((unsigned)&PORTA*8)+4;

volatile unsigned char           PORTB               @ 0x006;
// bit and bitfield definitions
volatile bit RB0                 @ ((unsigned)&PORTB*8)+0;
volatile bit RB1                 @ ((unsigned)&PORTB*8)+1;
volatile bit RB2                 @ ((unsigned)&PORTB*8)+2;
volatile bit RB3                 @ ((unsigned)&PORTB*8)+3;
volatile bit RB4                 @ ((unsigned)&PORTB*8)+4;
volatile bit RB5                 @ ((unsigned)&PORTB*8)+5;
volatile bit RB6                 @ ((unsigned)&PORTB*8)+6;
volatile bit RB7                 @ ((unsigned)&PORTB*8)+7;

can i use these values from the header file,, to make the function a few lines of code inside a loop ? 我可以使用头文件中的这些值来使函数在循环内几行代码吗?

Assuming that PORTB.B0 through PORTB.B5 are bits of the out port, doing this might be faster: 假设PORTB.B0至PORTB.B5是out端口的位,则这样做可能会更快:


volatile unsigned short d;  // global variable

void put_data(){
    RA3_bit = d & 1;
    d >>= 1;
    RA4_bit = d & 1;
    d >>= 1;

    PORTB &= 0x3F; // Mask out the low 6 bits
    PORTB |= d & 0x3f;  // Or in the low 6 bits of d

    // Clean out the bits if needed in d
    d >>= 6;
}

I would expect the assembler to come out roughly twice as fast as what you are doing now, maybe more. 我希望汇编程序的发布速度大约是您现在的两倍,甚至更多。 Or'ing the 3rd and 4th bit on the first 2 instructions is probably not worth it do to the location of the bits within the port. 将前2条指令的第3位和第4位相加或运算可能不值得在端口内的位位置进行。 To be sure though, always double check the assembler output. 当然,请始终仔细检查汇编器输出。 The opcodes for your assembler are simple enough that it would be pretty quick to confirm. 汇编程序的操作码非常简单,因此可以很快确认。

Read the current value of the PORT or LAT register for A & B. If that PIC has both PORT & LAT, it's important to know the difference, so I suggest you do some research if you're not sure which to use. 读取A和B的PORT或LAT寄存器的当前值。如果该PIC同时具有PORT和LAT,则了解两者之间的差异非常重要,因此,如果您不确定要使用哪个,我建议您进行一些研究。

Then do the shifting & masking necessary to make sure you only change the appropriate pins, and do just two write operations, one to port A and one to port B. 然后执行必要的移位和屏蔽操作,以确保仅更改相应的引脚,并且仅执行两次写操作,一个写到端口A,一个写到端口B。

Alternatively, if all the other pins on port A & B are set to input via the TRIS register associated with each port, skip the reading step. 或者,如果将端口A和B上的所有其他引脚都设置为通过与每个端口关联的TRIS寄存器输入,则跳过读取步骤。

So for example: 因此,例如:

tmp = PORTA;
tmp = (tmp & 0xFC) | (d & 0x3);
PORTA = tmp;

tmp = PORTB;
tmp = (tmp & 0xE0) | ((d >> 2) & 0x1F);
PORTB = tmp;

I think that will actually give you the opposite bit order from what your code does, so you may need to "reverse" the bit manipulation, or alternatively flip the bit order of d before using that code. 我认为这实际上会为您提供与代码相反的位顺序,因此您可能需要“反转”位操作,或者在使用该代码之前翻转d的位顺序。

Also, I won't guarantee that this actually produces faster PIC code. 另外,我不能保证这实际上会产生更快的PIC代码。 If you really require that absolute fastest approach, you might need to write a few lines of assembly. 如果您确实需要绝对最快的方法,则可能需要编写几行汇​​编。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM