P0口流水燈的編程實(shí)現(xiàn)
程序代碼:
#includereg51.h>
#includestdio.h>
#includeintrins.h>
sbit P0_0 = P0^0;//I/O口的定義方法,兩者的區(qū)別,不這樣定義的話就會說沒有定義
sbit P0_1 = P0^1;
sbit P0_2 = P0^2;
sbit P0_3 = P0^3;
sbit P0_4 = P0^4;
sbit P0_5 = p0^5;
sbit P0_6 = P0^6;
sbit P0_7 = P0^7;
void delay(unsigned char time);//函數(shù)聲明,重要
void main()
{
while(1)
{
P0_0 = 0;
delay(200);
P0_1 = 0;
delay(200);
P0_3 = 0;
delay(200);
P0_4 = 0;
delay(200);
P0_5 = 0;
delay(200);
P0_6 = 0;
delay(200);
P0_7 = 0;
delay(200);
}
void delay(unsigned char time)
{
unsigned char i,j;
for(i=0;i=time;i++)
{
for(j=0;j=200;j++)
{
_nop_()
}
}
}
控制亮滅程序:
#includereg51.h>//端口定義已包含在此頭文件中,故P0可以直接賦值
#includestdio.h>
#includeintrins.h>
void delay(unsigned char time);
void delay(unsigned char time)
{
unsigned i,j;
for(i=0;i=time;i++)
{
for(j=0;j=200;j++)
{
_nop_();
}
}
}
void main()
{
while(1)
{
P0 = 0x00;
delay(200);
P0 = 0xff;
delay(200);
}
}
評論