PIC16F72 timer C program

// APPLICATION NOTE E6000 IC EXPLORER *************** // *Title: FOR clock_time* // *Version: 00* // *Last Updated: 2006.06.29* // *MCU: PIC16F72* // *************************************************** // **ROM PCLATH.D3=0000H-7FFH; // **PCLATH.D3=1800H-FFFH; // *************************************************** // **RAM BANK0=20H---7FH STATUS RP0=0, RP1=0 // **BANK1=A0H---FFH STATUS RP0=1, RP1=0 // *************************************************** // ___________________ // MCLR/VPP/TH -|128|- RB7/PGD // RA0/AN0 -|227|- RB6/PGC // RA1/AN1 -|326|- RB5 // RA2/AN2/VRref --|425|- RB4 // RA3/AN3/VRref+- |524|- RB3/PGM // RA4/TOCKI -|623|- RB2 // RA5/AN4/SS -|722|- RB1 // VSS -|821|- RB0/INT // OSC1/CLKIN -|920|- VDD // OSC2/CLKOUT -|1019|- VSS // RC0/TISO/TICKI -|1118|- RC7/RX/DT // RC1/T1OSI/CCP2 -|1217|- RC6/TX/CK // RC2/CCP1 -|1316|- RC5/SDO // RC3/SCK/SCL -|1415|- RC4/SDA // |_________________| // 16F72 // *************************************************** #include #include #include #define uchar unsigned char #define uint unsigned int //------------------------------------------ #define KEY_UP RC1 #define KEY_DOWN RA4 #define KEY_SET RC2 //------------------------------------------ void com_buf(void); void display(void); void buf_initial(void); void ad_program(void); void add_colk(void); void keywork1(void); void PORT_DATA(void); void sub_colk(void); void timr0int(void); void colk_stt(void); //------------------------------------------ static volatile unsigned char ADRES @ 0x1E; static volatile unsigned char FLAG1 @ 0x20; // bit addressable status register static volatile bit SET @(unsigned) & FLAG1 * 8 + 2; static volatile bit UP @(unsigned) & FLAG1 * 8 + 1; static volatile bit DOWN @(unsigned) & FLAG1 * 8 + 0; //---------------------------------------------------- static bit B_JIAN0, B_JIAN1, B_JIAN2, B_JIAN3, B_ON, B_OFF, B_BUF, B_ADD; static bit B_DP, B_COM, B_END; // unsigned char buf[4]; unsigned char k, data, sreg, disppoint, msec, sec, ad_buf, SEL, spbuf; unsigned char T_ON0, T_ON1, T_ON2, T_ON3, T_OFF0, T_OFF1, T_OFF2, T_OFF3; unsigned char colk0, colk1, colk2, colk3; unsigned int i, kcon1, kcon2, PORT_BUF; const char table[10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xd8, 0x80, 0x90}; //********************************* /* delay function */ //********************************* void delay_10ms() { uint j; for (j = 0; j < 300; j++) { ; } } //********************************* void delay_50us() { uchar j; for (j = 0; j < 20; j++) { ; } } //========================================== // Delay maximum 255 milliseconds //========================================== void delay_ms(uchar ms) { uchar i, j; for (j = 0; j < ms; j++) { for (i = 0; i < 86; i++) { #asm Nop Nop #endasm } } } //=========================================== void delay_1s() { uint j; for (j = 0; j < 200; j++) { Display(); Keywork1(); } } //********************************* void ds_sp() { uchar i; uint j; for (i = 0; i < 100; i++) { for (j = 0; j < 20; j++) { Display(); Keywork1(); } RA2 = !RA2; } RA2 = 0; } //********************************* void sp() { uchar i; for (i = 0; i < 20; i++) { if (B_END == 1) { B_COM = 1; return; } ds_sp(); delay_1s(); } } //******************************* // Display clear 0 program //******************************* void buf_initial() { buf[0] = 0x00; buf[1] = 0x00; buf[2] = 0x00; buf[3] = 0x00; T_ON0 = 0x00; T_ON1 = 0x07; T_ON2 = 0x00; T_ON3 = 0x00; T_OFF0 = 0x00; T_OFF1 = 0x07; T_OFF2 = 0x00; T_OFF3 = 0x05; sreg = 0x00; msec = 0x00; } //********************************** void put_num(char c) { PORTB = table[c & 0x0f]; RB7 = 0; if (B_DP == 0) { RB7 = 1; } } //********************************* void display() { disppoint = 0; put_num(buf[disppoint]); RC4 = 0; delay_ms(1); RC4 = 1; disppoint++; put_num(buf[disppoint]); RC5 = 0; delay_ms(1); RC5 = 1; CLRWDT(); disppoint++; put_num(buf[disppoint]); RC6 = 0; delay_ms(1); RC6 = 1; disppoint++; put_num(buf[disppoint]); RC7 = 0; delay_ms(1); RC7 = 1; } //********************************* void ad_program() { TRISA = 0xFF; ADCON1 = 0B00000010; // AN0-AN4=AVREF=VDD PIE1 = 0; ADCON0 = 0B01001001; // fosc/8 RA1/AN1 ADON=1 INTCON = 0; ADIF = 0; ADRES = 0; delay_50us(); ADGO = 1; // GODONE=1 while (ADIF == 0); { ; } ad_buf = ADRES; ADCON1 = 0B00000111; // RA0-RA5=D TRISA = 0x38; // RA3-RA5=IN ADCON0 = 0B00000000; // fosc/2 RA0/AN0 ADON=0 } //********************************* void timr0int() { T0CS = 0; // timer mode PSA = 1; // no prescaler T0IF = 0; // clear flag T0IE = 1; // enable interrupt } //********************************** void keywait() { while ((PORTC & 0x06) != 0x06) { Display(); } } //********************************** void keywait1() { while ((PORTA & 0x10) != 0x10) { Display(); } } //***************************************** // Current time and boot, shutdown time comparison //***************************************** void com_buf() { if ((T_ON0 == buf[0]) && (T_ON1 == buf[1]) && (T_ON2 == buf[2]) && (T_ON3 == buf[3])) { RC3 = 1; return; } else if ((T_OFF0 == buf[0]) && (T_OFF1 == buf[1]) && (T_OFF2 == buf[2]) && (T_OFF3 == buf[3])) { RC3 = 0; B_COM = 1; sp(); return; } } //****************************************** void add_colk() { CLRWDT(); buf[3]++; B_COM = 0; B_END = 0; if (buf[3] > 9) { buf[3] = 0; buf[2]++; if (buf[2] > 5) { buf[2] = 0; buf[1]++; if (buf[1] == 4) { if (buf[0] == 2) { buf[0] = 0; buf[1] = 0; return; } return; } else if (buf[1] > 9) { buf[1] = 0; buf[0]++; } } } } //********************************** void sub_colk() { CLRWDT(); if (buf[3] != 0) { buf[3]--; return; } buf[3] = 9; if (buf[2] != 0) { buf[2]--; return; } buf[2] = 5; if (buf[1] != 0) { buf[1]--; return; } buf[1] = 9; if (buf[0] != 0) { buf[0]--; return; } buf[1] = 3; buf[0] = 2; } //*************************************************** void colk_to_time_on() { T_ON0 = buf[0]; T_ON1 = buf[1]; T_ON2 = buf[2]; T_ON3 = buf[3]; } //--------------------------------------------------- void colk_to_time_off() { T_OFF0 = buf[0]; T_OFF1 = buf[1]; T_OFF2 = buf[2]; T_OFF3 = buf[3]; } //--------------------------------------------------- void colk_to_disp_buf() { colk0 = buf[0]; colk1 = buf[1]; colk2 = buf[2]; colk3 = buf[3]; } //*************************************************** void colk_stt() { CLRWDT(); if (RC2 == 0) { B_ADD = 1; } if (RC1 == 0) { B_ADD = 0; } if (B_ON == 1) { if (B_ADD == 0) { add_colk(); colk_to_time_on(); return; } sub_colk(); colk_to_time_on(); return; } if (B_OFF == 1) { if (B_ADD == 0) { add_colk(); colk_to_time_off(); return; } sub_colk(); colk_to_time_off(); return; } if (B_BUF == 1) { if (B_ADD == 0) { add_colk(); colk_to_disp_buf(); return; } sub_colk(); colk_to_disp_buf(); return; } } //********************************************* void PORT_DATA() { TRISC = 0X01; // RC7--RC1 clears 0 as output, RC0 sets 1 as input RA0 = 1; // open LED display PORTC = PORT_BUF; // Restore I/O port status after button scan is completed } //********************************************* // Key processing subroutine //********************************************* void keywork1() { PORT_BUF = PORTC; // Protect I/O port status during key scan TRISC = 0X07; // RC0, RC1, RC2 set to 1 as input RC1 = 1; RC2 = 1; // RC1, RC2 is set to 1 RA0 = 0; // off LED display if (B_JIAN1 == 1) { JIAN_add: if (B_JIAN0 == 0) { if (B_JIAN1 == 0) { delay_10ms(); if ((RC2 == 0) || (RC1 == 0)) { B_JIAN1 = 1; CLRWDT(); JIAN_add1: if ((RC2 == 0) || (RC1 == 0)) { if (RC2 == 0) { B_ADD = 1; } if (RC1 == 0) { B_ADD = 0; } if ((--kcon1) == 0) { kcon1 = 500; B_JIAN0 = 1; colk_stt(); PORT_DATA(); return; } else { PORT_DATA(); return; } } B_JIAN0 = 0; B_JIAN1 = 0; B_JIAN2 = 0; B_JIAN3 = 0; colk_stt(); PORT_DATA(); return; } else { PORT_DATA(); return; } } goto JIAN_add1; } if ((RC2 == 0) || (RC1 == 0)) { if ((--kcon1) == 0) { kcon1 = 20; colk_stt(); PORT_DATA(); return; } else { PORT_DATA(); return; } } B_JIAN0 = 0; B_JIAN1 = 0; B_JIAN2 = 0; B_JIAN3 = 0; PORT_DATA(); return; } else if ((RC2 == 0) || (RC1 == 0)) { goto JIAN_add; } else if ((PORTA & 0x10) != 0x10) { delay_10ms(); if ((PORTA & 0x10) != 0x10) { Di(); B_DP = 0; B_COM = 1; B_END = 1; SEL++; if (SEL == 0X01) { Keywait1(); TRISC = 0X01; RA0 = 1; RC1 = 0; RC2 = 1; buf[0] = T_ON0; buf[1] = T_ON1; buf[2] = T_ON2; buf[3] = T_ON3; B_ON = 1; B_OFF = 0, B_BUF = 0; return; } if (SEL == 0X02) { Keywait1(); TRISC = 0X01; RA0 = 1; RC1 = 1; RC2 = 0; buf[0] = T_OFF0; buf[1] = T_OFF1; buf[2] = T_OFF2; buf[3] = T_OFF3; B_ON = 0; B_OFF = 1, B_BUF = 0; return; } if (SEL == 0X03) { Keywait1(); TRISC = 0X01; RA0 = 1; RC1 = 1; RC2 = 1; buf[0] = colk0; buf[1] = colk1; buf[2] = colk2; buf[3] = colk3; SEL = 0x00; sreg = 0x00; msec = 0x00; sec = 0x00; B_ON = 0; B_OFF = 0, B_BUF = 1; ei(); TMR0 = 0X15; return; } } } B_JIAN0 = 0; B_JIAN1 = 0; B_JIAN2 = 0; B_JIAN3 = 0; kcon1 = 20; PORT_DATA(); CLRWDT(); } //************************************************ *********** //************************************************ *********** // Key processing subroutine //************************************************ *********** void keywork() { if ((PORTC & 0x06) != 0x06) { delay_10ms(); if ((PORTC & 0x06) != 0x06) { if (KEY_SET == 0) { keywait(); ei(); TMR0 = 0X08; return; } else if (KEY_UP == 0) { keywait(); buf_initial(); Di(); return; } } } else if ((PORTA & 0x10) != 0x10) { delay_10ms(); if ((PORTA & 0x10) != 0x10) { Di(); keywait1(); add_colk(); return; } } } //******************************* //****** Tmr0 interrupt program *********** //******************************* void interrupt clkint(void) { TMR0 = 0x15; T0IF = 0; CLRWDT(); sreg = sreg + 1; if (sreg == 40) { sreg = 0; msec = msec + 1; if (msec > 99) { msec = 0; B_DP = !B_DP; sec = sec + 1; if (sec > 59) { sec = 0; add_colk(); } } } } //******************************* // Main function //******************************* void main(void) { OPTION = 0; STATUS = 0; ADCON1 = 0x07; INTCON = 0; PIE1 = 0; PIR1 = 0; T1CON = 0; TRISA = 0x10; TRISB = 0X00; TRISC = 0X01; PORTB = 0x00; PORTC = 0X07; PORTA = 0x10; B_DP = 0; B_COM = 0; B_END = 0; B_ON = 0; B_OFF = 0, B_BUF = 1; timr0int(); buf_initial(); Di(); while (1) { CLRWDT(); Display(); keywork1(); if (B_COM == 0) { com_buf(); } } }

