English 中文(简体)
如何使用 c 和 pic16f84a 优化此代码?
原标题:how to optimize this code using c and pic16f84a?
  • 时间:2012-05-22 20:59:34
  •  标签:
  • c
  • pic
  • mikroc

我使用 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;
}

此函数, 从 d (8 位元) 中取取每位元, 并将其输出为 RA3 、 RA4 、 RB0 、... 、 RB 5 端口针 。

我怎样才能优化这个代码 记忆是我关心的首要问题

<强> 更新日期:

来自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;

我可以使用信头文件中的这些值, 使该函数在循环中成为几行代码?

最佳回答

假设PORB.B0通过PORB.B5是外港的位子,


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;
}

我希望组装者能比你们现在做的快大约两倍,也许更多。 或者在前2个指令上输入第3和第4个点, 可能不值得在端口内找到比特的位置。 当然, 总是双倍检查组装者的输出。 您组装者的代码非常简单, 很快就可以确认了 。

问题回答

读取 A & amp; B 的 PORT 或 LAT 登记册的当前值。 如果 PIC 同时拥有 port & amp; LAT, 了解差异很重要, 因此我建议您做一些研究, 如果您不确定要使用哪种 。

然后做移动 & amp; 所需的掩码, 以确保您只更改合适的针, 并且只做两个写操作, 一个到 A 端口, 一个到 B 端口 。

或者,如果港口A & amp; B上的所有其他别针都设置在与每个港口相关的TRIS登记册上输入,则跳过读取步骤。

例如:

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

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

我认为这将实际上给您提供与您的代码所做的相反的位数顺序, 所以您可能需要“ 反转” 位数操作, 或者在使用该代码之前翻转 < code> d 的位数顺序 。

此外,我也不会保证这实际上能产生更快的事先知情同意代码。 如果你真的需要这种绝对最快的方法,你可能需要写几行装配线。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签