//采用芯片为:PIC16F877A
//实现LED灯的顺启逆停与急停
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
#define IN 4 //定义进去时间为4s
#define OUT 3 //定义3s后出来
#define DOWN 6 //定义下压时间为6s
__CONFIG(0X3F32);
uchar LEIJIA,SEC; //声明变量;累加,秒
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=123;j>0;j--);
}
void init_IO()
{
TRISB = 0xFF; //按键为输入
TRISC = 0X00; //RC口输出为LED
// TRISE = 0X00;
RC1 = 0;
RC2 = 1;
}
void init_timer0()
{
GIE = 1;
PEIE = 0;
T0IE = 0;
T0IF = 0;
T0CS = 0;
T0SE = 0;
PSA = 0;
PS2 = 1;PS1 = 1;PS0 = 1;
LEIJIA = 0;
}
void start_timer0()
{
GIE = 1;
PEIE = 1;
T0IE = 1;
T0IF = 0;
}
void stop_timer0()
{
T0IE = 0;
T0IF = 0;
TMR0 = 0X00;
LEIJIA = 0;
}
void interrupt ISR()
{
TMR0 = 70;
if(T0IF == 1)
T0IF = 0;
LEIJIA ++;
if(LEIJIA == 20) //定时到1s
{
LEIJIA = 0;
SEC--;
}
}
void key_scan()
{
if((RB0) == 0) //按下按钮1(启动)
{
delay(2);
if((RB0) == 0)
{
while(!RB0);
RC1 = 0; //LED1----亮
SEC = IN; //亮4s
start_timer0();
while(!(SEC == 0));
stop_timer0();
RC2 = 0; //LED2----亮
SEC = DOWN; //亮6s
start_timer0();
while(!(SEC == 0));
stop_timer0();
RC1 = 1; //LED1----灭
SEC = OUT; //亮3s
start_timer0();
while(!(SEC == 0));
stop_timer0();
RC2 = 1; //LED2----灭
if(RB1 == 0) //按下按钮2(急停)
{
delay(10);
if(RB1 == 0)
{
while(!RB1);
RC1 = 1; //LED1----灭
RC2 = 1; //LED2----灭
}
}
}
}
}
void main()
{
init_IO();
init_timer0();
start_timer0();
stop_timer0();
while(1)
{
key_scan();
}
}
|