PIC單片機一個實用的按鍵處理程序
#include
__CONFIG(0X034);
#define KEY RC5
#define LED RC2
int b;
void key(unsigned *p)
{
static bit valid=0;
static bit finish=0;
if(valid==0)
{
if(KEY==0)
{
if(finish==0)
{
finish=1;
RC0=1;//蜂鳴器接這里
b=50;
while(b--);
RC0=0;
if(*p>251)
{
*p=0x01;
}
else
{
*p+=50;
}
}
}
else
{
valid=0;
finish=0;
}
}
else
{
if(KEY==0)valid=1;
}
}
void tiaoguang(unsigned PWM)
{
unsigned char PwmH;
unsigned char PwmL;
unsigned char i;
for(i=0;i<20;i++)
{
PwmH=PWM;
PwmL=255-PWM;
LED=1;
while(PwmH--);
LED=0;
while(PwmL--);
while(PwmH--);
LED=0;
while(PwmL--);
}
}
void main()
{
unsigned char PWM=0;
RC2=0;
TRISC=0b00100000;
while(1)
{
key(&PWM);
tiaoguang(PWM);
void main()
{
unsigned char PWM=0;
RC2=0;
TRISC=0b00100000;
while(1)
{
key(&PWM);
tiaoguang(PWM);
}
}
程序思路:設(shè)定PWM變量,每按一次,PWM增加50個單位,加到到250,歸零。
評論