作者在 2011-03-10 12:39:26 发布以下内容
//-------------------------------------------
//头文件
//-------------------------------------------
#include<reg51.h>
#include<stdio.h>
typedef unsigned char uchar;
typedef unsigned char uint;
uchar counter=0;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
//-------------------------------------------
//延时子程序
//-------------------------------------------
void delay(uint index)
{
uint i,j;
for(i=0;i<index;i++)
{
for(j=0;j<124;j++);
}
}
//--------------------------------------------
//IO口初始化
//--------------------------------------------
void Init_IO(void)
{
P1=0xff;
key1=1;
key2=1;
key3=1;
key4=1;
}
//--------------------------------------------
//定时器T0初始化,T1为波特率发生器
//--------------------------------------------
void Init_timer(void)
{
TMOD=0x12;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=TL1=0xff;
TR0=1;
TR1=1;
IT0=1;
IT1=1;
}
//--------------------------------------------
//串口初始化
//--------------------------------------------
void Init_uart(void)
{
SCON=0x50;
PCON=0x80;
TI=1;
}
//--------------------------------------------
//开启中断
//--------------------------------------------
void Init_INT(void)
{
EA=1;
ET0=1;
ES=1;
}
//-----------------------------------------------
//判断哪一个按键被按下,实现对相应的LED灯的控制
//-----------------------------------------------
void Key_scan(void)
{
if(!(key1))
{
delay(100);//按键消抖
for(;;)
{
if(!(key1))
{
P1=0xfc;
delay(50000);//延时
P1=0xff;
delay(50000);//延时
}
else break;
}
}
if(!(key2))
{
delay(100);
for(;;)
{
if(!(key2))
{
P1=0xf3;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
if(!(key3))
{
delay(100);
for(;;)
{
if(!(key3))
{
P1=0xcf;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
if(!(key4))
{
delay(100);
for(;;)
{
if(!(key4))
{
P1=0x3f;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
}
void main (void)
{
//初始化
Init_IO();
Init_timer();
Init_uart();
Init_INT();
while(1);
}
void LED_CONTROL(void) interrupt 1 using 0 //定时器0中断
{
if(++counter==200)
{
Key_scan();
}
TH0=(65536-50000)/256;//T0预置数重新装入
TL0=(65536-50000)%256;
}
void SCI_INT(void) interrupt 4 using 1
{
//printf("HELLO WORLD!!\n");
}
//头文件
//-------------------------------------------
#include<reg51.h>
#include<stdio.h>
typedef unsigned char uchar;
typedef unsigned char uint;
uchar counter=0;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
//-------------------------------------------
//延时子程序
//-------------------------------------------
void delay(uint index)
{
uint i,j;
for(i=0;i<index;i++)
{
for(j=0;j<124;j++);
}
}
//--------------------------------------------
//IO口初始化
//--------------------------------------------
void Init_IO(void)
{
P1=0xff;
key1=1;
key2=1;
key3=1;
key4=1;
}
//--------------------------------------------
//定时器T0初始化,T1为波特率发生器
//--------------------------------------------
void Init_timer(void)
{
TMOD=0x12;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=TL1=0xff;
TR0=1;
TR1=1;
IT0=1;
IT1=1;
}
//--------------------------------------------
//串口初始化
//--------------------------------------------
void Init_uart(void)
{
SCON=0x50;
PCON=0x80;
TI=1;
}
//--------------------------------------------
//开启中断
//--------------------------------------------
void Init_INT(void)
{
EA=1;
ET0=1;
ES=1;
}
//-----------------------------------------------
//判断哪一个按键被按下,实现对相应的LED灯的控制
//-----------------------------------------------
void Key_scan(void)
{
if(!(key1))
{
delay(100);//按键消抖
for(;;)
{
if(!(key1))
{
P1=0xfc;
delay(50000);//延时
P1=0xff;
delay(50000);//延时
}
else break;
}
}
if(!(key2))
{
delay(100);
for(;;)
{
if(!(key2))
{
P1=0xf3;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
if(!(key3))
{
delay(100);
for(;;)
{
if(!(key3))
{
P1=0xcf;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
if(!(key4))
{
delay(100);
for(;;)
{
if(!(key4))
{
P1=0x3f;
delay(50000);
P1=0xff;
delay(50000);
}
else break;
}
}
}
void main (void)
{
//初始化
Init_IO();
Init_timer();
Init_uart();
Init_INT();
while(1);
}
void LED_CONTROL(void) interrupt 1 using 0 //定时器0中断
{
if(++counter==200)
{
Key_scan();
}
TH0=(65536-50000)/256;//T0预置数重新装入
TL0=(65536-50000)%256;
}
void SCI_INT(void) interrupt 4 using 1
{
//printf("HELLO WORLD!!\n");
}