当前位置: 代码迷 >> 单片机 >> 定时器水流灯程序
  详细解决方案

定时器水流灯程序

热度:28   发布时间:2016-04-28 15:51:44.0
定时器流水灯程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

void delay(uchar n)
{
  delay:TH0=(65536-50000)/256;
TL0=(65536-50000)%256;  
while(!TF0)
TF0=0;
n--;  
if(n)
goto delay;
TR0=0;

void main()
{
  uchar b,c,i,temp;
  TMOD=0X01;
  EA=1;
TR0=1;
ET0=1;
TH0=(65536-50000)/256;
  TL0=(65536-50000)%256;

  loop:
  temp=0xfe;
for(i=0;i<8;i++)
{
b=temp>>(8-i);
c=temp<<i;
P0=b|c;
delay(20);
}
temp=0x7f;
for(i=0;i<8;i++)
{
b=temp<<(8-i);
c=temp>>i;
P0=b|c;
delay(20);
}
goto loop;


}
我的这个程序怎么起不到流水灯的作用呢, 有人说我这个缺少定时器函数。可是我看书上就这么写的啊。难道错了,希望大家帮帮忙,给我看看,或者给个相关的程序。要定时器控制流水灯左移右移,不单边移动。

------解决方案--------------------
程序太长了,看着头疼

------解决方案--------------------
你的程序有点乱 给你个参考
#include <reg51.h> 

#define uchar unsigned char 
#define uint unsigned int 


unsigned char buf[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

uchar temp; 
uchar tt;
void main() 

TMOD=0X01; 
TH0=(65536-50000)/256; 
TL0=(65536-50000)%256; 
EA=1; 
ET0=1; 
TR0=1; 
 
 while(1) 

P0=buf[tt];





void timer0() interrupt 1 

TH0=(65536-50000)/256; 
TL0=(65536-50000)%256; 

tt++; 
if(tt==8) tt=0; 



------解决方案--------------------
#include<reg51.h>
 sbit Switch=P2^0;
 void main(void)
{
bit direction;
long int i;
unsigned char a=0xfe,b,c;
P1=0xff;
Switch=1;
if(!Switch)
for(i=0;i<100;i++)
if(Switch) break;

 while(1)
{
P1=a;
if(!direction)
{
if(a==0xbf)
direction=1;
b=a<<1;
c=a>>7;
a=b|c;

}
else
{if(a==0xfd)
direction=0;
b=a>>1;
c=a<<7;
a=b|c;
}
for (i=0;i<=300000;i++);
}
}


这个试试。
  相关解决方案