1.宏定义

.c —-就当前c文件使用
.h —-可能多个文件使用
数据封装

2.位段操作

  1. /* ------------ RCC registers bit address in the alias region ----------- */ RCC位段
  2. #define RCC_OFFSET (RCC_BASE - PERIPH_BASE) 偏移
  3. /* --- CR Register ---*/
  4. /* Alias word address of HSION bit */
  5. #define CR_OFFSET (RCC_OFFSET + 0x00) 基地址
  6. #define HSION_BitNumber 0x00
  7. #define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4)) 别名区地址 BIT Band
  8. /* Alias word address of PLLON bit */
  9. #define PLLON_BitNumber 0x18
  10. #define CR_PLLON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
  11. #ifdef STM32F10X_CL
  12. /* Alias word address of PLL2ON bit */
  13. #define PLL2ON_BitNumber 0x1A
  14. #define CR_PLL2ON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL2ON_BitNumber * 4))
  15. /* Alias word address of PLL3ON bit */
  16. #define PLL3ON_BitNumber 0x1C
  17. #define CR_PLL3ON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLL3ON_BitNumber * 4))
  18. #endif /* STM32F10X_CL */
  19. /* Alias word address of CSSON bit */
  20. #define CSSON_BitNumber 0x13
  21. #define CR_CSSON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))

3.位掩码

移位
按位取反等
在库中已经构造好了

Reset是位或清零
Set是与或置1

  1. /* CR register bit mask */
  2. #define CR_HSEBYP_Reset ((uint32_t)0xFFFBFFFF) //置0
  3. #define CR_HSEBYP_Set ((uint32_t)0x00040000) //
  4. #define CR_HSEON_Reset ((uint32_t)0xFFFEFFFF)
  5. #define CR_HSEON_Set ((uint32_t)0x00010000)
  6. #define CR_HSITRIM_Mask ((uint32_t)0xFFFFFF07)
  7. /* CFGR register bit mask */
  8. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL)
  9. #define CFGR_PLL_Mask ((uint32_t)0xFFC2FFFF)
  10. #else
  11. #define CFGR_PLL_Mask ((uint32_t)0xFFC0FFFF)
  12. #endif /* STM32F10X_CL */
  13. #define CFGR_PLLMull_Mask ((uint32_t)0x003C0000)
  14. #define CFGR_PLLSRC_Mask ((uint32_t)0x00010000)
  15. #define CFGR_PLLXTPRE_Mask ((uint32_t)0x00020000)
  16. #define CFGR_SWS_Mask ((uint32_t)0x0000000C)
  17. #define CFGR_SW_Mask ((uint32_t)0xFFFFFFFC)
  18. #define CFGR_HPRE_Reset_Mask ((uint32_t)0xFFFFFF0F)
  19. #define CFGR_HPRE_Set_Mask ((uint32_t)0x000000F0)
  20. #define CFGR_PPRE1_Reset_Mask ((uint32_t)0xFFFFF8FF)
  21. #define CFGR_PPRE1_Set_Mask ((uint32_t)0x00000700)
  22. #define CFGR_PPRE2_Reset_Mask ((uint32_t)0xFFFFC7FF)
  23. #define CFGR_PPRE2_Set_Mask ((uint32_t)0x00003800)
  24. #define CFGR_ADCPRE_Reset_Mask ((uint32_t)0xFFFF3FFF)
  25. #define CFGR_ADCPRE_Set_Mask ((uint32_t)0x0000C000)

分频

  1. static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
  2. static __I uint8_t ADCPrescTable[4] = {2, 4, 6, 8};

assert断言:
C语言用来判断一个东西是对的还是错的,如果是对的就忽略过去,如果错的以某种方式告诉我们
断言机制使用最多的是:库函数中用断言来检查用户调用该库函数的参数对不对

4.使用标准库实现RCC切换

  1. #include "clock.h"
  2. void Set_SysClockTo72M(void)
  3. {
  4. ErrorStatus sta = ERROR;
  5. //先关闭HSEON再打开HSEON
  6. RCC_HSEConfig(RCC_HSE_ON);
  7. //do while 等待HSEON稳定
  8. sta = RCC_WaitForHSEStartUp();
  9. if(sta == SUCCESS)
  10. {
  11. //设置FLASH
  12. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  13. FLASH_SetLatency(FLASH_Latency_2);
  14. // FLASH->ACR |= 0x10;
  15. // FLASH->ACR &= (~0x03);
  16. // FLASH->ACR |= (0x02);
  17. // 到这里HSE就ready了,下面再去配PLL并且等待他ready
  18. //配HLCK为SYSCLK/1
  19. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  20. //配置PLCK1为AHB的2分频
  21. RCC_PCLK1Config(RCC_HCLK_Div2);
  22. //配置PLCK2为AHB2的1分频
  23. RCC_PCLK2Config(RCC_HCLK_Div1);
  24. //设置HSE/1为PLL原始种,PLL倍频系数为9
  25. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  26. //使能PLL
  27. RCC_PLLCmd(ENABLE);
  28. while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  29. {
  30. }
  31. //设置PLL为系统时钟
  32. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  33. //等待PLL设置好时钟
  34. while(RCC_GetSYSCLKSource() != 0x08)
  35. {
  36. }
  37. }
  38. else
  39. {
  40. while(1)
  41. {
  42. }
  43. }
  44. }

PS:
注意:
在启动文件中
调用SystemInit函数,会初始化为72
我们关掉之后就可以看到速度的差别了

  1. EXPORT Reset_Handler [WEAK]
  2. IMPORT __main
  3. IMPORT SystemInit
  4. ;LDR R0, =SystemInit
  5. ;BLX R0
  6. LDR R0, =__main
  7. BX R0
  8. ENDP
  1. #include "stm32f10x.h"
  2. #include "clock.h"
  3. void led_init(void);
  4. void Delay(void);
  5. void flash(void);
  6. int main(void)
  7. {
  8. led_init();
  9. flash();
  10. Set_SysClockTo72M();
  11. while(1)
  12. {
  13. flash();
  14. };
  15. return 0;
  16. }
  17. void led_init(void)
  18. {
  19. RCC->APB2ENR = 0x00000100;
  20. GPIOG->CRL = (0x33 << 24);
  21. }
  22. void Delay(void)
  23. {
  24. unsigned int i = 0, j = 0;
  25. for( i = 0; i < 2000; i++)
  26. {
  27. for( j = 0; j < 2000; j++);
  28. }
  29. }
  30. void flash(void)
  31. {
  32. GPIOG->BSRR = 0x000000080;
  33. Delay();
  34. GPIOG->BSRR = 0x00000040;
  35. Delay();
  36. GPIOG->BSRR = 0x00ff0000;
  37. Delay();
  38. }