0% found this document useful (0 votes)
121 views

Code VDK

The document contains code for an Arduino/8051 project that displays temperature and humidity readings from a DS18B20 sensor and HS1101 humidity sensor on an LCD screen. The code includes functions to initialize the LCD, write commands and data to the LCD, read from and write to the DS18B20 and HS1101 sensors to get temperature and humidity values, and continuously display the updated readings on the LCD screen in the main loop. Interrupt routines are used to trigger sensor reads periodically and update the displayed values.

Uploaded by

Mạc David
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views

Code VDK

The document contains code for an Arduino/8051 project that displays temperature and humidity readings from a DS18B20 sensor and HS1101 humidity sensor on an LCD screen. The code includes functions to initialize the LCD, write commands and data to the LCD, read from and write to the DS18B20 and HS1101 sensors to get temperature and humidity values, and continuously display the updated readings on the LCD screen in the main loop. Interrupt routines are used to trigger sensor reads periodically and update the displayed values.

Uploaded by

Mạc David
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<REGX52.

H>
#include<stdio.h>
#define
#define
#define
#define
#define

RS P2_0
RW P2_1
EN P2_2
LCD_PORT P1
DQ P2_6

/////////khai bao bien toan cuc/////////////////


unsigned char temp;
unsigned char k;
unsigned int tanso;
unsigned int code
f[100]={7410,7392,7374,7357,7340,7323,7307,7290,7274,7259,7243,7228,7213,7198,7183,7169,7155,7140,7127,7113,7099,7086,7072,7059,7
046,7033,7020,7007,6995,6982,6969,6957,6945,6932,6920,6908,6896,6884,6872,6860,6848,6836,6824,6812,6800,6788,6776,6764,6752,6740,
6728,6716,6704,6692,6680,6668,6656,6643,6631,6619,6606,6594,6581,6568,6556,6543,6530,6517,6504,6491,6477,6464,6450,6437,6423,6409
,6395,6381,6367,6352,6338,6323,6309,6294,6279,6264,6248,6233,6217,6202,6186,6170,6154,6137,6121,6104,6087,6053,6036,6019};
////////cac ham de lay/////////////////////////
void delay_ms(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<Time;i++)
{
for(j=0;j<125;j++);
}
}
void delay_us(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<Time;i++)
{
for(j=0;j<2;j++);
}
}
void delay_18B20(unsigned char x)
{
unsigned char i;
for(i=0;i<x;i++);
}
///////////////////////////////////////////////
////////cac ham ve LCD 20X4////////////////////
void LCDWriteCmd(unsigned char c)
{
RS=0;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms(5);
}
void LCDWriteData(unsigned char c)
{
RS=1;
RW=0;
LCD_PORT=c;
EN=1;
EN=0;
delay_ms(5);
}

//CT con ghi lenh len LCD

//CT con ghi du lieu tu LCD

void LCD_init()
{
delay_ms(15);
LCDWriteCmd(0x38);
LCDWriteCmd(0x38);
LCDWriteCmd(0x0C);
LCDWriteCmd(0x01);
}

// Khoi tao LCD

void LCD_clear()
{
LCDWriteCmd(0x01);
}

//xoa man hinh LCD

void LCD_home()
{
LCDWriteCmd(0x80);
}

//dua con tro ve dau dong 1

void LCD_putstr(unsigned char *s)


{
while (*s)
{
LCDWriteData(*s);
s++;
}
}

//ghi 1 chuoi len LCD

