[STC MCU learning] Advanced Peripherals and Projects Chapter 3: Comprehensive Project-Temperature Alarm

[Mr. Zhu's course summary intrusion]
 

I will update this course during the winter vacation, so stay tuned!

The first part, chapter list

2.4.1. General knowledge of the single-chip microcomputer project
2.4.2. Introduction to the small single-chip project
2.4.3. Project establishment and framework construction
2.4.4. The first module-serial port
2.4.5. DS18B20 transplantation 1
2.4.6. DS18B20 transplantation 2
2.4 .7. Problem solving and LCD1602 transplantation
2.4.8. Temperature measurement and LCD1602 joint adjustment
2.4.9. DS1302 transplantation and joint debugging
2.4.10. Use structure to realize time reading and writing 1
2.4.11. Use structure to Implementation of time reading and writing 2
2.4.12. Time display debugging
2.4.13. Time writing function packaging
2.4.14. Buzzer packaging and debugging 1
2.4.15 Buzzer packaging and debugging 2
2.4.16
.Buzzer frequency setting problem solving 2.4.17. Stepper motor transplantation and debugging
2.4.18. Button transplantation and debugging
2.4.19. Button pin conflict resolution
2.4.20. Infrared transplantation and debugging
2.4. 21. Project function sorting and definition
2.4.22. LCD1602 realizes button control cursor movement and blinking 1
2.4.23. LCD1602 realizes button control cursor movement and blinking 2
2.4.24. LCD1602 realizes button control cursor movement and blinking 3
2.4.25. LCD1602 realizes button control cursor movement and blinking 4
2.4.26. Program flow sorting and basic display function realization
2.4.27
. Bug search and resolution 2.4.28. Increase debugging to solve the problem
2.4.29. Analysis of digital addition and subtraction adjustment function
2.4.30. Time addition and subtraction adjustment function realization 1
2.4.31. Time addition and subtraction function complete realization 2


The second part, chapter introduction

2.4.1. Common sense of single-chip microcomputer project
    This section explains the common sense content of single-chip project development from a macro perspective, and then talks about the foothold and goal of this project.
2.4.2. Introduction
    to small single-chip micro-projects This section will give a detailed explanation of the specific planning and steps of small projects, let everyone know what we want to make, how to do it, what problems may be encountered in the middle, and what help will be for you after learning And meaning.
2.4.3. Project establishment and framework construction
    This section establishes a standardized project project and standardizes the organizational structure
. 2.4.4. The first module-serial port
    This section transplants the serial code, and debugs and encapsulates
2.4.5. DS18B20 transplantation 1
    This section transplants the code of the
ds18b20 sensor, and performs testing and packaging 2.4.6.DS18B20 transplantation 2
    This section continues to transplant the code of the ds18b20 sensor, and tests and packages
2.4.7. Problem solving and LCD1602 transplantation
    This section is for the previous section Analyze the output problem of the serial port, then transplant the lcd1602, and perform packaging and testing
2.4.8. Temperature measurement and LCD1602 joint adjustment    
    This section jointly debugs the temperature module and LCD1602 display module, and finally realizes the real-time temperature display on the LCD.
2.4.9. Transplantation and joint debugging of
    DS1302 This section starts to transplant the driver of DS1302. In order to encapsulate the time, we introduce the structure in C language.
2.4.10. Use structure to realize time reading and writing 1.
    This section begins to write time reading and display functions based on structure encapsulation.
2.4.11. Use structure to realize time reading and writing 2
    This section continues the display of the completion time of the previous section on LCD1602, there are many programming details.
2.4.12. Debugging of time display
    This section debugs and verifies the program, and adds the code for BCD code and hexadecimal conversion.
2.4.13. Encapsulation of Time Write Function    
    This section mainly encapsulates the code to write time into DS1302. The function of final adjustment of time in the future will use these write functions
2.4.14. Encapsulation and debugging of buzzer 1
    this section Begin to transplant the driver of the buzzer, mainly to transplant the original code and understand and test the core code.
2.4.15. Encapsulation and debugging
    of the buzzer 2     This section continues from the previous section to encapsulate the operation interface of the buzzer, and encapsulate the usable interface for us to call outside.
2.4.16. Solution to
    the problem of setting the buzzer frequency This section will work together to study and solve the problem that the setting of the buzzer frequency cannot be successful.
2.4.17. Stepper motor transplantation and debugging
    This section transplants the stepper motor drive and encapsulates it for future use as an alarm device.
2.4.18. Key transplantation and debugging
    This section transplants and debugs the 4*4 matrix keyboard, obtains the key value and displays it on the LCD1602.
2.4.19.
    Resolution of button pin conflicts This section solves the problem of button pin conflicts, and thoroughly debugs the button API
2.4.20. Infrared transplantation and debugging
    This section transplants infrared remote control and debugs it, or use interrupt mode Achieved.
2.4.21. Sorting out and defining project functions
    This section defines the functions to be implemented by the product, and analyzes the implementation details clearly to facilitate subsequent hands-on
2.4.22. LCD1602 realizes button control cursor movement and blinking 1
    This section starts to realize the function of using buttons to control cursor movement and blinking. This section Mainly talked about the layered knowledge of the SCM project.
2.4.23. LCD1602 realizes the button control cursor movement and blinking 2
    This section continues the previous section to continue to realize the button control cursor movement and blinking function, mainly the realization of the mode adjustment function.
2.4.24. LCD1602 realizes the button control cursor movement and blinking. 3
    This section continues from the previous section to continue to realize the button control cursor movement and blinking function, mainly the realization of cursor movement.
2.4.25. LCD1602 realizes button control cursor movement and blinking 4    
    This section continues from the previous section to continue to realize the button control cursor movement and blinking function, mainly for debugging and delay impact testing.
2.4.26. Program flow sorting and basic display function realization
    This section clearly sorts out the program flow, and adds the display of temperature and time.
2.4.27. Bug finding and solving
    This section analyzes and rectifies the logic of the functions added in the previous section to make the entire process of the program more accurate.
2.4.28. Add debugging to solve problems
    This section adds serial debugging to find bugs, solve problems, and implement functions.
2.4.29. Analysis of digital addition and subtraction adjustment function
    This section starts to realize the function of adding and subtracting 1 to the number selected by the cursor. This section focuses on the addition and subtraction of temperature
2.4.30. Time addition and subtraction adjustment function realizes 1
    This section continues to realize the cursor The function of adding or subtracting 1 of the selected number, this section focuses on the addition and subtraction of time
2.4.31. Time addition and subtraction function is fully realized 2
    This section reimplements time addition and subtraction function with switch case, and adds temperature alarm function
    

    
Part Three, Classroom Record

2.4.1. Common sense of single-chip microcomputer project
2.4.1.1, industry common sense
(1) The main industries of single-chip microcomputer products: small household appliances, Internet of Things terminal equipment, etc., which require uncomplicated functions, no large-screen display, and most human-machine interfaces are buttons (mechanical Type, capacitive touch type), requiring long-term reliable operation.
(2) Product development includes: product definition (product manager) + appearance, structural design (structure engineer, etc.) + electronic hardware (hardware engineer) + software design (software engineer)
(3) General steps: use the scheme first after selecting the scheme The development board provided by the vendor is used for hardware connection and software development of core functions, while transplanting and designing its own hardware board (sending, buying components, taking back soldering, etc.), and then transplanting and debugging.
2.4.1.2. Solution selection
(1) Experience in the industry
(2) Choose a solution that you are familiar with and good at using
(3) Choose a direction-oriented solution for a more specialized direction
(4) Development efficiency, cost, performance expansion, and stable supply goods such as
2.4.1.3, this course positioning and thinking
(1) learning is for knowledge and capacity building, while doing the project is the product-oriented.
(2) The goal of this course is to make full use of the hardware resources at hand and do a practical project. The main purpose is to learn to write more project source code and more complex functions, and to exercise your programming ability, debugging ability, and Control ability.
(3) In practice, projects that use 51 to do will not be too complicated. The current larger use of 51 is to make dedicated SoC (such as capacitive touch IC, fingerprint recognition IC, etc.)


2.4.2. Introduction to small single-chip microcomputer projects
2.4.2.1 Project name: Temperature alarm based on 51 single-chip microcomputer
(1) Main control: stc51; programming language: C language; development environment: Keil; main functions:
(2) System man-machine Interface: Button and IR: Modification time
(3) 1602 screen displays time and temperature; when the temperature exceeds a predetermined value, the buzzer and motor work alarm
2.4.2.2, hardware resource allocation
    1602 screen P0; P2^7; P2^5; P2^6;
    4*4 button P2
    serial port P3.0 P3.1
    IR (infrared) P3.2
    sensor DS18B20 P3.7; DS1302 P3.4 P3.6
    Stepper motor (four-wire bipolar) P1.0- P1.5
    buzzer P1.6
2.4.2.3, project process
(1) writing, transplanting, packaging, testing the underlying hardware module operating library
(2) sorting out and defining application layer functions
(3) implementing each function one by one, and joint debugging , Test whether the function is normal
(4) Actual test and use, and solve the bug and continue to maintain it.
3.4.2.4. Some minor problems
(1) Some additional knowledge (such as C language structure) that needs to be used in the project will be discussed when it is used. I don't know how to talk about it systematically, but use it as the main thing. If you need additional learning, you need to look for additional information.
(2) Pay attention to the grammar and format
(3) The key is not to listen to the class, but to practice.


2.4.3. Project establishment and framework construction
2.4.3.1, establishment of project projects
(1) establish each folder
(2) establish Keil project
(3) create c and h files and try to compile
2.4.3.2, port allocation check and confirm
(1 ) Mainly to determine the P2 port
(2) Follow-up ideas: wire and debug the underlying drivers one by one, and try to adjust the
    1602 screen P0;P1^4;P1^5;P1^6;
    4*4 button P2
    serial port P3 if you encounter problems . 0 P3.1
    IR (infrared) P3.2
    sensor DS18B20 P3.7; DS1302 P3.4 P3.5 P3.6
    stepper motor (four-wire bipolar) P1.0-P1.3
    buzzer P1.7


2.4.4. The first module-serial port
2.4.4.1, transplant and debug and confirm basic functions
(1) driver source code transplantation
(2) test
2.4.4.2, package
(1) what is package
(2) package bottom interface practice
(3) ) Plus description header


