專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

PIC單片機(jī)一個(gè)實(shí)用的按鍵處理程序

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年10月20日   【字體:
程序?qū)崿F(xiàn)功能:按鍵調(diào)光,按鍵帶蜂鳴器響聲。

 #include<pic.h>
__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--);
}
}
void main()
{
unsigned char PWM=0;
RC2=0;
TRISC=0b00100000;
while(1)
{
key(&PWM);
tiaoguang(PWM);
}
 
}

程序思路:設(shè)定PWM變量,每按一次,PWM增加50個(gè)單位,加到到250,歸零。
關(guān)閉窗口

相關(guān)文章