您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 哈工大单片机实验代码(张毅刚版教材)(10个)
电路图用张毅刚版教材里给的图!!!!1、流水灯#includereg52.h#includeintrins.hvoiddelay(){unsignedchari,j;for(i=0;i255;i++)for(j=0;j255;j++);}voidmain(){P2=0xfe;while(1){P2=_crol_(P2,1);delay();}}2、单个外部中断#includereg52.h#includeintrins.hvoiddelay(unsignedchardelay_time){unsignedinti,j;for(i=0;i=delay_time;i++)for(j=0;j=100;j++);}voidinit_tx(){EA=1;EX0=1;IT0=0;}voidmain(){P1=0x00;init_tx();delay(200);while(1){P1=~P1;delay(200);}}voidtx0()interrupt0{P1=0xfe;while(!INT0){P1=_crol_(P1,1);delay(200);}P1=0xff;}3、定时器#includereg52.hunsignedchart=20;sbitbutton=P3^2;sbityellowled=P1^0;/*定义接口*/voidmain(){TMOD=0x01;/*选择中断方式*/EA=1;ET0=1;TH0=0x3c;TL0=0x0b;while(button==0){TR0=1;}yellowled=1;}voidtx()interrupt1{TH0=0x3c;TL0=0xb0;t--;if(t==0){yellowled=~yellowled;t=20;}}4、双机串行通信#includereg51.h#defineucharunsignedchar#defineuintunsignedint#defineoutkP1#defineout1P0#defineout2P2ucharcodeseg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};ucharkey,send,rec;ucharscan(void);voiddelay(unit);voidmain(void){TMOD=0X20;/*设置定时器T1为方式2*/TH1=0XF2;/*给定时器T1赋初值*/TH1=0XF2;TR1=1;/*接通定时器T1*/SCON=0X50;/*串行口方式1,REN=1允许接收*/PCON=0X00;/*SMOD=0*/EA=1;ES=1;EX1=1;EX0=1;while(1){outk=0x07;out1=seg[send];out2=seg[rec];}}ucharscan(void){uchark=10,m,n,in;delay(10);outk=0x07;if((outk&0x07)!=0x07){for(m=0;m3;m++){outk=~(0x01(m+3));for(n=0;n3;n++){in=outk;in=inn;if((in&0x01)==0){if((in&0x01)==0){k=n+m*3;break;}}}if(k!=10)break;}}return(k);}voiddelay(uintj){uchari;for(;j0;j--){i=250;while(--i);i=249;while(--i);}}voidext0()interrupt0{EX0=0;key=scan();if(key!=10)send=key+1;EX0=1;}voidext1()interrupt2{ES=0;SBUF=send;while(!TI);TI=0;ES=1;}voidcom()interrupt4{RI=0;rec=SBUF;}5、单片机与PC串行通信#includereg51.hvoidsend(unsignedchardat){SBUF=dat;while(TI==0);;TI=0;}voidinit_tx(){EA=1;EX0=1;}voiddelay(void){unsignedcharm,n;for(m=0;m250;m++)for(n=0;n250;n++);}voidmain(void){init_tx();TMOD=0x20;SCON=0x40;PCON=0x00;TH1=0xfd;TL1=0xfd;TR1=1;while(1);}voidtx0()interrupt0{unsignedchari=0;send(0x01);delay();i++;}6、扩展82c55并行IO接口#includereg51.h#includeabsacc.h#defineucharunsignedchar#defineuintunsignedintsbitrst_8255=P3^5;#definecon_8255XBYTE[0x7003]/*0x7003为82C55的控制寄存器地址*/#definepa_8255XBYTE[0x7000]/*以下定义pa、pb的端口地址*/#definepb_8255XBYTE[0x7001]voidreset_8255(void);voiddelay(uint);voidmain(void){uchartemp;rst_8255=1;/*复位*/delay(1);rst_8255=0;con_8255=0x82;while(1){temp=pb_8255;/*锁存*/pa_8255=temp;}}voiddelay(uintj)/*延时函数*/{uchari;for(;j0;j--){i=250;while(--i);i=249;while(--i);}}7、矩阵键盘#includereg51.hunsignedcharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};voiddelay(){unsignedchari,j;(延时)for(i=0;i=25;i++)for(j=0;j=25;j++);}unsignedcharkey_scan(){unsignedcharkey0,key1;unsignedcharkey_num=0;P1=0x0f;先将p1.0~P1.3置高key0=P1;if(key0!=0x0f)如果P1.4~P1.7不全为1,可能有键按下{delay();key0=P1;if(key0!=0x0f){P1=0xf0;key1=P1;if(key0==0x0e){switch(key1){case0xe0:key_num=0;break;case0xd0:key_num=1;break;case0xb0:key_num=2;break;case0x70:key_num=3;break;default:key_num=0;break;}}elseif(key0==0x0d){switch(key1){case0xe0:key_num=4;break;case0xd0:key_num=5;break;(处理5号键)case0xb0:key_num=6;break;case0x70:key_num=7;break;default:key_num=0;break;(无效按键,如多个键同时按下)}}elseif(key0==0x0b){switch(key1){case0xe0:key_num=8;break;case0xd0:key_num=9;break;case0xb0:key_num=10;break;case0x70:key_num=11;break;default:key_num=0;break;}}elseif(key0==0x07){switch(key1){case0xe0:key_num=12;break;case0xd0:key_num=13;break;case0xb0:key_num=14;break;case0x70:key_num=15;break;default:key_num=0;break;}}}}returnkey_num;}voidmain(){unsignedcharkey_num;do{key_num=key_scan();P2=table[key_num];}while(1);}8、1602液晶显示#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedint#defineoutP0sbitRS=P2^0;sbitRW=P2^1;sbitE=P2^2;voidlcd_initial(void);voidcheck_busy(void);voidwrite_command(ucharcom);voidwrite_data(uchardat);voidstring(ucharad,uchar*s);voidlcd_test(void);voiddelay(uint);voidmain(void){lcd_initial();while(1){string(0x82,HelloWelcome);string(0xC2,ToChina);delay(100);write_command(0x01);delay(100);}}voiddelay(uintj){uchari=250;for(;j0;j--){while(--i);i=249;while(--i);i=250;}}voidcheck_busy(void){uchardt;do{dt=0xff;E=0;RS=0;RW=1;E=1;dt=out;}while(dt&0x80);E=0;}voidwrite_command(ucharcom){check_busy();E=0;RS=0;RW=0;out=com;E=1;_nop_();E=0;delay(1);}voidwrite_data(uchardat){check_busy();E=0;RS=1;RW=0;out=dat;E=1;_nop_();E=0;delay(1);}voidlcd_initial(void){write_command(0x3c);write_command(0x0C);write_command(0x14);write_command(0x01);delay(1);}voidstring(ucharad,uchar*s){write_command(ad);while(*s0){write_data(*s++);delay(100);}}9、DA转换#includereg51.h#defineucharunsignedchar#defineuintunsignedint#defineoutP1sbitDAC_cs=P2^0;sbitDAC_wr=P2^1;voidmain(void){uchartemp,i=255;while(1){{out=temp;DAC_cs=0;/*片选端,芯片被选中*/DAC_wr=0;/*选通,并被锁存*/DAC_cs=1;/*以下开始转换*/DAC_wr=1;temp++;while(--i);/*延时函数!!,老师帮改的*/}}}10、AD转换#includereg51.h#defineucharunsignedchar#defineuintunsignedint#defineLEDP0#defineoutP1sbitstart=P2^1;sbitOE=P2^7;sbitEOC=P2^3;sbitCLOCK=P2^0;sbitadd_a=P2^4;sbita
本文标题:哈工大单片机实验代码(张毅刚版教材)(10个)
链接地址:https://www.777doc.com/doc-5568996 .html