嵌入式程序设计经验 创建复位函数
在设计嵌入式系统重新时
需要考虑软复位的情况,
软复位时 很多变量都需要重置为初始值,
如果一个个去赋值 很麻烦,
下面是一个简单的办法
主要是对结构体 复位的方法:
#include <stdint.h>
typedef struct {
uint8_t reg1;
uint8_t reg2;
uint8_t reg3;
} StruSimuStat1;
typedef struct {
uint8_t state1;
uint8_t state2;
uint8_t state3;
} StruSimuStat2;
typedef struct {
uint16_t wqt1;
uint16_t wqt2;
} StruSimuWQT;
// 默认值
StruSimuStat1 zonereg = {0xEF, 0x30, 0};
StruSimuStat2 powertstate = {0, 0, 0};
StruSimuWQT WQTReg = {0, 0};
uint16_t uTxFrame[14] = {0x01, 0x02, 0x03, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// 复位函数
void resetDefaults() {
zonereg = (StruSimuStat1){0xEF, 0x30, 0};
powertstate = (StruSimuStat2){0, 0, 0};
WQTReg = (StruSimuWQT){0, 0};
uint16_t defaultTxFrame[14] = {0x01, 0x02, 0x03, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
for (int i = 0; i < 14; i++) {
uTxFrame[i] = defaultTxFrame[i];
}
}