P0口流水灯的编程实现
扫描二维码
随时随地手机看文章
#include<reg51.h>
#include<stdio.h>
#inclde<intrins. h>
void delay(unsigned char time)//必须声明函数,否则会出现下面的错误:
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_();//空指令,根据单片机的机器周期计算
}
}
}
另一实现的方法:
sbit 位的应用
数据类型笔记:
程序代码:
#include<reg51.h>
#include<stdio.h>
#include<intrins.h>
sbit P0_0 = P0^0;//I/O口的定义方法,两者的区别,不这样定义的话就会说没有定义
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);//函数声明,重要
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);
}
}