你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM8/32程序编写格式】

[复制链接]
路鸣雨 发布时间:2018-3-22 11:26
本帖最后由 路鸣雨 于 2018-3-22 11:30 编辑

1、次级程序行开头空4格开始;
2、程序结构明晰,精简注释,多余的不用注释,否则既浪费时间,又影响读者思路和可读性;
3、定义变量时要在“xx.c”文件中,声明要在“xx.h”文件中,变量如果声明成外部变量,要在“xx.h”文件中进行,方便其他C文件包含引用;
4、多行程序可分块:

  1. if(init_flag == 1)
  2. {
  3.     init_flag=0;
  4.     //--
  5.     memset(HMI_string, 0, strlen(HMI_string));
  6.     strcat(HMI_string, "t0.txt="Loc:");
  7.     sprintf(cat_string, "%4X", SDB[(buf1[2]-0x18)].location_ID);
  8.     strcat(HMI_string, cat_string);
  9.     strcat(HMI_string, ""\xFF\xFF\xFF");
  10.     uart3SendBuf(HMI_string, strlen(HMI_string));//显示位置
  11.     //--
  12.     memset(HMI_string, 0, strlen(HMI_string));
  13.     strcat(HMI_string, "t1.txt="Temp:");
  14.     sprintf(cat_string, "%d", SDB[(buf1[2]-0x18)].temperature_point.parameter>>8);
  15.     strcat(HMI_string, cat_string);
  16.     strcat(HMI_string, ""\xFF\xFF\xFF");
  17.     uart3SendBuf(HMI_string, strlen(HMI_string));//显示温度值
  18. }
复制代码



main.c文件格式:

  1. //============================================================================
  2. //Project : Wireless_Sensor_Temp                                             =
  3. //FileName: main.c                                                           =
  4. //Version : V1.0                                                             =
  5. //Date    : 2015.01.23                                                       =
  6. //Author  : zty                                                              =
  7. //Company : todo studio                                                      =
  8. //                                                                           =
  9. //Chip type    : STM8L101K3T6/STM32L152RB                                    =
  10. //Clock Source : 24 MHz/32 MHz                                               =
  11. //Diary        :                                                             =
  12. //============================================================================
  13. //=====================================================================
  14. //= Includes------------头---文---件---包---含------------------------ =
  15. //=====================================================================
  16. #include "stm8l10x.h"
  17. #include "stm8l10x_clk.h"
  18. #include "stm8l10x_awu.h"
  19. #include "stm8l10x_gpio.h"
  20. #include "ds18b20.h"

  21. //=====================================================================
  22. //= Private variables------变---量---定---义-------------------------- =
  23. //=====================================================================
  24. bool ButtonPressed=FALSE;
  25. u8 txData[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26.                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27.                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28.                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

  29. //=====================================================================
  30. //= function prototypes---函---数---声---明--------------------------- =
  31. //=====================================================================
  32. void Delay(u16 nCount);
  33. static void CLK_Config(void);
  34. static void AWU_Config(void);
  35. static void IO_Config(void);
  36. uint32_t LSIMeasurment(void);

  37. //=====================================================================
  38. //= -------------------------主---函---数----------------------------- =
  39. //=====================================================================
  40. main()
  41. {
  42.     //======================变量定义=========================//
  43.     u16  Temperature;//温度值
  44.     u8   T_H, T_L;
  45.     //=====================初始化配置========================//        
  46.     CLK_Config();//系统时钟源配置
  47.     Delay(1600);//1ms
  48.     AWU_Config();//AWU配置
  49.     Delay(1600);//1ms
  50.     IO_Config();//IO口初始化
  51.     Delay(1600);//1ms
  52.     DS18B20_Init();//DS18B20初始化
  53.     Delay(1600);//1ms
  54.     Init_NRF24L01();//初始化NRF24L01
  55.     Delay(1600);//1ms
  56.                
  57.     enableInterrupts();//开启全局中断
  58.                
  59. while (1)
  60. {
  61. };
  62. }
复制代码




.h文件格式:
  1. //============================================================================
  2. //FileName: ds18b20.h                                                        =
  3. //Date    : 2015.01.23                                                       =
  4. //Author  : zty                                                              =
  5. //Diary   :                                                                  =
  6. //============================================================================
  7. #ifndef _DS18B20_H_
  8. #define _DS18B20_H_

  9. //=====================================================================
  10. //= Includes------------头---文---件---包---含------------------------ =
  11. //=====================================================================
  12. #include "stm8l10x_gpio.h"

  13. //=====================================================================
  14. //= Private typedef-------类---型---定---义--------------------------- =
  15. //=====================================================================
  16. typedef unsigned char  uchar;        
  17. typedef unsigned int   uint;

  18. //=====================================================================
  19. //= Private define-----------宏---定---义----------------Private macro =
  20. //=====================================================================
  21. #define DS18B20_DQ_OUT_L  GPIO_Init(GPIOD, GPIO_Pin_6, GPIO_Mode_Out_PP_Low_Slow)
  22. #define DS18B20_DQ_OUT_H  GPIO_SetBits(GPIOD, GPIO_Pin_6)
  23. #define DS18B20_DQ_IN     GPIO_Init(GPIOD, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT)
  24. #define DS18B20_DQ_PIN    GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6)

  25. //=====================================================================
  26. //extern variables prototypes-外-部-变-量----------------------------- =
  27. //=====================================================================

  28. //=====================================================================
  29. //= function prototypes---函---数---声---明--------------------------- =
  30. //=====================================================================
  31. unsigned char DS18B20_Init(void);
  32. unsigned int  DS18B20_ReadTemperature(void);

  33. #endif
复制代码



.c文件格式:
  1. //============================================================================
  2. //FileName: ds18b20.c                                                        =
  3. //Date    : 2015.01.23                                                       =
  4. //Author  : zty                                                              =
  5. //Diary   :                                                                  =
  6. //============================================================================
  7. //=====================================================================
  8. //= Includes------------头---文---件---包---含------------------------ =
  9. //=====================================================================
  10. #include "ds18b20.h"

  11. //=====================================================================
  12. //= Private functions---自---定---义---函---数------------------------ =
  13. //=====================================================================
  14. void _delay_us(u16 us)
  15. {//大概1.258us
  16.     us *= 2;//根据晶振该系数
  17.     while(us--);
  18. }

  19. //=====================================================================
  20. //= Public functions------公---共---函---数--------------------------- =
  21. //=====================================================================
  22. //=====================================================================
  23. //Function : DS18B20_Init( );                                         =
  24. //Author   : zty                                                      =
  25. //WriteData: 2014.08.17                                               =
  26. //EditData : 2014.08.17                                               =
  27. //Diary    : 1、DS18B20初始化一次;                                     =
  28. //=====================================================================
  29. unsigned char DS18B20_Init(void)
  30. {
  31. }
复制代码


收藏 评论4 发布时间:2018-3-22 11:26

举报

4个回答
maxtch 回答时间:2018-3-22 13:33:38
这种代码风格问题就见仁见智了,不一定要一棒子打死。我个人喜欢用 BSD 风格,但也有人喜欢 K&R。
robter 回答时间:2018-4-27 06:39:36
不错,值得学习
changhz 回答时间:2018-5-9 13:18:46
感谢LZ
STM1024 回答时间:2018-5-14 10:56:18
Tab键,而不是4个空格,哈哈
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版