2.4.5_6. DS18B20 transplantation 1_2
problem: double t = 25.4; To use the serial port to print 25.4 to the serial port assistant to display the
serial port assistant has two display modes: binary mode and text mode. The text method is the most intuitive, but what needs to be sent through the serial port is not a double, not an int, but an ASCII string. It
means: if you want to see 25.4, get uart_send_string("25.4");
so we need a function that can The double type t is converted into a corresponding string and sent to the serial port for display.
How to get this function? Method 1: Write by yourself; Method 2: Use library functions


2.4.7. Problem solving and LCD1602 transplantation
2.4.7.1, the problem analysis and solution ideas of the previous section
2.4.7.2, LCD1602 transplantation and packaging
1602 screen P0;P2^5;P2^6;P2^7;


2.4.8. Joint adjustment of temperature measurement and LCD1602


2.4.9
. DS1302 transplantation and joint debugging 2.4.9.1, DS1302 transplantation
2.4.9.2, time data encapsulation
(1) A time = year + month + day + hour + minute + second + day of the week, so the time is complicated Variables (compared to temperature is a simple variable)
(2) C language provides the grammatical skills of structure to deal with complex variables
(3) Clearly distinguish two concepts: structure type and structure variable
(4) structure this grammar There are routines when using: the first step is to define the structure type, the second step uses the type to produce the structure variable, the third step uses the structure variable (in fact, it uses the content wrapped in the structure variable)


