bọn mình làm đồ án là đèn giao thông tại 1 ngã tư
mình đã làm mạch thật nhưng nó k chạy gì cả.
đây là file proteus
http://www.mediafire.com/?q48g508dhdnzqy0
đây là file hex
http://www.mediafire.com/?rqp8sq7xdb60wo2
anh em nào giúp với.....
đây là code mình viết
mình đã làm mạch thật nhưng nó k chạy gì cả.
đây là file proteus
http://www.mediafire.com/?q48g508dhdnzqy0
đây là file hex
http://www.mediafire.com/?rqp8sq7xdb60wo2
anh em nào giúp với.....
đây là code mình viết
#include<AT89X51.h>
signed char set_do1,set_do2,set_vang1,set_vang2,set_xanh1,set_ xanh2;
signed char setdo1,setdo2,setvang1,setvang2,setxanh1,setxanh2;
signed char do1,do2,xanh1,xanh2,vang1,vang2;
signed int over=0;
sbit set=P3^2;
sbit add=P3^3;
sbit sub=P3^4;
unsigned char ma[10]={192,249,164,176,153,146,130,248,128,144};
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<200;y++);
}
}
void h_thi0(unsigned char i) //den 1 sang
{
P2=ma[i%10];
P0_4=0;
delay(5);
P0_4=1;
delay(5);
P2=0;
P2=ma[i/10];
P0_5=0;
delay(5);
P0_5=1;
delay(5);
P2=0;
}
void h_thi1(unsigned char i) //den 2 sang
{
P2=ma[i%10];
P0_6=0;
delay(5);
P0_6=1;
delay(5);
P2=0;
P2=ma[i/10];
P0_7=0;
delay(5);
P0_7=1;
delay(5);
P2=0;
}
void tat()
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
}
//----------------------------------------
typedef unsigned char bit8; // du lieu 8 bit
const bit8 spec = 0x38; // ô hien thi có 5x8 ðiem anh
const bit8 LcdCur_ON = 0x0f; // bat con tro
const bit8 LcdCur_OFF = 0x0c; // tat con tro
const bit8 Lcd_Clr = 0x01; // xoá het LCD
const bit8 Curser_Right = 0x06; // di chuyen con tro sang phai
const bit8 Curser_HOME = 0x02; // di chuyen con tro ve vi trí ðau
sbit busyCheck = P1^7;
//kiem tra LCD
void check()
{
P3_7= 0; /* Cho LCD hoat ðong */
busyCheck = 1; /* Bit ban = 1 */
P3_5= 0; /* Ra lenh cho LCD */
P3_6= 1; /* LCD ðoc lenh */
while (busyCheck != 0) /* Ðoi cho ðen khi LCD het ban*/
{
P3_7= 0;
P3_7= 1;
}
P3_7= 1;
}
//Xuat ma lenh cho LCD (DK)
void ghi_lenh(bit8 cmd)
{
check(); /* kiem tra trang thái ban? */
P3_7= 0; /* Cho phép LCD hoat ðong*/
P1 = cmd; /* ma lenh gui toi Port3 */
P3_5= 0; /*Chon thanh ghi*/
P3_6= 0; /* Cho phép ghi vào LCD */
P3_7= 1;
P3_7= 0;
}
// Cài ðat ban ðau cho LCD ( co con tro )
void set_lcd()
{
ghi_lenh(spec);
ghi_lenh(LcdCur_ON);
ghi_lenh(Lcd_Clr);
ghi_lenh(Curser_Right);
ghi_lenh(Curser_HOME);
}
// Cài ðat ban ðau cho LCD ( tat nhap nhay con tro )
void set_lcd1()
{
ghi_lenh(spec);
ghi_lenh(LcdCur_OFF);
ghi_lenh(Lcd_Clr);
ghi_lenh(Curser_Right);
ghi_lenh(Curser_HOME);
}
// vi tri con tro
void vi_tri(unsigned char row, unsigned char col)
{
ghi_lenh((0x80) + (row << 6) + col);
}
// gui ky tu len LCD
void writechar(unsigned char x)
{
check();
P3_7= 0;
P1=x; /* Xuat ma ASCII cua ky tu ra LCD*/
P3_5= 1;
P3_6= 0;
P3_7= 1;
P3_7= 0;
}
// gui chuoi
void write_string1(char *s)
{
while(*s)
{
writechar(*s);
s++;
}
}
void write_string2(char *s)
{
int i=0;
while(*s)
{
writechar(*s);
delay(150);
s++;
i++;
if(i>15)
ghi_lenh(0x1c);
}
}
void cho()
{
set_lcd1();
vi_tri(0,2);
write_string1("KHOA DIEN TU");
vi_tri(1,4);
write_string1("- HAUI -");
}
void gioi_thieu()
{
unsigned long int i;
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
set_lcd();
write_string2("KHOA DIEN TU DAI HOC CONG NGHIEP HA NOI");
for(i = 0; i<35000; i++);
set_lcd1();
vi_tri(0,0);
write_string1("NHOM 32:");
for(i = 0; i<1000; i++);
vi_tri(1,0);
write_string1("HOANG VAN TAN");
for(i = 0; i<50000; i++);
vi_tri(1,0);
write_string1("NGUYEN VAN NAM");
for(i = 0; i<50000; i++);
vi_tri(1,0);
write_string1("PHAM QUANG DUNG");
for(i = 0; i<50000; i++);
}
void sang_led()
{
while(1)
{
do2=setdo2;
xanh1=setxanh1;
do //xanh1 do2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_0=P3_1=1;
h_thi0(xanh1);
h_thi1(do2);
}
while(xanh1!=0);
vang1=setvang1;
do // vang1 do2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_1=P3_1=1;
h_thi0(vang1);
h_thi1(do2);
}
while(do2!=0);
do1=setdo1;
xanh2=setxanh2;
do
{ //do1 xanh2
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_2=P3_0=1;
h_thi0(do1);
h_thi1(xanh2);
}
while(xanh2!=0);
vang2=setvang2;
do //do1 vang2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_3=P3_0=1;
h_thi0(vang2);
h_thi1(do1);
}
while(do1!=0);
}
}
void main()
{
set_do1=set_do2=setdo1=setdo2=25;
set_xanh1=set_xanh2=setxanh1=setxanh2=20;
set_vang1=set_vang2=setvang1=setvang2=5;
TMOD=2; //timer0 o che do 8 bit tu nap lai //200 us
TH0=TL0=-200;
TR0=1;
IE=0x83; //ngat o t INT0 va timer 0
gioi_thieu();
while(1)
{
cho();
sang_led();
}
}
void lay_ma(unsigned char i,unsigned char colum,unsigned char row)
{
unsigned char d;
d=i/10;
vi_tri(colum,row-1);
writechar(d+0x30);
d=i%10;
vi_tri(colum,row);
writechar(d+0x30);
}
void ngoai_0(void) interrupt 0
{
unsigned int i;
set_do1=setdo1;
set_do2=setdo2;
set_xanh1=setxanh1;
set_xanh2=setxanh2;
set_vang1=setvang1;
set_vang2=setvang2;
IE=0; // cam ngat (nhung van cho timer 0 chay)
P0_1=P0_3=1;
P3_0=P3_1=P0_0=P0_2=0;
P2=255;
set_lcd();
vi_tri(0,0);
write_string1("Den 1:");
lay_ma(set_do1,0,7);
lay_ma(set_vang1,0,11);
lay_ma(set_xanh1,0,15);
vi_tri(1,0);
write_string1("Den 2:");
lay_ma(set_do2,1,7);
lay_ma(set_vang2,1,11);
lay_ma(set_xanh2,1,15);
vi_tri(0,7);
while(set==0);
up:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_do1++;
if(set_do1==100)
{
set_do1=0;
}
lay_ma(set_do1,0,7);
vi_tri(0,7);
while(add==0);
goto up;
}
if(sub==0)
{
set_do1--;
if(set_do1==-1)
{
set_do1=99;
}
lay_ma(set_do1,0,7);
vi_tri(0,7);
while(sub==0);
goto up;
}
while(set==0);
vi_tri(0,11);
//-----------------------
up1:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_vang1++;
if(set_vang1==100)
{
set_vang1=0;
}
lay_ma(set_vang1,0,11);
vi_tri(0,11);
while(add==0);
goto up1;
}
if(sub==0)
{
set_vang1--;
if(set_vang1==-1)
{
set_vang1=99;
}
lay_ma(set_vang1,0,11);
vi_tri(0,11);
while(sub==0);
goto up1;
}
while(set==0);
vi_tri(0,15);
up2:
//------------------------------
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_xanh1++;
if(set_xanh1==100)
{
set_xanh1=0;
}
lay_ma(set_xanh1,0,15);
vi_tri(0,15);
while(add==0);
goto up2;
}
if(sub==0)
{
set_xanh1--;
if(set_xanh1==-1)
{
set_xanh1=99;
}
lay_ma(set_xanh1,0,15);
vi_tri(0,15);
while(sub==0);
goto up2;
}
while(set==0);
vi_tri(1,7);
//------------------------
up3:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_do2++;
if(set_do2==100)
{
set_do2=0;
}
lay_ma(set_do2,1,7);
vi_tri(1,7);
while(add==0);
goto up3;
}
if(sub==0)
{
set_do2--;
if(set_do2==-1)
{
set_do2=99;
}
lay_ma(set_do2,1,7);
vi_tri(1,7);
while(sub==0);
goto up3;
}
while(set==0);
vi_tri(1,11);
//-------------------------
up4:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_vang2++;
if(set_vang2==100)
{
set_vang2=0;
}
lay_ma(set_vang2,1,11);
vi_tri(1,11);
while(add==0);
goto up4;
}
if(sub==0)
{
set_vang2--;
if(set_vang2==-1)
{
set_vang2=99;
}
lay_ma(set_vang2,1,11);
vi_tri(1,11);
while(sub==0);
goto up4;
}
while(set==0);
vi_tri(1,15);
//---------------------------------
up5:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_xanh2++;
if(set_xanh2==100)
{
set_xanh2=0;
}
lay_ma(set_xanh2,1,15);
vi_tri(1,15);
while(add==0);
goto up5;
}
if(sub==0)
{
set_xanh2--;
if(set_xanh2==-1)
{
set_xanh2=99;
}
lay_ma(set_xanh2,1,15);
vi_tri(1,15);
while(sub==0);
goto up5;
}
while(set==0);
if((set_vang1+set_xanh1==set_do2)&&(set_vang2+set_ xanh2==set_do1))
{
setdo1=set_do1;
setxanh1=set_xanh1;
setvang1=set_vang1;
setdo2=set_do2;
setxanh2=set_xanh2;
setvang2=set_vang2;
}
else
{
set_lcd();
vi_tri(0,0);
write_string1("THOI GIAN");
vi_tri(1,5);
write_string1("KHONG DUNG!");
for(i = 0; i<40000; i++);
}
do1=do2=xanh1=xanh2=vang1=vang2=0;
IE=0x83;
cho();
}
void time0() interrupt 1 //ngat timer 0
{ //cu 200 us thi tran 1 lan
over++;
if(over==5000) //(5000*200=1.000.000 us =1s )
{
over=0;
do2--;
xanh1--;
vang1--;
do1--;
xanh2--;
vang2--;
}
}
signed char set_do1,set_do2,set_vang1,set_vang2,set_xanh1,set_ xanh2;
signed char setdo1,setdo2,setvang1,setvang2,setxanh1,setxanh2;
signed char do1,do2,xanh1,xanh2,vang1,vang2;
signed int over=0;
sbit set=P3^2;
sbit add=P3^3;
sbit sub=P3^4;
unsigned char ma[10]={192,249,164,176,153,146,130,248,128,144};
void delay(unsigned char time)
{
unsigned char x,y;
for(x=0;x<time;x++)
{
for(y=0;y<200;y++);
}
}
void h_thi0(unsigned char i) //den 1 sang
{
P2=ma[i%10];
P0_4=0;
delay(5);
P0_4=1;
delay(5);
P2=0;
P2=ma[i/10];
P0_5=0;
delay(5);
P0_5=1;
delay(5);
P2=0;
}
void h_thi1(unsigned char i) //den 2 sang
{
P2=ma[i%10];
P0_6=0;
delay(5);
P0_6=1;
delay(5);
P2=0;
P2=ma[i/10];
P0_7=0;
delay(5);
P0_7=1;
delay(5);
P2=0;
}
void tat()
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
}
//----------------------------------------
typedef unsigned char bit8; // du lieu 8 bit
const bit8 spec = 0x38; // ô hien thi có 5x8 ðiem anh
const bit8 LcdCur_ON = 0x0f; // bat con tro
const bit8 LcdCur_OFF = 0x0c; // tat con tro
const bit8 Lcd_Clr = 0x01; // xoá het LCD
const bit8 Curser_Right = 0x06; // di chuyen con tro sang phai
const bit8 Curser_HOME = 0x02; // di chuyen con tro ve vi trí ðau
sbit busyCheck = P1^7;
//kiem tra LCD
void check()
{
P3_7= 0; /* Cho LCD hoat ðong */
busyCheck = 1; /* Bit ban = 1 */
P3_5= 0; /* Ra lenh cho LCD */
P3_6= 1; /* LCD ðoc lenh */
while (busyCheck != 0) /* Ðoi cho ðen khi LCD het ban*/
{
P3_7= 0;
P3_7= 1;
}
P3_7= 1;
}
//Xuat ma lenh cho LCD (DK)
void ghi_lenh(bit8 cmd)
{
check(); /* kiem tra trang thái ban? */
P3_7= 0; /* Cho phép LCD hoat ðong*/
P1 = cmd; /* ma lenh gui toi Port3 */
P3_5= 0; /*Chon thanh ghi*/
P3_6= 0; /* Cho phép ghi vào LCD */
P3_7= 1;
P3_7= 0;
}
// Cài ðat ban ðau cho LCD ( co con tro )
void set_lcd()
{
ghi_lenh(spec);
ghi_lenh(LcdCur_ON);
ghi_lenh(Lcd_Clr);
ghi_lenh(Curser_Right);
ghi_lenh(Curser_HOME);
}
// Cài ðat ban ðau cho LCD ( tat nhap nhay con tro )
void set_lcd1()
{
ghi_lenh(spec);
ghi_lenh(LcdCur_OFF);
ghi_lenh(Lcd_Clr);
ghi_lenh(Curser_Right);
ghi_lenh(Curser_HOME);
}
// vi tri con tro
void vi_tri(unsigned char row, unsigned char col)
{
ghi_lenh((0x80) + (row << 6) + col);
}
// gui ky tu len LCD
void writechar(unsigned char x)
{
check();
P3_7= 0;
P1=x; /* Xuat ma ASCII cua ky tu ra LCD*/
P3_5= 1;
P3_6= 0;
P3_7= 1;
P3_7= 0;
}
// gui chuoi
void write_string1(char *s)
{
while(*s)
{
writechar(*s);
s++;
}
}
void write_string2(char *s)
{
int i=0;
while(*s)
{
writechar(*s);
delay(150);
s++;
i++;
if(i>15)
ghi_lenh(0x1c);
}
}
void cho()
{
set_lcd1();
vi_tri(0,2);
write_string1("KHOA DIEN TU");
vi_tri(1,4);
write_string1("- HAUI -");
}
void gioi_thieu()
{
unsigned long int i;
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
set_lcd();
write_string2("KHOA DIEN TU DAI HOC CONG NGHIEP HA NOI");
for(i = 0; i<35000; i++);
set_lcd1();
vi_tri(0,0);
write_string1("NHOM 32:");
for(i = 0; i<1000; i++);
vi_tri(1,0);
write_string1("HOANG VAN TAN");
for(i = 0; i<50000; i++);
vi_tri(1,0);
write_string1("NGUYEN VAN NAM");
for(i = 0; i<50000; i++);
vi_tri(1,0);
write_string1("PHAM QUANG DUNG");
for(i = 0; i<50000; i++);
}
void sang_led()
{
while(1)
{
do2=setdo2;
xanh1=setxanh1;
do //xanh1 do2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_0=P3_1=1;
h_thi0(xanh1);
h_thi1(do2);
}
while(xanh1!=0);
vang1=setvang1;
do // vang1 do2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_1=P3_1=1;
h_thi0(vang1);
h_thi1(do2);
}
while(do2!=0);
do1=setdo1;
xanh2=setxanh2;
do
{ //do1 xanh2
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_2=P3_0=1;
h_thi0(do1);
h_thi1(xanh2);
}
while(xanh2!=0);
vang2=setvang2;
do //do1 vang2
{
P0_0=P0_1=P0_2=P0_3=P3_0=P3_1=0;
P0_3=P3_0=1;
h_thi0(vang2);
h_thi1(do1);
}
while(do1!=0);
}
}
void main()
{
set_do1=set_do2=setdo1=setdo2=25;
set_xanh1=set_xanh2=setxanh1=setxanh2=20;
set_vang1=set_vang2=setvang1=setvang2=5;
TMOD=2; //timer0 o che do 8 bit tu nap lai //200 us
TH0=TL0=-200;
TR0=1;
IE=0x83; //ngat o t INT0 va timer 0
gioi_thieu();
while(1)
{
cho();
sang_led();
}
}
void lay_ma(unsigned char i,unsigned char colum,unsigned char row)
{
unsigned char d;
d=i/10;
vi_tri(colum,row-1);
writechar(d+0x30);
d=i%10;
vi_tri(colum,row);
writechar(d+0x30);
}
void ngoai_0(void) interrupt 0
{
unsigned int i;
set_do1=setdo1;
set_do2=setdo2;
set_xanh1=setxanh1;
set_xanh2=setxanh2;
set_vang1=setvang1;
set_vang2=setvang2;
IE=0; // cam ngat (nhung van cho timer 0 chay)
P0_1=P0_3=1;
P3_0=P3_1=P0_0=P0_2=0;
P2=255;
set_lcd();
vi_tri(0,0);
write_string1("Den 1:");
lay_ma(set_do1,0,7);
lay_ma(set_vang1,0,11);
lay_ma(set_xanh1,0,15);
vi_tri(1,0);
write_string1("Den 2:");
lay_ma(set_do2,1,7);
lay_ma(set_vang2,1,11);
lay_ma(set_xanh2,1,15);
vi_tri(0,7);
while(set==0);
up:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_do1++;
if(set_do1==100)
{
set_do1=0;
}
lay_ma(set_do1,0,7);
vi_tri(0,7);
while(add==0);
goto up;
}
if(sub==0)
{
set_do1--;
if(set_do1==-1)
{
set_do1=99;
}
lay_ma(set_do1,0,7);
vi_tri(0,7);
while(sub==0);
goto up;
}
while(set==0);
vi_tri(0,11);
//-----------------------
up1:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_vang1++;
if(set_vang1==100)
{
set_vang1=0;
}
lay_ma(set_vang1,0,11);
vi_tri(0,11);
while(add==0);
goto up1;
}
if(sub==0)
{
set_vang1--;
if(set_vang1==-1)
{
set_vang1=99;
}
lay_ma(set_vang1,0,11);
vi_tri(0,11);
while(sub==0);
goto up1;
}
while(set==0);
vi_tri(0,15);
up2:
//------------------------------
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_xanh1++;
if(set_xanh1==100)
{
set_xanh1=0;
}
lay_ma(set_xanh1,0,15);
vi_tri(0,15);
while(add==0);
goto up2;
}
if(sub==0)
{
set_xanh1--;
if(set_xanh1==-1)
{
set_xanh1=99;
}
lay_ma(set_xanh1,0,15);
vi_tri(0,15);
while(sub==0);
goto up2;
}
while(set==0);
vi_tri(1,7);
//------------------------
up3:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_do2++;
if(set_do2==100)
{
set_do2=0;
}
lay_ma(set_do2,1,7);
vi_tri(1,7);
while(add==0);
goto up3;
}
if(sub==0)
{
set_do2--;
if(set_do2==-1)
{
set_do2=99;
}
lay_ma(set_do2,1,7);
vi_tri(1,7);
while(sub==0);
goto up3;
}
while(set==0);
vi_tri(1,11);
//-------------------------
up4:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_vang2++;
if(set_vang2==100)
{
set_vang2=0;
}
lay_ma(set_vang2,1,11);
vi_tri(1,11);
while(add==0);
goto up4;
}
if(sub==0)
{
set_vang2--;
if(set_vang2==-1)
{
set_vang2=99;
}
lay_ma(set_vang2,1,11);
vi_tri(1,11);
while(sub==0);
goto up4;
}
while(set==0);
vi_tri(1,15);
//---------------------------------
up5:
while(set!=0&&add!=0&&sub!=0);
if(add==0)
{
set_xanh2++;
if(set_xanh2==100)
{
set_xanh2=0;
}
lay_ma(set_xanh2,1,15);
vi_tri(1,15);
while(add==0);
goto up5;
}
if(sub==0)
{
set_xanh2--;
if(set_xanh2==-1)
{
set_xanh2=99;
}
lay_ma(set_xanh2,1,15);
vi_tri(1,15);
while(sub==0);
goto up5;
}
while(set==0);
if((set_vang1+set_xanh1==set_do2)&&(set_vang2+set_ xanh2==set_do1))
{
setdo1=set_do1;
setxanh1=set_xanh1;
setvang1=set_vang1;
setdo2=set_do2;
setxanh2=set_xanh2;
setvang2=set_vang2;
}
else
{
set_lcd();
vi_tri(0,0);
write_string1("THOI GIAN");
vi_tri(1,5);
write_string1("KHONG DUNG!");
for(i = 0; i<40000; i++);
}
do1=do2=xanh1=xanh2=vang1=vang2=0;
IE=0x83;
cho();
}
void time0() interrupt 1 //ngat timer 0
{ //cu 200 us thi tran 1 lan
over++;
if(over==5000) //(5000*200=1.000.000 us =1s )
{
over=0;
do2--;
xanh1--;
vang1--;
do1--;
xanh2--;
vang2--;
}
}
Comment