ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

嵌入式c编程常用套路总结

嵌入式c编程常用套路总结 状态机 动作有超时和重复次数。static uint8_t last_send_time = 0; static uint32_t login_cnt = 0; static uint8_t state = 0; // void run_state_machine(){ uint32_t curr_time = get_hal_time_s(); switch(state){ case 0: send_login_cmd(); state = 1; last_send_time = curr_time; login_cnt = 0; break; case 1: uint32_t curr_time = get_hal_time_s(); if (curr_time - last_send_time = 30){ send_logincmd(); login_cnt++; last_send_time = curr_time; if(login_cnt + MAX_LOGIN_CNT){ reboot(); } } break; case2: if(have_msg_need_send()){ send_msg(); state = 3; last_send_time = curr_time; } break; case 3: break; default: break }} // void recv_cmd(){ if(recv_ack_cmd()){ if(state == 1){ state = 2; } if(state == 3){ state = 2; } } }主循环添加定时任务。void upload_ontime() { static uint32_t last_time = 0; uint32_t curr_time = get_hal_time_s(); if(curr_time - last_time = (60)){ do_upload_task(); last_time = curr_time; } }用户配置数据。初始化值,获取,上电打印 ,设置值,保存。上电后先读取配置到变量,发现没初始化就把配
RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进