pic16f54可调LED亮度程序
按钮控制LED亮度 芯片PIC16F54 晶振4M#include<pic.h>
#define ucharunsigned char
#define uint unsigned int
__CONFIG (XT&WDTEN&CP) ;
#define LEDRB1
#defineK1 RB3
#defineK2 RB2
#defineKEY PORTB
#definekeyi0B1100
uchar LED_pc,h,l,LEDH,LEDL;
staticbitHL_bit;
static volatilebit TM_RBit @ ((unsigned)&TMR0*8)+5;
void TMR0_interrupt(void);
void TMR0_interrupt()
{if(TM_RBit)
{ TM_RBit=0;
if(HL_bit){if(h>0)h--; }
else {if(l>0)l--; }
}
else return ;
}
void main()
{ uchar i;
OPTION=0B00000000; // 预分频器分配给定时器0
TRISB=0B1101;
LEDH= h=0;
LEDL=l=255;
while(1){ CLRWDT();
TMR0_interrupt();
if(HL_bit){if(h>0)LED=1;else {l=LEDL;HL_bit=0;}}
else {if(l>0)LED=0;else {h=LEDH;HL_bit=1;}}
if((KEY&keyi)!=keyi)//按健扫描
{i--;
if((i==0) )
{
if(!K1&&LEDH<255){ LEDH++;LEDL--;}
if(!K2&&LEDL<255){ LEDH--;LEDL++;}
}
}
else i=0;//按健扫描计数器清0
}
}
页:
[1]