基于stc15f2k60s2芯片单片机编程(按键的长短按)

(一)程序现象

(DS1302+超声波)上电显示超声波,按下S7切换为时钟,松开切换为超声波测距

(二)程序思路

关键按键如何去设置,以下是按键程序。

if(s4==0)
	{
	Delay10ms();
		if(s4==0)
		{			
			
			while(1)
			{
			DS_get();
				saomiao[0]=abile[TIME[2]/16];
				saomiao[1]=abile[TIME[2]%16];
				saomiao[2]=0xbf;
				saomiao[3]=abile[TIME[1]/16];
				saomiao[4]=abile[TIME[1]%16];
				saomiao[5]=0xbf;
				saomiao[6]=abile[TIME[0]/16];
				saomiao[7]=abile[TIME[0]%16];
				if(s4!=0)break;
			}
		
		}
		while(!s4);
	}

当按键按下的时候(没松开),按键会一直在while里不出来。当按键松开的时候就可以跳出去。短按直接在while外面进行设置就好了。

(三)程序设计

main.c函数

#include 
#include 
#include "ds1302.h"


void shumaguan();
void Timer0Init();
void Delay500ms();

unsigned char abile[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};
unsigned char saomiao[]={0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF};
unsigned int distance;

void Send_Wave();
sbit TX=P1^0;
sbit RX=P1^1;
sbit s4=P3^0;
unsigned int t=0;
void Delay12us();
void mdistance();
void xianshi();
void Delay10ms();



void main()
{	

	Timer0Init();		 P2=0XA0;P0=0X00;  DS_init();

		while(1)
		{

		mdistance();xianshi();

		}
}




void xianshi()
{
		
 if(distance ==99)
		 {
			saomiao[0]=0x90;
			 saomiao[1]=0x90;
			 saomiao[2]=0x90;
			 Delay500ms();
			saomiao[3]=saomiao[4]=saomiao[5]=saomiao[6]=saomiao[7]=0xff;
		 }
		 else
		 {
		saomiao[0]=abile[distance%1000/1000];
		saomiao[1]=abile[distance%100/10];
		saomiao[2]=abile[distance%10];
    Delay500ms();
	  saomiao[3]=saomiao[4]=saomiao[5]=saomiao[6]=saomiao[7]=0xff;
		 }
	if(s4==0)
	{
	Delay10ms();
		if(s4==0)
		{			
			
			while(1)
			{
			DS_get();
				saomiao[0]=abile[TIME[2]/16];
				saomiao[1]=abile[TIME[2]%16];
				saomiao[2]=0xbf;
				saomiao[3]=abile[TIME[1]/16];
				saomiao[4]=abile[TIME[1]%16];
				saomiao[5]=0xbf;
				saomiao[6]=abile[TIME[0]/16];
				saomiao[7]=abile[TIME[0]%16];
				if(s4!=0)break;
			}
		
		}
		while(!s4);
	}
}


void Delay10ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
		while (--j);
	} while (--i);
}

void Delay500ms()		//@11.0592MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 22;
	j = 3;
	k = 227;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}



void weixuan() interrupt 1
{
	unsigned char i;
	 P2=P2&0x1f|0xe0;
	P0=0xff;
	P2=0x1f;
  P2=P2&0x1f|0xc0;
	P0=1<<i;
	P2=0x1f;
  P2=P2&0x1f|0xe0;
	P0=saomiao[i];
	P2=0x1f;
	i++;
	if(i==8) i=0;
}
void Timer0Init()		//[email protected]
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0x9A;		//??????
	TH0 = 0xA9;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????	
	EA=1;
	ET0=1;
}


void Send_Wave()
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
	TX=1;
	 Delay12us();
	 TX=0;
	Delay12us();
	}
}
void mdistance()
{	
	unsigned int time=0;
	TMOD&=0X0F;//定时器1模式
	TL1=0;
	TH1=0;//初始化
	Send_Wave();  //发送声波信号
	TR1=1;//启动定时器
	while((RX==1)&&(TF1==0)) ;//等待超声波信号返回或者等到测量超出范围
	TR1=0; //停止定时器
	
	if(TF1==0)
	{
		t=TH1;
		t=(t<<8)|TL1;  //将计算值装到16为计数器
		distance=t*0.017;//测量距离
	}
	else
	{   
	 	TF1=0;//禁止中断溢出
	 	distance=99;
	}
//	if(distance>=30)
//	{P2=0XA0;P0=0Xff;P2=0X1F;}
//	else 
//	{P2=0XA0;P0=0X00;P2=0X1F;}
		}
void Delay12us()            
{
	unsigned char i;
 
	_nop_();
	_nop_();
	i = 33;
	while (--i);
}

ds1302.c函数

#include

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302??												


unsigned char code READ_RTCADDR[7]={0X81,0X83,0X85,0X87,0X89,0X8B,0X8D};
unsigned char code WRITE_RTCADDR[7]={0x80,0X82,0X84,0X86,0X88,0X8C,0X8E};
 char TIME[7]={0x50,0x59,0x23};
 
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}
void DS_init()
{
	unsigned char n;
	Write_Ds1302_Byte(0x8e,0x00);//关闭写保护
  	for(n=0;n<3;n++)
	Write_Ds1302_Byte(WRITE_RTCADDR[n],TIME[n]);
  Write_Ds1302_Byte(0x8e,0x80);
}

void DS_get()
{
	unsigned char n;
	for(n=0;n<3;n++)
  TIME[n]=Read_Ds1302_Byte (READ_RTCADDR[n]);
}



ds1302.h函数

#ifndef __DS1302_H_
#define __DS1302_H_

#include
#include



void Write_Ds1302(unsigned  char temp) ;
void Write_Ds1302_Byte( unsigned char address,unsigned char dat ) ;
unsigned char Read_Ds1302_Byte ( unsigned char address );
void DS_init();
void DS_get();
extern char TIME[7];


//---??????--//


#endif

你可能感兴趣的:(基于stc15f2k60s2芯片单片机编程(按键的长短按))