控制LED一秒闪烁
#include"reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit led1=P2^0;
sbit led3=P2^2;
sbit led5=P2^4;
sbit led7=P2^6;
void Timer0Init()
{
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
EA=1;
TR0=1;
ET0=1;
}
void main()
{
Timer0Init();
while(1);
}
void Timer0() interrupt 1
{
static u16 i;
TH0=0XFC;
TL0=0X18;
i++;
if(i==1000)
{
i=0;
led1=~led1;
led3=~led3;
led5=~led5;
led7=~led7;
}
}
控制LED和蜂鸣器
#include"reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
sbit led1=P2^0;
sbit led3=P2^2;
sbit led5=P2^4;
sbit led7=P2^6;
sbit beep=P1^5;
void delay(u16 i)
{
while(i--);
}
void Timer0Init()
{
TMOD|=0X01;
TH0=0XFC;
TL0=0X18;
EA=1;
TR0=1;//溢出中断请求标志位
ET0=1;//中断允许位
}
void main()
{
Timer0Init();
while(1);
}
void Timer0() interrupt 1
{
static u16 i;
TH0=0XFC;
TL0=0X18;
i++;
if(i==1000)
{
i=0;
led1=~led1;
led3=~led3;
led5=~led5;
led7=~led7;
beep=~beep;
delay(10);
}
}
|