The if statement is also called a conditional statement and is one of the transfer statements in the C language. When designing a C language program, it is often necessary to determine the direction of program operation based on certain conditions. In this case, an if statement is needed to implement it. Any if statement (including the else part) can be used as a C statement.
Format: if (expression) statement 1
else statement 2
Else is a choice, which can be present or absent. Therefore, conditional statements can be divided into two types: if statements and if-else statements.
The expression in the formula is a relational expression or a logical expression and an expression composed of conditional operators (the relevant operators will be introduced later).
Function: Calculate the conditional expression in if first. If its value is true (when the condition is met), execute statement 1; if its value is false (when the condition is not met), if there is an else, execute statement 2.
If statements are commonly used in several forms:
⑴ if (conditional expression) statement
This form has no else option.
Example 1: if (a>b) c=b;
∥If the condition a>b is satisfied, assign b to c
Example 2: if (ttr == 9 9 9 9) ttr=0;
∥ The operation condition ttr==9999 is satisfied; assign 0 to ttr
⑵ if (conditional expression) statement 1
else statement 2
Example 1: if (a>b) c=b;
∥If a>b, assign b to c
else c=a;
∥ When a>b is not satisfied, assign a to c
Example 2: If you compare two integers a and b and want to send the larger one to x, you can use an if statement to implement this.
⑶ Conditional statement for expression assignment (simplified form). When readers read some books on single-chip C language programs, they will encounter a simplified form of conditional statements, the format of which is as follows:
expression1?expression2;expression3
For example, in Example 2 of the above if statement form (2), it can be simplified to an assignment statement of a conditional expression;
There are some other formats for If statements, which are omitted here.
3. Hardware circuit of LED digital tube display
In electronic technology, digital tube display (0~9) is the most common display technology. When displaying digital tubes, LCD (liquid crystal) or LED digital tubes can be used to display the numbers 0~9. Here we first introduce the display circuit of digital tube LED controlled by PIC16F84A, as shown in Figure 3. Figure 3 is a four-digit digital tube, which can also be expanded to more digits or reduced to a single-digit display.
Pin ④ of PIC16F84A is a low-level reset terminal, and the reset circuit is composed of an external reset button K0 and R1, a diode D0, and a capacitor C0. The microcontroller clock circuit is composed of an external crystal oscillator (4M) and capacitors C1 and C2. RA4 (pin ③) is externally connected to a micro switch button, and its use depends on the specific function of the circuit in Figure 3. If the circuit is used for counting, K1 can be used to start counting; if the circuit is used as a clock, K1 can be used to manually preset the starting time of the clock.
PIC16F84A's PORTB port, where RBO~BB6 bits are connected to the LED digital tube pen segment codes a, b, c, d, e, f, g respectively through current limiting resistors R3~R9. PORTA port's RA3, RA2, RA1 and RA0 are connected to the base of transistors (PNP type) V4~V1 through resistors R10~R13. The collectors of the transistors are connected to the power supply terminals (⑧, ③ pins) of the corresponding digital tubes. The emitters of the transistors are all connected to the power supply +5V. The transistors V4~V1 are used for the power on signal of the digital tube LED dynamic scanning. The symbol DP on the digital tube is the decimal point for digital display. Users can connect a 470Ω resistor and LED (in series with the resistor) to the DP of a certain digital tube as needed, which is used for the characteristic display mark of the four-digit digital tube, such as the hundreds segment when the digital tube counts; the hour segment when recording time, etc.
The digital tube LED of the circuit in Figure 3 uses a common anode digital tube, and its pins and internal circuit are shown in Figure 4. For the convenience of readers, the digital tube LED circuit in Figure 3 is drawn based on the actual structure. The circuit in Figure 3 will be used in the digital tube LED counting and clock (including preset clock) in the following text.
4. Software flow of LED digital tube counting display
Here we first introduce the display technology of using four-digit LED digital tube counting (0~9999), because counting is the basis of A/D conversion and clock circuit in single-chip microcomputer. According to the hardware circuit provided in Figure 3, the program flow of the 4-digit adder written in C language is shown in Figure 5. From the flow chart, it can be seen that the C language program is written based on the function requirements, and the functions used should be defined or declared, and then called by the main function main to complete the circuit counting (0~9999) function.
5. C program for LED digital tube counting
According to the hardware circuit in Figure 3, the source program of the 0-9999 add counter written in C language is named pic0.4.c. The program listing is as follows:
Note: In the display function of the above C program, the unsigned integer variable d in its description statement is an important variable. The value of d determines the time for each bit of the counter to increase. Changing the value of d to make it larger will increase the duration of each bit of the increment. This feature is the basis of the microcontroller timing (clock) C program. The delay after the field (SEG7) assigns a value to each bit only determines the time for the display value to turn on and off. This time is the visual effect (sense of continuity) for the human eye to observe the display value of the digital tube.
When reading the above C program, please refer to the notes to understand how to write the LED digital tube counting (0~9999) program. After understanding the above program, try to memorize it as much as possible to lay the foundation for the clock program and A/D conversion program introduced later.
Previous article:PIC microcontroller controlled remote anti-theft alarm circuit
Next article:What is a PIC microcontroller? What are its advantages?
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Domestic digital intelligent transmission sensor automatic control system comes out
- What software do you usually use when doing circuit simulation?
- AM335x Evaluation Board Quick Test (3)
- Industry Consulting
- Problems with the ping command
- About LoRa networking issues?
- Comparison of MC34063 circuit structures
- [Evaluation and experience of Zhongke Yihaiwei EQ6HL45 development platform] + Create a new FPGA project——ehiway_fpga_test0
- z I have a humble question. How many degrees should the serial numbers on the PCB assembly drawing be flipped?
- The board for showing goods + collecting dust