一、. C 文件注释格式

1.文件头

  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_adc.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the ADC firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. *
  19. * @Change Log
  20. * Data Author Notes
  21. * 2020-07-24 InTron First Version
  22. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  23. ******************************************************************************
  24. */
    • @file 文件名称
    • @author 作者
    • @version 版本
    • @date 时间
    • @brief 简单介绍
    • @attention 注意事项
    • @Change Log 版本更变细节

2.行内代码注释

2.1 头文件引用

  1. /* Includes ------------------------------------------------------------------*/

2.2 宏定义注释

  1. /* ------------ RCC registers bit address in the alias region ----------------*/
  2. #define AFIO_OFFSET (AFIO_BASE - PERIPH_BASE)
  3. /* --- EVENTCR Register -----*/
  4. /* Alias word address of EVOE bit */
  5. #define EVCR_OFFSET (AFIO_OFFSET + 0x00)
  6. #define EVOE_BitNumber ((uint8_t)0x07)
  7. #define EVCR_EVOE_BB (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))
  • 注意注释的层级,即“—-”的数量

2.3 代码块注释

  1. /** @defgroup ADC_Private_FunctionPrototypes
  2. * @{
  3. */
  4. 这里面加入定义的函数
  5. /**
  6. * @}
  7. */
  • @defgroup:定义代码块名称

3.行间注释

3.1 一般行间注释

  1. /* Configure ADCx: Dual mode and scan conversion mode */
  2. /* Set DUALMOD bits according to ADC_Mode value */
  3. /* Set SCAN bit according to ADC_ScanConvMode value */
  4. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_Mode | ((uint32_t)ADC_InitStruct->ADC_ScanConvMode << 8));
  5. /* Write to ADCx CR1 */
  6. ADCx->CR1 = tmpreg1;
  • 所有注释均不使用双斜线,必须使用//**的形式
  • 注释均在要解释的代码之前

3.2 功能分块注释

  1. /*---------------------------- ADCx CR2 Configuration -----------------*/
  2. /* Get the ADCx CR2 value */
  3. tmpreg1 = ADCx->CR2;
  4. /* Clear CONT, ALIGN and EXTSEL bits */
  5. tmpreg1 &= CR2_CLEAR_Mask;
  6. /* Configure ADCx: external trigger event and continuous conversion mode */
  7. /* Set ALIGN bit according to ADC_DataAlign value */
  8. /* Set EXTSEL bits according to ADC_ExternalTrigConv value */
  9. /* Set CONT bit according to ADC_ContinuousConvMode value */
  10. tmpreg1 |= (uint32_t)(ADC_InitStruct->ADC_DataAlign | ADC_InitStruct->ADC_ExternalTrigConv |
  11. ((uint32_t)ADC_InitStruct->ADC_ContinuousConvMode << 1));
  12. /* Write to ADCx CR2 */
  13. ADCx->CR2 = tmpreg1;
  14. /*---------------------------- ADCx SQR1 Configuration -----------------*/
  15. /* Get the ADCx SQR1 value */
  16. tmpreg1 = ADCx->SQR1;
  17. /* Clear L bits */
  18. tmpreg1 &= SQR1_CLEAR_Mask;
  19. /* Configure ADCx: regular channel sequence length */
  20. /* Set L bits according to ADC_NbrOfChannel value */
  21. tmpreg2 |= (uint8_t) (ADC_InitStruct->ADC_NbrOfChannel - (uint8_t)1);
  22. tmpreg1 |= (uint32_t)tmpreg2 << 20;
  23. /* Write to ADCx SQR1 */
  24. ADCx->SQR1 = tmpreg1;
  • 保持文本居中
  • 不能使用双斜线,必须使用/—————————————— ADCx SQR1 Configuration ————————-/

4.文件尾

  1. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

二、头文件注释格式

1.文件头

  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_adc.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the ADC firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. *
  19. * @Change Log
  20. * Data Author Notes
  21. * 2020-07-24 InTron First Version
  22. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  23. ******************************************************************************
  24. */
    • @file 文件名称
    • @author 作者
    • @version 版本
    • @date 时间
    • @brief 简单介绍
    • @attention 注意事项
    • @Change Log 版本更变细节

2.行内代码注释

2.1 引用

  1. /* Includes ------------------------------------------------------------------*/
  1. /* Define to prevent recursive inclusion -------------------------------------*/
  1. /*------------ Functions used for all STM32F10x devices -----*/
  2. void FLASH_SetLatency(uint32_t FLASH_Latency);
  3. void FLASH_HalfCycleAccessCmd(uint32_t FLASH_HalfCycleAccess);
  4. void FLASH_PrefetchBufferCmd(uint32_t FLASH_PrefetchBuffer);

