//设CPU是时钟12MHz的8051系列
data unsigned char ms
sbit K1=P0^0
void SetupTimer0()
{
TMOD &= 0XF0 //仅保留T1信息
TMOD |= 0X02 //设置T0: 定时功能,方式2,自动重载8位定时器/计数器
TH0 = 256-250//定时每0.25ms中断一次
TL0 = 256-250
仔饥 TR0 = 1 //开启定时器
ET0 = 1 //定时器0中断打开
}
void Timer0_ISR() interrupt 1 //中断服务程序
{
static data unsigned char us250
if (++us250>念搜返=4) {++ms us250=0}
}
void main()
{
data unsigned char p1,p2
ms=0
SetupTimer0()
EA = 1 漏凯 //全局中断打开
K1 = 1
while(1)
{
if (K1) {P1=p1=0XFF P2=p2=0x00 ms=0 continue}
if (ms<150) continue
ms=0
p1<<=1 P1=p1
p2<<=1 p2|=1 P2=p2
}
}
//P1外接8LED
低电平发光.
//
列程序
PROTEUS
仿真亮察
功
#include<烂戚reg52.h>
//-----------------------------
void
delay_ms(unsigned
int
s)
{
unsigned
int
x
for(s
s
>
0
s--)
{
x
=
98
while(x--)
}
}
//-----------------------------
void
main()
{
char
i,
j
while(1)
{
for(j
=
0
j
<
8
j++)
{
P1
=
255
//全灭,
P1外接LED,
低电平发光.
delay_ms(500)
//稍等片刻饥键陵.
for(i
=
0
i
<
8
i++)
{
P1
<<=
1
delay_ms(900
-
100
*
j)
//延
渐短
}
}
}
}
//-----------------------------
#include <iom16.h>#include <avr_macros.h>
void Init_IO(void)
{
DDRA = 0xFF
PORTA = 0x00
}
/*以上代码51忽略*/
void LED1 (unsigned char x,unsigned char LEDx)
{
unsigned char Count = 0
Count = LEDx * 36 //最小0,最大 7 * 36 = 252,char型最大不能超耐察洞过255;
Count = Count + 1 // 加1的目的是针对LED0不亮
if(x <= Count) // <= 昌枯是针对LED0不亮
PORTA |= (1 << LEDx) //51怎么改?。 P0 |= (1 << LEDx) PWM正占空
else
if(x > Count)
PORTA &= ~(1 << LEDx) //51怎么改?。 P0 &= ~(1 << LEDx) PWM负占空
}
void main( void )
{
unsigned char delay = 0
unsigned char LEDx = 0
Init_IO()
while (1)
{
delay ++
if(delay == 250)
{
delay = 0
LEDx ++
if(LEDx > 7)
没空 LEDx = 0
}
LED1(delay,LEDx)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)