简体   繁体   English

@登录C变量声明

[英]@ sign in C variable declaration

I found this header file for PIC microcontrollers by the name of pic1250.h and I'm unable to get the hang of some syntax used in it. 我找到了PIC微控制器的这个头文件,名称为pic1250.h,我无法理解其中使用的一些语法。

The source for the file is: 该文件的来源是:

/*
 *  Header file for the Microchip 
 *  PIC 12c508 chip 
 *  PIC 12c509 chip
 *  Baseline Microcontrollers
 */

static volatile unsigned char   RTCC    @ 0x01;
static volatile unsigned char   TMR0    @ 0x01;
static volatile unsigned char   PCL @ 0x02;
static volatile unsigned char   STATUS  @ 0x03;
static          unsigned char   FSR @ 0x04;
static volatile unsigned char   OSCCAL  @ 0x05;
static volatile unsigned char   GPIO    @ 0x06;

static          unsigned char control   OPTION  @ 0x00;
static volatile unsigned char control   TRIS    @ 0x06;

/*  STATUS bits */
static bit  GPWUF   @ (unsigned)&STATUS*8+7;
static bit  PA0 @ (unsigned)&STATUS*8+5;
static bit  TO  @ (unsigned)&STATUS*8+4;
static bit  PD  @ (unsigned)&STATUS*8+3;
static bit  ZERO    @ (unsigned)&STATUS*8+2;
static bit  DC  @ (unsigned)&STATUS*8+1;
static bit  CARRY   @ (unsigned)&STATUS*8+0;

/*  OPTION bits */
#define     GPWU    (1<<7)
#define     GPPU    (1<<6)
#define     T0CS    (1<<5)
#define     T0SE    (1<<4)
#define     PSA (1<<3)
#define     PS2 (1<<2)
#define     PS1 (1<<1)
#define     PS0 (1<<0)

/*  OSCCAL bits */
static bit  CAL7    @ (unsigned)&OSCCAL*8+7;
static bit  CAL6    @ (unsigned)&OSCCAL*8+6;
static bit  CAL5    @ (unsigned)&OSCCAL*8+5;
static bit  CAL4    @ (unsigned)&OSCCAL*8+4;

/*  GPIO bits   */
static bit  GP5 @ (unsigned)&GPIO*8+5;
static bit  GP4 @ (unsigned)&GPIO*8+4;
static bit  GP3 @ (unsigned)&GPIO*8+3;
static bit  GP2 @ (unsigned)&GPIO*8+2;
static bit  GP1 @ (unsigned)&GPIO*8+1;
static bit  GP0 @ (unsigned)&GPIO*8+0;

#define CONFIG_ADDR 0xFFF
#define FOSC0       0x01
#define FOSC1       0x02
#define WDTE        0x04
#define CP      0x08
#define MCLRE       0x0F

I'm unable to understand the whole modifer-datatype @ declaration-something. 我无法理解整个modifer-datatype @ declaration-something。 Can someone please help me out? 有人可以帮帮我吗? I'm just a newbie at this. 我只是个新手。

It's a compiler extension. 这是一个编译器扩展。

From PIC MPLAB XC8 compiler documentation (emphasis mine): 从PIC MPLAB XC8编译器文档(强调我的):

5.5.4 Absolute Variables 5.5.4绝对变量

Most variables can be located at an absolute address by following its declaration with the construct @ address , where address is the location in memory where the variable is to be positioned. 大多数变量可以通过遵循构造@地址的声明来定位在绝对地址,其中address是存储器中要定位变量的位置。 Such a variables is known as an absolute variables. 这种变量称为绝对变量。

5.5.4.1 ABSOLUTE VARIABLES IN DATA MEMORY 5.5.4.1数据存储器中的绝对变量

Absolute variables are primarily intended for equating the address of a C identifier with a special function register, but can be used to place ordinary variables at an absolute address in data memory. 绝对变量主要用于将C标识符的地址与特殊功能寄存器等同,但可用于将普通变量放在数据存储器的绝对地址中。

For example: 例如:

volatile unsigned char Portvar @ 0x06; volatile unsigned char Portvar @ 0x06;

will declare a variable called Portvar located at 06h in the data memory. 将声明一个名为Portvar的变量,位于数据存储器中的06h。 The compiler will reserve storage for this object (if the address falls into general-purpose RAM) and will equate the variable's identifier to that address. 编译器将为此对象保留存储空间(如果地址属于通用RAM),并将变量的标识符等同于该地址。

