用位操作和总线操作两种方法完成以下题目熟练建立KEIL工程点亮第一个发光管.#include<>sbitD1=P1^0;voidmain(){ D1=0;}#include<>voidmain(){ P1=0xfe;}},其中每个管亮500ms,灭500ms,亮时蜂鸣器响,灭时关闭蜂鸣器,一直重复下去。#include<>#include<>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P2^6;uchartemp;voiddelay();voidmain(){temp=0xfe;while(1){ P1=temp;temp=_crol_(temp,1);beep=0;delay();beep=1;}}voiddelay(){uintx,y;for(x=500;x>0;x--)for(y=110;y>0;y--);},第个管亮100ms,流动时让蜂鸣器发出“滴滴”声。#include<>#include<>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P2^6;uchartemp;voiddelay();voidmain(){while(1){ uinti;temp=0xfe; for(i=1;i<8;i++) { beep=0;P1=temp;delay(); beep=1;temp=_crol_(temp,1); } for(i=1;i<8;i++) { beep=0; P1=temp; delay();beep=1; temp=_cror_(temp,1); }}}voiddelay(){uintx,y;for(x=100;x>0;x--)for(y=110;y>0;y--);}4,用8个发光管演示出8位二进制数累加过程。#include<>//52单片机头文件#include<>//包含有左右循环移位子函数的库#defineuintunsignedint//宏定义#defineucharunsignedchar//宏定义voiddelay(uintz)//延时函数,z的取值为这个函数的延时ms数,如delay(200);大约延时200ms.{//delay(500);,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}voidmain()//主函数{uchara;while(1)//大循环{a++;P1=~a;delay(1000);}},再由下至上,再重复一次,然后全部熄灭再以300ms间隔全部闪烁5次。重复此过程。#include<>#include<>#defineuintunsignedint#defineucharunsignedcharvoiddelay(uint);uchartemp;voidmain(){while(1){uintj;for(j=0;j<2;j++){uinti; temp=0xfe; for(i=1;i<8;i++) {P1=temp; delay(200); temp=_crol_(temp,1); } for(i=1;i<8;i++) { P1=temp; delay(200); temp=_cror_(temp,1); }} for(j=0;j<5;j++) {P1=0xff; delay(300); P1=0x00; delay(300); }} }voiddelay(uintk){ uinti,j;for(i=110;i>0;i--)for(j=k;j>0;j--);}间隔300ms第一次一个管亮流动一次,第二次两个管亮流动,依次到8个管亮,然后重复整个过程。#include<>#include<>#defineuintunsignedint#defineucharunsignedcharuchartemp;voiddelay();voidmain(){uinti,j;while(1){temp=0xfe;for(j=0;j<8;j++){ for(i=0;i<8-j;i++) { P1=temp; delay(); temp=_crol_(temp,1); } temp=_crol_(temp,j); P1=0xff; temp=temp<<1;}}}voiddelay(){uinti,j;for(i=200;i>0;
郭天祥单片机作业 来自淘豆网m.daumloan.com转载请标明出处.