/////////////////////////////////////////////////////////////////
////////cac ham ve DS18B20//////////////////////////////////////
void Init_DS18B20()
{
unsigned char x=0;
DQ = 1;
delay_18B20(8);
DQ = 0;
delay_18B20(80);
DQ = 1;
delay_18B20(14);
x=DQ;
delay_18B20(20);

//ham khoi tao DS18B20

}
unsigned char ReadOneChar() //ham doc 1 byte vao DS18B20
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
delay_18B20(5);
}
return(dat);
}
void WriteOneChar(unsigned char dat) //ham viet 1 byte vao DS18B20
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
delay_18B20(5);
DQ = 1;
dat>>=1;
}
}
unsigned char ReadTemp()
{
unsigned char a=0;
unsigned char b=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
delay_ms(100);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
delay_ms(100);
a=ReadOneChar();
b=ReadOneChar();
Init_DS18B20();
temp=(b<<4)|(a>>4);
return(temp);
}
////////////////////////////////////////////////////////
////////ham ve HS1101//////////////////////////////////
void ngat_trantimer0() interrupt 1
{
unsigned int Time;
TH0=0xFC;
TL0=0x2B;
Time++;
if(Time==1000)
{
Time=0;
TR1=0;
tanso=(((TH1&0x00ff)<<8)|(TL1&0x00ff));
1s
TH1=0;
TL1=0;
TR1=1;
}
TF0=0;
}
unsigned char humid(unsigned int tanso)
{
unsigned char k;
while(tanso<f[k])
{
k++;

//tan so bang so xung dem duoc trong

}
return(k);
}
//////////////////////////////////////////////////
////////ham main/////////////////////////////////
void main()
{
float Nhiet_Do;
unsigned char do_am;
unsigned char Lcd_Buff[5];
unsigned char Lcd_Buff1[5];
TMOD=0x51;
IE=0x82;
TR0=1;
LCD_init();
LCD_clear();
LCD_home();
LCD_putstr("DO AN MON HOC 2");
LCDWriteCmd(0xC0);
LCD_putstr("DO NHIET DO & DO AM");
LCDWriteCmd(0x94);
LCD_putstr("NHIET DO:");
LCDWriteCmd(0xD4);
LCD_putstr("DO AM:");
delay_ms(50);
while(1)
{

//thiet lap ngat timer 0

Nhiet_Do = ReadTemp();
//sprintf(Lcd_Buff,"NHIET DO: %2.0f oC",Nhiet_Do);
sprintf(Lcd_Buff,"%.0f",Nhiet_Do);
LCDWriteCmd(0x9D);
LCD_putstr(Lcd_Buff);
delay_ms(300);
//do_am=humid();
//sprintf(Lcd_Buff,"DO AM: %2.0d",vitrf);
do_am=humid();
sprintf(Lcd_Buff1,"%d",do_am);
LCDWriteCmd(0xDC);
LCD_putstr(Lcd_Buff1);
delay_ms(300);
}
}

Bai 2

#include<at89x51.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
//========================= #DEFINE LCD
#define RS P3_5
#define RW P3_6//RW=0 => ghi
#define EN P3_7//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
//========================= #DEFINE ADC
#define INTR P3_2
#define RD P3_3
#define WR P3_4
//============================================================== DELAY
void delay_ms(int n)
{
int k,j;
for(k=0;k<n;k++)
{
for(j=0;j<500;j++);
}

}
//==========================
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)

