/*********************************
chuong trinh mo phong :
De Tai: Vi dieu khien 8051 ket noi 8 phim don do dien ap 1 chieu :0-> 15V.
Thuc hien: " Nhom 3-D5DCN2 : Thang-Sieu-Vuong "
*********************************/
#include <REGX51.H>
#include <string.h>
#include <stdio.h>
sbit RS_LCD=P2^0;
sbit RW_LCD=P2^1;
sbit EN_LCD=P2^2;
sbit INTR_ADC=P2^3;
sbit WR_ADC=P2^4;
sbit RD_ADC=P2^5;
sbit thang1=P1^0;
sbit thang2=P1^1;
sbit thang3=P1^2;
sbit thang4=P1^3;
sbit thang5=P1^4;
sbit thang6=P1^5;
sbit thang7=P1^6;
sbit thang8=P1^7;
unsigned char x,y;
float volt;
//ham tao time tre//
void delay(unsigned int time)
{
unsigned int i;
for(i=0;i<time;i++);
}
//ham kiem tra LCD ban//
void ban(void){
P0=0xff;
RW_LCD=1;//doc gia tri tu LCD//
RS_LCD=0;//lenh //
do
{
EN_LCD=1;
delay(50);
EN_LCD=0;
x=P0;
x=x&&0x80; }//giu lai bit D7 de so sanh//
while(x==0x80);//cho bit D7 bang 0 thi dung lai
}
//ham gui di 1 lenh
void lenh(unsigned char lcd_lenh)
{
ban();//kiem tra LCD ban
P0=lcd_lenh;//ma cua lenh can truyen
RS_LCD=0;//8051 ghi xuong LCD
RW_LCD=0;//chon truyen lenh
EN_LCD=1;
delay(50);
EN_LCD=0;
delay(50);
}
//ham gui ky tu xuong LCD
void write_data(unsigned char lcd_data)
{
ban();
P0=lcd_data;
RS_LCD=1;//chon truyen ky tu
RW_LCD=0;//8051 ghi xuong LCD
EN_LCD=1;
delay(50);
EN_LCD=0;
delay(50);
}
//ham gui 1 chuoi ky tu xuong LCD //
void write_chuoi(unsigned char*s)
{
while(*s)
{
write_data(*s);
s++;
}
}
//ham cai dat cho LCD//
void khoi_tao_LCD(void)
{
lenh(0x38);//LCD 2 dong,ma tran diem 5x7
lenh(0x0c);//bat man hinh tat con tro
lenh(0x01);// xoa man hinh
lenh(0x80);//dua con tro ve dau dong thu nhat
lenh(0x06);// dich con tro sang phai
}
//ham hien thi so tren LCD
void hienthi_so(void)
{
int tram,chuc,donvi,phan,phan1,phan2;
int n;
n=(int)(volt*100);
tram=n/10000;
chuc=(n%10000)/1000;
donvi=((n%10000)%1000)/100;
phan=((n%10000)%1000)%100;
phan1=phan/10;
phan2=phan%10;
if(tram!=0)
write_data(48+tram);
if((tram!=0)&&(chuc==0)&&(donvi==0))
write_data(48);
if((tram!=0)&&(chuc==0)&&(donvi!=0))
write_data(48);
if(chuc!=0)
write_data(48+chuc);
if((donvi!=0)||(phan1!=0)||(phan2!=0))
{
write_data(48+donvi);
write_data('.');
write_data(48+phan1);
write_data(48+phan2);
}
if((donvi==0)&&(phan1==0)&&(phan2==0))
write_data(48);
}
//ham chinh//
void main(void)
{
khoi_tao_LCD();//goi ham khoi tao LCD//
P3=0xff;
while(1) //vong lap vinh vien//
{
lenh(0x01);//xoa man hinh
lenh(0x80);//tro ve dau dong thu nhat
write_chuoi("DIEN AP:"); //hien thi chuoi DIEN AP //
WR_ADC=0;//ADC bat dau bien doi
delay(10);
RD_ADC=0; //cong dem 3 trang thai o dau ra Dout luon mo
while(INTR_ADC==0);
WR_ADC=1;
while(INTR_ADC==0);
x=P3;
if(thang1==0)
{
volt=(float)(x*15)/255; //bieu thuc tinh dien ap Vin
if((volt<0)||(volt>2))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang2==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>3.5))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai llll
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang3==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>5))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang4==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>7))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang5==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>10))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai llll
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang6==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>12))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang7==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>13.5))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai llll
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
if(thang8==0)
{
volt=(float)(x*15)/255;
if((volt<0)||(volt>15))
{
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi(" KHONG DO DUOC ");
delay(50000);
}
else
{
hienthi_so();
write_chuoi("V");
lenh(0xc0);//tro ve dau dong thu hai
write_chuoi("THANG-SIEU-VUONG");
delay(50000);
}
}
}
}
//chuong trinh mo phong ket thuc//
( empty)
Theo Cộng đồng cơ điện tử
0 Response to "Mạch đo điện áp 1 chiều"
Đăng nhận xét