打印
[单片机芯片]

我让chatGPT帮我优化代码,跟我想的一样

[复制链接]
333|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
以下代码出自CH32V103的库函数,此函数为实现5个不同分支的if elseif else 结构的判断工作,由于判断对象都是常量,因此我认为可以使用switch case 优化这些操作,以便看着更加简洁清爽。
于是我让chatGPT帮我完成这个事情。
我的提问截图

原始内容
void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
{
    uint8_t tmppre = 0;

    if(NVIC_Priority_Group == NVIC_PriorityGroup_0)
    {
        NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4);
    }
    else if(NVIC_Priority_Group == NVIC_PriorityGroup_1)
    {
        if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority == 1)
        {
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
        }
        else
        {
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
        }
    }
    else if(NVIC_Priority_Group == NVIC_PriorityGroup_2)
    {
        if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 1)
        {
            tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (4 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (tmppre << 4));
        }
        else
        {
            tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (4 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 2));
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (tmppre << 4));
        }
    }
    else if(NVIC_Priority_Group == NVIC_PriorityGroup_3)
    {
        if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 3)
        {
            tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (2 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (tmppre << 4));
        }
        else
        {
            tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (2 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 4));
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (tmppre << 4));
        }
    }
    else if(NVIC_Priority_Group == NVIC_PriorityGroup_4)
    {
        NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << 4);
    }

    if(NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
    {
        NVIC_EnableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
    }
    else
    {
        NVIC_DisableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
    }
}

chatGPT优化后的
void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
{
    uint8_t tmppre = 0;
   
    switch(NVIC_Priority_Group)
    {
        case NVIC_PriorityGroup_0:
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                   NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4);
            break;
        case NVIC_PriorityGroup_1:
            if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority == 1)
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (1 << 7) |
                        (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
            else
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (0 << 7) |
                        (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
            break;
        case NVIC_PriorityGroup_2:
            if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 1)
            {
                tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority +
                      (4 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (0 << 7) | (tmppre << 4));
            }
            else
            {
                tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority +
                       (4 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 2));
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (1 << 7) | (tmppre << 4));
            }
            break;
        case NVIC_PriorityGroup_3:
            if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 3)
            {
                tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority +
                       (2 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (0 << 7) | (tmppre << 4));
            }
            else
            {
                tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority +
                       (2 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 4));
                NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                        (1 << 7) | (tmppre << 4));
            }
            break;
        case NVIC_PriorityGroup_4:
            NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel,
                    NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << 4);
            break;
    }
   
    if(NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
        NVIC_EnableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
    else
        NVIC_DisableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:西安公路研究院南京院
简介:主要工作从事监控网络与通信网络设计,以及从事基于嵌入式的通信与控制设备研发。擅长单片机嵌入式系统物联网设备开发,音频功放电路开发。

1896

主题

15631

帖子

198

粉丝