英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
用C延时的程序
[打印本页]
作者:
winnie
时间:
2009-5-21 17:21
标题:
用C延时的程序
#include <pic.h>
#include <pic165x.h>
#include <math.h>
#include <stdio.h>
//////////////////////////////////////////////////////////
bit buzzerbit,
timepd,
secbit,
dsbit,
onoffbit,
keybit1,
keybit2;
//////////////////////////////////////////////////////////
unsigned char
keysec,
timecount,
timesave,
modecount,
feelcount,
feelsec,
racount;
/////////////////////////////////////////////////////////
static bank1 int buzzercount,
mscount,
bedmin,
sec;
//////////////////////////////////////////////////////////
unsigned char dispcount[5];
///////////////////////////////////////////////////////////
#define PORTCIT(adr,bit) ((unsigned)(&adr)*8+(bit))
static bit lowfanbit @ PORTCIT(PORTC,0);
static bit coolbit @ PORTCIT(PORTC,7);
#define PORTBIT(adr,bit) ((unsigned)(&adr)*8+(bit))
static bit feelbit @ PORTBIT(PORTB,2);
static bit oxybit @ PORTBIT(PORTB,3);
static bit swingbit @ PORTBIT(PORTB,4);
static bit bedbit @ PORTBIT(PORTB,5);
static bit hightfanbit @ PORTBIT(PORTB,6);
static bit midfanbit @ PORTBIT(PORTB,7);
////////////////////////////////////////////////////////////
main( )
{
csh( );
while(1)
{
CLRWDT( );
OPTION=7;
TRISA=0B1111;
TRISB=0;
TRISC=0B01000000;
dskgj( );
timetest( );
buzzer( );
timetest( );
key( );
}
}
////////////////////////////////////////////////////////////
timetest( )//定时查询;因16c5X类芯片无中断故在这儿做了一个定时查询
{
if(timepd==0)
{
timesave=RTCC;
timepd=1;
return;
}
if((timepd==1)&&(timesave!=RTCC))
{
timepd=0;
rftime++;
mscount++;
keysec++;//防键抖定时
if(mscount==3906)
{
secbit=1;
mscount=0;//秒加一
}
}
}
////////////////////////////////////////////////////////////
key( )//按键
{
if(keysec<100)//此为防键抖延时
{
return;
}
keysec=0;
RB1=0;
RB0=1;
#asm
nop
nop
nop
nop
#endasm
racount=PORTA;
switch(racount)
{
case 0b0001 :
if((keybit1==1)||(onoffbit==0))
{
return;
}
keybit1=1;
buzzerbit=1;
oxybit=oxybit^1;
break;
case 0b0100 :
if((keybit1==1)||(onoffbit==0))
{
return;
}
keybit1=1;
buzzerbit=1;
swingbit=swingbit^1;
break;
case 0b0010 :
if((keybit1==1)||(onoffbit==0))
{
return;
}
keybit1=1;
buzzerbit=1;
coolbit=coolbit^1;
break;
case 0b1000 :
if((keybit1==1)||(onoffbit==0))
{
return;
}
keybit1=1;
rfbit=1;
dispcount[1]=0;
dispcount[2]=0xff;
dispcount[3]=0x8c;
break;
default :
keybit1=0;
break;
}
RB0=0;
RB1=1;
#asm
nop
nop
nop
nop
#endasm
racount=PORTA;
switch(racount)
{
case 0b0010 :
if(keybit2==1)
{
return;
}
keybit2=1;
rfbit=1;
dispcount[1]=0;
dispcount[2]=0xff;
dispcount[3]=0x94;
break;
case 0b0100 :
if((keybit2==1)||(onoffbit==0))
{
return;
}
keybit2=1;
rfbit=1;
dispcount[1]=0;
dispcount[2]=0xff;
dispcount[3]=0x90;
break;
case 0b1000 :
if((keybit2==1)||(onoffbit==0))
{
return;
}
keybit2=1;
rfbit=1;
dispcount[1]=0;
dispcount[2]=0xff;
dispcount[3]=0x80;
break;
default :
keybit2=0;
return;
}
}
///////////////////////////////////////////////////////////////
dskgj( )
{
if(secbit==1)
{//秒计时
secbit=0;
if(dsbit==1)
{
sec=sec+1;
if(sec==1800)
{
sec=0;
timecount=timecount-1;
}//定时关机
if(timecount==0)
{
onoffbit=onoffbit^1;
dsbit=0;
timecount=0;
return;
}
}
}
}
/////////////////////////////////////////////////////////////
csh( )//初始化
{
OPTION=7;
TRISA=0B1111;
TRISB=0;
TRISC=0B01000000;
PORTB=0;
PORTC=0;
buzzerbit=1;
return;
}
////////////////////////////////////////////////////////////
buzzer( )//蜂鸣器
{
if(buzzerbit==1)
{
RTCC=0;
for(;buzzercount<3000;)//此处也可以看做一个延时
{
CLRWDT( );
if(RTCC==1)
{
RC5=RC5^1;
RTCC=0;
}
buzzercount++;
}
}
RC5=0;
buzzercount=0;
buzzerbit=0;
return;
}
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2