简体   繁体   English

如何迭代相邻的 PIC 单片机寄存器?

[英]How do I iterate over adjacent PIC microcontroller registers?

I'm writing an application for a PIC18F45K80 microcontroller.我正在为 PIC18F45K80 微控制器编写应用程序。 I would like to retrieve bytes from the eight RXB1D registers.我想从八个 RXB1D 寄存器中检索字节。 According to the datasheet, "Each receive buffer has an array of registers."根据数据表,“每个接收缓冲区都有一个寄存器数组”。 Also, studying the defines reveals that all 8 registers have consecutive memory addresses, just like an array.此外,研究定义表明,所有 8 个寄存器都有连续的 memory 地址,就像一个数组一样。

What is the correct syntax for looping through these registers?遍历这些寄存器的正确语法是什么?

I've tried for (i = 0; i < len; i++) databuf[i] = RXB1D0[i];我试过for (i = 0; i < len; i++) databuf[i] = RXB1D0[i]; , but the compiler returns error: subscripted value is not an array, pointer, or vector , and points to the RXB1D0 identifier. ,但编译器返回error: subscripted value is not an array, pointer, or vector ,并指向 RXB1D0 标识符。

I would discourage you from doing it this way as PICs use Hardware architecture and memory access via a pointer in some areas might not work as it requires special assembly instructions.我不鼓励您这样做,因为 PIC 使用硬件架构,并且在某些区域通过指针访问 memory 可能不起作用,因为它需要特殊的汇编指令。 They are only 8 so I would do it this way:他们只有 8 个所以我会这样做:

#define GETREG(b, i) (b)[i] = RXB1D ## i
#define GETREGS(b) do {GETREG(b,0);GETREG(b,1);GETREG(b,2);GETREG(b,3);\
                       GETREG(b,4);GETREG(b,5);GETREG(b,6);GETREG(b,7);}while(0)

and usage:和用法:

void foo()
{
    char databuff[8];
    GETREGS(databuff);
}

Every read will compile to two assembly instructions.每次读取都将编译为两个汇编指令。 Even if you find the way of looping using pointers it will result in much more instructions.即使你找到了使用指针循环的方式,它也会产生更多的指令。

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

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