Em đang làm đồ án môn học. Đề bài như sau : Thiết kế bộ đo và khống chế nhiệt độ hiển thị trên LCD
Em dung 89s52 , LM35, ADC0804,
Các anh chị xem hộ em cai code nay cai:
#include <REGX52.H>
#include<stdio.h>
#include<reg52.h>
#include<math.h>
sbit INTR_ADC=P3^6;
sbit WR_ADC=P3^7;
sbit RS=P3^5;
sbit R/W=P3^4;
sbit E=P3^3;
sbit T=P0^6;//nut bam Tang
sbit G=P0^7;//nut bam Giam
sbit D=P0^3;//den Do
sbit V=P0^4;//den Vang
sbit X=P0^5;//den Xanh
long int x,k,ch,dv,d,ndkc;
float v;
void tre (unsigned long int t)
{
for(i=0;i<=t;i++);
}
void delay(unsigned int t)
{
unsigned int i;
for(i=0;i<= t;++i);
}
void busy_flag(void)
{
P2=0xff;
RS=0;
RW=1;
do
{ EN = 1;
delay(50);
EN=0;
x = P2 ;
x = x&0x80;
}
while(x==0x80);
}
void write_command(unsigned char LCD_command)
{
busy_flag();
P2=LCD_command;
RS=0;
RW=0;
EN=1;
delay(50);
EN=0;
delay(50);
}
void write_data(unsigned char LCD_data)
{
busy_flag();
if (LCD_data!='\n')
P2=LCD_data;
else
{
write_command(0xC0);
return;
}
RS=1;
RW=0;
EN=1;
delay(50);
EN=0;
delay(50);
}
void write_string(char *s)
{
while(*s)
{
write_data(*s);
s++;
}
}
void init(void) // khoi tao LCD
{
write_command(0x03); //xoa man hinh dua con tro ve vi tri dau dong thu nhat
write_command(0x38); //chon che do hai dong va ma tran diem 5x7
write_command(0x06); // tang con tro ( dich con tro sang phai)
write_command(0x0e); // bat hien thi, nhap nay con tro
}
void dat()
{
if((T==1)||(G==1)) {d=0;tre(250);}//bam nut thi reset bien dem d, tao tre giua cac lan bam
d=d+1;//dem qua trinh lap cua chuong trinh nay
if (T==1) ndkc=ndkc+1;
if (G==1) ndkc=ndkc-1;
ch=ndkc/10;
dv=ndkc%10;
hienthi();
}
void main()
{
ndkc=20;
while(1)
{
T=0;
G=0;
if((T==1)||(G==1))//kiem tra nut bam nhiet do khong che
{
while(1)
{
dat();
if(d==100) break;//neu lap 100 lan ma k co su tac dong nut bam thi thoat
}
}
WR_ADC=0;//cho phep ADC bien doi
tre(100);//thoi gian tre cho ADC bien doi xong
WR_ADC=1;//khong cho ADC bien doi
x=P2;//gan gia tri bien doi vao x
v=x*2.5/255;
k=v*100;//nhiet do thuc
ch=k/10;
dv=k%10;
if(k>ndkc) {D=1;V=0;X=0;}
if(k==ndkc){D=0;V=1;X=0;}
if(k<ndkc) {D=0;V=0;X=1;}
hienthi();
}
}
Em dung 89s52 , LM35, ADC0804,
Các anh chị xem hộ em cai code nay cai:
#include <REGX52.H>
#include<stdio.h>
#include<reg52.h>
#include<math.h>
sbit INTR_ADC=P3^6;
sbit WR_ADC=P3^7;
sbit RS=P3^5;
sbit R/W=P3^4;
sbit E=P3^3;
sbit T=P0^6;//nut bam Tang
sbit G=P0^7;//nut bam Giam
sbit D=P0^3;//den Do
sbit V=P0^4;//den Vang
sbit X=P0^5;//den Xanh
long int x,k,ch,dv,d,ndkc;
float v;
void tre (unsigned long int t)
{
for(i=0;i<=t;i++);
}
void delay(unsigned int t)
{
unsigned int i;
for(i=0;i<= t;++i);
}
void busy_flag(void)
{
P2=0xff;
RS=0;
RW=1;
do
{ EN = 1;
delay(50);
EN=0;
x = P2 ;
x = x&0x80;
}
while(x==0x80);
}
void write_command(unsigned char LCD_command)
{
busy_flag();
P2=LCD_command;
RS=0;
RW=0;
EN=1;
delay(50);
EN=0;
delay(50);
}
void write_data(unsigned char LCD_data)
{
busy_flag();
if (LCD_data!='\n')
P2=LCD_data;
else
{
write_command(0xC0);
return;
}
RS=1;
RW=0;
EN=1;
delay(50);
EN=0;
delay(50);
}
void write_string(char *s)
{
while(*s)
{
write_data(*s);
s++;
}
}
void init(void) // khoi tao LCD
{
write_command(0x03); //xoa man hinh dua con tro ve vi tri dau dong thu nhat
write_command(0x38); //chon che do hai dong va ma tran diem 5x7
write_command(0x06); // tang con tro ( dich con tro sang phai)
write_command(0x0e); // bat hien thi, nhap nay con tro
}
void dat()
{
if((T==1)||(G==1)) {d=0;tre(250);}//bam nut thi reset bien dem d, tao tre giua cac lan bam
d=d+1;//dem qua trinh lap cua chuong trinh nay
if (T==1) ndkc=ndkc+1;
if (G==1) ndkc=ndkc-1;
ch=ndkc/10;
dv=ndkc%10;
hienthi();
}
void main()
{
ndkc=20;
while(1)
{
T=0;
G=0;
if((T==1)||(G==1))//kiem tra nut bam nhiet do khong che
{
while(1)
{
dat();
if(d==100) break;//neu lap 100 lan ma k co su tac dong nut bam thi thoat
}
}
WR_ADC=0;//cho phep ADC bien doi
tre(100);//thoi gian tre cho ADC bien doi xong
WR_ADC=1;//khong cho ADC bien doi
x=P2;//gan gia tri bien doi vao x
v=x*2.5/255;
k=v*100;//nhiet do thuc
ch=k/10;
dv=k%10;
if(k>ndkc) {D=1;V=0;X=0;}
if(k==ndkc){D=0;V=1;X=0;}
if(k<ndkc) {D=0;V=0;X=1;}
hienthi();
}
}
Comment