keil单片机流水灯c语言代码(流水灯单片机程序keil5)
51单片机16个灯流水循环,C语言
代码已修改,并仿真通过。
#includereg51.h?????//包含单片机寄存器的头文件
#include?"intrins.h"
#define?uchar?unsigned?char
#define?uint?unsigned?int
?sbit?CLK=P1^0;
?sbit?IN?=P1^1;
//uchar?code?a[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uint?code?w[]={0xfffe,0xfffd,0xfffb,0xfff7,0xffef,0xffdf,0xffbf,0xff7f,0xfeff,0xfdff,0xfbff,0xf7ff,0xefff,0xdfff,0xbfff,0x7fff};//【新建了一个流水表】
void?delay(uint?i)
{
????uint?j;
????uchar?k;
????for(j=i;j0;j--)
????????for(k=125;k0;k--);
}
//【发送1字节给74LS164】
void?send_byte(uchar?temp)
{
????uchar?i;
????for(i=0;i8;i++)
????{
????????if(temp??0x80)
????????{
????????????IN?=1;
????????}
????????else
????????{
???????????IN?=0;
????????}
????????CLK?=?0;
?????????_nop_();??//if?Keil,require?use?intrins.h
????????CLK?=?1;
????????
????????temp=temp1;//?temp左移1位
????}
}
//【发送2字节给74LS164】
void?send_word(uint?temp)
{
????
????send_byte((uchar)(temp8));//先发送高字节
????send_byte((uchar)(temp0x00FF));//再发送低字节
}
void?main(void)
{
????uint?i;
????
????while(1)
????{
????????for(i=0;i16;i++)
????????{
????????????//send_byte(a[i]);
????????????send_word(w[i]);//【发送2字节】
????????????delay(4000);
????????????
???????????
????????}
????}
}
keil c51单片机 流水灯。
#includereg51.h
unsigned int n;
unsigned char m = 0, temp = 254;
sbit SPK = P1^5;
main()
{
TMOD = 0x01;
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
TR0 = 1;
ET0 = 1;
EA = 1;
P2 = temp;
while(1);
}
void T0_INT() interrupt 1
{
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
n++;
if(n = 1000) {
n = 0;
m++;
if (m == 8) {m = 0; temp = 254;}
else {temp = 1; temp += 1;}
}
if(n 500) {SPK = ~SPK; P2 = temp;}
else P2 = 255;
}
以上程序已经实验成功。
keil软件用单片机控制8个LED流水灯来回点亮(C语言程序)
#include "reg51.h"
main()
{
unsigned int i,j;
while(1)
{
P1=0xfe; //点亮第一个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xfd; //点亮第二个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xfb; //点亮第三个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xf7; //点亮第四个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xef; //点亮第五个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xdf; //点亮第六个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0xbf; //点亮第七个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
P1=0x7f; //点亮第八个LED
for(i=200;i0;i--)
for(j=500;j0;j--);
}
}
或:
#include "reg51.h"
void delay(unsigned int t)
{
unsigned inti,j;
for(i=t;i0;i--)
for(j=110;j0;j--);
}
main()
{
unsigned char w,i;
while(1)
{
w=0xfe;
for (i=0;i8;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;i0;i--)
for(j=110;j0;j--);
}
main()
{
unsigned char temp;
temp=0xfe;
while(1)
{
P1=temp;
delay(500); //延时
temp=_crol_(temp,1); //点亮LED的位置循环左移一位
}
}