❶ keil軟體用單片機控制8個LED流水燈來回點亮(C語言程序)
#include "reg51.h"
main()
{
unsigned int i,j;
while(1)
{
P1=0xfe; //點亮第一個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xfd; //點亮第二個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xfb; //點亮第三個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xf7; //點亮第四個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xef; //點亮第五個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xdf; //點亮第六個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xbf; //點亮第七個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0x7f; //點亮第八個LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
}
}
或:
#include "reg51.h"
void delay(unsigned int t)
{
unsigned inti,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
main()
{
unsigned char w,i;
while(1)
{
w=0xfe;
for (i=0;i<8;i++)
{
P1=w; //循環點亮LED
w<<=1; //點亮燈的位置移動,最低位補0
w=w|0x01; //將最低位置1
delay(500); //延時
}
}
}
或:
#include "reg51.h"
//程序中使用_crol_函數,所以要包含頭文件"intrins.h"
#include "intrins.h"
void delay(unsigned int t)
{
unsigned int i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
main()
{
unsigned char temp;
temp=0xfe;
while(1)
{
P1=temp;
delay(500); //延時
temp=_crol_(temp,1); //點亮LED的位置循環左移一位
}
}
❷ 51單片機C語言如何實現8個流水燈左移三次,後右移三次;如此循環
51單片機C語言實現循環8個流水燈左移三次,後右移三次。
常式:
#include<reg51.h>//51單片機頭文件
#include<intrins.h>//包含有左右循環移位子函數的庫
#defineuintunsignedint//宏定義
#defineucharunsignedchar//宏定義
sbitbeep=P2^3;
voiddelay(uintz)//延時函數,z的取值為這個函數的延時ms數,如delay(200);大約延時200ms.
{//delay(500);大約延時500ms.
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidmain()//主函數
{
uchara,i,j;
while(1)//大循環
{
a=0xfe;//賦初值
for(j=0;j<3;j++)for(i=0;i<8;i++)//左移三次
{
P1=a;//點亮小燈
beep=0;//開啟蜂鳴器
delay(50);//延時50毫秒
beep=1;//關閉蜂鳴器
delay(50);//再延時50毫秒
a=_crol_(a,1);//將a變數循環左移一位
}
a=0x7f;
for(j=0;j<3;j++)for(i=0;i<8;i++)//右移三次
{
P1=a;//點亮小燈
beep=0;//開啟蜂鳴器
delay(50);//延時50毫秒
beep=1;//關閉蜂鳴器
delay(50);//再延時50毫秒
a=_cror_(a,1);//將a變數循環右移一位
}
}
}
51單片機是對所有兼容Intel 8031指令系統的單片機的統稱。該系列單片機的始祖是Intel的8031單片機,後來隨著Flash rom技術的發展,8031單片機取得了長足的進展,成為應用最廣泛的8位單片機之一,其代表型號是ATMEL公司的AT89系列,它廣泛應用於工業測控系統之中。很多公司都有51系列的兼容機型推出,今後很長的一段時間內將佔有大量市場。51單片機是基礎入門的一個單片機,還是應用最廣泛的一種。