for(j=0;j<4;j++){}
}
//===========================
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
//==============================================================
//============================== GUI LENH CHO LCD
void LCD_CODE(unsigned char c)
{
RS=0;//code
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//=============================== KHOI TAO LCD
void LCD_INIT()
{
delay_15ms();
LCD_CODE(0x38); //che do 8bit,2 hang,kieu ky tu 5x8 diem anh.
LCD_CODE(0x0C); //hien thi man hinh,c con tro, con tro nhp' nhy.
LCD_CODE(0x01); // Xoa man hinh LCD
}
//============================== IN KY TU
void LCD_DATA(unsigned char c)
{
RS=1;//data
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//=============================== IN CHUOI KY TU
void LCD_STRING(unsigned char *s)
{
while(*s) //den NULL thi thoi
{
LCD_DATA(*s);
s++;
}
}
//==============================================================
void SetupSerial()
{
TMOD=0x20;
/* timer 1 che do 2: 8-Bit tu dong nap lai.
*/
TH1=0xFD;
/* toc do 9600 baud
*/
TL1=0xFD;
SCON=0x52;
/* Che do 1: 8-bit UART, cho phep truyen */
TR1=1;
/* timer 1 run
*/
TI=0;
/* co ngat nha^n.=0*/
RI=0;
/* co ngat' truye^n =0*/
ES=1;
/* cho phep ngat noi tiep */
}
//==============================================================
void HIENTHI_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
LCD_DATA(t+48);
else
if(t<100)
{
LCD_DATA(t/10+48);
LCD_DATA(t%10+48);
}
else
{
v=t/10;
LCD_DATA(v/10+48);
LCD_DATA(v%10+48);
LCD_DATA(t%10+48);
}
}
//==============================================================
void send(unsigned char a)
{
if(a<10)
{
SBUF=a+48;
while(TI==0){}
TI=0;
}

LCD

KHOI TAO RS232

HIEN THI GIA TRI ADC LEN LCD

HAM GUI SO LIEU LEN VB

if(a>9&&a<100)
{
unsigned char c=a/10;
unsigned char d=a%10;
SBUF=c+48;
while(TI==0){}
TI=0;
SBUF=d+48;
while(TI==0){}
TI=0;
}
if(a>99)
{
unsigned char t=a/100;
unsigned char c=a/10-10*t;
unsigned char d=a%10;
SBUF=t+48;
while(TI==0){}
TI=0;
SBUF=c+48;
while(TI==0){}
TI=0;
SBUF=d+48;
while(TI==0){}
TI=0;
}
}
//============================================================== NGAT NOI TIEP
void RS232(void) interrupt 4
{
unsigned char *c,count=0;
while(RI==0){}
RI=0;
*c=SBUF;
if(count==1&&*c=='1') *c='3';
if(count==1&&*c=='2') *c='4';
switch(*c)
{
case '0': //xoa man hinh LCD
{
LCD_CODE(0x01);
}
break;
//o day co the su dung ma thap phan! (ngoai tru cac ky tu dieu khien)
case '-': //lui con tro hien thi LCD 1 don vi.
{
LCD_CODE(0x10);
}
break;
case '1': //truyen len may tinh: gia tri do duoc.
{
send(P2);
}
break;
case '2': //truyen len may tinh: gia tri do duoc.
{
send(P2);
}
break;
case '9': //thoat khoi ham ngat, cho ADC lm viec.
{
TI=0; //cho phep thoat khoi ham ngat.
count=0; //ket thuc viec truyen ky tu len LCD
}
break;
default : //truyen ky tu xuong LCD ^^.
{
if(TI==0)
LCD_CODE(0x01);
LCD_DATA(*c);
TI=1; //de cho chuong trinh ko thoat khoi ham ngat,phuc vu viec truyen ky tu len LCD.
count=1; //danh dau bat dau viec truyen ky tu len LCD
}
}
}
//============================================================== MAIN
void main()
{
SetupSerial();
//Khoi tao cac thong so cho truyen thong noi tiep
LCD_INIT();
//LCD_CODE(0x80);//hien thi 2 hang : dia chi hien thi + 80
EA = 1;
//Cho phep ngat nhung chi? c ngat noi tiep duoc dung trong code nay
while(1)

{
//if(P0_3==1)
{
P0_0=P0_1=P0_2=0;
WR=0;
//Bat dau chuyen doi gia tri tu ADC
delay_ms(10);
//Tao tre de cap nhat du lieu tu ADC
WR=1;
//Ket thuc chuyen doi
RD=0;
//Chot du lieu da duoc chuyen doi: P2
LCD_CODE(0x01);
LCD_putstr("Nhiet do:");
HIENTHI_ADC(P2);
LCD_STRING(" oC");
delay_ms(300);
}
}
} - See more at: http://www.linhkiendientu.co/2012/02/code-mau-cho-8051-o-nhiet-o-hien-thi.html#sthash.j4dvgg3X.dpuf

#include "reg52.h"
#include "LCDset.c"
#include "stdio.h"
unsigned long x;
sbit WR_ADC = P3^4;
sbit RD_ADC = P3^5;
sbit INTR_ADC = P3^3;
unsigned long volt;
void chuyendoi()
{
WR_ADC = 0;
delay_short();
WR_ADC = 1;
while (!INTR_ADC);
x=P0;
volt = x;
}
void main(){
setting();
ghi_lenh(0x01);
ghi_lenh(0x80);
ghi_chuoi("http://");
ghi_lenh(0xc0);
ghi_chuoi("machdientu.net");
delay(100);
ghi_lenh(0x01);
ghi_chuoi("Nhiet do la: ");
RD_ADC = 0;
WR_ADC = 0;
INTR_ADC = 1;
delay(10);
P0 = 0xff;
while(1){
ghi_lenh(0x80+9);
chuyendoi();
ghi_lenh(0xc0);
ghi_lenh(0x0c);
ghi_so_nguyen(volt);
ghi_lenh(0xc0+3);
ghi_nhanh_chuoi("oC ");
}
}

#include <AT89X51.H>
#include <stdio.h>
#include<string.h>

#define RS P3_5
#define RW P3_6//RW=0 => ghi
#define EN P3_7//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
#define INTR P3_2
#define RD P3_3
#define WR P3_4

//===========================
void delay_ms(int n)
{
int k,j;
for(k=0;k<n;k++)
{
for(j=0;j<500;j++);
}
}
//==========================
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
//===========================
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
//============================
void LCDWriteCmd(unsigned char c) //CT con ghi du lieu len LCD
{
RS=0;//code
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//==============================
void LCDWriteData(unsigned char c) //CT con doc du lieu tu LCD
{
RS=1;//data
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();

}
//===============================
void LCD_init() // Khoi tao LCD
{
delay_15ms();
LCDWriteCmd(0x38); //che do 8bit,2 hang,kieu ky tu 5x8 diem anh.
LCDWriteCmd(0x0C); //hien thi man hinh.
LCDWriteCmd(0x06); //tu dong tang bo dem dia chi
LCDWriteCmd(0x01); // Xoa man hinh LCD
}
//================================
void LCD_clear()
{
LCDWriteCmd(0x01);
}
//===============================
void LCD_home()
{
LCDWriteCmd(0x80);
}
//=============================
void LCD_putstr(unsigned char *s)
{
while (*s)
{
LCDWriteData(*s);
s++;
}
}
//============================
void HienThi_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
LCDWriteData(t+48);
else if(t<100)
{
LCDWriteData(t/10+48);
LCDWriteData(t%10+48);
}
else
{
v=t/10;
LCDWriteData(v/10+48);
LCDWriteData(v%10+48);
LCDWriteData(t%10+48);
}
}
//==========================================
void main (void)
{
unsigned char gt=0;
//gt la bie^n' cho gia tri 8bit ADC
LCD_init();
while(1)
{

WR=0;
//
Bat dau chuyen doi gia tri tu ADC
delay_ms(5);
// Tao tre de cap nhat du lieu tu ADC
WR=1;
//ket thuc chuyen doi.
// while(!INTR);
RD=0;
// Nhan du lieu da duoc chuyen doi
gt=P2;
//cap nhat ADC.
LCDWriteCmd(0x01);
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
LCDWriteCmd(0xC0);
LCD_putstr("Nhiet do:");
HienThi_ADC(gt);
delay_ms(150);
}
}
#include <AT89X51.H>
#include <stdio.h>
#include<string.h>
#define RS P3_5
#define RW P3_6//RW=0 => ghi
#define EN P3_7//RW=1 => doc
//RS=0 => code
//RS=1 => data
#define LCD_PORT P1
#define INTR P3_2
#define RD P3_3
#define WR P3_4

//===========================
void delay_ms(int n)
{
int k,j;
for(k=0;k<n;k++)
{
for(j=0;j<500;j++);
}
}
//==========================
void delay_5ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<4;j++){}
}
//===========================
void delay_15ms()
{
int i,j;
for(i=0;i<250;i++)
for(j=0;j<100;j++){}
}
//============================
void LCDWriteCmd(unsigned char c) //CT con ghi du lieu len LCD
{
RS=0;//code
RW=0;//ghi
LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//==============================
void LCDWriteData(unsigned char c) //CT con doc du lieu tu LCD
{
RS=1;//data
RW=0;//ghi

LCD_PORT=c;
EN=1;
EN=0;
delay_5ms();
}
//===============================
void LCD_init() // Khoi tao LCD
{
delay_15ms();
LCDWriteCmd(0x38); //che do 8bit,2 hang,kieu ky tu 5x8 diem anh.
LCDWriteCmd(0x0C); //hien thi man hinh.
LCDWriteCmd(0x06); //tu dong tang bo dem dia chi
LCDWriteCmd(0x01); // Xoa man hinh LCD
}
//================================
void LCD_clear()
{
LCDWriteCmd(0x01);
}
//===============================
void LCD_home()
{
LCDWriteCmd(0x80);
}
//=============================
void LCD_putstr(unsigned char *s)
{
while (*s)
{
LCDWriteData(*s);
s++;
}
}
//============================
void HienThi_ADC(unsigned char t)
{
unsigned char v;
if(t<10)
LCDWriteData(t+48);
else if(t<100)
{
LCDWriteData(t/10+48);
LCDWriteData(t%10+48);
}
else
{
v=t/10;
LCDWriteData(v/10+48);
LCDWriteData(v%10+48);
LCDWriteData(t%10+48);
}
}
//==========================================
void main (void)
{
unsigned char gt=0; //gt la bie^n' cho gia tri 8bit ADC
LCD_init();
while(1)
{
WR=0; // Bat dau chuyen doi gia tri tu ADC
delay_ms(5); // Tao tre de cap nhat du lieu tu ADC
WR=1; //ket thuc chuyen doi.
// while(!INTR);
RD=0; // Nhan du lieu da duoc chuyen doi
gt=P2; //cap nhat ADC.
LCDWriteCmd(0x01);
LCDWriteCmd(0x83);
LCD_putstr("8051 & ADC");
LCDWriteCmd(0xC0);
LCD_putstr("Nhiet do:");
HienThi_ADC(gt);
delay_ms(150);
}
}

You might also like