//*APPLICATION NOTE E6000 ICEXPLORER ***************
//* Title: FOR colk_time www.picavr.com *
//* Version: 00 *
//* Last Updated: 2006.06.29 *
//* MCU: PIC16F72 *
//***************************************************
//****ROM PCLATH.D3=0 000H-7FFH ;
// PCLATH.D3=1 800H-FFFH ;
//***************************************************
//** RAM BANK0=20H---7FH STATUS RP0=0,RP1=0 *
//** BANK1=A0H---FFH STATUS RP0=1,RP1=0 *
//***************************************************
// __________________
// MCLR/VPP/TH-| 1 28 |-RB7/PGD
// RA0/AN0-| 2 27 |-RB6/PGC
// RA1/AN1-| 3 26 |-RB5
// RA2/AN2/VRref--| 4 25 |-RB4
// RA3/AN3/VRref+-| 5 24 |-RB3/PGM
// RA4/TOCKI-| 6 23 |-RB2
// RA5/AN4/SS-| 7 22 |-RB1
// VSS-| 8 21 |-RB0/INT
// OSC1/CLKIN-| 9 20 |-VDD
// OSC2/CLKOUT-| 10 19 |-VSS
// RC0/TISO/TICKI-| 11 18 |-RC7/RX/DT
// RC1/T1OSI/CCP2-| 12 17 |-RC6/TX/CK
// RC2/CCP1-| 13 16 |-RC5/SDO
// RC3/SCK/SCL-| 14 15 |-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 // www.picavr.com
//------------------------------------------
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; //可位寻址的状态寄存器
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}; //
//*********************************
/*延时函数*/
//*********************************
void delay_10ms() //
{ uint j; //
for(j=0;j<300;j++) //
{;} //
} //
//*********************************
void delay_50us() //
{ uchar j; //
for(j=0;j<20;j++) //
{;} //
} //
//=================================
//延时最大255毫秒
//=================================
void delay_ms(uchar ms) //
{
uchar i,j; //
for(j=0;j
{
#asm //
nop //
nop //
#endasm //
}
}
//==================================
//**********************************
void delay_1s() //延时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(); //
}
}
//*******************************
//显示清0程序
//*******************************
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; //
}
//*********************************
//*********************************
//AD转换初始化子程序 //
//*********************************
void ad_program() //
{
TRISA=0XFF; //
ADCON1=0B00000010; //AN0-AN4=A VREF=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
}
//*********************************
//*********************************
//tmr0 initialization subroutine
//*************************************
void timr0int()
{
T0CS=0; //tmr0 works in timer mode
PSA=1; //tmr0 does not need frequency division
T0IF=0; //clear tmr0 interrupt flag
T0IE=1; //tmr0 interrupt enable
}
//**************************************
//**********************************
//Key waiting program
//**************************************
void keywait() //
{
while((PORTC & 0x06)!=0x06) //
{
display(); //
} //
} //
//**************************************
void keywait1() //
{
while((PORTA & 0x10)!=0x10) //
{
display(); //
} //
} //
//****************************************
//*****************************************
//Compare current time with power-on and power-off time
//*****************************************
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; //
} //
} //
//**********************************************
//******************************************
//Clock display program
//**********************************************
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清0为输出,RC0置1为输入
RA0=1; //开LED显示
PORTC=PORT_BUF; //按键扫描完成后恢复I/O口状态
}
//*********************************************
//按键处理子程序 //
//*********************************************
void keywork1()
{
PORT_BUF=PORTC; //在按键扫描期间保护I/O口状态
TRISC=0X07; //RC0,RC1,RC2置1为输入
RC1=1;RC2=1; //RC1,RC2置1
RA0=0; //关LED显示
if(B_JIAN1==1) //连加标志位 B_JIAN1=1
{
//-------------------------------------------------------
JIAN_add: if(B_JIAN0==0) //按键按下时间为3S后置B_JIAN0=1 ,B_JIAN1=0为点动按键
{
if(B_JIAN1==0) //B_JIAN1=1时, 计算第一次按下的时间,B_JIAN1=0为点动按键
{
delay_10ms();
if((RC2==0)||(RC1==0))
{
B_JIAN1=1; //KEY_UP 按下,B_JIAN1=1
CLRWDT(); //
//______________________________________________________
JIAN_add1: if((RC2==0)||(RC1==0)) //
{
if(RC2==0) //
{
B_ADD=1; //Time subtraction flag
}
if(RC1==0) //
{
B_ADD=0; //Time addition flag
}
if((--kcon1)==0) //
{
kcon1=500; //Set the count value of the first key press
B_JIAN0=1; //
colk_stt(); //Time addition
PORT_DATA(); //
return; //Subroutine returns
}
else
{
PORT_DATA(); //
return;//Subroutine returns
}
}
B_JIAN0=0;B_JIAN1=0;B_JIAN2=0;B_JIAN3=0; //Clear key flag
colk_stt(); // Time + 1
PORT_DATA(); //
return;//Subroutine returns
}
else
{
PORT_DATA(); //
return; //Subroutine returns
}
}
goto JIAN_add1; // When B_JIAN1=1, go to JIAN_add1 to calculate the time RB7 is pressed
//_________________________________________________________________________
}
if((RC2==0)||(RC1==0)) //
{
//_________________________________________
if((--kcon1)==0) //
{
kcon1=20; //Set the speed count value when the key is pressed continuously
colk_stt(); //
PORT_DATA(); //
return;//Subroutine returns
}
else
{
PORT_DATA(); //
return; //Subroutine returns
}
}
B_JIAN0=0;B_JIAN1=0;B_JIAN2=0;B_JIAN3=0;
PORT_DATA(); ///
return;
}
//---------------------------------------------
else if((RC2==0)||(RC1==0)) //Compare whether the KEY_UP key is pressed or not
{
goto JIAN_add; //RB7=0, KEY_UP is pressed, go to JIAN_ADD
}
//----------------------------------------------
else if((PORTA&0x10)!=0x10) //Reread P1.2-P1.4 bits, if they are still not all 1, a key must be pressed
{
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; //Open LED display
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
= 0P 01 ; _ON= 0; B_OFF =0, B_BUF=1 ; // ei(); // turn on the total interrupt TMR0=0X15 ; //TMR0 reset return; } } } // ---------------------------------------------------------- B_JIAN0=0;B_JIAN1 =0;B_JIAN2=0;B_JIAN3=0 ; //clear the key flag kcon1=20; // PORT_DATA(); // CLRWDT () ; // } //***********************************************************
//***************************************************************
//Key processing subroutine //
//***********************************************************
void keywork() //
{
if((PORTC&0x06)!=0x06) //If P1.2--P1.4 are not all 1s, a key may be pressed
{
delay_10ms(); //Delay de-bounceif
((PORTC&0x06)!=0x06) //Reread P1.2-P1.4 bits. If they are still not all 1s, a key must be pressed
{
//----------------------------------
if (KEY_SET==0) //Press the SET key. If in the SET state, exit, otherwise enter
{
keywait();
ei(); //
TMR0=0X08; //
return; //
}
else if (KEY_UP==0) //Press the UP key
{
keywait(); //
buf_initial(); //
di(); //
return;
}
}
}
else if((PORTA&0x10)!=0x10) //If P1.2--P1.4 are not all 1, a key may be pressed
{
delay_10ms(); //Delay de-bounceif
((PORTA&0x10)!=0x10) //Reread P1.2-P1.4 bits. If they are still not all 1, a key must be pressed
{
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; // RA0-RA5=D
INTCON=0; //
PIE1 =0 ; //
PIR1 =0; //
T1CON =0; //
TRISA=0x10; //00111000= RA3,RA4,RA5
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(); //
}
}
}
Previous article:PIC microcontroller serial port debugging program
Next article:pic MCU DS18B20 sampling LCD1602 display
- Popular Resources
- Popular amplifiers
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
- 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
- Practical Application Skills of DC Motors
- Minimalist USB TYPE-C PD3.0/3.1 spoof trigger solution
- [Synopsys IP Resources] Using the ARC SEM130FS processor to meet the safety protection requirements of new automotive SOCs
- 【MicroPython】Using MCO as a clock
- Good news! Owning an ultra-high performance UXR series oscilloscope is no longer a dream!
- Gallium Nitride—Not Just for Defense Anymore
- Solution design: an electronic smart lock with integrated Wi-Fi
- SVPWM source code and comments
- [STM32F769Discovery development board trial] Serial port idle interrupt indefinite length reception & PWM output square wave
- Source code CCS compilation error