mình đang làm BTL máy tính điện tử. mô phỏng thì chạy ngon lành cành đào. nhưng làm mạch thật có 2 lỗi với cái LCD.
1/ hiển thị chỉ được một dòng
2/ toàn hiển thì kí tự đặc biệt.
mình dùng chip 89S52, TA 12MHz. ai biết chỉ giáo với.
đây là code và mạch mô phỏng
mô phỏng: máy tính điện tử
1/ hiển thị chỉ được một dòng
2/ toàn hiển thì kí tự đặc biệt.
mình dùng chip 89S52, TA 12MHz. ai biết chỉ giáo với.
đây là code và mạch mô phỏng
Code:
#include <regx52.h> //khai bao thu vien #include <string.h> #include <stdlib.h> #include <ctype.h> #define RS P2_7 //dinh nghia cac chan #define RW P2_6 //RW=0 => ghi #define EN P2_5 //RW=1 => doc //RS=0 => code //RS=1 => data #define LCD_PORT P0 sbit cot0=P1^2; // khai bao sbit cot1=P1^4; sbit cot2=P1^6; sbit cot3=P1^7; sbit hang0=P1^0; sbit hang1=P1^1; sbit hang2=P1^3; sbit hang3=P1^5; int sttphim; unsigned char dau; /***************************cac ham con delay************************/ void delay(unsigned int time) // Thoi gian time ms { TMOD = 0x01; // Timer 0 che do 1 while (time--) { TH0 = -1000/256; TL0 = -1000%256; TR0 = 1; while (!TF0); TR0 = 0; TF0 = 0; } } void delay_short() //ham tre 15ms { int i; for(i=0;i<5;i++); } /***************************Cac hàm xu ly LCD************************/ void kt_ban() //ham ktra co ban LCD { unsigned char x; LCD_PORT = 0xFF; RS = 0; RW = 1; do { EN = 1; delay(50); EN = 0; x=LCD_PORT; x=x&0x80; } while(x!=0x80); } void LCDWriteCmd(unsigned char c) { kt_ban(); delay(50); LCD_PORT = c; RS = 0; // Chon thanh ghi lenh RW = 0; // Chon che do ghi du lieu tu 8051 vao LCD EN = 1; delay_short(); EN = 0; } void LCDWriteData(unsigned char c) { kt_ban(); delay(50); LCD_PORT = c; RS = 1; // Chon che do doc du lieu tu 8051 RW = 0; EN =1; delay_short(); EN = 0; } void LCD_init(void) { LCDWriteCmd(0x38); LCDWriteCmd(0x38); LCDWriteCmd(0x38); LCDWriteCmd(0x01); // Xoa man hinh LCDWriteCmd(0x0f); // Co dich hien thi } void LCD_clear() { LCDWriteCmd(0x01); } void LCD_putstr(unsigned char *s) { while(*s) { LCDWriteData(*s); s++; } } void LCDwritei(long d) { unsigned int i,j; unsigned char a[7]; a[6]=d%10; d=d/10; a[5]=d%10; d=d/10; a[4]=d%10; d=d/10; a[3]=d%10; // Chia lay phan du, duoc chu so hang don vi d=d/10; // Chia lay phan nguyen, duoc nhung chu so da bo hang don vi a[2]=d%10; // Duoc chu so hang chuc d=d/10; // Nhung chu so da bo hang don vi va hang chuc a[1]=d%10; // Duoc hang tram d=d/10; // Duoc chu so da bo hang don vi, chuc, tram a[0]=d%10; // Duoc hang ngan for(i=0;i<7;i++) { if(a[i]!=0) break; } for(j=i;j<7;j++) { LCDWriteData(48+a[j]); } } /***************************Ham quet phim************************/ void quetphim() { int y; P1=0xD4; if((!cot0)|(!cot1)|(!cot2)|(!cot3)) { for(y=0;y<5000;y++){ if(cot0==0) //neu la cot 0 { hang0=1; hang1=1; hang2=1; hang3=1; cot0=0; if(!hang0) {delay(1); while(hang0==0); sttphim=0;} if(!hang1) {delay(1); while(hang1==0); sttphim=1;} if(!hang2) {delay(1); while(hang2==0); sttphim=2;} if(!hang3) {delay(1); while(hang3==0); sttphim=3;} } else if(cot1==0) //neu la hang 1 { hang0=1; hang1=1; hang2=1; hang3=1; cot1=0; if(!hang0) {delay(1); while(hang0==0); sttphim=4;} if(!hang1) {delay(1); while(hang1==0); sttphim=5;} if(!hang2) {delay(1); while(hang2==0); sttphim=6;} if(!hang3) {delay(1); while(hang3==0); sttphim=7;} } if(cot2==0) { hang0=1; hang1=1; hang2=1; hang3=1; cot2=0; if(!hang0) {delay(1); while(hang0==0); sttphim=8;} if(!hang1) {delay(1); while(hang1==0); sttphim=9;} if(!hang2) {delay(1); while(hang2==0); dau='+';} if(!hang3) {delay(1); while(hang3==0); dau='-';} } if(cot3==0) { hang0=1; hang1=1; hang2=1; hang3=1; cot3=0; if(!hang0) {delay(1); while(hang0==0); dau='*';} if(!hang1) {delay(1); while(hang1==0); dau='/';} if(!hang2) {delay(1); while(hang2==0); dau='C';} if(!hang3) {delay(1); while(hang3==0); dau='=';} } } } } /**************************chuong trinh chinh***********************/ void main(void) { long fnumber; long lnumber; long result,sodu,dev; unsigned char sign; unsigned char hienthi; int i,kt,j; kt=0; i=0; LCD_init(); LCDWriteCmd(0x01); LCDWriteCmd(0x0C); LCD_putstr("http://"); LCDWriteCmd(0xC0); LCDWriteCmd(0x0C); LCD_putstr("nhoclove1711"); delay(300); while(1) { dau='w'; sttphim=10; quetphim(); if(sttphim!=10) { if(kt==0){ if(i==0) {LCD_clear();} fnumber=fnumber*10+sttphim; i=1; } else{ lnumber=lnumber*10+sttphim; } hienthi=sttphim+48; LCDWriteData(hienthi); } else { switch(dau) { case 'C': { LCDWriteCmd(0x01); break; } case '+': { if(i==0) {LCD_clear();} LCDWriteData(dau); kt=1; sign='+'; break; } case '-': { if(i==0) {LCD_clear();} LCDWriteData(dau); kt=1; sign='-'; break; } case '*': { if(i==0) {LCD_clear();} LCDWriteData(dau); kt=1; sign='*'; break; } case '/' : { if(i==0) {LCD_clear();} LCDWriteData(dau); kt=1; sign='/'; break; } case '=' : { if(i==0) {LCD_clear();} if(sign=='+') { LCDWriteData(dau); result = fnumber+lnumber; LCDWriteCmd(0xC0); if(result>999999999){LCD_putstr("ERROR");} else { LCDwritei(result);} LCDWriteCmd(0x80); kt=0; fnumber=0; lnumber=0; i=0; } else if(sign=='-') { LCDWriteData(dau); if(fnumber>lnumber) {result = fnumber-lnumber;} else{ LCDWriteData('-'); result = lnumber-fnumber; } LCDWriteCmd(0xC0); if(result>999999999){LCD_putstr("ERROR");} else { LCDwritei(result);} LCDWriteCmd(0x80); kt=0; fnumber=0; lnumber=0; i=0; } else if(sign=='*') { LCDWriteData(dau); result = fnumber*lnumber; LCDWriteCmd(0xC0); if(result>999999999){LCD_putstr("ERROR");} else { LCDwritei(result);} LCDWriteCmd(0x80); kt=0; fnumber=0; lnumber=0; i=0; } else if(sign=='/') { LCDWriteData(dau); if(lnumber==0) { LCDWriteCmd(0xC0); LCD_putstr("ERROR"); LCDWriteCmd(0x80); } else { result = fnumber/lnumber; LCDWriteCmd(0xC0); if(fnumber<lnumber) { LCD_putstr("0"); } else { LCDwritei(result); } sodu = fnumber%lnumber; } if(sodu!=0) { LCDWriteData(','); for(j=0;j<9;j++) { dev=(sodu*10)/lnumber; LCDwritei(dev); dev=(sodu*10)%lnumber; sodu=dev; } LCDWriteCmd(0x80); kt=0; fnumber=0; lnumber=0; i=0; } else { kt=0; fnumber=0; lnumber=0; i=0; } } else break; } default : break; } } } }
Comment