keil5(蜂鸣报警器)

前言

10秒内的报警器,若想增加,可更改代码


详细代码

main.c

#include 
#include"Twonum.h"
#include"key.h"
#include"Delay.h"

sbit Buzzer=P2^5;      //ÊÓƵΪP3^5

unsigned char keynum;
unsigned int i,s;
void main()
{
	Twonum(3,s);
	while(1)
	{
		keynum=key();
		if(keynum)
		{
			if(keynum==1)
			{
				s+=1;
				Twonum(3,s);
				if(s==10)
				{
					s=0;
					Twonum(3,s);
				}
			}
			if(keynum==2)
			{
				while(1)
				{
					Twonum(3,s);
					Delay(1000);
					Twonum(3,s);
					s--;
					if(s<=0)
					{
						s=0;
						Twonum(3,s);
						while(s==0)
						{
							for(i=0;i<=500;i--)
							{
							Buzzer=!Buzzer;
							Delay(1);
							}
						}
					}
				}
			}
		}
	}
}

 key.c

#include"Delay.h"
#include 
unsigned char key()
{
	unsigned char keynum = 0;
	if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);keynum=1;}
	if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);keynum=2;}
	if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);keynum=3;}
	if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);keynum=4;}
	return keynum;
}

Twonum.c

#include 
#include"Delay.h"
unsigned char a[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0x07,0X7F,0X6F}; 
void Twonum(unsigned char Loc,Num)
{
	switch(Loc)
	{
		case 1:P2_4=1;P2_3=1;P2_2=1;break;  
		case 2:P2_4=1;P2_3=1;P2_2=0;break;
		case 3:P2_4=1;P2_3=0;P2_2=1;break;
		case 4:P2_4=1;P2_3=0;P2_2=0;break;
		case 5:P2_4=0;P2_3=1;P2_2=1;break;
		case 6:P2_4=0;P2_3=1;P2_2=0;break;
		case 7:P2_4=0;P2_3=0;P2_2=1;break;
		case 8:P2_4=0;P2_3=0;P2_2=0;break;
	}
	P0=a[Num];
	Delay(1);
}

Delay.c 

void Delay(unsigned char xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

key.h 

#ifndef __KEY_H__
#define __KEY_H__
unsigned char key();
#endif

//ÖжϺ¯Êý
//unsigned int T0Count;
//void Timer0_Routine() interrupt 1
//{
//	TL0 = 0x18;		
//	TH0 = 0xFC;		
//	T0Count++;
//	if(T0Count>=500)
//	{
//		T0Count=0;
//		if(LEDmood==1)
//			P2=_crol_(P2,1);
//		if(LEDmood==2)
//			P2=_cror_(P2,1);
//		if(LEDmood==3)
//			P2=_cror_(P2,0);
//	}
//}

Delay.h 

#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned char xms);
#endif

Twonum.h 

#ifndef __TWONUM_H__
#define __TWONUM_H__
void Twonum(unsigned char Loc,Num);
#endif

总结

keil5+c语言实现10秒内的报警器,若想增加,可更改代码

 

你可能感兴趣的:(51单片机,c语言,单片机,开发语言)