1 Implementation Principle
See the previous content of timer 0 and external interrupt
2 Implementation circuit diagram
3 Source code
/*----------------Function function:
Application of Timer 0+External Interrupt
Function 1: LED0 keeps flashing;
Function 2: When no button is pressed, LED1 is off. When a button is pressed, LED1 is on.
--------------------------*/
#include //#include"delay.h"//Call the delay sub-function __CONFIG(0xFF32); //Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off //__CONFIG(HS&WDTDIS&LVPDIS); /*-----------Macro definition--------------*/ #define uint unsigned int #define uchar unsigned char uint i; /*----------------Sub-function declaration------------------*/ /*----------------Main function--------------------*/ void main() { // The corresponding data direction register is TRISA. // Setting a TRISA bit (= 1) will make the corresponding PORTA an input. // Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output. TRISB0=1; //Set RB0/INT port as input, press the button TRISD0=0; //Set RD0 port to output, LED0 TRISD1=0; //Set RD1 port to output, LED1 // 1 = Port pin is > VIH, i.e. high level; 0 = Port pin is < VIL, i.e. low level RD0=0; //Set data direction RD0 to output RD1=0; //RB0=0; /******************Timer TMR0 initialization****************************/ // Timer mode is selected by clearing bit T0CS. Timer mode is selected by clearing bit T0CS (T0CS=0). // In Timer mode, the Timer0 module will increment every instruction cycle (without prescaler). // Counter mode is selected by setting bit T0CS. Counter mode is selected by setting T0CS bit (T0CS=1). // In Counter mode, Timer0 will increment either on every rising or falling edge of pin RA4/T0CKI // The incrementing edge is determined by the Timer0 Source Edge Select bit, (T0SE=1). // Clearing bit T0SE selects the rising edge. In counter mode, you need to select the rising edge (T0SE=0) or the falling edge (T0SE=1) trigger; T0CS=0; //TMR0 clock source selection bit If T0CS=0, the clock source of TMR0 selects the internal instruction cycle (fosc/4) // The following two lines of statements select the rising edge trigger in counter mode //T0CS=0; //Counter mode //T0SE=0; //Rising edge trigger // There is only one prescaler available which is mutually exclusively shared between // the Timer0 module and the Watchdog Timer. // The PSA and PS2:PS0 bits determine the prescaler assignment and prescale ratio. //Don't use prescaler //PSA=1; //The prescaler is assigned to WDT (watchdog), that is, 1:1 frequency division. At this time, PS0~PS2 are invalid //To prescale the frequency, the following four instructions replace the above one instruction PSA=0; //Prescaler allocation bit PS0=0; //Pre-scaling 1:8, the corresponding codes of the three bits are 010 PS1=1; // Eight system clocks, the value of the counter register +1 PS2=0; //The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h. // Timing 2000us=250us*8 (eighth frequency division), initial value TMR0=256-250=6 TMR0=0x06; //8-bit counter register initial value // This overflow sets bit TMR0IF(T0IF). T0IF=1 indicates that the count register has overflowed. T0IF=0; //The overflow interrupt flag of TMR0 is cleared, so that when the count register is full, T0IF generates a rising edge. //The interrupt can be masked by clearing bit TMR0IE(T0IE). //T0IE=1; //TMR0IE interrupt enable control bit 1 TMR0IE=1; //TMR0IE=T0IE is equivalent to the previous statement /*********************External interrupt initialization********************/ // External interrupt on the RB0/INT pin is edge triggered, // either rising if bit INTEDG (OPTION_REG<6>) is set or falling if the INTEDG bit is clear. INTEDG=1; //Set RB0/INT to rising edge trigger INTF=0; // Clear the interrupt flag of RB0/INT // This interrupt can be disabled by clearing enable bit, INTE INTE=1; // RB0/INT overflow interrupt flag enable bit 1 //****************Open global interrupt settings********************/ // A global interrupt enable bit, GIE (INTCON<7>), enables (if set) all unmasked interrupts // or disables (if cleared) all interrupts // External interrupt RB0/INT is set to enable interrupt, and global interrupt is required here GIE=1; //General interrupt enabled // External interrupt RB0/INT is set to interrupt enable, here we need to allow peripheral interrupts PEIE=1; // Enable peripheral interrupts while(1) // infinite loop, after the microcontroller is initialized, it will keep running this infinite loop { } } /********************Interrupt service routine**************************/ void interrupt ISR(void)//All interrupts of PIC microcontrollers have such an entry { // The TMR0 interrupt is generated(T0IF==1) when the TMR0 register overflows from FFh to 00h. // When the counter register changes from all 1 to all 0, T0IF==1. if(T0IF==1) //need to further determine whether it is T0 interrupt { //After the timer is interrupted, reset the initial value to prepare for the next interrupt TMR0=0x13; // Bit TMR0IF must be cleared in software by the Timer0 module Interrupt Service Routine // before re-enabling this interrupt. Explains why it is cleared // Overflow interrupt flag cleared. An interrupt will only occur when a rising edge appears on T0IF, so it must be cleared after the interrupt occurs. T0IF=0; //Execute the interrupt handler, that is, the interrupt occurs, what function do we want to execute if(++i>250) //2ms interrupt once, then count 250 times to get 500ms { i=0; RD0=!RD0; // Invert to achieve one second flashing } } // When a valid edge appears on the RB0/INT pin, flag bit, INTF(INTCON<1>), is set. else if(INTF==1) // Need to further determine whether the interrupt flag of RB0/INT is { // The interrupt flag bit(s) must be cleared in software before // re-enabling interrupts to avoid recursive interrupts // Clear the overflow interrupt flag. If a rising edge appears on INTF, an interrupt will be generated, so it must be cleared after the interrupt occurs. INTF=0; // Execute the interrupt handler to execute the function you want to execute when the interrupt occurs RD1=1; // When an external interrupt occurs, the LED lights up } }
Previous article:PIC16F877A microcontroller (ADC)
Next article:PIC16F877A microcontroller (external interrupt)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Giant microbit (giga:bit)
- Renesas CPK-RA6M4 Development Board Review + Unboxing and Building Environment
- Has anyone used the ionizing radiation sensor module? Discuss some technical issues
- RS-485 communication interface encoder
- FPGA Implementation of Fully Parallel FFT
- RT-Thread has been ported to W600
- MakeCode now supports STM103
- Please help me with the trip zone configuration of TI's 28034!!!
- Is the threshold for electromagnetic wave and antenna major getting lower and lower? Is the reliance on simulation software getting higher and higher?
- The input capture of stm8s cannot enter the interrupt. Please help? ? Thank you!!!