2.2 代码块注释

  1. /** @defgroup ADC_Private_FunctionPrototypes
  2. * @{
  3. */
  4. 这里面加入定义的函数
  5. /**
  6. * @}
  7. */
  • @defgroup:定义代码块名称
  • @brief:子定义,注意这个没有/{@……@}/
  • @addtogroup:

3.声明注释

3.1 结构体

  1. /** @defgroup ADC_Exported_Types
  2. * @{
  3. */
  4. /**
  5. * @brief ADC Init structure definition
  6. */
  7. typedef struct
  8. {
  9. uint32_t ADC_Mode; /*!< Configures the ADC to operate in independent or
  10. dual mode.
  11. This parameter can be a value of @ref ADC_mode */
  12. FunctionalState ADC_ScanConvMode; /*!< Specifies whether the conversion is performed in
  13. Scan (multichannels) or Single (one channel) mode.
  14. This parameter can be set to ENABLE or DISABLE */
  15. FunctionalState ADC_ContinuousConvMode; /*!< Specifies whether the conversion is performed in
  16. Continuous or Single mode.
  17. This parameter can be set to ENABLE or DISABLE. */
  18. uint32_t ADC_ExternalTrigConv; /*!< Defines the external trigger used to start the analog
  19. to digital conversion of regular channels. This parameter
  20. can be a value of @ref ADC_external_trigger_sources_for_regular_channels_conversion */
  21. uint32_t ADC_DataAlign; /*!< Specifies whether the ADC data alignment is left or right.
  22. This parameter can be a value of @ref ADC_data_align */
  23. uint8_t ADC_NbrOfChannel; /*!< Specifies the number of ADC channels that will be converted
  24. using the sequencer for regular channel group.
  25. This parameter must range from 1 to 16. */
  26. }ADC_InitTypeDef;
  27. /**
  28. * @}
  29. */
  • 注释不能使用双斜杠,必须使用/**/形式
  • 注释必须对齐保证一致性
  • 必须以!<开头,以和其他注释进行区分

3.2 宏定义

  1. #define FLASH_FLAG_BANK2_BSY ((uint32_t)0x80000001) /*!< FLASH BANK2 Busy flag */
  2. #define FLASH_FLAG_BANK2_EOP ((uint32_t)0x80000020) /*!< FLASH BANK2 End of Operation flag */
  3. #define FLASH_FLAG_BANK2_PGERR ((uint32_t)0x80000004) /*!< FLASH BANK2 Program error flag */
  4. #define FLASH_FLAG_BANK2_WRPRTERR ((uint32_t)0x80000010) /*!< FLASH BANK2 Write protected error flag */
  5. #define FLASH_FLAG_BANK1_BSY FLASH_FLAG_BSY /*!< FLASH BANK1 Busy flag*/
  6. #define FLASH_FLAG_BANK1_EOP FLASH_FLAG_EOP /*!< FLASH BANK1 End of Operation flag */
  7. #define FLASH_FLAG_BANK1_PGERR FLASH_FLAG_PGERR /*!< FLASH BANK1 Program error flag */
  8. #define FLASH_FLAG_BANK1_WRPRTERR FLASH_FLAG_WRPRTERR /*!< FLASH BANK1 Write protected error flag */
  • 注释不能使用双斜杠,必须使用/**/形式
  • 每一组必须对齐保证一致性
  • 必须以!<开头,以和其他注释进行区分

3.3 防重复符号注释

  1. #ifndef __FILE_H__
  2. #define __FILE_H__
  3. #endif/* __FILE_H__ */
  • 在#endif后要有注释说明

4.文件尾

  1. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

三、函数名称、声明

  1. /**
  2. * @brief Checks whether the specified FLASH flag is set or not.
  3. * @note This function can be used for all STM32F10x devices.
  4. * - For STM32F10X_XL devices, this function checks whether the specified
  5. * Bank1 or Bank2 flag is set or not.
  6. * - For other devices, it checks whether the specified Bank1 flag is
  7. * set or not.
  8. * @param FLASH_FLAG: specifies the FLASH flag to check.
  9. * This parameter can be one of the following values:
  10. * @arg FLASH_FLAG_BSY: FLASH Busy flag
  11. * @arg FLASH_FLAG_PGERR: FLASH Program error flag
  12. * @arg FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
  13. * @arg FLASH_FLAG_EOP: FLASH End of Operation flag
  14. * @arg FLASH_FLAG_OPTERR: FLASH Option Byte error flag
  15. * @retval The new state of FLASH_FLAG (SET or RESET).
  16. */
  17. FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG)
  • @brief:函数的简单介绍
  • @note:函数的注意事项
  • @param:参数。@param:参数名 简单介绍

@param:参数名 简单介绍

  • @arg:子参数。参数名:简单介绍
  • @retval:返回值的介绍。没有为None

四、其他需要注意的事项

请使用英文做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。
源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的上方或右方,其他位置都是非法的。