261|7

2922

帖子

4

TA的资源

五彩晶圆(中级)

楼主
 

如何在单片机中使用<math.h>中的函数 [复制链接]

我为RA6M5编写程序,在程序中使用了sin(x)函数,但是无论我怎么努力都无法使用

情况一、

使用sin函数得到'sin' has unknown return type; cast the call to its declared return type的错误。但是程序可以顺利编译和执行。

相关代码如下:

/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "common_data.h"
#include "fsp_common_api.h"
#include "hal_data.h"
#include "r_icu.h"

#include "app_print.h"
#include <stdbool.h>

#include <math.h>
#include <stdint.h>

#define APP_ERR_TRAP()   __asm("BKPT #0\n")
#define PI   3.1415926


void R_BSP_WarmStart(bsp_warm_start_event_t event);

extern bsp_leds_t g_bsp_leds;

volatile bool g_sw1_press = false;
volatile bool g_sw2_press = false;

volatile bool g_dac_sw = false;

/*******************************************************************************************************************//**
 * [url=home.php?mod=space&uid=159083]@brief[/url] Blinky example application
 *
 * Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
 *
 **********************************************************************************************************************/
void hal_entry (void)
{
#if BSP_TZ_SECURE_BUILD

    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif

    fsp_err_t err                           = FSP_SUCCESS;
    double  cycle = 0;
    double  dac_value = 0;
    uint16_t freq = 0;
    uint16_t output = 0;
    /* Define the units to be used with the software delay function */
    const bsp_delay_units_t bsp_delay_units = BSP_DELAY_UNITS_MILLISECONDS;
    const double cyclePien = (2 * PI)/1000;

    /* Set the blink frequency (must be <= bsp_delay_units */
    const uint32_t freq_in_hz = 2;

    /* Calculate the delay in terms of bsp_delay_units */
    const uint32_t delay = bsp_delay_units / freq_in_hz;

    /* LED type structure */
    bsp_leds_t leds = g_bsp_leds;

    /* If this board has no LEDs then trap here */
    if (0 == leds.led_count)
    {
        while (1)
        {
            ;                          // There are no LEDs on this board
        }
    }

    /* Holds level to set for pins */
    bsp_io_level_t pin_level = BSP_IO_LEVEL_LOW;

    err = R_SCI_UART_Open(&g_uart7_ctrl, &g_uart7_cfg);

    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }

    /* Initialize External IRQ driver*/
    err = R_ICU_ExternalIrqOpen(&g_external_irq9_ctrl, &g_external_irq9_cfg);

    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }
    

    err = R_ICU_ExternalIrqEnable(&g_external_irq9_ctrl);

    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }


    /* Initialize External IRQ driver*/
    err = R_ICU_ExternalIrqOpen(&g_external_irq10_ctrl, &g_external_irq10_cfg);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }
        
    err = R_ICU_ExternalIrqEnable(&g_external_irq10_ctrl);

    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }


    err = R_DAC_Open (&g_dac0_ctrl, &g_dac0_cfg);	

	if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }

    while (1)
    {
        
        if(g_sw1_press)
        {
            freq = (uint16_t) ( freq++ )%10;
            g_sw1_press = false;
            APP_ERR_PRINT("Freg Add\r\n");
        }

        if(g_sw2_press)
        {
            g_dac_sw = !g_dac_sw;

            if(g_dac_sw)
            {
                 APP_ERR_PRINT("OUT ON\r\n");
            }
            else
            {
                APP_ERR_PRINT("OUT OFF\r\n");
            }
             g_sw2_press = false;

        }

        cycle = cycle + cyclePien/(freq+1);

        if (cycle >= (2 * PI))
        {
            cycle = 0;
        }
        
        dac_value = (sin(cycle) + 1)  * 2047;

        if(!g_dac_sw)
        {
            dac_value = 0;
        }
        output = (uint16_t) dac_value;

        err = R_DAC_Write (&g_dac0_ctrl, output);
	    err = R_DAC_Start (&g_dac0_ctrl);    R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);

        /* Delay */
        R_BSP_SoftwareDelay(delay, bsp_delay_units);
    }
}

 但是,cyclePien的数值是正确的。(下面会说)

 

  程序无法正确执行。

情况二、

使用keil进行编译,则非常离谱,cyclePien的变量居然是错误的,而且无论怎么定义,都不可以。cycle始终为0,cycle==0;

  相关代码如下:

