#include <REG52.H>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DATA P0
#define K 100 //校准系数
sbit RS = P2^5;
sbit RW = P2^6;
sbit EN = P2^7;
unsigned char code digit[ ]={"0123456789"};//定义字符数组显示数字?
unsigned char code string[ ]={"Test Result"};//定义字符数组显示提示信息?
unsigned char D1,D2,D3,D4,D5;//分别储存采集的个位、十位、百位、千位和万位数字
uint Tcounter = 0; //时间计数器
bit Flag_Fresh = 0; // 刷新标志
bit Flag_clac = 0; //计算转速标志
uint speed ;
uint num;
unsigned char SETSPEED=150;
unsigned char ZKB1;
void delay(unsigned int ms) //延时
{
unsigned int i;
while(ms--)
for(i=110;i>0;i--);
}
unsigned Busy_Check() //检测液晶是否忙
{ //UINT8 LCD_Status;
RS=0;
RW=1;
EN=1;
delay(1);
LCD_DATA= P0; //液晶的数据端
EN=0;
return LCD_DATA;
}
void Write_LCD_Command(uchar cmd) //液晶写命令
{
Busy_Check(); //液晶验忙
RS=0;
RW=0;//当RS和R/W共同为低电平时可以写入指令或者显示地址
EN=0;
delay(1); //开启液晶
P0=cmd;
delay(1);
EN=1;
delay(1);
EN=0;
}
void Write_LCD_Data(uchar dat) //液晶写数据
{
Busy_Check();
RS=1;
RW=0;//当RS为高电平R/W为低电平时可以写入数据。
EN=0;
P0=dat;
delay(1);
EN=1;
delay(1);
EN=0;
}
void display_LCD_Data(uchar LCD_Data) //显示数据
{
Busy_Check(); //液晶验忙
RS=1;
RW=1;
EN=0;
LCD_DATA =LCD_Data;
EN=1;
delay(1);
EN=0;
}
void Set_LCD_POS(unsigned char lcd_data) //设置显示的位置
{
Write_LCD_Command(lcd_data| 0x80); //加上0x80,才可显示 (单行显示 0x80 双行显示0xc0)
}
void Display_LCD_String(unsigned char *lcd_string) //显示字符串
{
unsigned char i=0;
while(lcd_string[i]!=0x00)
{
display_LCD_Data(lcd_string[i]);
i++;
}
}
void Init_LCD(void) //液晶初始化
{
Write_LCD_Command(0x38); //显示模式设置
delay(1);
Write_LCD_Command(0x38); //显示模式设置
delay(1);
Write_LCD_Command(0x08); //显示关闭
delay(1);
Write_LCD_Command(0x01); //清屏
delay(1);
Write_LCD_Command(0x06); //光标右移
delay(1);
Write_LCD_Command(0x0C); //画面显示,
delay(1);
}
void it_
timer0() interrupt 1
{
TF0 = 0; //d定时器 T0用于数码管的动态刷新
TH0 = 0x3C;
TL0 = 0xB0;
Flag_Fresh = 1;
Tcounter++;
if(Tcounter==20)
{
Flag_clac = 1;//周期到,该重新计算转速了
Tcounter=0;
TH1=TL1=0;
}
}
void init_timer1() //初始化定时器T1
{
TMOD= 0x51;
TH1 = 0x00; //设置初值。
TL1 = 0x00;
ET1=1;
EA=1;
TR1=1;
}
void init_timer0() //初始化定时器
{
TMOD= 0x51; /* GATE0=0; C/T0#=0; M10=0; M00=1; */
TH0 = 0x3C; //定时50毫秒
TL0 = 0xB0;
ET0=1;
EA=1;
TR0=1;
while(1);
}
void main(void)
{ unsigned char i;//定义变量i指向字符串数组元素
init_timer0();
init_timer1();
Init_LCD() ;
delay(1);
Set_LCD_POS(0x00);
i=0;//指向字符数组的第1个元素
while(string[i]!=' ')
{
Write_LCD_Data(string[i]);
i++;//指向下字符数组一个元素
}
num = TH1*256 + TL1;
speed=K*num*60;
D5=speed/10000;//计算万位数字
D4=(speed%10000)/1000;//计算千位数字
D3=(speed%1000)/100;//计算百位数字
D2=(speed%100)/10;//计算十位数字
D1=speed%10;//计算个位数字
Set_LCD_POS(0x45);
Write_LCD_Data(digit[D5]);//将万位数字的字符常量写入LCD
Write_LCD_Data(digit[D4]);//将千位数字的字符常量写入LCD
Write_LCD_Data(digit[D3]);//将百位数字的字符常量写入LCD
Write_LCD_Data(digit[D2]);//将十位数字的字符常量写入LCD
Write_LCD_Data('.');//将小数点的字符常量写入LCD
Write_LCD_Data(digit[D1]);//将个位数字的字符常量写入LCD
for(i=0;i<4;i++)//延时1s(每1s采集一次数据)
delay(250);//延时250ms
}
-
第二:加中断嵌套
一周热门 更多>