P0口流水燈的編程實(shí)現(xiàn)
#include<reg51.h>
#include<stdio.h>
#inclde<intrins. h>
void delay(unsigned char time)//必須聲明函數(shù),否則會(huì)出現(xiàn)下面的錯(cuò)誤:
void main()
{
P0 = 0xfe;//P必須大寫
while(1)
{
delay(200);
P0 = _cror_(P0,1);
}
}
void delay()
{
unsigned char I,j;
for(i=0;i<=time;i++)
{
for(j=0;j<=200;j++)
{ _nop_();//空指令,根據(jù)單片機(jī)的機(jī)器周期計(jì)算
}
}
}
另一實(shí)現(xiàn)的方法:
sbit 位的應(yīng)用
數(shù)據(jù)類型筆記:
程序代碼:
#include<reg51.h>
#include<stdio.h>
#include<intrins.h>
sbit P0_0 = P0^0;//I/O口的定義方法,兩者的區(qū)別,不這樣定義的話就會(huì)說沒有定義
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_()
}
}
}
控制亮滅程序:
#include<reg51.h>//端口定義已包含在此頭文件中,故P0可以直接賦值
#include<stdio.h>
#include<intrins.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);
}
}