You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CCS 5.5.0中代码出现#29 expected an expression错误的原因

CCS 5.5.0中Goertzel算法代码的for循环语法错误问题

我是C语言初学者,正在实现Goertzel算法以检测data.bin文件中的8种频率。在CCS 5.5.0环境下,代码里的两个for循环起始行均出现#29 "expected an expression"错误。代码尚未完成,我怀疑是语法问题但无法定位,用VSCode打开未发现问题,相关代码如下:

/*
 *  ======== gtz.c ========
 */

#include <xdc/std.h>
#include <xdc/runtime/System.h>

#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Clock.h>
#include <ti/sysbios/knl/Task.h>

#include <xdc/runtime/Types.h>
#include <xdc/runtime/Timestamp.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "gtz.h"

void clk_SWI_Read_Data(UArg arg0);
void clk_SWI_GTZ_All_Freq(UArg arg0);

extern void task0_dtmfGen(void);
extern void task1_dtmfDetect(void);

extern int sample, tdiff, tdiff_final, gtz_out[8];
extern short coef[8];
extern int flag;

short data[NO_OF_SAMPLES];
short *buffer;
static int i;

/*
 *  ======== main ========
 */
int main() {
    System_printf("\n System Start\n");
    System_flush();

    /* Read binary data file */
    FILE* fp = fopen("../data.bin", "rb");
    if(fp==0) {
        System_printf("Error: Data file not found\n");
        System_flush();
        return 1;
    }
    fread(data, 2, NO_OF_SAMPLES, fp);
    buffer = (short*)malloc(2*8*10000);

    /* Create a Clock Instance */
    Clock_Params clkParams;

    /* Initialise Clock Instance with time period and timeout (system ticks) */
    Clock_Params_init(&clkParams);
    clkParams.period = 1;
    clkParams.startFlag = TRUE;

    /* Instantiate ISR for tone generation  */
    Clock_create(clk_SWI_Read_Data, TIMEOUT, &clkParams, NULL);

    /* Instantiate 8 parallel ISRs for each of the eight Goertzel coefficients */
    Clock_create(clk_SWI_GTZ_All_Freq, TIMEOUT, &clkParams, NULL);

    /* Start SYS_BIOS */
    BIOS_start();
}

/*
 *  ====== clk_SWI_Generate_DTMF =====
 *  Dual-Tone Generation
 *  ==================================
 */
void clk_SWI_Read_Data(UArg arg0) {
    static int tick;
    tick = Clock_getTicks();
    sample = data[tick%NO_OF_SAMPLES];
}

/*
 *  ====== clk_SWI_GTZ =====
 *  gtz sweep
 *  ========================
 */
void clk_SWI_GTZ_All_Freq(UArg arg0) {
    // define variables for timing
    static int start, stop;

    // define feedback times as N
    static int N = 0;

    //Record start time
    start = Timestamp_get32();

    short input = (short) (sample);

    //static int number_bits_used;
    //static float normalized_value_coef1;

    int Goertzel_values[8] = {0,0,0,0,0,0,0,0};
    short delay_values[8] = {0,0,0,0,0,0,0,0};
    short delay_1_values[8] = {0,0,0,0,0,0,0,0};
    short delay_2_values[8] = {0,0,0,0,0,0,0,0};
    short prod1_values[8] = {0,0,0,0,0,0,0,0};
    short prod2_values[8] = {0,0,0,0,0,0,0,0};
    short prod3_values[8] = {0,0,0,0,0,0,0,0};

    int R_in = sample;
    input = (short) R_in;
    input = input>>4;

    input = (short)sample;
    input = input>>4;

    /*
     * TODO 1. Complete the feedback loop of the GTZ algorithm */
    /* ========================= */

    for (int i=0; i<8; i++){ // **Where the error appears**
        prod1_values[i] = (delay_1_values[i] * coef[i]) >> 14;
        delay_values[i] = input + (short) prod1_values[i] - delay_2_values[i];
        delay_2_values[i] = delay_1_values[i];
        delay_1_values[i] = delay_values[i];
    }
    N++;
    /* ========================= */

    //Record stop time
    stop = Timestamp_get32();
    //Record elapsed time
    tdiff = stop-start;

    if (N == 206) {
        //Record start time
        start = Timestamp_get32();

        /* TODO 2. Complete the feedforward loop of the GTZ algorithm*/
        /* ========================= */
        for (int i=0; i<8; i++){ 
            prod1_values[i] = (delay_1_values[i] * delay_1_values[i]) >> 14;
            prod2_values[i] = (delay_2_values[i] * delay_2_values[i]) >> 14;
            prod3_values[i] = (delay_1_values[i] * coef[i]) >> 14;
            prod3_values[i] = prod3_values[i] * delay_2_values[i];

            Goertzel_values[i] =prod1_values[i]+prod2_values[i] - prod3_values[i];
            Goertzel_values[i] <<= 4;
            gtz_out[i] = Goertzel_values[i];
        }
        /* gtz_out[..] = ... */
        /* gtz_out[..] = ... */
        /* ========================= */
        flag = 1;
        N = 0;

        //Record stop time
        stop = Timestamp_get32();
        //Record elapsed time
        tdiff_final = stop-start;
    }
}

问题原因

CCS 5.5.0默认使用的编译器遵循C89标准,而C89不允许在for循环的初始化部分声明变量(即for (int i=0; ...)这种写法)。VSCode默认采用C99或更高标准,所以不会检测到这个语法问题。

解决方案

方案1:提前声明循环变量

在函数clk_SWI_GTZ_All_Freq的开头提前声明int i;,然后把两个for循环里的int去掉:

void clk_SWI_GTZ_All_Freq(UArg arg0) {
    // 新增:提前声明循环变量
    int i;
    // 原有的其他变量声明...

    // 修改第一个for循环
    for (i=0; i<8; i++){ 
        prod1_values[i] = (delay_1_values[i] * coef[i]) >> 14;
        delay_values[i] = input + (short) prod1_values[i] - delay_2_values[i];
        delay_2_values[i] = delay_1_values[i];
        delay_1_values[i] = delay_values[i];
    }

    // ...

    // 修改第二个for循环
    for (i=0; i<8; i++){ 
        prod1_values[i] = (delay_1_values[i] * delay_1_values[i]) >> 14;
        prod2_values[i] = (delay_2_values[i] * delay_2_values[i]) >> 14;
        prod3_values[i] = (delay_1_values[i] * coef[i]) >> 14;
        prod3_values[i] = prod3_values[i] * delay_2_values[i];

        Goertzel_values[i] =prod1_values[i]+prod2_values[i] - prod3_values[i];
        Goertzel_values[i] <<= 4;
        gtz_out[i] = Goertzel_values[i];
    }
}

方案2:开启C99标准支持

在CCS中调整编译选项,让编译器支持C99:

  1. 右键项目 -> Properties
  2. 进入Build -> ARM Compiler -> Code Generation
  3. 将"C dialect"设置为ISO C99 (-std=c99)
  4. 应用并重新编译项目

内容的提问来源于stack exchange,提问作者Charles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 05:24:56