sbit RS = P2^5;//Pin4(2)脈搏檢測處理程序
sbit RW = P2^6; //Pin5
sbit E = P2^7;//Pin6
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar data TimeNum[]=" ";
uchar data Test1[]=" ";
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1;/* Crystal frequency in 12MHz*/
while(--uscnt);
}
/******************************************************************/
void DelayMs(unsigned char ms)//delay Ms
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
void WriteCommand(unsigned char c)
{
DelayMs(5);//short delay before operation
E=0;
RS=0;
RW=0;
_nop_();
E=1;
P1=c;
E=0;
}
/****************************************************************/
void WriteData(unsigned char c)
{
DelayMs(5); //short delay before operation
E=0;
RS=1;
RW=0;
_nop_();
E=1;
P1=c;
E=0;
RS=0;
}
/*********************************************************************/
void ShowChar(unsigned char pos,unsigned char c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0; //是第二行則命令代碼高4位為0xc
else
p=pos+0x80; //是第二行則命令代碼高4位為0x8
WriteCommand (p);//write command
WriteData (c); //write data
}
/*************************************************************************/
void ShowString (unsigned char line,char *ptr)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<16;i++)
ShowChar (l++,*(ptr+i));//循環顯示16個字符
}
/*********************************************************************/
void InitLcd()
{
DelayMs(15);
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x38); //display mode
WriteCommand(0x06); //顯示光標移動位置
WriteCommand(0x0c); //顯示開及光標設置
WriteCommand(0x01); //顯示清屏
}
unsigned int i,n,ci,dd[11],jj,j,k,tmp;
bit w=0;
uchar bh;
ulong time;
sbit spd = P2^0;
external0() interrupt 0//外部中斷服務程序
{
w=~w;
if(w==0)
{
EX0=0;
ET0=1;
TH0=0x0;
TL0=0x0;
n=0;
}
else
{
time=n*65536+TH0*256+TL0;
dd[jj]=30000000/time; //計算脈搏時間
jj++;
if(jj>10)
{
jj=0;
for(j=0;j<11;j++)
for(k=0;k<11-j;k++)
{
if(dd[k]>dd[k+1])
{
tmp=dd[k];
dd[k]=dd[k+1];
dd[k+1]=tmp;
}
}
if(dd[5]>50&&dd[5]<200)ci=((dd[4]+dd[5]+dd[6])/3)+11;
}
TH0=0x0;
TL0=0x0;
n=0;
}
}
void timer0(void) interrupt 1
{
n++;
}
void main(void)
{
InitLcd();//
DelayMs(15);
IT0=1; //INT0下降沿中斷
EX0=1; //允許INT1中斷
TMOD=0x1;
TH0=0x0;
TL0=0x0;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(n>10)
{
n=0;
jj=0;
ci=0;
TimeNum[0]='N';
TimeNum[1]='O' ;
TimeNum[2]=' ' ;
TimeNum[3]='I' ;
TimeNum[4]='N' ;
TimeNum[5]='P' ;
TimeNum[6]='U' ;
TimeNum[7]='T' ;
TimeNum[8]=' ' ;
ShowString(0,TimeNum);
ShowString(1,Test1);
}
else
{
if(w==0&&EX0==0)
{
delay();
EX0=1;
}
TimeNum[0]=' ';
TimeNum[1]=' ' ;
TimeNum[2]=' ' ;
TimeNum[3]=' ' ;
TimeNum[4]=' ' ;
TimeNum[5]=' ' ;
if(ci/100>0)TimeNum[6]=ci/100+'0' ; else TimeNum[6]=' ' ;
if(ci/100==0&&ci%100/10>0)TimeNum[7]=ci%100/10+'0' ; else TimeNum[7]=' ' ;
TimeNum[8]=ci%10+'0' ;
ShowString(0,TimeNum);
ShowString(1,Test1);
if(ci>100)spd=0;else spd=1;
}
}
;
}
歡迎光臨 (http://www.zg4o1577.cn/bbs/) | Powered by Discuz! X3.1 |