请教:MCS-51单片机的P1口能否在输入输出状态下相互转换? 点击:1314 | 回复:1



高建华

    
  • 精华:0帖
  • 求助:0帖
  • 帖子:9帖 | 2回
  • 年度积分:0
  • 历史总积分:73
  • 注册:2004年8月10日
发表于:2005-07-22 15:29:00
楼主
求助:MCS-51单片机的P1口(8位)能否在输入输出状态下相互转换?       我在搞一个控制系统,由于I/O口限制,所有输入输出只能由P1口控制,特请教各位大侠:       1.MCS-51单片机的P1口(8位)能否在输入输出状态下相互转换(在同一 个控制程序中)?       2.也就是说在控制程序中(8位)一会儿充当输入口,一会儿又充当输出口,如何实现?       3.能否给我一个参考程序?不胜感激.



天啊啊

  • 精华:0帖
  • 求助:0帖
  • 帖子:20帖 | 179回
  • 年度积分:0
  • 历史总积分:378
  • 注册:2005年3月11日
发表于:2005-07-22 18:01:00
1楼
当然可以了,,,比如用51来控制1602液晶的时候就是直接控制的,,, //------------------------------------------------------------------------------------------------ // Topway Test Program // Copyright 2004.6.10 POTIAN Inc. //------------------------------------------------------------------------------------------------ #include <stdio.h> #include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int uchar key_temp; bit key1=1; //当该健按下一次,自动运行和单步运行交替操作 bit key2=0; //自动运行 bit key3=0; //调节对比度 bit key4=0; //处于单步运行时,每按一次按键,往下步运行一步 uchar temp_P1; uchar temp_P3; // static bit step_f; uchar error_num; //1:表示状态忙判断超时导致的错误; //2: //3: //4: //------------------------------------------------------------------------------------------------ #define DATABUS P1 sbit LCD_RS =P3^1; sbit LCD_RW =P3^7; sbit LCD_E =P3^2; bit LCD_CS1=0; //测试备用 bit LCD_CS2=0; //测试备用 bit LCD_CS3=0; //测试备用 uint *p_data; //------------------------------------------------------------------------------------------------ //define variable etc. //------------------------------------------------------------------------------------------------ uchar code topwayname[6]={0x54,0x4f,0x50,0x57,0x41,0x59};//TOPWAY uchar code charlcdtest[12]={0x43,0x68,0x61,0x72,0x4c,0x43,0x44,0x20,0x74,0x65,0x73,0x74}; uchar code DBDB[]={ 0x1F,0x00,0x1F,0x00,0x1F,0x00,0x1F,0x00, //"4 lines1" 0x00,0x1F,0x00,0x1F,0x00,0x1F,0x00,0x1F, //"4 lines2" 0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15, //"3 lines" 0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A,0x0A, //"2 lines" 0x15,0x0A,0x15,0x0A,0x15,0x0A,0x15,0x0A, //"dot1" 0x0A,0x15,0x0A,0x15,0x0A,0x15,0x0A,0x15, //"dot2" 0x1F,0x11,0x11,0x11,0x11,0x11,0x11,0x1F, //"square" 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}; //"black" //------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ //辅助函数 //------------------------------------------------------------------------------------------------ void delayxus(uchar aa) //delay TIMER1*1 ms for 12MHz { uchar data i; for(i=0;i<=aa;i++) { _nop_(); _nop_(); } } void delay1ms(void) //delay TIMER1*1 ms for 12MHz { uchar data i; for(i=0;i<=250;i++) { _nop_(); _nop_(); } } void delay100ms(void) { uchar data j; for(j=0;j<=50;j++) { delay1ms(); } } void delay500ms(void) { uchar data l; for(l=0;l<=1;l++) { delay100ms(); } } void delay1s(void) { uchar data l; for(l=0;l<=5;l++) { delay100ms(); } } //------------------------------------------------------------------------------------------------ //以下是驱动函数 //------------------------------------------------------------------------------------------------ //------------------------------------ void wcode(uchar cc) //send insruction { LCD_RW=0; LCD_RS=0; DATABUS=cc; LCD_E=0; _nop_(); _nop_(); LCD_E=1; delayxus(20); LCD_E=0; delayxus(20); } void wdata(uchar dd) //send data { LCD_RW=0; LCD_RS=1; DATABUS=dd; LCD_E=0; _nop_(); _nop_(); LCD_E=1; delayxus(20); LCD_E=0; delayxus(20); } //------------------------------------------------------------------------------------------------ //以下是应用函数 //------------------------------------------------------------------------------------------------ void wr_dis_h(uchar dis_data1,dis_data2) { uchar i; wcode(0x02); delayxus(50);//return home for(i=0;i<=0x4f;i++) { wdata(dis_data1); delayxus(50); } wcode(0xc0); delayxus(50);//2 line mode;display on for(i=0x40;i<=0x67;i++) { wdata(dis_data2); delayxus(50); } } void wr_dis(uchar dis_data1,dis_data2) { uchar i; wcode(0x02); delayxus(50);//return home for(i=0;i<=0x4f;i++) { wdata(dis_data1); delayxus(50); } wcode(0xc0); delayxus(50);//2 line mode;display on for(i=0x40;i<=0x67;i++) { wdata(dis_data2); delayxus(50); } } void wr_disram(uchar dis_data) { uchar i; wcode(0x02); delayxus(50);//return home wcode(0xc0); delayxus(50);//2 line mode;display on wcode(0x80); delayxus(50);//set cgram adress for(i=0;i<=0x4f;i++) { wdata(dis_data); delayxus(50); } } //------------------------------------------------------------------------------------------------ //以下是演示函数 //------------------------------------------------------------------------------------------------ void w_data_entw() { uchar j; uchar *P_data; P_data=topwayname; wcode(0x02); delayxus(50);//return home for(j=0;j<4;j++) { wdata(0x20); delayxus(50); } for(j=0;j<6;j++) { wdata(*P_data); delayxus(50);P_data++; } for(j=0;j<30;j++) { wdata(0x20); delayxus(50); } wcode(0xc0); delayxus(50);//2 line mode;display on P_data=charlcdtest; for(j=0;j<1;j++) { wdata(0x20); delayxus(50); } for(j=0;j<12;j++) { wdata(*P_data); delayxus(50); P_data++; } for(j=0;j<27;j++) { wdata(0x20); delayxus(50); } } void dis_test(uchar dis_number) //screen number:2,6,7,10,11,12,13 { switch(dis_number) { case 1: wr_disram(7); break; case 2: wr_dis_h(0x20,0x20); break; case 3: wr_disram(0); break;// case 4: wr_disram(1); break;// case 5: wr_disram(2); break;// case 6: wr_disram(3); break;// case 7: wr_disram(4); break;// case 8: wr_disram(5); break;// case 9: wr_disram(6); break;// case 10: wr_dis(0x41,0x42); break; case 11: wr_dis(0x43,0x44); break; case 12: wr_dis(0x45,0x46); break; case 13: wr_dis(0x47,0x48); break; default: error_num=2; break; } } void display_frame() { uchar i,j; wcode(0x02); delayxus(50);//return home for(i=0;i<40;i++) { if((i%10)==0) j=0x30; else j++; wdata(j); delayxus(50); } wcode(0xc0); delayxus(50);//2 line mode;display on j=0x41; for(i=40;i<80;i++) { if(j == 0x5b) j=0x41; wdata(j); delayxus(50); j++; } } void dis_jap_eur() { uchar i,j; dis_test(2); wcode(0x02); delayxus(50);//return home j=0xa1; for(i=0;i<40;i++) { wdata(j); delayxus(50); j++; } wcode(0xc0); delayxus(50);//2 line mode;display on j=0xa1+40; for(i=40;i<80;i++) { wdata(j); delayxus(50); j++; } } //------------------------------------------------------------------------------------------------ //以下是按键功能函数 //------------------------------------------------------------------------------------------------ void time0_scan() interrupt 1 using 2 { bit cs11=0,cs22=0; cs11=LCD_CS1; cs22=LCD_CS2; //测试备用 LCD_CS1=1; LCD_CS2=1; //测试备用 temp_P1=P1; temp_P3=P3; key_temp=0x00; P1=temp_P1|0x0f; P3=temp_P3&0xfe; _nop_(); _nop_(); _nop_(); _nop_(); key_temp=P1|0xf0; _nop_(); _nop_(); _nop_(); if(key_temp==0xfe) { key1 =0; key2=0; key3=0; key4=0; }//key1按下时为单步 else if(key_temp==0xfd) { key2=1; key3=0; key4=0; } else if(key_temp==0xfb) { key3=1; key2=0; key4=0; } else if(key_temp==0xf7) { key4=1; key2=0; key3=0; } TH0=0xe1; TL0=0xdf;//置定时器1初值0xffff-20000 P3=temp_P3|1; P1=temp_P1; _nop_(); _nop_(); _nop_(); LCD_CS1=cs11; LCD_CS2=cs22; _nop_(); _nop_(); _nop_(); _nop_(); //测试备用 } uchar light_temp=0x18; uchar key2_t=0; uchar key3_t=0; void keyfun() { EA = 1; if(key1) //如果按下,即key1=1,表示处于单步状态 { for(;;) { if(key4) { key4=0; break; } if(!key1) break; } } EA = 0; if(!key1) delay500ms(); } //--------------------------------------------------------------------------------- void LCD_init() { uchar i,*data_p; wcode(0x38); delayxus(50);//Function set; 2 line mode;display on wcode(0x0c); delayxus(50);//Display on / off control; d:2-display on;d:1-cursor on;d:0-blink on wcode(0x01); //clear delay1ms(); delay1ms(); wcode(0x06); delayxus(50);//Entry mode set; i/d sh //初始化CGRAM wcode(0x40); delayxus(50);//set cgram adress data_p=DBDB; for(i=0;i<64;i++) { wdata(*data_p); data_p++; delayxus(50); } } //------------------------------------------------------------------------------------------------ //以下是主函数 //------------------------------------------------------------------------------------------------ void main() { delay100ms(); DATABUS=0xff; SP=0x60; P3=0x98; LCD_E=0; LCD_RW=0; LCD_RS=0; EA = 0; /* disable interrupts */ LCD_init(); TR0 = 0; /* stop timer 0 */ IE = 0x82; TMOD = 0x01; /* TMOD */ TH0=0xe1; TL0=0xdf;//置定时器1初值0xffff-20000 _nop_(); TR0 = 1; /* start timer 0 */ while(1) { w_data_entw(); delay500ms(); keyfun(); //显示托微电子 display_frame(); delay500ms(); keyfun(); dis_jap_eur(); delay500ms(); keyfun(); dis_test(1); delay500ms(); keyfun(); dis_test(2); delay500ms(); keyfun(); dis_test(3); delay500ms(); keyfun(); dis_test(4); delay500ms(); keyfun(); dis_test(5); delay500ms(); keyfun(); dis_test(6); delay500ms(); keyfun(); dis_test(7); delay500ms(); keyfun(); dis_test(8); delay500ms(); keyfun(); dis_test(9); delay500ms(); keyfun(); dis_test(10); delay500ms(); keyfun(); dis_test(11); delay500ms(); keyfun(); dis_test(12); delay500ms(); keyfun(); dis_test(13); delay500ms(); keyfun(); } }

热门招聘
相关主题

官方公众号

智造工程师