我 想用单片机监控PC接收的数据,并通过RS232发命令,使PC控制3路电机的运动,和灯的通断情况! 程序如下:请各位指教(第一次玩单片机,帮忙改改,批评批评才能进步!) #include <reg52.h>
#define uchar unsigned char #define uint unsigned int unsigned char Axis[3]; #define fosc 11.0592 //晶振频率11.0592 unsigned char Time_delay;
void delay_nms(uint n) //N ms延时函数 { uint i,j; for(i=n;i>0;i--) for(j=0;j<124;j++) {;;} } void delay(unsigned char n)//延时处理 { Time_delay = n; while(Time_delay != 0) {} } void time0_init(void) interrupt 1 { /* 延时2ms定时 */ TH0 = -2000/256; TL0 = -2000%256; Read_key(); } extern unsigned char Axis[3]; /*20个按键扫描驱动程序,横向扫描第一行:1.4 第二行:p1.5第三行:p1.6 第四行:p1.7 第五行:p2.1 纵向扫描:第一列:p1.0 第二列:p1.1 第三列:p1.2 第四列:p1.3*/
Read_key(void)//按键处理,返回按键的值 { uchar i , j ; uchar flag = 0 ; //按键标志 uchar dat = 0x1EF; uchar Temp; Temp = P0&0x00F; // 读取P03->P00 到Temp if (Temp != 0x00F ) // 假如Temp==0x0f,则表示无键按下,则结束 { delay_nms(10); // 延时10ms 左右去抖动 Temp=P0&0x00F; if ( Temp != 0x00F ) // 确认有无键按下 { for ( i =0 ; i < 5 ; i++ ) // 输出口P2_0,P07->P04 轮流输出 { // 11110,11101,11011,10111,01111 Temp=dat|0x0F; P0=Temp; Res_L=Temp&0x1F0; delay(2); Res_R = P0 &0x0F; // 取P03->P00 到Res_R Result=Res_L+Res_R; for(j=0;j<20;j++) // 查表 { if ( Matrix[j] == Result) { flag=1; // 设置扫描结束标志位
break; } } if (flag ==1 ) // 如果标志位被设置退出此层循环 { delay(2); break; } dat=dat<<1; // dat 左移一位 } } } while ( ( P0&0x0F ) != 0x0F ) ; //等待按键释放 } void Key_Process (void ) { while(1) Read_key(); switch (Key_value) { case 0x1EE: { delay(2); //t i; //for (i = 0; i < 16; i++) Axis[0] = table1[0]; // Axis[] = Table1; } break; case 0x1ED: { delay(2); Axis[0] = Table2[0]; } break; case 0x1EB: { delay(2); Axis[0] = Table2[0]; } break; case 0x1E7: { sd_com(0x11); } break; case 0x1DE: Number = shuzi_1; break; case 0x1DD: Number = shuzi_2; break; case 0x1DB: Number = shuzi_3; break; case 0x1BE: Number = shuzi_4; break; case 0x1BD: Number = shuzi_5; break; case 0x1BB: Number = shuzi_6; break; case 0x17E: Number = shuzi_7; break; case 0x17D: Number = shuzi_8; break; case 0x17B: Number = shuzi_9; break; case 0x0FE: Number = shuzi_0; break; case 0x0FD: Number = zhen ; break; case 0x0FB: Number = fu ; break; case 0x1D7: { sd_com(0x00); }; break; case 0x1B7: { write_com(0x04); Number[]-- ; sd_com(0x01); sd_data(Number); }; break; case 0x177: { write_com(0x06); Number[]++; sd_com(0x02); sd_data(Number); }; break; case 0x0F7: { if(enterflag = flag); sd_com(0x03); else { return; } };
;break; case (0x1E7 & 0x1DE): { Lamp = on; sd_com(0x04); } break; case (0x1E7 & 0x1DD): { Lamp = 0ff; sd_com (0x05); delay(30); } break; case (0x1E7 & 0x0FD): { } break; case (0x1E7 & 0x0FB): { // Number[0] = space; // Number[1] = space; // Number[2] = space; // Number[3] = shuzi_0; // Number[4] = shuzi_0; sd_com = 0x06; }break; default: break; }
} /*------------------检查忙位-----------------------------*/ void lcd_busy() { RS=0; RW=1; E=1; Lcd_Bus=0xff; while((Lcd_Bus & 0x80) ==0x80) E=0; }
/*------------------写命令到LCD------------------------------*/ void write_com(unsigned char uccmdcode) { lcd_busy(); RS=0; RW=0; E=1; Lcd_Bus=uccmdcode; delay(5);////////////////////在数据写入的时候加入适当的延时 E=0; delay(5); }
/*-------------------写数据到LCD----------------------------*/ void write_data(unsigned char ucDispdata) { lcd_busy();
RS=1; RW=0; E=1; Lcd_Bus=ucDispdata; delay(5);////在数据写入的时候加入适当的延时 // E = 0; delay(5); }
/*------------------初始化LCD屏--------------------------*/ void lcdreset() { delay(1000); write_com(0x30); delay(10); //选择基本指令集 write_com(0x30); //选择8bit数据流 delay(5); write_com(0x0c); //开显示(无游标、不反白) delay(10); write_com(0x01); //清除显示,并且设定地址指针为00H delay(500); write_com(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位 delay(0); } /*------------------显示字符--------------------- void Putchar(unsigned char ucRow,unsigned char ucLine,unsigned char *ucC) { write_com(0x30); //选择8bit数据流 for(ucRow=0; ucRow<8; ucRow++) for(ucLine = 0;ucLine < 8; ucLine++) { write_data(*ucC); } }-*/ /*------------------显示字符串--------------------------*/ void Putstring(unsigned char ucRow,unsigned char ucLine ,unsigned char *s) { while(*s > 0) { //for(ucRow= 0; ucRow < 16; ucRow++) // for(ucLine = 0; ucLine < 8; ucLine++) { write_com(0x36); // write_com(ucLine+0x80); // write_com(ucRow+0x80); write_com(0x30); write_data(*s); s++; } delay(50); } } void ShowNumber(unsigned char ucRow,unsigned char ucLine,unsigned char Number) { unsigned char Number1[5]; unsigned char Count; unsigned char NumFront; // unsigned char Single[1]; if(Number <(-300.0 )&& Number > 300.0) return; if(Number>=0) Number = zhen_1; else Number = fu_1; Number1[1]=Number/100;
Number1[2]=(Number-100*(float)Number1[0])/10;
Number1[3]=(Number-100*(float)Number1[0]-10*(float)Number1[1])/1;
Number1[4]=((Number-100*(float)Number1[0]-10*(float)Number1[1]-1*(float)Number1[2])/10)*10;
for(NumFront = 0; NumFront < 5; NumFront++) if(Number1[NumFront] != 0) break; for(Count =1; Count < 5; Count++) { switch(Number1[Count]) { case 0: Putchar(ucRow,ucLine+Count*8,shuzi_0); break;
case 1: Putchar(ucRow,ucLine+Count*8,shuzi_1); break;
case 2: Putchar(ucRow,ucLine+Count*8,shuzi_2); break;
case 3: Putchar(ucRow,ucLine+Count*8,shuzi_3); break; case 4: Putchar(ucRow,ucLine+Count*8,shuzi_4); break; case 5: Putchar(ucRow,ucLine+Count*8,shuzi_5); break; case 6: Putchar(ucRow,ucLine+Count*8,shuzi_6); break; case 7: Putchar(ucRow,ucLine+Count*8,shuzi_7); break; case 8: Putchar(ucRow,ucLine+Count*8,shuzi_8); break; case 9: Putchar(ucRow,ucLine+Count*8,shuzi_9); break; default:break; } } } /*定义轴缓冲区*/ void Write_Axis(unsigned char ucRow,unsigned char ucLine, unsigned char *Axis) { // unsigned char Axis[2]; unsigned char count[2] = {1, 2}; switch(Axis[count[0]]) { case 0: Putstring(1,2,table1); break; case 1: Putstring(1,2,Table2); break; case 2: Putstring(1,2,Table3); break; default:break; } } /*------------------清屏命令--------------------------*/ void clrscreen(void) { write_com(0x01); delay_nms(10); }
/*------------------首屏显示--------------------------*/ void display1(unsigned char ucRow,unsigned char ucLine ,unsigned char code *img) { int k; for (k = 0;k<9;k+=8) for(ucRow = 0; ucRow < 8 ; ucRow++) for(ucLine = 0; ucLine < 32; ucLine++) { write_com(0x36);//功能设置---8BIT控制界面,扩充指令集 write_com(ucLine+0x80); //行地址 write_com(ucRow+0x80+k); //列地址 write_com(0x30); // write_data(hz1[2*ucLine++]);
// write_data(hz1[2*ucLine++]); } } void initial(void) {
TMOD = 0X20; TH1 = 250; TL1 = 250; TR1 = 1; PCON = 0x80; //SMOD =1; SCON = 0x50; //工作方式1,波特率9600kbps,允许接收 } /*******发送数据**********/ void send(unsigned char *temp, int j) { int i = 0; lamp = 0; EA = 0; for(i =0;i<=j;i++) { ACC = *(temp +i); TB8 = P; SBUF = ACC; //发送数据 while(TI ==0) //发送数据完毕时,TI自动置高 TI = 0; //发送完时,TI清0,准备下次发送 } EA = 1; lamp = 1; } /*********接受数据*************/ void Rcv_Int(void) interrupt 1 { lamp = 1; if(RI) { ACC = SBUF; if(P ==RB8) { rd_data = ACC; //将寄存器A中的数据存入变量RCVdata EvRcv = 1; //将已经接收到数据的标志只高? } RI = 0; } lamp = 1; } main() { lcdreset(); display1(0,0,0 hz1); while(1) {}; }
|
|