[技术问答] 发现空函数无法通过指针切换回来了

[复制链接]
 楼主| mintspring 发表于 2021-7-24 23:38 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. #include "NuMicro.h"



  3. void SYS_Init(void)
  4. {

  5.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  6.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
  7.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;

  8.     /*---------------------------------------------------------------------------------------------------------*/
  9.     /* Init System Clock                                                                                       */
  10.     /*---------------------------------------------------------------------------------------------------------*/

  11.     /* Enable HIRC clock */
  12.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  13.     /* Wait for HIRC clock ready */
  14.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  15.     /* Select HCLK clock source as HIRC and HCLK clock divider as 1 */
  16.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  17.     /* Enable HXT clock */
  18.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  19.     /* Wait for HXT clock ready */
  20.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  21.     /* Enable PLL */
  22.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  23.     /* Waiting for PLL stable */
  24.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  25.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  26.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  27.     /* Enable UART module clock */
  28.     CLK_EnableModuleClock(UART0_MODULE);

  29.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  30.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  31.     /*---------------------------------------------------------------------------------------------------------*/
  32.     /* Init I/O Multi-function                                                                                 */
  33.     /*---------------------------------------------------------------------------------------------------------*/

  34.     /* Set multi-function pins for UART0 RXD and TXD */
  35.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  36. }

  37. void UART0_Init(void)
  38. {
  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init UART                                                                                               */
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Reset UART0 */
  43.     SYS_ResetModule(UART0_RST);

  44.     /* Configure UART0 and set UART0 baud rate */
  45.     UART_Open(UART0, 115200);
  46. }





  47. void blink1(void)
  48. {
  49.                 static int i=0;
  50.           i++;
  51.         CLK_SysTickDelay(1000000);
  52.         printf("%d\n",i);
  53. }

  54. void blink2(void)
  55. {
  56.                 CLK_SysTickDelay(500000);
  57.                 GPIO_TOGGLE(PB10);
  58. }


  59. void blink3(void)
  60. {
  61.                 CLK_SysTickDelay(100000);
  62.                 GPIO_TOGGLE(PB10);
  63. }

  64. /*---------------------------------------------------------------------------------------------------------*/
  65. /*  Main Function                                                                                          */
  66. /*---------------------------------------------------------------------------------------------------------*/
  67. int32_t main(void)
  68. {

  69.                 void (*blink)(void);
  70.                 blink=blink1;


  71.     /* Unlock protected registers */
  72.     SYS_UnlockReg();

  73.     /* Init System, peripheral clock and multi-function I/O */
  74.     SYS_Init();

  75.     /* Lock protected registers */
  76.     SYS_LockReg();

  77.     /* Init UART0 for printf */
  78.     UART0_Init();

  79.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  80.     printf("+-------------------------------------------------+\n");
  81.     printf("|    PB.10(Output) and PG.4(Input) Sample Code     |\n");
  82.     printf("+-------------------------------------------------+\n\n");

  83.     /*-----------------------------------------------------------------------------------------------------*/
  84.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  85.     /*-----------------------------------------------------------------------------------------------------*/
  86.     printf("  >> LED at PB.10 ; Button1 at PG.4 \n");


  87.     /* Configure PB.10 as Output mode and PG.4 as Input mode then close it */
  88.     GPIO_SetMode(PB, BIT10, GPIO_MODE_OUTPUT);
  89.     GPIO_SetMode(PG, BIT4,  GPIO_MODE_INPUT);
  90.     PB10 = 1;

  91.     printf("GPIO PB.10(output mode) and PD.7(input mode) ......");
  92.                
  93.                
  94.         while(1)
  95.         {
  96.                 blink();
  97.                
  98.                 if(PG4==0)
  99.                         {
  100.                                 CLK_SysTickDelay(50);
  101.                                 while(PG4==0){}
  102.                                         {
  103.                                                 if (blink==blink1) blink=blink2;
  104.                                                 else if(blink==blink2) blink=blink3;
  105.                                                 else if(blink==blink3) blink=blink1;
  106.                                        
  107.                                         }

  108.                         }
  109.         }


  110. }

初始化的时候我让blink指向了blink1,但是如果我删除blink1中的内容,那么按键操作后发现无法切换回什么都不做的blink1,如果我给blink1填充一些内容,比如上面代码中打印一些编号。
就可以切换回来,好奇怪啊。
 楼主| mintspring 发表于 2021-7-24 23:40 | 显示全部楼层
第一次进行这种测试,不知道是不是优化掉了。
 楼主| mintspring 发表于 2021-7-24 23:49 | 显示全部楼层
明白为什么了,因为直接就判断第一条了,所以在调试单步模式正常,正常运行不正常。。。因为执行完第三条,这个时候就满足了第一条,直接跳过了。
 楼主| mintspring 发表于 2021-7-24 23:50 | 显示全部楼层
