This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] CCS/MSP432P401R:#664预计会出现整数常量误差

Guru**** 3001220 points
请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/tools/code-composer-studio-group/ccs/f/code-composer-studio-forum/924081/ccs-msp432p401r-664-expected-an-integer-constant-error

器件型号:MSP432P401R

工具/软件:Code Composer Studio

对于上下文,我将获取某些人的 ID#的最后3位数字,然后使用它来控制板载 LED 闪烁的延迟时间,在这里我遇到 了__DELAY_CYCLS ()问题; 函数、它说它需要一个整数常量、即使我提供一个整数、甚至没有使我的整数名称 Red_light 成为常量 int 似乎可以解决这个问题、作为解决这个问题的一种方法、您会给我什么建议?  

#include "msp.h"

void main (void)
{
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD;//停止看门狗计时器

字符 ID[2];
printf ("请输入学校 ID 的最后3位数字\n");//此部分代码的用户输入一个字符串的数字
scanf ("%s"、&ID);//然后将转换为下面的整数

int n1 = ID[0]-'0';//这三行将 char 成功转换回 int、只需按照
int n2 = ID[1]-'0';// int n1 =某些数字-'0'公式;
int n3 = ID[2]-'0';

const int Red_light =(n1 * 10)* 1000;//常量整数值应位于内部
const int Green_light =(n2 * 10)* 1000;//__delay_cycles ()函数;
const int Blue_light =(n3 * 10)* 1000;

while (1){
P1DIR = P1DIR | 0x09;//P2.0至逻辑一
_DELAY_CYCLES (Red_light);//关闭前的延迟
P1DIR = 0x00;

P2DIR = P2DIR | 0x02;//绿灯
P2OUT &= 0xFE;//P2.0至逻辑零
P2OUT |= 0x02;//P2.1至逻辑一
P2OUT &= 0xFB;//P2.2至逻辑零
_DELAY_CYCLES (Green_light);//延迟

P2DIR |= 0x04;//蓝色
P2OUT &= 0xFE;//P2.0至逻辑零//蓝光
P2OUT &= 0xFD;//P2.1至逻辑零
P2OUT |= 0x04;//P2.2至逻辑一
_DELAY_CYCLES (Blue_light);//关闭前的延迟
P2DIR = 0x00;

}
}

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    _ 的 delay_cycles ()存在问题;函数中说,即使我为它提供整数

    ,它也会期望一个整数常量,如  __delay_cycle 内在函数的文档中所述:

    [报价]延迟的周期数必须是编译时常量。[/报价]

    而 Red_light、 Green_light 和 Blue_light 不是编译时常量、因为它们取决于运行时读取的 ID 值。