//########################################################################################
//例程功能:延時1s流水燈
//例程作者
//時間:2016年11月18日
//########################################################################################
#include<reg52.h>
#include<intrins.h> //循環移動頭文件
#define uint unsigned int //將unsigned int定義為uint
#define uchar unsigned char //將unsigned char定義為uchar
uchar temp;
void delay(uint z); //函數的聲明
void main()
{
temp=0xfe; //使temp為:1111 1110
P1=temp; //點亮第一個燈
while(1) //一個總循環
{
temp=_crol_(temp,1); //調用函數,使temp循環左移一位
delay(1200); //延時
P1=temp; //使P1為 1111 1101
}
}
//#####################################延時1s子程序##########################################
void delay(uint z) //定義含參數子程序
{
uint x,y; //定義兩個無符號整型變量x,y,稱為局部變量,只在子函數中有效
for(x=100;x>0;x--)
for(y=z;y>0;y--); //此行for循環后加一分號且上一個for后沒分號,表示該for循環嵌套在上一個for循環中,
//z的值在主函數中賦
//第二個for循環后無內容,表示什么也不執行;執行完上述循環后由于分號?跳出循環
}
|