无法将#define整数作为uint8_t / int类型的参数或作为数组索引传递

时间:2018-08-30 20:47:51

标签: c embedded

有一个奇怪的问题,基本上我不能使用 #define 指令声明一个整数,然后将该整数用作函数参数或索引数组。

使以下示例工作的唯一方法是将它们声明为

const uint8_t LOC3 = 3;

const uint8_t PORTA = 0;

这是定义:

#define LOC3 3;

这是函数原型:

void spi_Setup(USART_TypeDef *spi,
            uint8_t location, <-- this parameter here
            bool master,
            char* rxBuffer,
            char* txBuffer);

即使将参数声明为 int 类型,它仍然不允许我这样调用函数:

spi_Setup(USART1, LOC3, true, receiveBuffer, transmitBuffer);

同样适用于数组索引:

#define PORTA 0;

这里正在调用该结构:

GPIO->P[PORTA].DOUTCLR = 1 << 1;

如前所述,这些示例都不起作用,


编译器设置:

方言:C99 编译器:GNU ARM嵌入式工具链

编译器标志:

  

-g -gdwarf-2 -mcpu = cortex-m0plus -mthumb -std = c99'-DDEBUG = 1''-DEFM32ZG222F32 = 1'-I“ / home / access / Downloads / simplicitystudio / pkg / simplicitystudio / opt /simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//platform/CMSIS/Include“ -I” /home/access/Downloads/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3/ / hardware / kit / EFM32ZG_STK3200 / config“ -I” /home/access/Downloads/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//hardware/kit/common/bsp“ -I “ / home / access /下载/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//platform/Device/SiliconLabs/EFM32ZG/Include” -I“ / home / access / Downloads / simplicitystudio /pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//hardware/kit/common/drivers“ -O0 -Wall -c -fmessage-length = 0 -mno-sched-prolog -fno-builtin -ffunction-sections -fdata-sections

汇编器标志:

  

-g -gdwarf-2 -mcpu = cortex-m0plus -mthumb -c -x带有cpp的汇编器-I“ / home / access /下载/ simplicitystudio / pkg / simplicitystudio / opt / simplicitystudio / developer / sdks / gecko_sdk_suite / v2.3 // platform / CMSIS / Include“ -I” /家庭/访问/下载/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//hardware/kit/EFM32ZG_STK3200/ config“ -I” / home / access /下载/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//hardware/kit/common/bsp“ -I” / home / access /下载/simplicitystudio/pkg/simplicitystudio/opt/simplicitystudio/developer/sdks/gecko_sdk_suite/v2.3//platform/Device/SiliconLabs/EFM32ZG/Include"-I"/home/access/Downloads/simplicitystudio/pkg/simplicitystudio/opt/ simplestudio / developer / sdks / gecko_sdk_suite / v2.3 // hardware / kit / common / drivers“'-DEFM32ZG222F32 = 1'

链接器标志:

  

-g -gdwarf-2 -mcpu = cortex-m0plus -mthumb -T“ $ {BuildArtifactFileBaseName} .ld” -Xlinker --gc-sections -Xlinker -Map =“ $ {BuildArtifactFileBaseName} .map” –规范= nano.specs

我问我的老人是谁,他说这很奇怪。我同意,因为我从未遇到过使用#defines这样的问题,实际上,这被认为是惯例。


我试图将数字用一组括号括起来

#define LOC3 (3)

但是那也不起作用。

以前有人遇到过这个问题吗?

预先感谢, MM25

1 个答案:

答案 0 :(得分:5)

#define LOC3 3;

您需要删除;,否则您的函数调用看起来像

spi_Setup(USART1, 3; , true, receiveBuffer, transmitBuffer);

这不是有效的C语法。

#define LOC3 3

将起作用。