em đang làm cái đồ án môn liên quan đến LCD làm mô phỏng thì chạy được nhưng khi làm mạch thật thì không thấy chạy gì cả.
code:
#include<regx52.h>
sbit RS= P2^6; // 0 ghi lenh -- 1 ghi du lieu
sbit RW= P2^5; // 0 ghi vao LCD -- 1 doc tu LCD
sbit EN= P2^4; // cho phep
sbit SOS=P2^7; // LED bao tran bo dem
//++++++++++++++++++ cac ham tre ++++++++++++++++++++++++++++++++++++++
void delay_ms(unsigned int time) // Thoi gian tre ms
{
TMOD = 0x10; // Timer 0 che do 1
while (time--)
{
TH1 = -1000/256;
TL1 = -1000%256;
TR1 = 1;
while (!TF1);
TR1 = 0;
TF1 = 0;
}
}
//++++++++++++++++++++
void delay_short(void) // tre khoang 50 micro giay
{
unsigned int i;
for(i=0;i<50;i++);
}
//------------------ ghi lenh ra LCD -----------------------------
void write_command (unsigned char command)
{
delay_ms(10);
P1 = command;
RS = 0; // ghi lenh
RW = 0; // ghi vao LCD
EN = 1;
delay_short();
EN = 0;
delay_short();
}
//------------------ ghi du lieu ----------------------------
void write_data(unsigned char LCD_data)
{
delay_ms(5);
P1 = LCD_data;
RS = 1; // ghi du lieu
RW = 0; // ghi vao LCD
EN = 1;
delay_short();
EN = 0;
delay_short();
}
//--------------- ghi chuoi ky tu ----------------------------
void write_string( char *s)
{
while(*s)
{
write_data(*s);
s++;
}
}
//--------------- khoi tao LCD -------------------------------
void init(void)
{
write_command(0x01); // xoa man hinh hien thi
write_command(0x03); // dua con tro ve vi tri dau dong
write_command(0x38); // che do hien thi 2 dong font chu 5x7
write_command(0x06); // dich con tro sang phai
write_command(0x0c); // bat hien thi - tat con tro
}
//++++++++++++ chuong trinh chinh ++++++++++++++++++++
void main(void)
{
SOS = 1; // led bao tat
IE = 0x81; // su dung ngat 0
IT0 = 1; // ngat suon am
delay_ms(30);
init(); // thiet lap cho LCD
write_string("SO SAN PHAM:");
write_command(0xc0); // xuong dong
write_data('T');
while(1);
}
//-------chuong trinh ngat khi co san pham di qua------
void ngat1(void)interrupt 0
{
SOS = 0;
write_command(0x01); // xoa man hinh hien thi
write_command(0x03); // dua con tro ve vi tri dau dong
write_string("HIEN THI LCD");
write_command(0xc0); // xuong dong
write_string("4 5 6 M");
}
mạch nguyên lý
Dien dan Sinh vien Dai Hoc Cong Nghiep Ha Noi
code:
#include<regx52.h>
sbit RS= P2^6; // 0 ghi lenh -- 1 ghi du lieu
sbit RW= P2^5; // 0 ghi vao LCD -- 1 doc tu LCD
sbit EN= P2^4; // cho phep
sbit SOS=P2^7; // LED bao tran bo dem
//++++++++++++++++++ cac ham tre ++++++++++++++++++++++++++++++++++++++
void delay_ms(unsigned int time) // Thoi gian tre ms
{
TMOD = 0x10; // Timer 0 che do 1
while (time--)
{
TH1 = -1000/256;
TL1 = -1000%256;
TR1 = 1;
while (!TF1);
TR1 = 0;
TF1 = 0;
}
}
//++++++++++++++++++++
void delay_short(void) // tre khoang 50 micro giay
{
unsigned int i;
for(i=0;i<50;i++);
}
//------------------ ghi lenh ra LCD -----------------------------
void write_command (unsigned char command)
{
delay_ms(10);
P1 = command;
RS = 0; // ghi lenh
RW = 0; // ghi vao LCD
EN = 1;
delay_short();
EN = 0;
delay_short();
}
//------------------ ghi du lieu ----------------------------
void write_data(unsigned char LCD_data)
{
delay_ms(5);
P1 = LCD_data;
RS = 1; // ghi du lieu
RW = 0; // ghi vao LCD
EN = 1;
delay_short();
EN = 0;
delay_short();
}
//--------------- ghi chuoi ky tu ----------------------------
void write_string( char *s)
{
while(*s)
{
write_data(*s);
s++;
}
}
//--------------- khoi tao LCD -------------------------------
void init(void)
{
write_command(0x01); // xoa man hinh hien thi
write_command(0x03); // dua con tro ve vi tri dau dong
write_command(0x38); // che do hien thi 2 dong font chu 5x7
write_command(0x06); // dich con tro sang phai
write_command(0x0c); // bat hien thi - tat con tro
}
//++++++++++++ chuong trinh chinh ++++++++++++++++++++
void main(void)
{
SOS = 1; // led bao tat
IE = 0x81; // su dung ngat 0
IT0 = 1; // ngat suon am
delay_ms(30);
init(); // thiet lap cho LCD
write_string("SO SAN PHAM:");
write_command(0xc0); // xuong dong
write_data('T');
while(1);
}
//-------chuong trinh ngat khi co san pham di qua------
void ngat1(void)interrupt 0
{
SOS = 0;
write_command(0x01); // xoa man hinh hien thi
write_command(0x03); // dua con tro ve vi tri dau dong
write_string("HIEN THI LCD");
write_command(0xc0); // xuong dong
write_string("4 5 6 M");
}
mạch nguyên lý
Dien dan Sinh vien Dai Hoc Cong Nghiep Ha Noi
Comment