??? 05/05/09 17:57 Read: times |
#165076 - Control program via buttons and via terminal |
Hi, i am doing a logic game and i must allow control via buttons on PORT1 and via RS232 (terminal)
Here is my code, which i wrote.. its using GLCD 128x64 #include <reg51.h> #include <intrins.h> #include <stdio.h> typedef unsigned char byte; typedef unsigned int word; sbit GRST = P2^5; sbit GE = P2^4; sbit GRW = P2^3; sbit GRS = P2^2; sbit GCS2 = P2^1; sbit GCS1 = P2^0; #define GDATA P0 void signal_init(void) { GRST = 1; GCS1 = 0; GCS2 = 0; GDATA = 0; GRW = 0; GE = 0; GRS = 0; } void wait_reset(bit controller) { byte b = 0; GCS1 = controller; GCS2 = ~controller; GDATA = 0xff; GRS = 0; GRW = 1; do { GE = 1; _nop_(); _nop_(); _nop_(); b = GDATA; GE = 0; } while (b & 0x10); return; } void wait_busy(bit controller) { byte b = 0; GCS1 = controller; GCS2 = ~controller; GDATA = 0xff; GRS = 0; GRW = 1; do { GE = 1; _nop_(); _nop_(); _nop_(); b = GDATA; GE = 0; } while (b & 0x80); return; } void write_cmnd(byte g_cmnd, bit controller) { GCS1 = controller; GCS2 = ~controller; GRS = 0; GRW = 0; GDATA = g_cmnd; GE = 1; _nop_(); _nop_(); GE = 0; return; } void write_data(byte g_data, bit controller) { GCS1 = controller; GCS2 = ~controller; GRS = 1; GRW = 0; GDATA = g_data; GE = 1; _nop_(); _nop_(); GE = 0; return; } byte cekej100 = 0; void timer0(void) interrupt 1 { if (cekej100 > 0) cekej100--; } #define WAIT(doba) {cekej100 = doba; while (cekej100) ;} #define SET_G_PAGE(page, radic) {write_cmnd(0xb8 + page, radic); wait_busy(radic); } #define SET_G_YADR(addr, radic) {write_cmnd(0x40 + addr, radic); wait_busy(radic); } ///Funct for wirite nums and letters to GLCD - OK void write_num(char num,byte strana) { byte i; char chars[]= { 0x3e,0x51,0x49,0x45,0x3e, /* 0 0x30 */ 0x00,0x42,0x7f,0x40,0x00, /* 1 */ 0x42,0x61,0x51,0x49,0x46, /* 2 */ 0x21,0x41,0x45,0x4b,0x31, /* 3 */ 0x18,0x14,0x12,0x7f,0x10, /* 4 */ 0x27,0x45,0x45,0x45,0x39, /* 5 */ 0x3c,0x4a,0x49,0x49,0x30, /* 6 */ //0x00,0x00,0x77,0x00,0x00, /* | */ 0x00,0x00,0x4f,0x00,0x00, /* ! */ 0x3e,0x41,0x41,0x41,0x3e, /* O */ //0x7f,0x08,0x14,0x22,0x41 /* K */ }; for (i=num*5;i<num*5+5;i++) { write_data(chars[i], strana); } } //Blink on actual position - OK void blink(int x,int actual_page,int num,strana) { int i; SET_G_PAGE(actual_page, strana); SET_G_YADR(x*8, strana); for(i = 0; i < 8; i++) { write_data(0, 0); wait_busy(0); } for (i=0;i<10000;i++) _nop_(); SET_G_PAGE(actual_page, strana); SET_G_YADR(x*8, strana); write_num(num,strana); wait_busy(0); for (i=0;i<10000;i++) _nop_(); } //delete GLCD - OK void smazat() { byte p,x; ///smazat for (p = 0; p < 8; p++) { SET_G_PAGE(p, 0); SET_G_YADR(0, 0); for(x = 0; x < 64; x++) { write_data(0, 0); wait_busy(0); } SET_G_PAGE(p, 1); SET_G_YADR(0, 1); for(x = 0; x < 64; x++) { write_data(0, 1); wait_busy(1); } } } int main(void) { byte actual_x=0; // byte actual_page=1; //souradnice y disp byte pole[8]; //Save user input into array byte polerand[8]; //Array with opponents data byte zad=0; byte x = 0; //for for byte pom=0; // byte aa=0; //num of trays byte pocet=6; //num of combinations allowed unsigned char c; //should be for terminal later for (x=0;x<8;x++) {pole[x]=0;} //delete array signal_init(); //P3 = ~(++u); //Set timer+interrupts TMOD = 0x22; TH0 = 256 - 100; TR0 = 1; ET0 = 1; EA = 1; //Set Serial TH1 = 0xEF; // pro rychlost 1200/2400 Bd pri 8MHz krystalu TR1 = 1; // start citace SCON = 0x52; // standardni nastaveni serioveho kanalu PCON |= 0x80; // SMOD=1 => zdvojeni seriove rychlosti (na 24 ES=1; //inicializace LCD GRST = 1; WAIT(250); GRST = 0; WAIT(250); GRST = 1; WAIT(250); wait_reset(0); wait_reset(1); write_cmnd(0x3f, 0); wait_busy(0); write_cmnd(0x3f, 1); wait_busy(1); //vymazani LCD smazat(); SET_G_PAGE(0, 0); SET_G_YADR(0, 0); //Display Z - zadavani //0x61,0x51,0x49,0x45,0x43, /* Z */ write_data(0x61,0); write_data(0x51,0); write_data(0x49,0); write_data(0x45,0); write_data(0x43,0); //////////////////////// //puts("nZadavanin"); while(1) { ///// blink(actual_x,actual_page,pole[actual_x],0); //blink on actual position if (P1==0xfe) //press button - left { if (pom==0) { pom=1; if (actual_x==7) actual_x=-1; //if we are on end... actual_x++; SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); //set GLCD to actual position for(x = 0; x < 8; x++) //delete actual num at first { write_data(0, 0); wait_busy(0); } SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); //Print num from array to disp if (pole[actual_x]==0) {pole[actual_x]=1; } write_num(pole[actual_x],0); wait_busy(0); WAIT(100000000); } } if (P1==0xff) WAIT(100000000); pom=0; if (P1==0xfd) { //button num 2 - same as before, but to right if (pom==0){ pom=1; if (actual_x==0) actual_x=8; actual_x--; SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); for(x = 0; x < 8; x++) { write_data(0, 0); wait_busy(0); } SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); if (pole[actual_x]==0) {pole[actual_x]=1; } write_num(pole[actual_x],0); wait_busy(0); WAIT(100000000); } } if (P1==0xfb){ //change num - button num 3 if (pom==0){ pom=1; SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); for(x = 0; x < 8; x++) { write_data(0, 0); wait_busy(0); } SET_G_PAGE(actual_page, 0); SET_G_YADR(actual_x*8, 0); pole[actual_x]++; //increase num in array if (pole[actual_x]>=pocet) pole[actual_x]=1; //was max num? write_num(pole[actual_x],0); //print to GLCD wait_busy(0); WAIT(100000000); } } if (P1==0xf7){ //ENTER - button num. 4 if (pom==0){ pom=1; if (zad==1) //opponent already type their combination { byte actual_x_v=0; byte xx; byte polepom[8]; SET_G_PAGE(actual_page, 1); SET_G_YADR(actual_x_v*8, 1); //print "|" //write_num(7,1); //0x00,0x00,0x77,0x00,0x00 | //print | write_data(0x00,1); write_data(0x00,1); write_data(0x77,1); write_data(0x00,1); write_data(0x00,1); wait_busy(0); actual_x_v++; //position on GLCD for (x=0;x<8;x++) { polepom[x]=0;} //delete "help" array for (x=0;x<8;x++) { //test if user input is same as type opponent on start if (pole[x]==polerand[x]) { polepom[x]=1; SET_G_PAGE(actual_page, 1); SET_G_YADR(actual_x_v*8, 1); write_num(7,1); // wait_busy(0); actual_x_v++; aa++; //count num of OK } } for (x=0;x<8;x++) //Test good nums, but on wrong position { for (xx=0;xx<8;xx++) { if ((polerand[xx]==pole[x])&&(polepom[x]==0)) { SET_G_PAGE(actual_page, 1); SET_G_YADR(actual_x_v*8, 1); write_num(8,1); wait_busy(0); actual_x_v++; polepom[x]=2; } } } //SET_G_PAGE(actual_page, 1); SET_G_YADR(actual_x_v*8, 1); actual_page++; actual_x=0; if (aa==8) //All was OK { smazat(); actual_x=0; actual_page=0; //0x3e,0x41,0x41,0x41,0x3e, /* O */ //0x7f,0x08,0x14,0x22,0x41 /* K */ SET_G_PAGE(2, 0); SET_G_YADR(10, 0); //print OK write_data(0x3e,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x3e,0); SET_G_YADR(18, 0); write_data(0x7f,0); wait_busy(0); write_data(0x08,0); wait_busy(0); write_data(0x14,0); wait_busy(0); write_data(0x22,0); wait_busy(0); write_data(0x41,0); wait_busy(0); } if (actual_page==8)//try 8x and was wrong { smazat(); actual_x=0; actual_page=0; SET_G_PAGE(2, 0); SET_G_YADR(10, 0); //print KO wait_busy(0); write_data(0x7f,0); wait_busy(0); write_data(0x08,0); wait_busy(0); write_data(0x14,0); wait_busy(0); write_data(0x22,0); wait_busy(0); write_data(0x41,0); wait_busy(0); SET_G_YADR(18, 0); wait_busy(0); write_data(0x3e,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x41,0); wait_busy(0); write_data(0x3e,0); } } else //save opponent combination { zad=1; for (x=0;x<8;x++) { polerand[x]=pole[x]; //presuneme vsechno z pole do polerand a pole nulujeme pole[x]=0; } smazat(); //smazeme disp actual_x=0; actual_page=0; } wait_busy(0); }} } } I am not sure howto optimize it, i am new in programming for 8052. I can now type combination via buttons on gate P1 soso :) But i need also type it via terminal (RS232). I am not sure, howto do it. I will probably need to use interrupt from serial? Can someone help me with this code please? Thank You very much |
Topic | Author | Date |
Control program via buttons and via terminal | 01/01/70 00:00 | |
Good info on this site | 01/01/70 00:00 | |
Divide and conquer | 01/01/70 00:00 | |
Control program via buttons and via terminal | 01/01/70 00:00 | |
howto test input from serial without stop all operations![]() | 01/01/70 00:00 | |
tutorial, faq | 01/01/70 00:00 |