当前位置:网站首页>Friends let me see this code
Friends let me see this code
2022-07-25 13:14:00 【Embedded Linux,】
On the weekend , A wechat friend asked me to look at a piece of code
Before writing the following article , Let me start with a few words
Writing code is a very interesting thing , At the same time, it is also a matter that we need to take seriously , I don't think it's necessary to write the code as unreadable as the great God , But at least be logical , Cannot appear confusion Don't know which What happened .
Last year , I am also constantly improving my programming ability , Previous work , Many of them do things like processing systems and drivers , But the actual application programming does not have a strong foundation . Is also constantly learning and moving forward , I also thank my friends and colleagues for their help and support .
——
This code looks like this
static unsigned short timer_tick_cnt = 0;
static ___interrupt USER_TIMER void timer_isr(void)// Interrupt service function
{
if (TMR->CON & BIT(15)) // Timer overflow interrupt flag bit
{
TMR->CON |= BIT(14);// Clearing and interrupting
///putchar('@');
//todo, Interrupt function execution program ...
timer_tick_cnt++;
}
}
static unsigned short timer_tick_get_current_timer(void)
{
unsigned short timer_tick_count = timer_tick_cnt;
return timer_tick_count;
}
unsigned short usRecord = 0;
unsigned char timer_tick_timeout_wait(unsigned short *p_var, unsigned short timeout)
{
if( timer_tick_get_current_timer() - *p_var < timeout )
{
///usRecord = timer_tick_get_current_timer();
///printf("=======rec=%d,*p_var=%d=====\n",usRecord,*p_var);
///printf("====TIMER return 0====\n");
return 0;
}
*p_var = timer_tick_get_current_timer();
///usRecord///printf("====TIMER return 1====\n");
return 1;
}
unsigned short testTimer = 0;
#define TIME_TICK_1MS_1S 1000
void testHandler(void)
{
if(timer_tick_timeout_wait(&testTimer,TIME_TICK_1MS_1S))//1ms Interrupt overflow of , so 1000 by 1s
{
printf("helloworld\n");// The problem is if timer_tick_timeout_wait Parameters of timeout Defined as unsigned short, In about 65s(65535ms) This function always returns 0 Instead of returning 1,unsigned int normal
}
}
int main(void)
{
timerInit();//1ms The timer is interrupted , This function is not a problem , The problem lies below
while(1)
{
testHandler();
}
}This is the original code , I didn't make any changes .
The problem he described in the code is relatively clear
// The problem is if timer_tick_timeout_wait Parameters of timeout Defined as unsigned short, In about 65s(65535ms) This function always returns 0 Instead of returning 1,unsigned int normal
Careful people will find a problem

this timer_tick_cnt Damn variable keeps increasing
I wrote an article before , It's the timestamp problem in the kernel , If there is a problem with the variable definition of saving timestamp , That may also cause time problems .
That's why he found something wrong with his code .
——
Let me talk about other situations
The original intention of the system he designed is that the system timer executes a function after a specified time , But directly take the variables in the timer to the outside to judge , It's not very good here .
The timer should only complete the timed things , As for the time , Just tell other tasks outside , This can achieve high cohesion .
Variable timer_tick_cnt Operate in other places , It will be very uncomfortable to check the following problems .


——
Variable name 、 Function name 、 Code style.
It's just ugly , Underline some places 、 Humps are used in some places .

And the execution of this function , I can't understand it

CPU That's how you're tired .
About variable and function naming website , I recommend this , This website is very suitable for us
https://www.chtml.cn

It's not much better than you think .
——
The meaning of macros is not clear

Naming macros like this is very bad , First, 1MS again 1S, So what is this macro , It's hard to understand the meaning without looking at the code .
If I were , I'll show you what this macro does , But then add MS, Because this event is MS Time for .
——
You may also find the naming of functions , Some are humps , Some underline .
That's not the point , The key is that the intention of some functions is not clear , I don't know what the function is .
These are my own personal views
I think writing code is to treat a work of Art , Do this thing well , Well done , It's a pleasure for us .
Let's encourage it !


边栏推荐
- 卷积神经网络模型之——AlexNet网络结构与代码实现
- ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
- 从输入网址到网页显示
- The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep
- 零基础学习CANoe Panel(12)—— 进度条(Progress Bar)
- Excel录制宏
- The world is exploding, and the Google server has collapsed
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- [operation and maintenance, implementation of high-quality products] interview skills for technical positions with a monthly salary of 10k+
- [six articles talk about scalablegnn] around www 2022 best paper PASCA
猜你喜欢

Cyberspace Security penetration attack and defense 9 (PKI)

【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)

微软提出CodeT:代码生成新SOTA,20个点的性能提升
![[CSDN year-end summary] end and start, always on the way -](/img/51/a3fc5eba0eeb22b600260ee81ff9e6.png)
[CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "

2022 年中回顾 | 大模型技术最新进展 澜舟科技

【重温SSM框架系列】15 - SSM系列博文总结【SSM杀青篇】

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心

并发编程 — 内存模型 JMM

零基础学习CANoe Panel(12)—— 进度条(Progress Bar)

工业互联网的内涵及其应用
随机推荐
Date and time function of MySQL function summary
Shell常用脚本:检测某域名、IP地址是否通
Design and principle of thread pool
Shell common script: get the IP address of the network card
CONDA common commands: install, update, create, activate, close, view, uninstall, delete, clean, rename, change source, problem
Shell常用脚本:获取网卡IP地址
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
yum和vim须掌握的常用操作
[machine learning] experimental notes - emotion recognition
详解浮点数的精度问题
B tree and b+ tree
MLIR原理与应用技术杂谈
面试官问我:Mysql的存储引擎你了解多少?
程序员成长第二十七篇:如何评估需求优先级?
R language GLM generalized linear model: logistic regression, Poisson regression fitting mouse clinical trial data (dose and response) examples and self-test questions
Any time, any place, super detective, seriously handle the case!
Convolutional neural network model -- googlenet network structure and code implementation
TCP的拥塞控制
mysql函数汇总之日期和时间函数
Atcoder beginer contest 261e / / bitwise thinking + DP