当前位置:网站首页>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_col
function - Write the callback function and call
set_key_callback
Tell the driver - call
keyi_init
Function to initialize the driver - Keep calling
key_thread
Function 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 .
边栏推荐
- QQ computer version cancels escape character input expression
- [rust notes] 17 concurrent (Part 2)
- 1996. number of weak characters in the game
- [leetcode] day94 reshape matrix
- 【LeetCode】Day94-重塑矩阵
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- Leetcode recursion
- Appium automation test foundation - Summary of appium test environment construction
- [rust notes] 17 concurrent (Part 1)
- Appium foundation - use the first demo of appium
猜你喜欢
LaMDA 不可能觉醒吗?
QQ电脑版取消转义符输入表情
Appium foundation - use the first demo of appium
leetcode-6111:螺旋矩阵 IV
leetcode-6108:解密消息
MySQL advanced part 1: stored procedures and functions
MIT-6874-Deep Learning in the Life Sciences Week 7
MySQL advanced part 1: index
Data visualization chart summary (I)
Leetcode-6110: number of incremental paths in the grid graph
随机推荐
Leetcode backtracking method
Daily question 1342 Number of operations to change the number to 0
数据可视化图表总结(二)
leetcode-556:下一个更大元素 III
927. 三等分 模拟
Records of some tools 2022
MySQL advanced part 1: View
MySQL advanced part 1: index
Navicat連接Oracle數據庫報錯ORA-28547或ORA-03135
【Rust 笔记】17-并发(下)
Leetcode-9: palindromes
leetcode-31:下一个排列
SPI details
The sum of the unique elements of the daily question
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
【Rust 笔记】16-输入与输出(下)
Quickly use Amazon memorydb and build your own redis memory database
MIT-6874-Deep Learning in the Life Sciences Week 7
Leetcode array operation
LeetCode 0107. Sequence traversal of binary tree II - another method