PIC12F629 can operate normally without external vibration, it is the first choice for low cost, the principle is as follows: press the button once, LED1 is low light, press twice, LED2 is bright, press three times, LED3 is bright, keep cycling, long press the button for 3 seconds and release it, it can be counted into the automatic cycle mode, long press again for 3 seconds to exit and enter the standby mode; Pin 2 is connected to the ground with a touch switch, which can be used as a setting key (the schematic diagram comes from the Internet, so VDD is connected to +5V, missing, SET setting key is not drawn, I will correct the schematic diagram when I have time), used to set the time for each LED to light up, without affecting each other! The source code has been compiled, and a PCB is made of the perforated board, it is operating normally, please do not spray if you are new to it.
The microcontroller source program is as follows:
/******************************************************************************
Design requirements: Press the button once to open the first camera, press it twice to open the second camera, and press it three times to open the third camera.
Long press for 3 seconds to start the cycle mode, each time for 30 seconds, and it will cycle until the button is pressed.
* IC: PIC12F629 OSC: On-chip 4MHz
*****************************************************************************/
#include #define uchar unsigned char #define uint unsigned int #define LED1 GPIO0 #define LED2 GPIO1 #define LED3 GPIO2 #define KEY GPIO4 #define SET GPIO5 __CONFIG(FOSC_INTRCIO & WDTE_ON & PWRTE_ON & MCLRE_OFF & BOREN_ON); /********* Configuration word************/ void interrupt ISR( void) ; void delay(uint x); void IOinit (void) ; void TMR1init(void) ; uint time = 0; uint t =0; fly cnt =0; fly cnt1 = 0; fly cnt2 = 0; fly i , k = 0; void IOinit (void) { PCON = 0X00; // Power control all off CMCON = 0X07; //Turn off the simulator TRISIO = 0X38; // 00111000 GP<5:3> input GP<2:0> output GPIO = 0X3F; // 00111111 GP<2:0> outputs high level WPU = 0X30; // GP<5:4> internal weak pull-up is enabled IOC = 0X30; //GP<5:4> port level change interrupt enable } void TMR1init (void) { OPTION_REG = 0X0F; // PSA =1, PS2:PS0 =111, watchdog timer enabled, prescaler 128, others disabled INTCON = 0XC0 ; // GIE = 1 , PEIE = 1 Enable global interrupt Enable peripheral interrupt Disable the rest PIE1 = 0X01; // Peripheral interrupt enable PIR1 = 0X00; //TMR1IF = 1 Turn on TIMR1 peripheral interrupt flag, which needs to be cleared by software TMR1H = 0X3C; //TIMER1 initial overflow time is 50000 (50ms) TMR1L = 0XB0; // 65536- 50000 = 15536 (0x3CB0) T1CON = 0X01; // Open TIMR1 } void main ( ) { IOinit () ; TMR1init() ; //Main loop while(1) { if(i==0 ) { if(SET==0) { delay(10); if(SET==0) { while(!SET); k=!k; cnt=0; cnt1=0; LED3=!LED3; } } } if(KEY == 0) //If the key is pressed { delay(20); if(KEY == 0) { time = 0; //delay flag clear while(!KEY); //Wait for the key to be released, counted by the interrupt function if(time<40) //The pressing time is less than 50ms*40=2000 milliseconds (2 seconds) { if (k==0) { cnt1++; //Increase the number of key presses by 1, change the working status if(cnt1>3) cnt1 = 0; } else if( k==1) { cnt++; if (cnt>10) cnt=0; t=cnt*100; LED1 =0; delay(20); LED1 =1; } } if(time >= 40 && time < 100) //Press and hold for a period between 2 and 5 seconds { i = !i; time =0; cnt2 =0; cnt1=0; } else if (time >=100) { break; } } } if (k==0 ) { if (i == 1) { switch (cnt2) { case 0: GPIO =0X3E; //00111110 turn on relay 1 break; case 1: GPIO =0X3D; //00111101 turn on relay 2 break; case 2: GPIO =0X3B; //00111011 turn on relay 3 break; default: break; } } if (i==0 ) { switch(cnt1) //Detect the number of times a button is pressed { case 0: GPIO = 0x3F; break; // No key is pressed, all closed case 1: GPIO =0X3E; //00111110 turn on relay 1 break; // Click to turn on RELAY1 2,3 off case 2: GPIO =0X3D; //00111101 turn on relay 2 break; case 3: GPIO =0X3B; //00111011 turn on relay 3 break; default: break; } } } } } /****************************************************************************** * Interrupt handling function *****************************************************************************/ void interrupt ISR( void) { if(TMR1IF == 1) //Timer 1 overflow interrupt { asm ("CLRWDT"); //Feed the dog regularly ++time; //delay count if (time == t) //t is the set time { time = 0; cnt2++; if( cnt2 > 2 ) // Each increment is t seconds { cnt2 = 0; } } TMR1H = 0x3C; //Reset the initial value of TIMER1 to make the TMR1 overflow time 50000 clock cycles TMR1L = 0xB0; TMR1IF = 0; //Timer 1 overflow interrupt flag cleared } } void delay(uint x) { uint a,b; for(a=x;a>0;a--) for(b=71;b>0;b--); }
Previous article:PIC18F, ADC multi-channel acquisition, timing acquisition
Next article:Multifunctional electronic clock based on PIC16F877 microcontroller
- 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
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- AD20 Select PCB Components cannot be used?
- Newly purchased MicroPython development board
- STM32F 7508DK I2C BH1570 driver development and I2C usage
- C2000 MCU, Vienna Rectifier-Based Three-Phase Power Factor Correction Reference Design
- The 5G era has arrived. This is a rare opportunity. What ideas do you have to share?
- General method of displaying pictures and texts on TFT color screen
- Motor inductance problem
- I need a simulation diagram of a car reversing radar system (paid)
- Microcontroller touch dimming desk lamp program
- An interview question