本帖最后由 路鸣雨 于 2018-3-22 11:30 编辑
1、次级程序行开头空4格开始;
2、程序结构明晰,精简注释,多余的不用注释,否则既浪费时间,又影响读者思路和可读性;
3、定义变量时要在“xx.c”文件中,声明要在“xx.h”文件中,变量如果声明成外部变量,要在“xx.h”文件中进行,方便其他C文件包含引用;
4、多行程序可分块:
- if(init_flag == 1)
- {
- init_flag=0;
- //--
- memset(HMI_string, 0, strlen(HMI_string));
- strcat(HMI_string, "t0.txt="Loc:");
- sprintf(cat_string, "%4X", SDB[(buf1[2]-0x18)].location_ID);
- strcat(HMI_string, cat_string);
- strcat(HMI_string, ""\xFF\xFF\xFF");
- uart3SendBuf(HMI_string, strlen(HMI_string));//显示位置
- //--
- memset(HMI_string, 0, strlen(HMI_string));
- strcat(HMI_string, "t1.txt="Temp:");
- sprintf(cat_string, "%d", SDB[(buf1[2]-0x18)].temperature_point.parameter>>8);
- strcat(HMI_string, cat_string);
- strcat(HMI_string, ""\xFF\xFF\xFF");
- uart3SendBuf(HMI_string, strlen(HMI_string));//显示温度值
- }
复制代码
main.c文件格式:
- //============================================================================
- //Project : Wireless_Sensor_Temp =
- //FileName: main.c =
- //Version : V1.0 =
- //Date : 2015.01.23 =
- //Author : zty =
- //Company : todo studio =
- // =
- //Chip type : STM8L101K3T6/STM32L152RB =
- //Clock Source : 24 MHz/32 MHz =
- //Diary : =
- //============================================================================
- //=====================================================================
- //= Includes------------头---文---件---包---含------------------------ =
- //=====================================================================
- #include "stm8l10x.h"
- #include "stm8l10x_clk.h"
- #include "stm8l10x_awu.h"
- #include "stm8l10x_gpio.h"
- #include "ds18b20.h"
- //=====================================================================
- //= Private variables------变---量---定---义-------------------------- =
- //=====================================================================
- bool ButtonPressed=FALSE;
- u8 txData[32]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- //=====================================================================
- //= function prototypes---函---数---声---明--------------------------- =
- //=====================================================================
- void Delay(u16 nCount);
- static void CLK_Config(void);
- static void AWU_Config(void);
- static void IO_Config(void);
- uint32_t LSIMeasurment(void);
- //=====================================================================
- //= -------------------------主---函---数----------------------------- =
- //=====================================================================
- main()
- {
- //======================变量定义=========================//
- u16 Temperature;//温度值
- u8 T_H, T_L;
- //=====================初始化配置========================//
- CLK_Config();//系统时钟源配置
- Delay(1600);//1ms
- AWU_Config();//AWU配置
- Delay(1600);//1ms
- IO_Config();//IO口初始化
- Delay(1600);//1ms
- DS18B20_Init();//DS18B20初始化
- Delay(1600);//1ms
- Init_NRF24L01();//初始化NRF24L01
- Delay(1600);//1ms
-
- enableInterrupts();//开启全局中断
-
- while (1)
- {
- };
- }
复制代码
.h文件格式:
- //============================================================================
- //FileName: ds18b20.h =
- //Date : 2015.01.23 =
- //Author : zty =
- //Diary : =
- //============================================================================
- #ifndef _DS18B20_H_
- #define _DS18B20_H_
- //=====================================================================
- //= Includes------------头---文---件---包---含------------------------ =
- //=====================================================================
- #include "stm8l10x_gpio.h"
- //=====================================================================
- //= Private typedef-------类---型---定---义--------------------------- =
- //=====================================================================
- typedef unsigned char uchar;
- typedef unsigned int uint;
- //=====================================================================
- //= Private define-----------宏---定---义----------------Private macro =
- //=====================================================================
- #define DS18B20_DQ_OUT_L GPIO_Init(GPIOD, GPIO_Pin_6, GPIO_Mode_Out_PP_Low_Slow)
- #define DS18B20_DQ_OUT_H GPIO_SetBits(GPIOD, GPIO_Pin_6)
- #define DS18B20_DQ_IN GPIO_Init(GPIOD, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT)
- #define DS18B20_DQ_PIN GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_6)
- //=====================================================================
- //extern variables prototypes-外-部-变-量----------------------------- =
- //=====================================================================
- //=====================================================================
- //= function prototypes---函---数---声---明--------------------------- =
- //=====================================================================
- unsigned char DS18B20_Init(void);
- unsigned int DS18B20_ReadTemperature(void);
- #endif
复制代码
.c文件格式:
- //============================================================================
- //FileName: ds18b20.c =
- //Date : 2015.01.23 =
- //Author : zty =
- //Diary : =
- //============================================================================
- //=====================================================================
- //= Includes------------头---文---件---包---含------------------------ =
- //=====================================================================
- #include "ds18b20.h"
- //=====================================================================
- //= Private functions---自---定---义---函---数------------------------ =
- //=====================================================================
- void _delay_us(u16 us)
- {//大概1.258us
- us *= 2;//根据晶振该系数
- while(us--);
- }
- //=====================================================================
- //= Public functions------公---共---函---数--------------------------- =
- //=====================================================================
- //=====================================================================
- //Function : DS18B20_Init( ); =
- //Author : zty =
- //WriteData: 2014.08.17 =
- //EditData : 2014.08.17 =
- //Diary : 1、DS18B20初始化一次; =
- //=====================================================================
- unsigned char DS18B20_Init(void)
- {
- }
复制代码
|