/*
* Copyright (c) 2020 - 2024 Renesas Electronics Corporation and/or its affiliates
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "hal_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>

#define APP_ERR_TRAP()   __asm("BKPT #0\n")

#define PI   3.1415926

void R_BSP_WarmStart(bsp_warm_start_event_t event);

extern bsp_leds_t g_bsp_leds;

volatile bool g_sw1_press = false;
volatile bool g_sw2_press = false;

volatile bool g_dac_sw = false;

/* Macro definition */
#define CARRIAGE_ASCII            (13u)     /* Carriage return */
#define ZERO_ASCII                (48u)     /* ASCII value of zero */
#define NINE_ASCII                (57u)     /* ASCII value for nine */
#define DATA_LENGTH               (4u)      /* Expected Input Data length */
#define UART_ERROR_EVENTS         (UART_EVENT_BREAK_DETECT | UART_EVENT_ERR_OVERFLOW | UART_EVENT_ERR_FRAMING | \
                                    UART_EVENT_ERR_PARITY)    /* UART Error event bits mapped in registers */
#define RESET_VALUE               (0x00)


/* Flag for user callback */
static volatile uint8_t g_uart_event = RESET_VALUE;
static uint8_t g_temp_buffer[DATA_LENGTH] = {RESET_VALUE};
/* Counter to update g_temp_buffer index */
static volatile uint8_t g_counter_var = RESET_VALUE;
/* Flag to check whether data is received or not */
static volatile uint8_t g_data_received_flag = false;

int APP_PRINT( const char * sFormat, ... );
int v_uart( const char * sFormat, va_list * pParamList);
/*******************************************************************************************************************//**
 * @brief  Blinky example application
 *
 * Blinks all leds at a rate of 1 second using the software delay function provided by the BSP.
 *
 **********************************************************************************************************************/
void hal_entry (void)
{
#if BSP_TZ_SECURE_BUILD

    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif

    fsp_err_t err = FSP_SUCCESS;
    float  cycle = 0.0;
    float  dac_value = 0.0;
    uint16_t freq = 0;
    uint16_t output = 0;
	
    const float cyclePien = (2 * PI)/1000;
  
	  //const float cyclePien = 0.0062831852f;
	
    /* Define the units to be used with the software delay function */
    const bsp_delay_units_t bsp_delay_units = BSP_DELAY_UNITS_MILLISECONDS;

    /* Set the blink frequency (must be <= bsp_delay_units */
    const uint32_t freq_in_hz = 2;

    /* Calculate the delay in terms of bsp_delay_units */
    const uint32_t delay = bsp_delay_units / freq_in_hz;

    /* LED type structure */
    bsp_leds_t leds = g_bsp_leds;

    /* If this board has no LEDs then trap here */
    if (0 == leds.led_count)
    {
        while (1)
        {
            ;                          // There are no LEDs on this board
        }
    }

    /* Holds level to set for pins */
    bsp_io_level_t pin_level = BSP_IO_LEVEL_LOW;

		err = R_SCI_UART_Open(&g_uart7_ctrl, &g_uart7_cfg);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }

    /* Initialize External IRQ driver*/
    err = R_ICU_ExternalIrqOpen(&g_external_irq9_ctrl, &g_external_irq9_cfg);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }
    

    err = R_ICU_ExternalIrqEnable(&g_external_irq9_ctrl);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }


    /* Initialize External IRQ driver*/
    err = R_ICU_ExternalIrqOpen(&g_external_irq10_ctrl, &g_external_irq10_cfg);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }
        
    err = R_ICU_ExternalIrqEnable(&g_external_irq10_ctrl);
    if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }


    err = R_DAC_Open (&g_dac0_ctrl, &g_dac0_cfg);	
	  if(FSP_SUCCESS != err)
    {
        APP_ERR_TRAP();
    }

		
    while (1)
    {
        /* Enable access to the PFS registers. If using r_ioport module then register protection is automatically
         * handled. This code uses BSP IO functions to show how it is used.
         */
//        R_BSP_PinAccessEnable();

//        /* Update all board LEDs */
//        for (uint32_t i = 0; i < leds.led_count; i++)
//        {
//            /* Get pin to toggle */
//            uint32_t pin = leds.p_leds[i];

//            /* Write to this pin */
//            R_BSP_PinWrite((bsp_io_port_pin_t) pin, pin_level);
//        }

//        /* Protect PFS registers */
//        R_BSP_PinAccessDisable();

//        /* Toggle level for next write */
//        if (BSP_IO_LEVEL_LOW == pin_level)
//        {
//            pin_level = BSP_IO_LEVEL_HIGH;
//        }
//        else
//        {
//            pin_level = BSP_IO_LEVEL_LOW;
//        }

        if(g_sw1_press)
        {
            freq = (uint16_t)(( freq++ )%10);
            g_sw1_press = false;
            APP_PRINT("Freg Add\r\n");
        }

        if(g_sw2_press)
        {
            g_dac_sw = !g_dac_sw;

            if(g_dac_sw)
            {
                 APP_PRINT("OUT ON\r\n");
            }
            else
            {
                APP_PRINT("OUT OFF\r\n");
            }
             g_sw2_press = false;

        }

        cycle += cyclePien/(freq+1);

        if (cycle >= (2 * PI))
        {
            cycle = 0;
        }
        
        dac_value = (sin(cycle) + 1)  * 2047;

        if(!g_dac_sw)
        {
            dac_value = 0;
        }
        output = (uint16_t) dac_value;

        err = R_DAC_Write (&g_dac0_ctrl, output);
	      err = R_DAC_Start (&g_dac0_ctrl);    R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS);

				
				/* Delay */
        R_BSP_SoftwareDelay(1, bsp_delay_units);
    }
}

 

