In digital circuit systems, square waves are frequently used signal sources with a very wide range of applications. In ordinary circuit design, 555 timers are often used to form multivibrators to generate square wave signals. Although it is relatively simple, the frequency accuracy is not high. If you want to generate square wave signals of different frequencies, it is troublesome to replace resistors and capacitors. Based on this starting point, this article uses a single-chip microcomputer for circuit design, and uses software programming control instead of hardware replacement. It can flexibly and simply generate square wave signals of different frequencies, and the circuit is simpler, the frequency is more accurate, and the performance is more stable.
1 Introduction to software platform and chip
There are many types of single-chip microcomputers at present, including Intel's 8051 series, Motorola's M68 series, ATMEL's AT series, Microchip's PIC series, etc. This article mainly introduces the circuit design of PIC single-chip microcomputer based on MPLAB-IDE 6.62 integrated development environment developed by Microchip. MPLAB-IDE integrated development software is a development tool specially designed for text editing, online debugging and compilation of PIC series single-chip microcomputers. The PIC chip uses PIC16F877A, 40-pin package, with 5 input/output ports. Each pin in each port is individually programmed by the user as needed and set as an output pin or input pin.
2 Square wave signal generator circuit design
2.1 Hardware circuit analysis
The timer/counter TMR0 module in PIC16F877A is used as a hardware timer to control the generation of square waves of different frequencies on port pin RC0. Since TMR0 is 8 bits wide and has an optional prescaler, it can generate 8 square wave signals; at the same time, different frequencies can be generated by setting different initial values. The initial value of this design is set to 131, and an overflow occurs after counting 125 instruction cycles, that is, it takes 125us for TMR0 to overflow from counting. The purpose of generating different frequencies is achieved by changing the frequency division ratio. When TMR0 overflows once, the output level of port pin RC0 is reversed once, and each reversal of the pin level twice forms a cycle of the square wave signal. When the frequency division ratio is 1:256, the delay of TMRO is 125 us×256=32000 us, the period of the square wave signal is 32000 us×2=64000 us=64 ms, and the corresponding square wave signal frequency is 15.625 Hz, the lowest level; when the frequency division ratio is 1:2, the delay of TMR0 is 125 us×2=250 us, the period of the square wave signal is 250 us×2=500 us=0.5 ms, and the corresponding square wave signal frequency is 2000 Hz, the highest level. By changing the initial value of TMR0, 8 square wave signals of 15.625 Hz, 32.25 Hz, 62.5 Hz, 125 Hz, 250 Hz, 500 Hz, 1000 Hz, and 2000 Hz can be generated respectively. Its working principle is shown in Figure 1. The push button switch SW1 connected to the port pin RB0 is used as a cyclic switching control switch; the push button switch SW2 on the MCLR pin is used as the microcontroller reset switch; port RC0 is used as a square wave signal output, and is connected to a light-emitting diode and a buzzer as output indication; R3 and C1 form an RC oscillation mode, which is connected to OSC1 as the input of the microcontroller system clock. Its advantages are economy and cost saving.
Figure 1 Schematic diagram of the working principle of the hardware circuit
2.2 Software design
In the PIC16F877A microcontroller, there are 4 special function registers related to the timer/counter TMR0, the cumulative count register TMR0, the interrupt control register INTCON, the option register OPTION_REG and the port RA direction control register TRISA. The meaning of each bit of the option register OPTION_REG is:
when its 5th bit (TOCS) is set to 1, the TMR0 module is set to counter mode, and its frequency division ratio is determined by D2:DO (PS2~PS0) of OPTION_REG, which are 1:2, 1:4, 1:8, 1:16, 1:32, 1:64, 1:128, 1:256 respectively, which can generate 8 frequency division signals, that is, 8 square wave signals. The source program is written in assembly language, as follows:
Program List fbxh.asm
LIST P=16F877A
INCLUDE "P16F877A.INC"
TMR0 EQU 01H
;Timer/Counter 0 register address
STATUS EQU 3H
OPTION_REG EQU 81H
OPTION_B EQU 22H
INTCON EQU 0BH
PORTB EQU 6H ;Port B address
TRISB EQU 86H
PORTC EQU 7H ;Port C address
TRISC EQU 87H
TMR0B EQU D\'256\'-D\'125\'
;Define TMR0 initial value
RP0 EQU 5H[page]
;************Main program************
ORG 000H
MAIN NOP
BSF STATUS,RP0
MOVLW 0FEH
MOVWF TRISC
CLRF PORTC
MOVLW 0FFH
MOVWF TRISB
MOVLW 07H ;Set the frequency division ratio to 1:256
MOVWF OPTION_REG
BCF STATUS,RP0
KEYIN BTFSC PORTB,0 ;Test whether SW1 is pressed?
GOTO LOOP
CALL DELAY
BTFSC PORTB,0
GOTO LOOP
CHECK BTFSS PORTB,0 ;Test whether SW1 is disconnected?
GOTO CHECK
CALL DELAY
;Call the delay subroutine. Eliminate the influence of disconnection jitter
BTFSS PORTB,O
GOTO CHECK
BSF STATUS,RP0
DECF OPTION_REG,1
MOVF 07H
ANDWF OPTION_REG
MOVF OPTION_REG,1
BCF STATUS,2
GOTO MAIN
LOOP MOV LW01H
XORWF PORTC
BCF INTCON,2
MOVLW TMR0B
MOVWF TMR0
; Restart the timer to start counting
TEST BTFSS INTCON,2
; Check TMR0 overflow flag
GOTO TEST
GOTO KEYIN
;*******TMR0 delay subroutine 8 ms*******
DELAY
BSF STATUS,RP0
MOVF OPTION_REG,0
MOVWF OPTION_B
MOVLW 04H
MOVWF OPTION_REG
BCF STATUS,RP0
BCF INTC0N,2
MOVLW 06H
MOVWF TMR0
LOOP1 BTFSS INTCON,2
GOTO LOOP1
BSF STATUS,RP0
MOVF OPTION_B,0
MOVWF OPTION_REG
BCF STATUS,RP0
RETURN
; ***********************
END ; End of source program
2.3 Compilation and programming based on MPLAB-IDE6.62 software platform
(1) Start MPLAB-IDE6.62, create project FBXH, create source file fbxh.asm, compile and generate target program HEX file.
(2) Select device as PIC16F877A, select communication interface (USB port), establish communication connection, and set MPLAB ICD2 as debugging tool. Set the debugging options and load the debugging code. The debugging code will be automatically programmed into the top space of the program memory for debugging of MPLAB ICD2 and simulation.
(3) After the simulation is correct, select the device configuration power supply and interface, set the programming options, program the download program, and complete the writing of the chip program.
2.4 Design, installation and debugging of circuit boards
Use PROTEL99SE to make the circuit PCB board for installation and welding. SW1 and SW2 use button switches. SW1 is the cycle switching control switch, SW2 is the reset switch, POWER is connected to 5V power supply, U1 should use IC holder during installation to facilitate updating or replacing IC, LS1 can be connected to piezoelectric buzzer, D0 is light-emitting diode, D0 lights up when there is output, OUT is the output end, and can be observed with an oscilloscope. After debugging is completed, the design of the square wave signal generator is completed.
Previous article:Application of PIC16F874 single chip microcomputer in DC motor stepless speed regulation system
Next article:Assembly program for communication between PIC16F877 microcontroller and PC
Recommended ReadingLatest update time:2024-11-16 15:28
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- TMS320VC5509A cannot connect to the emulator problem search and summary
- Let’s talk about digital filtering algorithms. Reply to get points!
- This is an equalizer. I bought it but never used it. I want to share it with you.
- Join the thread and have a chat: If I give you a motor board, what would you want to do?
- 【China Academy of Electronics Science】Recruitment of embedded/algorithm engineers
- Create a healthy life and achieve powerful tools
- Infineon XMC4800 Relax EtherCAT Kit Review Summary
- Analog Discovery 2 Review (5) Acquisition System Hardware Analysis
- GD32 Competition - Motor Control - Hardware Design
- Guo Tianxiang's New Concept 51 MCU C language tutorial. Getting started, improving, and developing