2.4.10_11. Use structure to realize time reading and writing 1_2


2.4.12. Time display debugging
2.4.12.1. How to debug the program
(1) The first and most critical step is to locate the problem
(2) The second step, modify the code, see the effect, and then modify...

C language bit operation, refer to the 4th part of "Teacher Zhu Youpeng Embedded Linux Core Course" and the second part of "C Language Advanced Topics"


2.4.13. Package of time write function


2.4.14. Packaging and debugging of the buzzer 1


2.4.15. Packaging and debugging of the buzzer 2


2.4.16. The problem of setting the buzzer frequency is solved


2.4.17. Porting and debugging of stepper motor
Wiring:
P1.0-A-
P1.1-A+
P1.2-B-
P1.3-B+


2.4.18. Porting and debugging of keys

    
2.4.19. Resolution of key pin conflicts

    1602 screen P0;P2^5;P2^6;P2^7;
    4*4 buttons P1
    serial port P3.0 P3.1
    IR (infrared) P3.2
    sensor DS18B20 P3.7; DS1302 P3.4 P3.5 P3. 6
    stepper motor (four-wire bipolar) P2.0-P2.3
    buzzer P2.4


2.4.20. Infrared transplantation and debugging
Step 1: Use interruption to decode infrared reception, complete the
second step: Because the interruption of infrared reception is too long, it is easy to conflict with other modules. How to do? The solution is
to delay outside (not in the interrupt isr) and receive a frame of infrared data, not in the interrupt.
Where are the difficulties and problems:
1. Do you want to interrupt? Because the infrared remote control signal is asynchronous, it must be processed by interrupt
2, but the complete receiving time is too long to be placed in the interrupt, so we start the reception in the interrupt, and then hand it over to the outside to do the entire receiving work.
3. The implementation of the precise delay function is the key when it is done externally. You can't use the delay method, but use the timer to realize the
final summary: in fact, it is very complicated and troublesome.


