当前位置:网站首页>Single chip computer engineering experience - layered idea
Single chip computer engineering experience - layered idea
2022-07-05 06:16:00 【Blue worry cloud maple】
SCM engineering experience - Stratification thought
The experience of single-chip microcomputer engineering is a summary of my experience in single-chip microcomputer programming , I hope to improve the efficiency of your career , Don't fall into the situation of building wheels repeatedly all day . Life is more than work , And life .
Stratification thought
The idea of stratification is not mysterious at all , It simply divides a project into multiple levels , Only the upper layer can call the lower interface , And cannot be called across levels . Here I simply divide the SCM program into 3 layer .
| application layer |
|---|
| Driver layer |
| Hardware layer |
Matrix keyboard as an example
Here we take matrix keyboard as an example .
- take ROW1 ~ ROW4 Set to output mode 、 take COL1 ~ COL4 Set to input .
- take ROW1 Output low level ,ROW2、ROW3、ROW4 Output high level .
- Read COL1 ~ COL4 level , If the level of one column is low , Press the corresponding key , such as K2 When pressed COL2 Will receive low level , And then K5~K16 Whether pressed or not , It has no effect on this level , Because even if you press , Just add one 3.3V Pull it up .
- Will be different ROW Output low level , Other output high repeat reads COL The level on the . So every time on which line ROW Set it low to read this ROW On 4 The state of the keys .
layered
Hardware layer
ROW1 ~ ROW4,COL1 ~ COL4 Need to be connected to the single chip 8 Ordinary IO On the mouth , And different microcontrollers IO Port operation registers or library functions are different , If there is no layering here , Then changing a single-chip microcomputer will have to rewrite the drive of the matrix keyboard , This is obviously what we don't want .
First of all, we are right IO Port operation is abstracted , We found that IO Mouth operation is nothing more than setting IO I / O mode , Output high and low level , Check the input level .
Then we can abstract these three functions
drv_pin_mode(int id,int mode);
drv_pin_write(int id,int level);
drv_pin_read(int id);
Because it's different IO Ports can be configured separately , So we're going to IO Passing in functions , It's different here id Can represent different IO mouth .
The implementation of these three functions of different single-chip computers is different , But it doesn't matter , As long as we write the implementation of these three functions into a single source file , So is it OK to change the source file as long as you change the MCU ? In this way, I realize the layering of the hardware layer .
Driver layer
The driver layer needs to consider what we need to provide for the upper application layer , For example, the matrix keyboard needs to provide which key is pressed 、led Need to provide light on, light off and flashing 、 Temperature sensor needs to provide temperature value . And for upper tier applications , I don't need to know how you achieved it , Drivers only need to provide what the application layer wants .
For matrix keyboards , The application layer only needs one function
char get_key_date();
The application layer can call this function in real time to know which key has been pressed .
Or we can use callback function .
void set_key_callback(void (*key_date)(char key));
The upper application writes the function first , This function can handle different key values to press , Then call the above function to set the callback , When the matrix keyboard driver detects that the key is pressed, it can automatically call this callback function .
Using these two methods, the matrix keyboard driver can be written , If you need to use it next time, you can copy the driver directly without changing any code .
Matrix keyboard driver implementation
First, let's think about the relationship between matrix keyboard and hardware .
- ROW and COL The number of is uncertain
- Every ROW and COL Corresponding IO Mouth is uncertain .
Because these two are uncertain , So we need to pass these two uncertain conditions into the driver
void set_key_row_col(int *rowMap,int rowNum,int *colMap,int colNum);
here rowMap yes ROW Column pins id Array ,rowNum It's array length ,col Also in the same way .
such as row Of id yes 1357,col Of id yes 2468, So you can call
int row[] = {
1,3,5,7};
int col[] = {
2,4,6,8};
set_key_row_col(row,4,col,4);
In the driver layer , We can save these parameters , For later use .
Then we need to think about the concrete implementation of matrix keyboard , We know that at the beginning, we need to row Set to output ,col Set to input , So you can define an initialization function
void keyi_init()
{
int i = 0;
for(i = 0; i < rowNum;i++)
{
drv_pin_mode(rowMap[i],PIN_MODE_OUT);
drv_pin_write(rowMap[i],1);
}
for(i = 0; i < colNum;i++)
{
drv_pin_mode(colMap[i],PIN_MODE_IN);
}
}
Then we have to consider taking turns to read col Value .
void key_thread()
{
static int rowIndex = 0;
int i = 0;
drv_pin_write(rowMap[rowIndex],0);// Put a certain row Output low level
for(i = 0; i < colNum;i++)
{
if(drv_pin_read(colMap[i]) == 0)// If a low level is read
{
if(key_date)// If the user sets the callback function
{
key_date(i*rowNum + rowIndex);// Execute the callback function and enter the key value
}
}
}
drv_pin_write(rowMap[rowIndex],1);// Put this row Output high level
rowIndex++;// Switch to the next output low level
if(rowIndex >= rowNum)// If all row After checking, go back to No 0 row Start checking
{
rowIndex = 0;
}
}
As long as the application layer keeps calling key_thread() function , Then you can easily get the key value . Of course, this driver doesn't consider key chattering , Identify long press, double click, etc , Only ideas are introduced here , Without further study , You can think about how to improve this program according to these ideas .
application layer
After the above introduction , What the application layer needs to do is very simple
- Well defined row and col Of id, And then call
set_key_row_colfunction - Write the callback function and call
set_key_callbackTell the driver - call
keyi_initFunction to initialize the driver - Keep calling
key_threadFunction to complete the operation of the driver
thus , We can find out , No matter what MCU , No matter how many ports of matrix keyboard , Just copy the hardware layer and driver layer into the project , All you need is the above 4 You can complete the use of the matrix keyboard .
summary
The advantage of hierarchical thinking is , Both the hardware layer and the driver layer only need to be written once , Greatly reduce the workload , Engineers can concentrate on the implementation of the application layer . And because these two layers do not involve logic at all , You can easily write unit test code , Only one test passes , In the future, these two layers of code will be used without worry bug Appearance , Reduce test pressure .
Of course, the layered idea is not without shortcomings , The first is the increase of resource occupation , Layering is bound to add a lot of redundant code , And each layer has its own resource allocation , Although different levels of resources represent the same things , But in order to reduce coupling, we have to allocate additional resources , At the same time, some compilers may have restrictions on the call stack , Layered implementation will aggravate the depth of function calls . And some of the code may not support , such as 51 There may be problems in the use of function pointer by single chip microcomputer .
So in summary , For the resource rich modern MCU, it is suggested to use the layered idea to build the program , Such a program is not only fast to write but also of high quality . If SCM is a backward product in the 1980s and 1990s , Then don't use it .
边栏推荐
- Smart construction site "hydropower energy consumption online monitoring system"
- Shutter web hardware keyboard monitoring
- [rust notes] 14 set (Part 1)
- 7. Processing the input of multidimensional features
- SQLMAP使用教程(一)
- The difference between CPU core and logical processor
- leetcode-6109:知道秘密的人数
- 【Rust 笔记】16-输入与输出(下)
- Introduction to LVS [unfinished (semi-finished products)]
- leetcode-6111:螺旋矩阵 IV
猜你喜欢

数据可视化图表总结(一)

MySQL advanced part 2: MySQL architecture

MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
![[practical skills] technical management of managers with non-technical background](/img/4d/1081c71df6ee2087359111baf7498a.png)
[practical skills] technical management of managers with non-technical background

LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively

Smart construction site "hydropower energy consumption online monitoring system"

Overview of variable resistors - structure, operation and different applications

MySQL advanced part 2: optimizing SQL steps

Navicat连接Oracle数据库报错ORA-28547或ORA-03135

做 SQL 性能优化真是让人干瞪眼
随机推荐
[rust notes] 15 string and text (Part 1)
Leetcode-6110: number of incremental paths in the grid graph
[rust notes] 17 concurrent (Part 2)
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
927. 三等分 模拟
Groupbykey() and reducebykey() and combinebykey() in spark
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
[rust notes] 16 input and output (Part 1)
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Daily question 1984 Minimum difference in student scores
In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
leetcode-556:下一个更大元素 III
Leetcode-1200: minimum absolute difference
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
One question per day 1020 Number of enclaves
1.14 - assembly line
Leetcode-6108: decrypt messages
6. Logistic model
liunx启动redis
wordpress切换页面,域名变回了IP地址