[方案讨论] HK32F030C8T6把OSC_IN和OSC_OUT作为普通的IO口使用

[复制链接]
 楼主| 键盘手没手 发表于 2023-1-12 11:43 | 显示全部楼层 |阅读模式
IO, os
将OSC_IN和OSC_OUT初始化相应的模式
  1.     GPIO_InitStructure.GPIO_Pin = CSB_GPIO_PIN;
  2.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  3.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  4.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  5.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  6.         GPIO_Init(CSB_GPIO_PORT, &GPIO_InitStructure);

  7.         GPIO_InitStructure.GPIO_Pin = RSTB_GPIO_PIN;
  8.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  9.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  10.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  11.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       
  12.         GPIO_Init(RSTB_GPIO_PORT, &GPIO_InitStructure);


 楼主| 键盘手没手 发表于 2023-1-12 11:44 | 显示全部楼层
这样配置完之后发现GPIO输出的高电平电压完全达不到3.3V。原因是我们在初始化systemclock时,把外部晶振也初始化了。所以我们只需要把外部晶振不使能,就可以把OSC_IN和OSC_OUT当作普通的IO口使用了。
 楼主| 键盘手没手 发表于 2023-1-12 11:45 | 显示全部楼层
打开函数 SystemInit();
 楼主| 键盘手没手 发表于 2023-1-12 11:45 | 显示全部楼层
就会看到
  1. void SystemInit (void)
  2. {   
  3.   /* Set HSION bit */
  4.   RCC->CR |= (uint32_t)0x00000001;

  5.   /* Reset SW[1:0], HPRE[3:0], PPRE[2:0], ADCPRE and MCOSEL[2:0] bits */
  6.   RCC->CFGR &= (uint32_t)0xF8FFB80C;
  7.   
  8.   /* Reset HSEON, CSSON and PLLON bits */
  9.   RCC->CR &= (uint32_t)0xFEF6FFFF;

  10.   /* Reset HSEBYP bit */
  11.   RCC->CR &= (uint32_t)0xFFFBFFFF;

  12.   /* Reset PLLSRC, PLLXTPRE and PLLMUL[3:0] bits */
  13.   RCC->CFGR &= (uint32_t)0xFFC0FFFF;

  14.   /* Reset PREDIV1[3:0] bits */
  15.   RCC->CFGR2 &= (uint32_t)0xFFFFFFF0;

  16.   /* Reset USARTSW[1:0], I2CSW, CECSW and ADCSW bits */
  17.   RCC->CFGR3 &= (uint32_t)0xFFFFFEAC;

  18.   /* Reset HSI14 bit */
  19.   RCC->CR2 &= (uint32_t)0xFFFFFFFE;

  20.   /* Disable all interrupts */
  21.   RCC->CIR = 0x00000000;

  22.   /* Configure the System clock frequency, AHB/APBx prescalers and Flash settings */
  23.   SetSysClock();
  24. }
 楼主| 键盘手没手 发表于 2023-1-12 11:46 | 显示全部楼层
打开SetSysClock();并把 RCC->CR |= ((uint32_t)RCC_CR_HSEON) 修改为RCC->CR &= ~((uint32_t)RCC_CR_HSEON);
 楼主| 键盘手没手 发表于 2023-1-12 11:47 | 显示全部楼层
  1. static void SetSysClock(void)
  2. {
  3.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;

  4. /******************************************************************************/
  5. /*            PLL (clocked by HSE) used as System clock source                */
  6. /******************************************************************************/
  7.   
  8.   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  9.   /* Enable HSE */   
  10.   //RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  11.   RCC->CR &= ~((uint32_t)RCC_CR_HSEON);
  12.   /* Wait till HSE is ready and if Time out is reached exit */
  13.   do
  14.   {
  15.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  16.     StartUpCounter++;  
  17.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  18.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  19.   {
  20.     HSEStatus = (uint32_t)0x01;
  21.   }
  22.   else
  23.   {
  24.     HSEStatus = (uint32_t)0x00;
  25.   }  

  26.   if (HSEStatus == (uint32_t)0x01)
  27.   {
  28.     /* Enable Prefetch Buffer and set Flash Latency */
  29.     FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  30.     /* HCLK = SYSCLK */
  31.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  32.       
  33.     /* PCLK = HCLK */
  34.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  35.     /* PLL configuration */
  36.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  37.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  38.             
  39.     /* Enable PLL */
  40.     RCC->CR |= RCC_CR_PLLON;

  41.     /* Wait till PLL is ready */
  42.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  43.     {
  44.     }

  45.     /* Select PLL as system clock source */
  46.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  47.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  48.     /* Wait till PLL is used as system clock source */
  49.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  50.     {
  51.     }
  52.   }
  53.   else
  54.   { /* If HSE fails to start-up, the application will have wrong clock
  55.          configuration. User can add here some code to deal with this error */
  56.   }  
  57. }
 楼主| 键盘手没手 发表于 2023-1-12 11:48 | 显示全部楼层
完成这样的步骤就可以把OSC_IN和OSC_OUT当作IO口使用了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

1260

帖子

0

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

104

主题

1260

帖子

0

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