如何在单片机中使用<math.h>中的函数
<p>我为RA6M5编写程序,在程序中使用了sin(x)函数,但是无论我怎么努力都无法使用</p><p><span style="font-size:22px;"><strong>情况一、</strong></span></p>
<p>使用sin函数得到'sin' has unknown return type; cast the call to its declared return type的错误。但是程序可以顺利编译和执行。</p>
<p></p>
<p>相关代码如下:</p>
<pre>
<code class="language-cpp">/*
* 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;
/*******************************************************************************************************************//**
* @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;
doublecycle = 0;
doubledac_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);
}
}
</code></pre>
<p> 但是,cyclePien的数值是正确的。(下面会说)</p>
<p> </p>
<p> 程序无法正确执行。</p>
<p><span style="font-size:22px;"><strong>情况二、</strong></span></p>
<p>使用keil进行编译,则非常离谱,cyclePien的变量居然是错误的,而且无论怎么定义,都不可以。cycle始终为0,cycle==0;</p>
<p> 相关代码如下:</p>
<pre>
<code class="language-cpp">/*
* 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 = {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);
/*******************************************************************************************************************//**
* @briefBlinky 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;
floatcycle = 0.0;
floatdac_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;
// /* 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);
}
}
</code></pre>
<p> </p>
<p>而我即时修改了,相关的配置也不可以。无论怎么修改都不正确!</p>
<p> </p>
<p>太过邪门,请大家解惑!!!</p>
<p>确实邪门。。。。。。。。。。。。不应该的呀。。。。。。。。。<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/smile.gif" width="48" /></p>
<p>链接加-lm试试</p>
freebsder 发表于 2024-12-17 17:22
链接加-lm试试
<p>这个方法我已经试过了,但是不管事,而且据说是位置不对</p>
<pre>
<code class="language-makefile"># 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"
)
</code></pre>
<p> </p>
bigbat 发表于 2024-12-18 09:14
这个方法我已经试过了,但是不管事,而且据说是位置不对
# This file was automatically generated...
<p>你用的keil还是cmake?</p>
freebsder 发表于 2024-12-18 19:53
你用的keil还是cmake?
<p>我用的是vscode+cmake的组合,由于没有成功,就由使用了keil,有说:</p>
<p>(1)keil 需要 Micro LIB。</p>
<p>(2)cmake需要增加 -lm 编译链接参数。</p>
<p>但是都不行!!!</p>
<p> </p>
<div class='shownolgin' data-isdigest='no'><p>你看一下 -T;script/fsp.ld ,把要链接的库可以放这里面。</p>
</div><script>showreplylogin();</script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <div class='shownolgin' data-isdigest='no'>freebsder 发表于 2024-12-20 17:10
你看一下 -T;script/fsp.ld ,把要链接的库可以放这里面。
<p>谢谢,我使用DSP库可以完成了,速度还快。主要是想了解这种编译方式。</p>
</div><script>showreplylogin();</script>
页:
[1]