而我即时修改了,相关的配置也不可以。无论怎么修改都不正确!

 

太过邪门,请大家解惑!!!

此帖出自编程基础论坛

最新回复

你看一下 -T;script/fsp.ld ,把要链接的库可以放这里面。   详情 回复 发表于 前天 17:10
点赞 关注
 

回复
举报

1259

帖子

1

TA的资源

五彩晶圆(初级)

沙发
 

确实邪门。。。。。。。。。。。。不应该的呀。。。。。。。。。

此帖出自编程基础论坛
 
 
 

回复

7534

帖子

2

TA的资源

五彩晶圆(高级)

板凳
 

链接加-lm试试

此帖出自编程基础论坛

点评

这个方法我已经试过了,但是不管事,而且据说是位置不对 # This file was automatically generated by RASC and should not be edited by the user. # Use CMakeLists.txt to override the settings in this f  详情 回复 发表于 4 天前
 
个人签名

默认摸鱼,再摸鱼。2022、9、28

 
 

回复

2922

帖子

4

TA的资源

五彩晶圆(中级)

4
 

这个方法我已经试过了,但是不管事,而且据说是位置不对

# This file was automatically generated by RASC and should not be edited by the user.
# Use CMakeLists.txt to override the settings in this file 

set(RASC_TARGET_DEVICE R7FA6M5BH)
set(RASC_TARGET_ARCH cortex-m33)
set(RASC_PROJECT_NAME FSP_DAC_EXT)
SET(RASC_TOOLCHAIN_NAME GCC)

SET(RASC_CMAKE_ASM_FLAGS "-mfloat-abi=hard;-mcpu=cortex-m33;-mfpu=fpv5-sp-d16;-Wunused;-Wuninitialized;-Wall;-Wextra;-Wmissing-declarations;-Wconversion;-Wpointer-arith;-Wshadow;-Wlogical-op;-Waggregate-return;-Wfloat-equal;-fmessage-length=0;-fsigned-char;-ffunction-sections;-fdata-sections;-mthumb;-x;assembler-with-cpp;-MMD;-MP")
SET(RASC_CMAKE_C_FLAGS "-mfloat-abi=hard;-mcpu=cortex-m33;-mfpu=fpv5-sp-d16;-Wunused;-Wuninitialized;-Wall;-Wextra;-Wmissing-declarations;-Wconversion;-Wpointer-arith;-Wshadow;-Wlogical-op;-Waggregate-return;-Wfloat-equal;-fmessage-length=0;-fsigned-char;-ffunction-sections;-fdata-sections;-mthumb;-std=c99;-MMD;-MP")
SET(RASC_CMAKE_CXX_FLAGS "-mfloat-abi=hard;-mcpu=cortex-m33;-mfpu=fpv5-sp-d16;-Wunused;-Wuninitialized;-Wall;-Wextra;-Wmissing-declarations;-Wconversion;-Wpointer-arith;-Wshadow;-Wlogical-op;-Waggregate-return;-Wfloat-equal;-fmessage-length=0;-fsigned-char;-ffunction-sections;-fdata-sections;-mthumb;-std=c++11;-MMD;-MP")
SET(RASC_CMAKE_EXE_LINKER_FLAGS "-mfloat-abi=hard;-mcpu=cortex-m33;-mfpu=fpv5-sp-d16;-Wunused;-Wuninitialized;-Wall;-Wextra;-Wmissing-declarations;-Wconversion;-Wpointer-arith;-Wshadow;-Wlogical-op;-Waggregate-return;-Wfloat-equal;-fmessage-length=0;-fsigned-char;-ffunction-sections;-fdata-sections;-mthumb;-T;script/fsp.ld;-Wl,--gc-sections;-Wl,-Map,${PROJECT_NAME}.map;--specs=nano.specs;-lm;-o;${CMAKE_BINARY_DIR}/${PROJECT_NAME}.elf")
SET(RASC_CMAKE_DEFINITIONS "_RA_CORE=CM33;_RA_ORDINAL=1;_RENESAS_RA_")
SET(RASC_ASM_FILES "${CMAKE_CURRENT_SOURCE_DIR}/ra_gen/*.asm")



