#include <reg52.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char u8;
typedef unsigned int u16;
u16 e,pos=0;
u8 code table[]="ohm gauge";
u8 code num[]="0123456789";
u8 code space[]=" ";
u16 pulse_w=0;
sbit output=P1^0;
sbit RS=P2^6;
sbit E=P2^7;
sbit RW=P3^6;
sbit KEY=P3^3;
sbit Sound=P2^4;
sbit in=P3^2;
//void init()
//{
// TMOD=0x60; //计数器T1工作方式2,P3.5
// TH1=0x00;
// TL1=0x00; //设置初值
// ET1=1;
// EA=1;
// TR1=1; //允许计数
//}
//
//void init1() interrupt 3 using 1
//{
// TF1=0;
//}
void delay(ms)/*延时函数ms*(123*2+3)=249(us)*/
{
u16 i,j;
for(i=0;i<ms;i++){
for(j=0;j<123;j++){
}
}
}
void delay_25584us()//子函数
{
int i,j,k;
for(i=0;i<4;i++)
for(j=0;j<12;j++)
for(k=0;k<66;k++);
}
/* RS:LCD数据命令选择端口,RS=0对LCD指令操作,RS=1对LCD数据操作
RW:LCD读写选择端口,RW=0对LCD进行写操作,RW=1对LCD进行读操作
EN:LCD使能端口
指令38H:八位数据接口,两行显示
0CH:显示开,光标关,闪烁关
80H:设置光标位置*/
//写指令
void write_com(u8 com)
{
E=0;
RS=0;
RW=0;
P0=com;
delay(5);
E=1;
delay(5);
E=0;
}
//写数据
void write_data(u8 dat)
{
E=0;
RS=1;
RW=0;
P0=dat;
delay(5);
E=1;
delay(5);
E=0;
}
void init_lcd()
{
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void init_c()
{
u16 i;
write_com(0x80);
for(i=0;i<9;i++)
{
write_data(table[i]);
}
}
void display()
{
u16 a,b,c,d,e;
e=82*(pulse_w*0.001)-270;
a=e/1000;//千位;
b=e%1000/100;//百位
c=e%1000%100/10;//十位
d=e%1000%100%10;//个位
if(e<11){
write_com(0x80+0x40+pos);
write_data(space[0]);
pos++;
write_com(0x80+0x40+pos);
write_data(space[0]);
pos++;
write_com(0x80+0x40+pos);
write_data(space[0]);
pos++;
write_com(0x80+0x40+pos);
write_data(num[0]);
delay(5);
pos++;
write_com(0x80+0x40+pos);
write_data(0xf4);
delay(10);
pos=0;
}
if(e>11){
if(a==1)//显示千位
{
write_com(0x80+0x40+pos);
write_data(num[a]);
delay(5);
pos++;
write_com(0x80+0x40+pos);
write_data(num[0]);
delay(5);
pos++;
write_com(0x80+0x40+pos);
write_data(num[0]);
delay(5);
pos++;
write_com(0x80+0x40+pos);
write_data(num[0]);
delay(5);
pos++;
pos=0;
}else{
write_com(0x80+0x40+pos);
write_data(space[0]);
delay(5);
pos++;
}
if(b!=0)//显示百位
{
write_com(0x80+0x40+pos);
write_data(num[b]);
pos++;
}else{
write_com(0x80+0x40+pos);
write_data(space[0]);
pos++;
}
//显示十位
write_com(0x80+0x40+pos);
write_data(num[c]);
delay(5);
pos++;
//显示个位
write_com(0x80+0x40+pos);
write_data(num[d]);
delay(10);
pos++;
write_data(0xf4);
pos=0;
}
}
//void init()
//{
// TMOD = 0x09 ; //定时器0,模式1,GATE0 = 1
// TH0 = 0 ; //填初值
// TL0 = 0 ;
// TR0 = 0 ;
// TH1=0x1F;
// TL1=0x40;
// TR1=0;
// TF1=0;
//}
void init(){
TMOD=9;//T0工作在方式1,打开gate位
TL0=0;
TH0=0;
}
//void ignore(){
// int i,j;
// for(i=0;i<10;i++)
// for(j=0;j<98;j++);
//}
void main(){
u16 mark=1;
init();
init_lcd();
init_c();
while(1){
pulse_w=0;
init();
output=1;
delay_25584us();
output=0;
TR0=1;//启动定时
while(!in);//等待高电平
while(in);//等待低电平
TR0=0;//停止定时
pulse_w=TH0;
pulse_w<<=8;
pulse_w|=TL0;
display();
delay_25584us();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
双积分AD转换器设计.zip (2个子文件)
双积分AD设计电阻表+LCD显示简单代码.c 3KB
Multisim设计文件.ms14 266KB
共 2 条
- 1
苹果头子
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0