Note that MPLAB XC8 is not the only compiler to have the same @ construct to place an object in the specific memory location. 请注意,MPLAB XC8不是唯一具有相同@构造的编译器,用于将对象放置在特定的内存位置。

Another well known compiler is Freescale CodeWarrior (at least for HCS08). 另一个众所周知的编译器是Freescale CodeWarrior(至少对于HCS08而言)。

Another one is IAR C Compiler (at least for MSP430 and AVR). 另一个是IAR C编译器(至少对于MSP430和AVR)。

It's an extension in the PIC compiler, to place a variable at a specific memory position. 它是PIC编译器的扩展,用于将变量放在特定的内存位置。 No other compiler I know have that extension. 我知道没有其他编译器具有该扩展名。

It's a C language extension supported by the PIC compiler that allows assigning variables to specific RAM addresses. 它是PIC编译器支持的C语言扩展,允许将变量分配给特定的RAM地址。

exemples: exemples:

char a @ 0x25;  /* place A at address 0x25 */
bit b @ 0x25.3; /* place B at the third bit of address 0x25 */

There are three uses for this: 这有三个用途:

  • In embedded system you often have very little memory and need to pack several variables in the same byte. 在嵌入式系统中,您通常只有很少的内存,需要在同一个字节中打包几个变量。 This syntax makes it easier, although a standard bit field would work too. 尽管标准位字段也可以使用,但这种语法更容易实现。
  • Another reason is that PIC registers are mapped to RAM, so that you can access them like any other memory location. 另一个原因是PIC寄存器映射到RAM,因此您可以像访问任何其他内存位置一样访问它们。 With this syntax you can define synonyms for the bits you are interested in and use them like normal variables. 使用此语法,您可以为您感兴趣的位定义同义词,并像普通变量一样使用它们。
  • Put you own variable (regardless of size) at a specific location. 在特定位置放置您自己的变量(无论大小)。 This is occasionally useful. 这偶尔会有用。

Remember that embedded programming is all about total control of the hardware. 请记住,嵌入式编程完全是关于硬件的完全控制。

In addition to what has already been said, please note that the non-standard @ operator is a superfluous feature. 除了已经说过的内容之外,请注意非标准的@运算符是一个多余的功能。 You can achieve exactly the same behavior with standard C: 您可以使用标准C实现完全相同的行为:

#define RTCC (*(volatile uint8_t*)0x0001u)

Since the variables in this case are hardware registers, you don't need to worry about allocation, they are already present in the hardware. 由于这种情况下的变量是硬件寄存器,因此您无需担心分配,它们已经存在于硬件中。 If you want to allocate a variable at a custom address, there should be a linker file of some kind to fix that (since the @ operator only solves specific allocation for variables, not for code). 如果要在自定义地址分配变量,应该有某种链接器文件来修复它(因为@运算符只解决变量的特定分配,而不是代码)。

The main reason why many embedded compilers come up with some non-standard operator like @ is because they can't think outside the box when designing the debugger. 许多嵌入式编译器提出像@这样的非标准运算符的主要原因是因为在设计调试器时他们无法思考。 They expect some sort of variable to be present in the object file which is fed to the debugger, but if you use #define, no such "debug information object" is allocated. 他们期望在提供给调试器的目标文件中存在某种变量,但是如果使用#define,则不会分配这样的“调试信息对象”。

If the debugger looked at the source code instead, or better yet, had MCU awareness built-in, then non-standard code like this wouldn't be necessary. 如果调试器改为查看源代码,或者更好,但内置了MCU感知,那么就不需要像这样的非标准代码。 High quality tools from companies that focus solely on debuggers always come with built-in support for viewing register maps. 来自专注于调试器的公司的高质量工具总是内置支持查看寄存器映射。

A short extension: 简短的延期:

This is no longer working since xc8 2.0 and up. 自从xc8 2.0及更高版本开始,这已不再适用。 You now had to write: 你现在必须写:

unsigned char a __at(0x025);

to put a variable ( a ) at an absolute address ( 0x025 ). 将变量( a )放在绝对地址( 0x025 )。

With XC8 2.0 it is possible to compile your old code using the @ syntax if you set the compiler settings to use "C90" format. 使用XC8 2.0,如果将编译器设置设置为使用“C90”格式,则可以使用@语法编译旧代码。 The setting looks like this, it is under "XC8 Global Options" and is called "C standard". 设置如下所示,它位于“XC8 Global Options”下,称为“C标准”。

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

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