Automotive Push Button Switches

Automotive Push Button Switches

Automotive Push Button Switches, also called Push Button Starter Switch, is one kind of switches to turn on the car. As for the Automotive Switches, we have Automotive Push Button Switches, Automotive Toggle Switches, Automotive Rocker Switches, Automotive Rotary Switches and Automotive Battery Switches for our customers to choose.

The Starter button switches mainly based on the car button switch. The main function is to start the car, switch on the car power supply or start the car internal equipment. The automotive starter switch is mainly based on DC current and voltage, and has a wide range of dimensions. The installation size ranges from 8.5mm to 22mm directly.


Automotive Push Button Switches


Our push button switches can be matched with the dust proof and waterproof cap. When the car equipment is working in a harsh environment, our Yeswitch car start button switch could still be regularly used.

Our variety of Automotive Momentary Push Button Switch , can match different colors and different appearances of dust-proof and waterproof caps, dust-proof and waterproof grade can reach IP67 grade. Along with excellent electrical life times, high load current and voltage, our Yeswitch start button switch is not limited to automotive field , heavy machinery, and ships are also widely recognized by customers.


Push Button Switches

Automotive Push Button Switch,Push Button Switch,Automotive Switch,Automotive Switches,Micro Push Button Switch,Mini Push Button Switch

YESWITCH ELECTRONICS CO., LTD. , https://www.yeswitches.com