以下代码出自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);
- }
|