PIC microcontroller C language programming example: C language if statement and LED digital tube display

Publisher:ww313618Latest update time:2020-01-19 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:PIC microcontroller C language programming example: C language if statement and LED digital tube display

Previous article:PIC microcontroller controlled remote anti-theft alarm circuit
Next article:What is a PIC microcontroller? What are its advantages?

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号