当前位置:首页 > 单片机 > 单片机
[导读]由于P0口内部没有上拉电阻,所以P0在用作输入、输出时要外接上拉电阻,一般取10k左右。程序如下:#include //头文件#define uchar unsigned char#define uint unsigned int //宏定义uchar code table[]={0xfe,0xfd,0

由于P0口内部没有上拉电阻,所以P0在用作输入、输出时要外接上拉电阻,一般取10k左右。



程序如下:

#include //头文件
#define uchar unsigned char
#define uint unsigned int //宏定义
uchar code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //数据表
uchar i,j,a;
void delay(uchar x) //延时子函数
{

for(i=x;i>0;i--)
for(j=110;j>0;j--);
}

void main() //主程序
{
while(1)
{
for(a=0;a<8;a++) //有八个灯,做八次循环
{
P0=table[a]; //P0口依次取表中的数据,将对应的灯点亮
delay(100); //延时大约100ms
}
}
}

程序的另一种实现方法:

#include
#include //调用库函数,这个里面有具有循环移动功能的函数
#define uchar unsigned char

void delay(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}

void main()
{
uchar temp;
temp=0xfe;
P0=temp;
while(1)
{
temp= _crol_(temp,1); //temp后面的1,表示左移的位数

delay(100);
P0=temp;
delay(100);
}
}

仿真结果如下图所示:



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