Design of square wave signal generator circuit based on PIC16F877A

Publisher:快乐航程Latest update time:2012-11-27 Source: 21IC Keywords:PIC16F877A Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:PIC16F877A Reference address:Design of square wave signal generator circuit based on PIC16F877A

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

PIC16 MCU LCD1602 Driver PIC16F877A PIC16F887
/* LCD display related */ #define DATA 1 /* LCD writes data to 1 */ #define COM 0 /* LCD write command is 0 */ #define LINE1 0b10000000 #define LINE2 0b11000000 #define LCD_E RD6 #define LCD_RW RD5 #define LCD_RS RD4 void LCD_WRITE_4( unsigned char R1, unsigned char FLAG ); void LCD_WRITE( unsigned char R1
[Microcontroller]
PIC16 MCU LCD1602 Driver PIC16F877A PIC16F887
C51中断(void timer1(void) interrupt 3 using 3)
interrupt indicates the interrupt priority, and using indicates the working register group used. interrupt x using y        The xx value after interrupt is the interrupt number, that is, which interrupt port this function corresponds to. Generally in 51,        0 is external interrupt 0          1 is timer 0        2
[Microcontroller]
Detailed explanation of port B of pic16f877a
The following is an excerpt from the introduction of Port B:   When we write programs, we must pay attention to the judgment of interrupts, and the difference between reading and writing.
[Microcontroller]
Detailed explanation of port B of pic16f877a
PIC16F877A serial port transmission
PIC16F877A serial port sending and querying methods. It took me a day to find out this. It turned out to be a problem with the serial port chip voltage. The summary is as follows: 1. Pay attention to the power supply voltage of the 232 serial port chip, there are 5V and 3.3V 2. Pay attention to the TXD and RXD wirin
[Microcontroller]
PIC16F877A serial port transmission
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号