英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
pic16f54可调LED亮度程序
[打印本页]
作者:
star
时间:
2012-8-20 10:50
标题:
pic16f54可调LED亮度程序
按钮控制LED亮度 芯片PIC16F54 晶振4M
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG (XT&WDTEN&CP) ;
#define LED RB1
#define K1 RB3
#define K2 RB2
#define KEY PORTB
#define keyi 0B1100
uchar LED_pc,h,l,LEDH,LEDL;
static bit HL_bit;
static volatile bit 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
}
}
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2