0% found this document useful (0 votes)
7 views8 pages

CODE

Uploaded by

Phạm Tài
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

CODE

Uploaded by

Phạm Tài
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include <REGX51.

H>

// Dinh nghia cac chan LED 7 doan


#define LED1 P2_0
#define LED2 P2_1

// Dinh nghia cac chan cua ADC0804


#define CS P3_0
#define RD P3_1
#define WR P3_2
#define INTR P3_3

// Khai bao mang ma 7 doan


unsigned char LED_Code[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90
};

// Ham delay
void delay(int time) {
while (time--); // Delay
}

// Ham doc gia tri tu ADC0804


unsigned char doc_adc() {
unsigned char gia_tri;
CS = 0; // Kich hoat chip select
WR = 0; // Gui xung ghi den ADC0804
WR = 1; // Ket thuc xung ghi
while (INTR == 0); // Cho den khi co tin hieu ngat tu ADC (da san sang du lieu)
RD = 0; // Bat dau doc du lieu tu ADC
gia_tri = P1; // Doc du lieu tu P1
RD = 1; // Ket thuc doc
return gia_tri;
}

// Ham tinh toan nhiet do tu gia tri ADC


float tinhtoan_nhietdo(unsigned char adc_value) {
float u = (adc_value * 5) / 255; // Chuyen gia tri ADC sang dien ap (0-5V)
float nhietdo = u * 150/1.5; // Chuyen dien ap thanh nhiet do (LM35:
10mV/°C)
return nhietdo;
}

// Ham hien thi nhiet do len LED 7 doan


void hienthi(int x) {
unsigned char chuc, donvi;
chuc = x % 100 / 10; // Tach phan hang chuc
donvi = x % 10; // Tach phan hang don vi

// Hien thi phan hang chuc


LED1 = 1; // Bat LED1 (Hien thi phan chuc)
P0 = LED_Code[chuc]; // Hien thi gia tri hang chuc
delay(300); // Delay
LED1 = 0; // Tat LED1

// Hien thi phan hang don vi


LED2 = 1; // Bat LED2 (Hien thi phan don vi)
P0 = LED_Code[donvi]; // Hien thi gia tri hang don vi
delay(300); // Delay
LED2 = 0; // Tat LED2
}

void main() {
unsigned char adc_value;
float nhietdo;

while (1) {
adc_value = doc_adc(); // Doc gia tri ADC tu ADC0804
nhietdo = tinhtoan_nhietdo(adc_value); // Tinh toan nhiet do tu gia tri ADC
hienthi((int)nhietdo); // Hien thi nhiet do len LED 7 doan
}
}

#include <AT89X52.H>
#define LED1 P2_0
#define LED2 P2_1
#define SEG7 P0
#define rd P3_1 // Ch?n d?c d? li?u t? ADC
#define wr P3_2 // Ch?n ghi d? b?t d?u chuy?n d?i ADC
#define cs P3_0 // Ch?n ch?n chip (Chip Select)
#define intr P3_3 // Ch?n ng?t b?o hi?u ho?n th?nh chuy?n d?i
#define adc P3
void timer(void);
void Delay_ms(unsigned int);
unsigned char R5,R6;
unsigned int conv, get_value,i,chuc,donvi;
unsigned char maled[10] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
void timer()//tao ngat tran timer voi ck 1ms
{
TMOD = 0x01; // Timer0 mode1
TH0=0xFC; //initial value for 1ms
TL0=0x17;
TR0 = 1; // timer0 start
while (TF0 == 1); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
void Delay(unsigned int count)// delay_ms
{
unsigned int r;
for(r=0;r<count;r++)
{
timer();
}
}
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++) {
// L?p d? t?o d? tr? (t?c d? thay d?i t?y t?n s? th?ch anh)
}
}
}
unsigned char adc_read() {
cs = 0;
wr = 0;
rd = 1;
wr = 1;
while(intr==1);
rd = 0;
conv = P3;
Delay(1);
cs = 1;
return conv;
}
void main() {
P3 = 0xFF;
while (1) {
get_value = adc_read();
get_value = (get_value*500/256);
chuc = (get_value/10);
donvi = (get_value%10);
LED1 = 1;
SEG7 = maled[chuc];
delay_ms(1);
LED1 = 0;
LED2 = 1;
SEG7 = maled[donvi];
delay_ms(1);
LED2 = 0;
rd = 1;
}
}
3.
#include <REGX51.H>

// Dinh nghia cac chan ket noi


#define LED1 P2_0 // LED 7 doan hien thi hang chuc
#define LED2 P2_1 // LED 7 doan hien thi hang don vi
#define SEG7 P0 // Thanh ghi de hien thi LED 7 doan
#define CS P3_0 // Chip Select (kich hoat ADC)
#define RD P3_1 // Read (doc du lieu tu ADC)
#define WR P3_2 // Write (bat dau chuyen doi ADC)
#define INTR P3_3 // Interrupt (tin hieu bao ADC da san sang)

// Ma LED 7 doan
unsigned char maled[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90};

// Ham tao tre


void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 125; j++); // Dieu chinh tan so thach anh neu can
}
}

// Ham doc gia tri tu ADC0804


unsigned char adc_read() {
unsigned char adc_value;

CS = 0; // Kich hoat ADC


WR = 0; // Bat dau chuyen doi
WR = 1; // Ket thuc chuyen doi
while (INTR == 1); // Doi ADC hoan thanh chuyen doi
RD = 0; // Doc du lieu tu ADC
adc_value = P1; // Lay gia tri tu cong P1
RD = 1; // Ket thuc doc du lieu
CS = 1; // Huy kich hoat ADC

return adc_value;
}

// Ham hien thi nhiet do len LED 7 doan


void display_temperature(unsigned int temperature) {
unsigned char chuc, donvi;

chuc = temperature / 10; // Lay phan hang chuc


donvi = temperature % 10; // Lay phan hang don vi

// Hien thi hang chuc


LED1 = 1; // Bat LED1
SEG7 = maled[chuc]; // Hien thi gia tri hang chuc
delay_ms(5); // Tre de on dinh hien thi
LED1 = 0; // Tat LED1

// Hien thi hang don vi


LED2 = 1; // Bat LED2
SEG7 = maled[donvi]; // Hien thi gia tri hang don vi
delay_ms(5); // Tre de on dinh hien thi
LED2 = 0; // Tat LED2
}

// Chuong trinh chinh


void main() {
unsigned char adc_value;
unsigned int temperature;

// Khoi tao cong


P3 = 0xFF; // Cau hinh cong P3 la dau vao

while (1) {
adc_value = adc_read(); // Doc gia tri tu ADC
temperature = (adc_value * 500) / 256; // Quy doi gia tri ADC sang nhiet do
(°C)
display_temperature(temperature); // Hien thi nhiet do len LED 7 doan
}
}

You might also like