这些工具代码助你嵌入式开发
原文:https://zhuanlan.zhihu.com/p/653484840
嵌入式开发中常用的C语言工具代码确实很重要。
今天分享一些利剑级别的C语言工具代码示例,以及它们的简要讲解。
1.循环队列(Circular Buffer):
typedefstruct {
int buffer[SIZE];
int head;
int tail;
int count;
} CircularBuffer;
voidpush(CircularBuffer *cb, int data) {
if (cb->count < SIZE) {
cb->buffer[cb->head] = data;
cb->head = (cb->head +1) % SIZE;
cb->count++;
}
}
intpop(CircularBuffer *cb) {
if (cb->count >0) {
int data = cb->buffer[cb->tail];
cb->tail = (cb->tail +1) % SIZE;
cb->count--;
return data;
}
return-1; // Buffer is empty
}
#define assert(expression) ((void)0)
#ifndef NDEBUG
#undef assert
#define assert(expression) ((expression) ? (void)0 : assert_failed(__FILE__, __LINE__))
#endif
voidassert_failed(constchar*file, int line) {
printf("Assertion failed at %s:%d\n", file, line);
// Additional error handling or logging can be added here
}
unsignedintreverse_bits(unsignedint num) {
unsignedint numOfBits =sizeof(num) *8;
unsignedint reverseNum =0;
for (unsignedint i =0; i < numOfBits; i++) {
if (num & (1<< i)) {
reverseNum |= (1<< ((numOfBits -1) - i));
}
}
return reverseNum;
}
typedefint16_t fixed_t;
#define FIXED_SHIFT 8
#define FLOAT_TO_FIXED(f) ((fixed_t)((f) * (1 << FIXED_SHIFT)))
#define FIXED_TO_FLOAT(f) ((float)(f) / (1 << FIXED_SHIFT))
fixed_t fixed_multiply(fixed_t a, fixed_t b) {
return (fixed_t)(((int32_t)a * (int32_t)b) >> FIXED_SHIFT);
}
uint16_tswap_bytes(uint16_t value) { return (value >>8) | (value <<8); }
#define BIT_MASK(bit) (1 << (bit))
#include <avr/io.h>
voidsetup_timer() {
// Configure timer settings
}
uint16_tread_timer() {
return TCNT1;
}
intbinary_search(int arr[], int size, int target) {
int left =0, right = size -1;
while (left <= right) {
int mid = left + (right - left) /2;
if (arr[mid] == target) {
return mid;
} elseif (arr[mid] < target) {
left = mid +1;
} else {
right = mid -1;
}
}
return-1; // Not found
}
#include <stdint.h>
typedefstruct {
uint32_t bits;
} Bitset;
voidset_bit(Bitset *bitset, int bit) {
bitset->bits |= (1U<< bit);
}
intget_bit(Bitset *bitset, int bit) {
return (bitset->bits >> bit) &1U;
}
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604215
Linux技术交流群:2636170
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
阅读原文 关键词
函数
数据结构
条件
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。