而如果有语句,会先进子函数,所以会正常。
 楼主| mintspring 发表于 2021-7-24 23:59 | 显示全部楼层
所以最后要搞一个跳出循环的应该就可以了。

  1. #include <stdio.h>
  2. #include "NuMicro.h"



  3. void SYS_Init(void)
  4. {

  5.     /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
  6.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
  7.     SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;

  8.     /*---------------------------------------------------------------------------------------------------------*/
  9.     /* Init System Clock                                                                                       */
  10.     /*---------------------------------------------------------------------------------------------------------*/

  11.     /* Enable HIRC clock */
  12.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  13.     /* Wait for HIRC clock ready */
  14.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  15.     /* Select HCLK clock source as HIRC and HCLK clock divider as 1 */
  16.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  17.     /* Enable HXT clock */
  18.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  19.     /* Wait for HXT clock ready */
  20.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  21.     /* Enable PLL */
  22.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  23.     /* Waiting for PLL stable */
  24.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  25.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  26.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  27.     /* Enable UART module clock */
  28.     CLK_EnableModuleClock(UART0_MODULE);

  29.     /* Select UART module clock source as HIRC and UART module clock divider as 1 */
  30.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  31.     /*---------------------------------------------------------------------------------------------------------*/
  32.     /* Init I/O Multi-function                                                                                 */
  33.     /*---------------------------------------------------------------------------------------------------------*/

  34.     /* Set multi-function pins for UART0 RXD and TXD */
  35.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  36. }

  37. void UART0_Init(void)
  38. {
  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init UART                                                                                               */
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Reset UART0 */
  43.     SYS_ResetModule(UART0_RST);

  44.     /* Configure UART0 and set UART0 baud rate */
  45.     UART_Open(UART0, 115200);
  46. }





  47. void blink1(void)
  48. {
  49.         PB10=1;
  50. }

  51. void blink2(void)
  52. {
  53.                 CLK_SysTickDelay(500000);
  54.                 GPIO_TOGGLE(PB10);
  55. }


  56. void blink3(void)
  57. {
  58.                 CLK_SysTickDelay(100000);
  59.                 GPIO_TOGGLE(PB10);
  60. }

  61. /*---------------------------------------------------------------------------------------------------------*/
  62. /*  Main Function                                                                                          */
  63. /*---------------------------------------------------------------------------------------------------------*/
  64. int32_t main(void)
  65. {

  66.                 void (*blink)(void);
  67.                 blink=blink1;


  68.     /* Unlock protected registers */
  69.     SYS_UnlockReg();

  70.     /* Init System, peripheral clock and multi-function I/O */
  71.     SYS_Init();

  72.     /* Lock protected registers */
  73.     SYS_LockReg();

  74.     /* Init UART0 for printf */
  75.     UART0_Init();

  76.     printf("\n\nCPU @ %dHz\n", SystemCoreClock);

  77.     printf("+-------------------------------------------------+\n");
  78.     printf("|    PB.10(Output) and PG.4(Input) Sample Code     |\n");
  79.     printf("+-------------------------------------------------+\n\n");

  80.     /*-----------------------------------------------------------------------------------------------------*/
  81.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  82.     /*-----------------------------------------------------------------------------------------------------*/
  83.     printf("  >> LED at PB.10 ; Button1 at PG.4 \n");


  84.     /* Configure PB.10 as Output mode and PG.4 as Input mode then close it */
  85.     GPIO_SetMode(PB, BIT10, GPIO_MODE_OUTPUT);
  86.     GPIO_SetMode(PG, BIT4,  GPIO_MODE_INPUT);
  87.     PB10 = 1;

  88.     printf("GPIO PB.10(output mode) and PD.7(input mode) ......");
  89.                
  90.                
  91.         while(1)
  92.         {
  93.                 blink();
  94.                
  95.                 if(PG4==0)
  96.                         {
  97.                                 CLK_SysTickDelay(50);
  98.                                 while(PG4==0)
  99.                                         {
  100.                                                 if(PG4==1)
  101.                                                 {
  102.                                                         if (blink==blink1) blink=blink2;
  103.                                                         else if(blink==blink2) blink=blink3;
  104.                                                         else if(blink==blink3) blink=blink1;       
  105.                                                 }
  106.                                
  107.                                         }

  108.                         }
  109.         }


  110. }

huahuagg 发表于 2021-7-25 15:36 | 显示全部楼层
不知道啊,有时候就是你调试时一步一步正常,但是正常执行时候不正常。
sadicy 发表于 2021-8-11 10:47 | 显示全部楼层
没明白,楼主自己都解决了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

302

主题

4962

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部