# ADD COMPILE FLAGS FOR GCC version >= 12.2
if (CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.2)
    list(INSERT RASC_CMAKE_C_FLAGS 0 "--param=min-pagesize=0;-Wno-format-truncation;-Wno-stringop-overflow")
    list(INSERT RASC_CMAKE_CXX_FLAGS 0 "--param=min-pagesize=0;-Wno-format-truncation;-Wno-stringop-overflow")
endif()

SET(RASC_DEBUG_FLAGS "-g;-O2")
SET(RASC_RELEASE_FLAGS "-O2")
SET(RASC_MIN_SIZE_RELEASE_FLAGS "-Os")
SET(RASC_RELEASE_WITH_DEBUG_INFO "-g;-O2")

# Custom targets are defined below
include_guard()

# Create platform-specific command line for running Smart Configurator as a backgrounded
# process, which avoids blocking any calling IDEs
file(TO_NATIVE_PATH "${RASC_EXE_PATH}" RASC_EXE_NATIVE_PATH)
if(CMAKE_HOST_WIN32)
    set(RASC_COMMAND start "" /b "${RASC_EXE_NATIVE_PATH}" configuration.xml)
else()
    set(RASC_COMMAND sh -c \"${RASC_EXE_NATIVE_PATH} configuration.xml &\")
endif()

# Make target for opening the FSP Configuration in Smart Configurator
add_custom_target(rasc
    COMMAND ${RASC_COMMAND}
    WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
    COMMENT "FSP Smart Configurator"
)

 

此帖出自编程基础论坛

点评

你用的keil还是cmake?  详情 回复 发表于 4 天前
 
 
 

回复

7534

帖子

2

TA的资源

五彩晶圆(高级)

5
 
bigbat 发表于 2024-12-18 09:14 这个方法我已经试过了,但是不管事,而且据说是位置不对 # This file was automatically generated ...

你用的keil还是cmake?

此帖出自编程基础论坛

点评

我用的是vscode+cmake的组合,由于没有成功,就由使用了keil,有说: (1)keil 需要 Micro LIB。 (2)cmake需要增加 -lm 编译链接参数。 但是都不行!!!    详情 回复 发表于 3 天前
 
个人签名

默认摸鱼,再摸鱼。2022、9、28

 
 

回复

2922

帖子

4

TA的资源

五彩晶圆(中级)

6
 
freebsder 发表于 2024-12-18 19:53 你用的keil还是cmake?

我用的是vscode+cmake的组合,由于没有成功,就由使用了keil,有说:

(1)keil 需要 Micro LIB。

(2)cmake需要增加 -lm 编译链接参数。

但是都不行!!!

 

此帖出自编程基础论坛
 
 
 

回复

7534

帖子

2

TA的资源

五彩晶圆(高级)

7
 

你看一下 -T;script/fsp.ld ,把要链接的库可以放这里面。

此帖出自编程基础论坛

点评

谢谢,我使用DSP库可以完成了,速度还快。主要是想了解这种编译方式。  详情 回复 发表于 昨天 10:48
 
个人签名

默认摸鱼,再摸鱼。2022、9、28

 
 

回复

2922

帖子

4

TA的资源

五彩晶圆(中级)

8
 
freebsder 发表于 2024-12-20 17:10 你看一下 -T;script/fsp.ld ,把要链接的库可以放这里面。

谢谢,我使用DSP库可以完成了,速度还快。主要是想了解这种编译方式。

此帖出自编程基础论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表