The following is an example of controlling the on and off of any of the 8 LEDs connected to the port register PORTB of the PIC16F84A microcontroller to introduce the writing of the relevant C language program. The circuit is shown in the figure below.
The C language source program generated by using MPLABIDEV7.40 integrated development environment and PICC compiler is also called source file.
When writing C source program, you need to define the hardware header file (also called include header file or header file). Because the standard register addresses and their bit addresses that users often use are defined in the Pic.h header file. According to the syntax rules of the C language compiler to compile the source program, when writing the C language program, you must use the #include statement to include this pic.h header file into your own source program. That is, for the PIC intermediate product microcontroller, the beginning of the C language program is a fixed format #include. When the compiler compiles and processes the #include statement, it will copy the content of pic.h to your source program, so that PICC will think that the port address and the bit address of the standard register used by the user have been defined, and the source program will legally use these standard registers and their corresponding bits.
The program list for lighting up 8 LEDs connected to PORTB of PIC16F84A microcontroller and lighting up the LEDs connected to positions 1, 2, and 6 respectively is as follows (source file name is PIC01.C):
Description: The above is a complete C program that can execute the LED lighting in the above figure. It mainly consists of the bit definition of the port register PORTB, the definition of the delay function delay(); the main function main(), the delay function voiddelay() and various statements.
The description text starting with the symbol "∥" in the third line of the C program is a comment, which can be written in one line or in multiple lines. Comments can be written anywhere in the program to help read and understand the program, explain the relevant functions and precautions of the program, and explain the relevant algorithms. Comments should be as simple as possible. When compiling, comments do not generate code. C programs can also use the content between the beginning of "/#" and the end of "#/" as comments, which is equivalent to the comments starting with the symbol "∥". The
first line of the above program is the header file with the #include statement at the beginning of the boot program.
The second line is the bit definition of the PIC microcontroller PORTB port, which refers to 8 bits here, that is, (&add)*8.
The third line is the comment of the bit definition of the port register PORTB in the second line.
The fourth to sixth lines are the definitions of the three bits RB0, RB1, and RB6 of the port register PORTB, so that they can be assigned values in the subsequent program.
The seventh line is the definition comment of the three bits RB0, RB1, and RB6.
The eighth line is the definition of the delay function. The functions from the ninth line to the end (see the relevant comments).
Previous article:Touch button design based on 8-bit MCU
Next article:Development of an Embedded Radar Transmitter Fault Monitoring System
- 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
- [Today at 10:00 am] Keysight metrology experts talk about: Electronic instrument metrology calibration knowledge and calibration cycle
- Innovative power supply questions - Analysis of 2019 National Power Equipment A Question
- bq4050 power jump
- EEWORLD University Hall----Using MATLAB for FPGA Design
- Why should computer science students learn to use Linux systems?
- EEWORLD University Hall----Live Replay: Secure Boot of Microprocessors
- Texas Instruments Action Camera and Handheld Gimbal Solutions
- Reading Notes on the Good Book "Operational Amplifier Parameter Analysis and LTspice Application Simulation" 05--LTspice Simulation
- The crystal pins are black, what should I do?
- Revealed: Tektronix's first oscilloscope for you is now on the market