查看: 853|回复: 2

变量加法异常,加的数会加双倍,比如+1变+2,+2变+4

[复制链接]

1

主题

1

帖子

49

积分

新手上路

Rank: 1

积分
49
发表于 2022-4-21 23:38:29 | 显示全部楼层 |阅读模式
调试SPI通讯时发现一个奇怪的现象,加法加的数会自动加双倍,比如a+1输出的结果值是a+2的值.例如下方图中buf每次递增1,cmd[0]是buf,cmd[1]是buf+1,cmd[2]是buf+2,每次输出cmd数组,
用的是e2studio,只操作hal_entry.c文件,hal_entry.c里相关程序也贴在后面了,
整个项目打包在附件里了[url=]05_SPI.rar[/url]


#include "hal_data.h"
volatile unsigned char t10ms_cnt = 0;
volatile unsigned char buf = 0;
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
void timer0_callback(timer_callback_args_t *p_args)
{
    if(p_args->event == TIMER_EVENT_CYCLE_END)
        t10ms_cnt ++;
    if(t10ms_cnt%2)
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_LOW);
    else
        R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_04_PIN_00, BSP_IO_LEVEL_HIGH);
}

volatile bool g_transfer_complete = false;
void spi_callback (spi_callback_args_t * p_args)
{
    if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
    {
        g_transfer_complete = true;
    }
}

/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
    unsigned char cmd[3];
    /* TODO: add your own code here */
    R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);  //初始化定时器模块
    R_GPT_Start(&g_timer0_ctrl); //启动GPT定时器
    R_SPI_Open(&g_spi0_ctrl, &g_spi0_cfg);  //开启SPI
    while(1)
    {
        if(t10ms_cnt>=100)
        {
            t10ms_cnt = 0;
            buf++;
            //         SPI模块        数据      长度      传输位数(8bit,16bit,32bit)
            cmd[0] = buf;
            cmd[1] = (unsigned char)(buf+1);
            cmd[2] = (unsigned char)(buf+2);
            R_SPI_Write(&g_spi0_ctrl, cmd, 3, SPI_BIT_WIDTH_8_BITS);

        }
    }

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

/*******************************************************************************************************************//**
* This function is called at various points during the startup process.  This implementation uses the event that is
* called right before main() to set up the pins.
*
* @param[in]  event    Where at in the start up process the code is currently at
**********************************************************************************************************************/
void R_BSP_WarmStart(bsp_warm_start_event_t event)
{
    if (BSP_WARM_START_RESET == event)
    {
#if BSP_FEATURE_FLASH_LP_VERSION != 0

        /* Enable reading from data flash. */
        R_FACI_LP->DFLCTL = 1U;

        /* Would normally have to wait tDSTOP(6us) for data flash recovery. Placing the enable here, before clock and
         * C runtime initialization, should negate the need for a delay since the initialization will typically take more than 6us. */
#endif
    }

    if (BSP_WARM_START_POST_C == event)
    {
        /* C runtime environment and system clocks are setup. */

        /* Configure pins. */
        R_IOPORT_Open (&g_ioport_ctrl, g_ioport.p_cfg);
    }
}

#if BSP_TZ_SECURE_BUILD

BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ();

/* Trustzone Secure Projects require at least one nonsecure callable function in order to build (Remove this if it is not required to build). */
BSP_CMSE_NONSECURE_ENTRY void template_nonsecure_callable ()
{

}
#endif


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

0

主题

1

帖子

64

积分

注册会员

Rank: 2

积分
64
发表于 2022-4-25 09:49:52 | 显示全部楼层
外设接上了吗,把外设接上试试,不接上可能会有问题
回复

使用道具 举报

3

主题

15

帖子

2519

积分

金牌会员

Rank: 6Rank: 6

积分
2519
发表于 2022-5-5 09:05:07 | 显示全部楼层
在线debug看一下
回复

使用道具 举报

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

本版积分规则

用户排行榜

RA助手

主题: 116帖子:134精华:0

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 45帖子:134精华:0

xujiwei263

主题: 16帖子:73精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 9帖子:95精华:0
快速回复 返回顶部 返回列表