#include<reg52.h>
#include<intrins.h>
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
#define INT 5
unsigned char count=0,cnt=0;
//unsigned long N;
unsigned long value[ INT ];
unsigned char str_buff[30];
void inil( void );
void DelayL( );
void DelayS( );
void WriteCommand( unsigned char c );
void WriteData( unsigned char c );
void ShowChar( unsigned char pos,unsigned char c );
void ShowString( unsigned char line,char *ptr );
void InitLcd( );
void main( void )
{
unsigned char i;
unsigned long m;
unsigned char code str1[]=" LM331 Testing ";
unsigned char code str2[]=" 2008-6-6 ";
unsigned char line0[]="Frequnecy is: ";
unsigned char line1[]=" . Hz ";
DelayL( );
InitLcd( );
DelayL( );
ShowString( 0,str1 );
ShowString( 1,str2 );
inil( );
while( 1 )
{
if( count==INT )//累加到6个时序状态
{
for( i=0;i<INT;i++ )//记录出6个时序状态计数值(6个时序状态=12个晶振周期)
{
m=m+value;
}
m=m/INT;
m=1843200/m;
//sprintf(str_buff,"%-8.3fHz",m);
line1[0]=m/10000+0x30;//字符数字“0”ASCII码是0x30,数字“0”的ASCII码是0x00
m=m%10000;
line1[1]=m/1000+0x30;
m=m%1000;
line1[2]=m/100+0x30;
m=m%100;
line1[3]=m/10+0x30;
line1[4]=m%10+0x30;
ShowString( 0,line0 );
ShowString( 1,line1 );
for( i=0;i<=15;i++ )
{
void DelayL( );
}
m=0;
count=0;
cnt=0;
EX0=1;
ET0=1;
EA=1;
}
}
}
void inil( void )
{
EA=0;
PX0=1;
INT0=1;
IT0=1;
TMOD=( TMOD&0XF0 )|0X01;
TR0=0;
TH0=TL0=0;
EX0=1;
ET0=1;
EA=1;
}
void intlint0( void ) interrupt 0
{
unsigned char th,tl,a;
unsigned long N;
if( TH0==TL0&&TL0==0 ) TR0=1;//外部中断未触发
else
{
TR0=0;//下一外部中断到来
EX0=0;//````````````````
th=TH0;
tl=TL0;
a=cnt;
TH0=TL0=0;
cnt=0;
if( a==0 )//未进入T0中断计数
{
N=th*256+tl;
}
else
{
N=65536*a+th*256+tl;//进入T0计数累加时间
}
value[ count++ ]=N;//记录每次进入中断累加的计数时间
if( count==INT )//累加到6次外部中断,用来记录6次外部中断发生时间
{
EX0=0;//马上关外部中断
ET0=0;//关T0中断
TH0=TL0=0;//T0清0
EA=0;
}
else
{
EX0=1; //未到6次继续开外部中断接收频率信号
}
}
}
void intlt0( void ) interrupt 1
{
cnt++;
TH0=TL0=0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LCD
void DelayL( )
{
unsigned char i,j;
i=0xF0;
j=0xFF;
while( i-- )
while( j-- );
}
void DelayS( )
{
unsigned char i;
i=0x1F;
while( i-- );
}
void WriteCommand( unsigned char c )
{
DelayS( );
EN=0;
RS=0;
RW=0;
_nop_( );
EN=1;
P1=c;
EN=0;
}
void WriteData( unsigned char c )
{
DelayS( );
EN=0;
RS=1;
RW=0;
_nop_( );
EN=1;
P1=c;
EN=0;
RS=0;
}
void ShowChar( unsigned char pos,unsigned char c )
{
unsigned char p;
if( pos>=0x10 )
p=pos+0xB0;//
else
p=pos+0x80;//
WriteCommand( p );
WriteData( c );
}
void ShowString( unsigned char line,char *ptr )
{
unsigned char l,i;
l=line<<4;
for( i=0;i<16;i++ )
ShowChar( l++, *(ptr+i) );
}
/*
//带定位参数的字符写函数
void lcd_put_xys(uchar x,uchar y,uchar *str)
{
lcd_goto_xy(x,y);
while(*str)
{
loop_lcd1602_is_busy();
LCD_DW = *str;
str++;
}
}
*/
void InitLcd( )
{
DelayL( );
WriteCommand( 0x38 );
DelayL( );
WriteCommand( 0x38 );
DelayL( );
WriteCommand( 0x06 );
WriteCommand( 0x0c );
WriteCommand( 0x01 );
WriteCommand( 0x80 );
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
-----------------------------------------------------------------------
飘过。。。。
一周热门 更多>