www.pudn.com > lcd_cesu.rar > gfsd.c, change:2014-05-02,size:2446b


#include <reg51.h> 
 
#define uchar unsigned char 
#define uint unsigned int 
#define lcddata P0 
 
sbit rs=P2^5; 
sbit rw=P2^6; 
sbit en=P2^7; 
sbit fxz=P1^0; 
sbit fxf=P1^1; 
 
int time; 
int tmp=0; 
int count; 
int l; 
 
uchar table[6]; 
uchar table1[6]; 
uchar v_table[3]="V: "; 
uchar v_table1[6]="r/min "; 
uchar v_table2[4]="RD: "; 
uchar v_table3[2]="Z "; 
uchar v_table4[2]="F "; 
uchar v_table5[2]="P "; 
 
//延时					    
void delay(uint t) 
{ 
	uchar j; 
    for(;t>0;t--) 
	for(j=19;j>0;j--) ; 
} 
 
//LCD写指令 
void writecommand(uchar lcdcom) 
{ 
	en=0; 
	rs=0; 
	rw=0; 
	lcddata=lcdcom; 
	delay(10); 
	en=1; 
	delay(200); 
	en=0; 
} 
 
//LCD写数据 
void writedata(uchar dat) 
{	 
	en=0; 
	rs=1; 
	rw=0; 
	lcddata=dat; 
	delay(10); 
	en=1; 
	delay(200); 
	en=0; 
} 
 
//LCD显示位置设定 
void set_XY( int x, int y )  
{  
  int address;  
  if (y ==0)    address = 0x80 + x;  
  else          address = 0xC0 + x;  
  writecommand(address);   
} 
 
//LCD字符串显示 
void writestring(int X,int Y,uchar *s)  
{  
  set_XY( X, Y );    //设置地址 
  while(*s!=' ')              //写字符串  
  {  
    writedata(*s); 
	s++;    
  }  
} 
 
//LCD浮点数据转字符串 
void value_string(int v) 
{	 
	table[0]=v/10000+0x30; 
	table[1]=v%10000/1000+0x30; 
	table[2]=v%10000%1000/100+0x30; 
	table[3]=v%10000%1000%100/10+0x30; 
	table[4]=v%10000%1000%100%10+0x30; 
	table[5]=' '; 
			 
} 
 
//方向判断 
void selectfx() 
{ 
	if(fxz==0) 
	writestring(3,1,v_table3); 
	if(fxf==0) 
	writestring(3,1,v_table4); 
	if(count==0) 
	writestring(3,1,v_table5);	 
} 
 
//LCD初始化 
void init_lcd() 
{ 
	delay(300); 
	writecommand(0x38); 
	delay(100); 
	writecommand(0x38); 
	delay(100); 
	writecommand(0x08); 
	writecommand(0x0c); 
	writecommand(0x38); 
	writecommand(0x01); 
	writecommand(0x06); 
	writecommand(0x0c); 
} 
 
//定时器和外部中断初始化 
void InitTimer0(void) 
{ 
    TMOD = 0x01; 
    TH0 = 0x3C; 
    TL0 = 0x0B0; 
    EA = 1; 
    ET0 = 1; 
    TR0 = 1; 
	IT0=1; 
	EX0=1; 
} 
 
 
 
//主函数 
void main() 
{ 
	double t; 
	InitTimer0(); 
	init_lcd();  
	while(1) 
	{ 
	if(time==20) 
	{ 
	 
	 time=0; 
	 count=tmp; 
	 tmp=0; 
 
	} 
	t=count*6/10; 
    writestring(0,0,v_table); 
	value_string(t); 
	writestring(2,0,table); 
	writestring(7,0,v_table1); 
	writestring(0,1,v_table2); 
	selectfx(); 
	} 
} 
 
//外部中断计数 
void external0()  interrupt 0 
{ 
	tmp++;  
}								    
 
void timer0()  interrupt 1 
{ 
	TH0=0X3C; 
	TL0=0XB0; 
	time++; 
}