2.4.21. Project function combing and definition
2.4.2.1.1 Function planning
(1) Display time and temperature
by default (2) Display and adjustment of temperature threshold
(3) Alarm after temperature exceeds the threshold
(4) Time adjustment
(5) Expansion Function: alarm clock
2.4.2.2, detailed realization
(1) Time and temperature display format
T: 23.6-37.9
20170423-162312
(2) Set a cursor when displayed on LCD1602, the display is divided into 2 modes, normal mode and adjustment mode. There is no cursor display in the normal mode, but there is a cursor in the adjustment mode. The grid where the cursor flashes is the grid content to be adjusted.
(3) There is a mode switch button in the buttons (for example, the button with a defined key value of 0), the default is the normal mode, press once to switch to the adjustment mode, and then press to switch back to the normal mode.
(4) The movement of the cursor in the adjustment mode depends on the keys. Among the keys, 4 keys are up, down, left, and right.
(5) When the cursor is on a grid, press the + or-button, the number will be processed by adding 1 and subtracting 1.
(6) Hardware alarm clock, software alarm clock


2.4.22. LCD1602 realizes button control cursor movement and blinking 1.
Layering of single-chip microcomputer projects:
two-layer system: driver layer, application layer
three-layer system: bottom driver layer, upper driver layer, and application layer are
divided into three layers System: driver layer, middle layer, application layer


2.4.23. LCD1602 realizes button control cursor movement and blinking 2

Two kinds of inversion of C language
! Logic inversion
~ Bitwise inversion ~0b1011 == 0b0100
x = !!y;

Definition: When keyValue = 1, this button is defined as a mode adjustment button.


2.4.24_25. LCD1602 realizes button control cursor movement and blinking 3_4
button definition: 2 to the left, 3 to the top, 4 to the right

Absolute coordinate system, relative coordinate system


2.4.26. Program flow sorting and basic display function realization
2.4.26.1 Program flow sorting
(1) Reasonable arrangement of key acquisition and other codes
(2) Only one single-chip microcomputer program can only have one main program
2.4.26.2 Basic display Function realization
(1) Temperature display
(2) Threshold temperature display
(3) Time display
(4) To be implemented: Threshold temperature adjustment


2.4.27. Bug finding and solving

Program operation logic:
1. When the mode adjustment button is pressed, if it is changed from the normal mode to the adjustment mode, then: stop refreshing the temperature and time (the content displayed on the LCD1602 at this time is what is being displayed just now), and respond to the key event (There are mainly 2 types, 2, 3, 4 buttons are to adjust the cursor position, 5, 6 buttons are to adjust the number of the cursor position)
2. When the mode adjustment button is pressed, if it is changed from the adjustment mode to the normal mode Then: restore the refresh of temperature and time, stop responding to buttons 2, 3, 4, 5, 6, and only respond to button 1. At the same time, the adjustment result made in the adjustment mode just now needs to be effective (for example, the temperature threshold needs to be updated if the temperature threshold is changed, for example, the time adjustment needs to be written into the ds1302 to take effect)


2.4.28. Increase debugging to solve the problem


2.4.29. Analysis of digital addition and subtraction adjustment function


2.4.30. Time addition and subtraction adjustment function realization 1

20170425153919-2


2.4.31. Time addition and subtraction function is fully realized 2
use switch case

In the bare-metal program of 51 single-chip microcomputer, the timing time of DS18B20 is calculated by delay method. In fact, this is an
atypical approach. The real mainstream approach is to use timers to achieve precise timing (system systick is
used for timing under the operating system ) so that timing will not be affected by other codes.
The problem we encountered here should be that the code of the buzzer affects the delay function in the main program, causing the delay to be inaccurate.
So sometimes the temperature read by the DS18B20 is wrong. The solution is to use a timer to realize the delay of the DS18B20. can.

The general 51 single-chip microcomputer program is not so complicated, 51 is originally something decades ago, to do simple tasks. It
is better to use STM32 for more complex designs, and STM32 is now also very cheap and more cost-effective.

Guess you like

Origin blog.csdn.net/qq_27148893/article/details/111302179