当前位置:网站首页>好友让我看这段代码
好友让我看这段代码
2022-07-25 12:48:00 【嵌入式Linux,】
周末的时候,一个微信好友让我旁边看一段代码
在写下面的文章之前,我先简单说下
写代码是一件非常有意思的事情,同时也是一件需要我们认真对待的事情,我不认为一定要把代码写的和大神一样看不明白,但是至少要逻辑清晰,不能出现混淆不清的情况发生。
最近一年,我也在不断的提高自己的编程能力,之前的工作,很多是做处理系统和驱动等事情,但是实际的应用编程并没有很强的功底。也是在不断学习和前进,也非常感谢朋友和同事们对我的帮助和支持。
——
这段代码是这样的
static unsigned short timer_tick_cnt = 0;
static ___interrupt USER_TIMER void timer_isr(void)//中断服务函数
{
if (TMR->CON & BIT(15)) //定时器溢出中断标志位
{
TMR->CON |= BIT(14);//清中断
///putchar('@');
//todo,中断函数执行程序...
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的中断溢出,故1000为1s
{
printf("helloworld\n");//问题点是如果timer_tick_timeout_wait的参数timeout定义为unsigned short,在约65s(65535ms)后该函数一直返回0而不再返回1,unsigned int正常
}
}
int main(void)
{
timerInit();//1ms的定时器中断,该函数不是问题点,问题点在下面
while(1)
{
testHandler();
}
}这个是原始的代码,我没有做任何的修改。
问题他在代码里面描述的比较清晰
//问题点是如果timer_tick_timeout_wait的参数timeout定义为unsigned short,在约65s(65535ms)后该函数一直返回0而不再返回1,unsigned int正常
细心的人会发现一个问题

这 timer_tick_cnt 该死的变量一直递增
我之前写过一篇文章,说是内核里面时间戳的问题,如果保存时间戳的变量定义有问题,那可能也会导致时间有问题。
这也是他发现他的代码有问题的原因。
——
我再说下其他的情况
他设计的这个系统的初衷是系统定时器到一个指定的时间后去执行一个函数,但是直接把在定时器里面的变量拿到外面去判断,这里就不很好。
定时器应该只完成定时的事情,至于到了多少时间,告诉外面的其他任务就好了,这样可以做到高内聚。
变量timer_tick_cnt在其他地方操作,后续有问题排查起来肯定会很难受。


——
变量的命名、函数的命名、代码风格
简直不堪入目,有的地方用下划线、有的地方用驼峰。

还有这个函数的执行,我有点看不懂

CPU就是这样被你累坏的啊。
关于变量和函数命名的网站,我推荐这个,这个网站非常适合我们
https://www.chtml.cn

这不比你自己想好多了。
——
宏的含义不清晰

这样给宏命名是非常不好的,先是1MS 又是1S,所以这个宏到底是啥,如果不看代码的话是很难理解意思。
如果是我,我会表明这个宏做什么事情,但是后面要加上MS,因为这个事件是MS的时间。
——
函数的命名大家可能也发现了,有些是驼峰,有些下划线。
这不是关键,关键是有些函数的用意没有表明清楚,不知道这个函数的作用是什么。
以上是我自己的个人观点
我觉得写代码是要对待一个艺术品,把这个东西做得好,做得优秀,是一件令我们愉悦的事情。
大家共勉之!


边栏推荐
- ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
- 【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
- [300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
- [CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
- Migrate PaloAlto ha high availability firewall to panorama
- cv2.resize函数报错:error: (-215:Assertion failed) func != 0 in function ‘cv::hal::resize‘
- 卷积神经网络模型之——VGG-16网络结构与代码实现
- 如何理解Keras中的指标Metrics
- Shell common script: judge whether the file of the remote host exists
- Want to go whoring in vain, right? Enough for you this time!
猜你喜欢

2022.07.24 (lc_6126_design food scoring system)

Word style and multi-level list setting skills (II)

【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21

Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement

录制和剪辑视频,如何解决占用空间过大的问题?

深度学习的训练、预测过程详解【以LeNet模型和CIFAR10数据集为例】

Selenium use -- installation and testing

业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/20/bb43ab1bc447b519c3b1de0f809b31.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
随机推荐
Zero basic learning canoe panel (15) -- CAPL output view
Substance Designer 2021软件安装包下载及安装教程
go : gin 自定义日志输出格式
机器学习强基计划0-4:通俗理解奥卡姆剃刀与没有免费午餐定理
外围系统调用SAP的WebAPI接口
Docekr学习 - MySQL8主从复制搭建部署
【问题解决】org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession.1 字节的 UTF-8 序列的字
牛客论坛项目部署总结
Mlx90640 infrared thermal imager temperature sensor module development notes (V)
【运维、实施精品】月薪10k+的技术岗位面试技巧
微软提出CodeT:代码生成新SOTA,20个点的性能提升
Shell common script: check whether a domain name and IP address are connected
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
word样式和多级列表设置技巧(二)
The larger the convolution kernel, the stronger the performance? An interpretation of replknet model
网络空间安全 渗透攻防9(PKI)
Leetcode 1184. distance between bus stops
2022.07.24 (lc_6126_design food scoring system)
ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
Common operations for Yum and VIM