蓝桥杯第七届国赛——电压频率设备

main.c

#include "STC15F2K60S2.h"
#include "ds1302.h"
#include "iic.h"

#define u8 unsigned char       
#define key_state_0 0
#define key_state_1 1
#define key_state_2 2
#define NO_KEY 0xff
//#define SetKeyBoard(x) P4=(x>>3)|(x>>4);P3=x
//#define GetKeyBoard() ((P4&0x10)<<3)|((P4&0x04)<<4)|(P3&0x3f)

sbit buzzer=P0^6;
sbit relay=P0^4;

u8 code smg_duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00};
u8 code smg_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};


u8 key_scan()
{
  static unsigned char key_state=key_state_0;
  u8 key_temp,key_reutrn=0;
  u8 key1,key2;
  P30=0;P31=0;P32=0;P33=0;P34=1;P35=1;P42=1;P44=1;
  if(P44==0)key1=0x70;
  if(P42==0)key1=0xb0;
  if(P35==0)key1=0xd0;
  if(P34==0)key1=0xe0;
  if((P34==1)&&(P35==1)&&(P42==1)&&(P44==1))key1=0xf0;

  P30=1;P31=1;P32=1;P33=1;P34=0;P35=0;P42=0;P44=0;
  if(P33==0)key2=0x07;
  if(P32==0)key2=0x0b;
  if(P31==0)key2=0x0d;
  if(P30==0)key2=0x0e;
  if((P30==1)&&(P31==1)&&(P32==1)&&(P33==1))key2=0x0f;
  key_temp=key1|key2;
 
  switch(key_state)
  {
    case key_state_0:
	if(key_temp!=NO_KEY)
	{
	  key_state=key_state_1;
	}
	break;

	case key_state_1:
	if(key_temp==NO_KEY)
	{
	  key_state=key_state_0;
	}
	else
	{
	    switch(key_temp)
		 {
		   case 0x77:key_reutrn=4;break;
		   case 0x7b:key_reutrn=5;break;
		   case 0x7d:key_reutrn=6;break;
		   case 0x7e:key_reutrn=7;break;
		
		   case 0xb7:key_reutrn=8;break;
	       case 0xbb:key_reutrn=9;break;
		   case 0xbd:key_reutrn=10;break;
		   case 0xbe:key_reutrn=11;break;
		
		   case 0xd7:key_reutrn=12;break;
		   case 0xdb:key_reutrn=13;break;
		   case 0xdd:key_reutrn=14;break;
		   case 0xde:key_reutrn=15;break;
		
		   case 0xe7:key_reutrn=16;break;
		   case 0xeb:key_reutrn=17;break;
		   case 0xed:key_reutrn=18;break;
		   case 0xee:key_reutrn=19;break;
		 }
		 key_state=key_state_2;
	}
	break;

	case key_state_2:
	if(key_temp==NO_KEY)
	{
	   key_state=key_state_0;
	}
	break;
  }
  return key_reutrn;
}



void Timer0Init(void)		//1毫秒@11.0592MHz      //用作计数器 P34是计数器0的脉冲输入引脚
{
    AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x04;
	TL0 = 0x00;		//设置定时初值
	TH0 = 0x00;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
}


void Timer1Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0xCD;		//设置定时初值
	TH1 = 0xD4;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1 = 1;
	EA = 1;
}




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

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



bit key_flag;
bit smg_blink_flag;

//1.time display
u8 time_display[8];
u8 shi,fen,miao;
u8 shezhi_shi,shezhi_fen,shezhi_miao;

u8 menu_index;
u8 s7_count=0,s4_count=0;
u8 shezhi_display[8];
//2.电压信号测量
unsigned int xdata volt=0,last_volt=0;
u8 volt_display[8];
u8 xdata volt_yuzhi_display[8];
unsigned int yuzhi_low=1000,yuzhi_high=2000;
u8 volt_sec_count=0;
bit volt_flag;

//3.频率测量
unsigned int frequence=0,zhouqi=0;
bit fre_flag;
u8 fre_display[8];
u8 zhouqi_display[8];
bit F_T_flag=0;      //0--fre    1--zhouqi

//4.事件记录
u8 volt_state=0;          //1 超过最大电压      2 小于最小电压
u8 ee_shi,ee_fen,ee_miao;       //用于存到eeprom中
//u8 cunchu_volt;                   //eeprom中提取出来的,不受到影响
//u8 cunchu_shi,cunchu_fen,cunchu_miao;  //eeprom中提取出来的,不受到影响
u8 leixing_display[8];      //事件类型
u8 shijian_time[8];          //事件时间
u8 s9_count=0;
bit occur_flag=0;        //表示时间发生的标志位

void main()
{
  u8 keyvalue;
  P2=0x80;P0=0xff;P2=0;
  P2=0xa0;P0=0;P2=0;
  Timer1Init();
  Timer0Init();
  set_sfm(23,59,50);                             //事件记录有问题

  ET1=0;
  yuzhi_high=read_24c02(1)*100;
  yuzhi_low=read_24c02(2)*100;
  volt_state=read_24c02(3);       //1 or 2
  ee_shi=read_24c02(4);
  ee_fen=read_24c02(5);
  ee_miao=read_24c02(6);
  ET1=1;
  
  leixing_display[6]=smg_duan[0];
  leixing_display[7]=smg_duan[volt_state];      //1 or 2

  shijian_time[0]=smg_duan[ee_shi/10];
  shijian_time[1]=smg_duan[ee_shi%10];
  shijian_time[2]=0x40;
  shijian_time[3]=smg_duan[ee_fen/10];
  shijian_time[4]=smg_duan[ee_fen%10];
  shijian_time[5]=0x40;
  shijian_time[6]=smg_duan[ee_miao/10];
  shijian_time[7]=smg_duan[ee_miao%10];
  while(1)
  {
    miao=Read_Ds1302_Byte(0x81);
	fen=Read_Ds1302_Byte(0x83);
	shi=Read_Ds1302_Byte(0x85);
//	ee_shi=shi/16*10+shi%16;
//	ee_fen=fen/16*10+fen%16;
//	ee_miao=miao/16*10+miao%16;
	if(menu_index==0) 
	{
	  shezhi_shi=shi/16*10+shi%16;
	  shezhi_fen=fen/16*10+fen%16;
	  shezhi_miao=miao/16*10+miao%16;	  
	  
	  time_display[0]=smg_duan[shi/16];
	  time_display[1]=smg_duan[shi%16];
	  time_display[2]=0x40;
	  time_display[3]=smg_duan[fen/16];
	  time_display[4]=smg_duan[fen%16];
	  time_display[5]=0x40;
	  time_display[6]=smg_duan[miao/16];
	  time_display[7]=smg_duan[miao%16];
	}


	
	//时间设置界面处理---------------------------------
	if(menu_index==1)           //时间设置界面
	{
		if(s4_count==0)
		{
		  shezhi_display[3]=smg_duan[shezhi_fen/10];
		  shezhi_display[4]=smg_duan[shezhi_fen%10];
		  shezhi_display[6]=smg_duan[shezhi_miao/10];
		  shezhi_display[7]=smg_duan[shezhi_miao%10];
		  if(smg_blink_flag)
		  {
		    shezhi_display[0]=smg_duan[shezhi_shi/10];
		    shezhi_display[1]=smg_duan[shezhi_shi%10];
		  }
		  else
		  {
		    shezhi_display[0]=0x00;
		    shezhi_display[1]=0x00;
		  }
		}
		if(s4_count==1)
		{
		  shezhi_display[0]=smg_duan[shezhi_shi/10];
		  shezhi_display[1]=smg_duan[shezhi_shi%10];
		  shezhi_display[6]=smg_duan[shezhi_miao/10];
		  shezhi_display[7]=smg_duan[shezhi_miao%10];
		  if(smg_blink_flag)
		  {
		    shezhi_display[3]=smg_duan[shezhi_fen/10];
			shezhi_display[4]=smg_duan[shezhi_fen%10];
		  }
		  else
		  {
		    shezhi_display[3]=0x00;
		    shezhi_display[4]=0x00;
		  }
		}
		if(s4_count==2)
		{
		  shezhi_display[0]=smg_duan[shezhi_shi/10];
		  shezhi_display[1]=smg_duan[shezhi_shi%10];
		  shezhi_display[3]=smg_duan[shezhi_fen/10];
		  shezhi_display[4]=smg_duan[shezhi_fen%10];
		  if(smg_blink_flag)
		  {
		    shezhi_display[6]=smg_duan[shezhi_miao/10];
		    shezhi_display[7]=smg_duan[shezhi_miao%10];
		  }
		  else
		  {
		    shezhi_display[6]=0x00;
		    shezhi_display[7]=0x00;
		  }
		}
		shezhi_display[2]=0x40;
		shezhi_display[5]=0x40;
	}
	//---------------------------------------------
	//电压部分
 	  if(volt_flag)
	  { 
	    volt_flag=0;
	    ET1=0;
	    volt=read_adc(0x03)/255.f*5000;       //mv
	    ET1=1;
	  }
//	  last_volt=volt;
	  volt_display[0]=0x40;
	  volt_display[1]=smg_duan[1];
	  volt_display[2]=0x40;
	  volt_display[3]=0x00;
	  volt_display[4]=smg_duan[volt/1000];
	  volt_display[5]=smg_duan[volt%1000/100];
	  volt_display[6]=smg_duan[volt%100/10];
	  volt_display[7]=smg_duan[volt%10];   
	
	//---------volt 阈值显示-------------------
	if(menu_index==3)
	{
		if(volt_sec_count==1)   //high blink
		{
		  volt_yuzhi_display[4]=smg_duan[yuzhi_low/1000];
		  volt_yuzhi_display[5]=smg_duan[yuzhi_low%1000/100];
		  volt_yuzhi_display[6]=smg_duan[yuzhi_low%100/10];
		  volt_yuzhi_display[7]=smg_duan[yuzhi_low%10];
		  if(smg_blink_flag)
		  {
		    volt_yuzhi_display[0]=smg_duan[yuzhi_high/1000];
			volt_yuzhi_display[1]=smg_duan[yuzhi_high%1000/100];
			volt_yuzhi_display[2]=smg_duan[yuzhi_high%100/10];
			volt_yuzhi_display[3]=smg_duan[yuzhi_high%10];
		  }
		  else
		  {
		    volt_yuzhi_display[0]=0x00;
			volt_yuzhi_display[1]=0x00;
			volt_yuzhi_display[2]=0x00;
			volt_yuzhi_display[3]=0x00;
		  }
		}
		else
		{
		  volt_yuzhi_display[0]=smg_duan[yuzhi_high/1000];
		  volt_yuzhi_display[1]=smg_duan[yuzhi_high%1000/100];
		  volt_yuzhi_display[2]=smg_duan[yuzhi_high%100/10];
		  volt_yuzhi_display[3]=smg_duan[yuzhi_high%10];
		  if(smg_blink_flag)
		  {
		    volt_yuzhi_display[4]=smg_duan[yuzhi_low/1000];
		    volt_yuzhi_display[5]=smg_duan[yuzhi_low%1000/100];
		    volt_yuzhi_display[6]=smg_duan[yuzhi_low%100/10];
		    volt_yuzhi_display[7]=smg_duan[yuzhi_low%10];
		  }
		  else
		  {
		    volt_yuzhi_display[4]=0x00;
			volt_yuzhi_display[5]=0x00;
			volt_yuzhi_display[6]=0x00;
			volt_yuzhi_display[7]=0x00;
		  }
		}
	}
	
	//--------frequence--------------------------
	if(fre_flag)
	{
	  fre_flag=0;
	  TR0=0;
	  frequence=(TH0<<8)|TL0;
	  frequence*=5;                      //1s 
	  zhouqi=1000000/frequence;
	  TH0=TL0=0;
	  TR0=1;                  //重新开启,开始下一轮测量
	  fre_display[0]=0x40;
	  fre_display[1]=smg_duan[2];   //界面编号2
	  fre_display[2]=0x40;
	  fre_display[3]=smg_duan[frequence/10000];
	  fre_display[4]=smg_duan[frequence%10000/1000];
	  fre_display[5]=smg_duan[frequence%1000/100];
	  fre_display[6]=smg_duan[frequence%100/10];
	  fre_display[7]=smg_duan[frequence%10];
	  
	  zhouqi_display[0]=0x40;
	  zhouqi_display[1]=smg_duan[2];
	  zhouqi_display[2]=0x40;
	  zhouqi_display[3]=smg_duan[zhouqi/10000];
	  zhouqi_display[4]=smg_duan[zhouqi%10000/1000];
	  zhouqi_display[5]=smg_duan[zhouqi%1000/100];
	  zhouqi_display[6]=smg_duan[zhouqi%100/10];
	  zhouqi_display[7]=smg_duan[zhouqi%10];
	}
	
	//-----------------------事件记录---------------------        volt_state--  1 超过最大电压      2 小于最小电压
	if(menu_index==2)   //volt display
	{
		if(volt>yuzhi_high && last_volt<yuzhi_high)          //超过最大设定电压  
		{
		  volt_state=1;//超过最大电压
		  ee_shi=shi/16*10+shi%16;
	      ee_fen=fen/16*10+fen%16;
		  ee_miao=miao/16*10+miao%16;
		  ET1=0;
		  write_24c02(3,volt_state);   
		  Delay10ms();   
		  write_24c02(4,ee_shi);        //ee_shi是十进制的  
		  Delay10ms();   
		  write_24c02(5,ee_fen);   
		  Delay10ms();   
		  write_24c02(6,ee_miao);   
		  Delay10ms();   
		  ET1=1;                       
		} 
		else if(volt<yuzhi_low && last_volt>yuzhi_low)       //掉到最小电压以下  (这个有问题)
		{
		  volt_state=2;//小于最小电压
		  ee_shi=shi/16*10+shi%16;
	      ee_fen=fen/16*10+fen%16;
		  ee_miao=miao/16*10+miao%16;
		  ET1=0;
		  write_24c02(3,volt_state);  
		  Delay10ms();
		  write_24c02(4,ee_shi);        //ee_shi是十进制的  
		  Delay10ms();   
		  write_24c02(5,ee_fen);   
		  Delay10ms();   
		  write_24c02(6,ee_miao); 
		  Delay10ms();  
		  ET1=1;
		}
		last_volt=volt;                 //感觉要放到此处
		leixing_display[6]=smg_duan[0];
		leixing_display[7]=smg_duan[volt_state];      //1 or 2
		
		shijian_time[0]=smg_duan[ee_shi/10];
		shijian_time[1]=smg_duan[ee_shi%10];
		shijian_time[2]=0x40;
		shijian_time[3]=smg_duan[ee_fen/10];
		shijian_time[4]=smg_duan[ee_fen%10];
		shijian_time[5]=0x40;
		shijian_time[6]=smg_duan[ee_miao/10];
		shijian_time[7]=smg_duan[ee_miao%10];
	}
	
	
//----------------------------------------------
	
	
    if(key_flag)
	{
	  key_flag=0;
	  keyvalue=key_scan();
	  switch(keyvalue)
	  {
	    case 4:                                //时钟设置界面选中调整        ,电压阈值调整
		       if(menu_index==1)        //时钟
			   {
		         s4_count++;
			     if(s4_count==3)s4_count=0;        //0,1,2      0--shi_blink
			   }
			   if(menu_index==2)      //volt测量 
			   {
			     menu_index=3;         //volt_yuzhi
			   }
			   if(menu_index==3)
			   {
			     volt_sec_count++;         //0,1        1--high blink
				 if(volt_sec_count==2)
				 	volt_sec_count=0;
			   }
			   if(menu_index==4)
			   {
			     F_T_flag=~F_T_flag;
			   }
		       break;
		case 5:menu_index=4;       //频率display
		       break;
		case 6:                         //电压		  
			   if(menu_index==3)
			   {
			     ET0=0;
			     write_24c02(1,yuzhi_high/100);          //写入eeprom
				 Delay10ms();
				 write_24c02(2,yuzhi_low/100);
				 Delay10ms();
				 ET0=1;
			   }
			    menu_index=2;              //电压测量显示
		       break;
		case 7:                        //时钟
		       s7_count++;                      
			   if(s7_count==2)     //0--1
			   {
			     s7_count=0;       //时间界面切换
			   }
			   if(s7_count==0)
			   {
			     menu_index=0;         //返回走时
				 set_sfm(shezhi_shi,shezhi_fen,shezhi_miao);
			   }
			   else if(s7_count==1)
			   {
		         menu_index=1;              //time shezhi_display
			   }
			   
		       break;
		case 8:
		       break;	
		case 9:    //查询                                       
			        //0,1 
			   menu_index=5;          //事件类型 
		       break;
		case 10:                           //减
		       if(menu_index==1)       //设置界面
			   {
			     if(s4_count==0)     //shezhi_shi
				 {
				   if(shezhi_shi>0)
				   {
				     shezhi_shi--;
				   }
				 }
				  if(s4_count==1)     //shezhi_shi
				 {
				   if(shezhi_fen>0)
				   {
				     shezhi_fen--;
				   }
				 }
				  if(s4_count==2)     //shezhi_shi
				 {
				   if(shezhi_miao>0)
				   {
				     shezhi_miao--;
				   }
				 }
			   }
			   //------------------------
			   if(menu_index==3)      //volt 阈值
			   {
			     if(volt_sec_count==1)    //high blink
				 {
				   if(yuzhi_high>0)
				   {
				     yuzhi_high-=500;
				   }
				 }
				 else if(volt_sec_count==0)
				 {
				   if(yuzhi_low>0)
				   {
				     yuzhi_low-=500;
				   }
				 }
			   }
		       break;
		case 11:                           //加
			    if(menu_index==1)       //设置界面
			   {
			     if(s4_count==0)     //shezhi_shi
				 {
				   if(shezhi_shi<23)
				   {
				     shezhi_shi++;
				   }
				 }
				  if(s4_count==1)     //shezhi_shi
				 {
				   if(shezhi_fen<59)
				   {
				     shezhi_fen++;
				   }
				 }
				 //---------------------------------
				  if(s4_count==2)     //shezhi_shi
				 {
				   if(shezhi_miao<59)
				   {
				     shezhi_miao++;
				   }
				 }
			   }
			   if(menu_index==3)      //volt 阈值
			   {
			     if(volt_sec_count==1)    //high blink
				 {
				   if(yuzhi_high<5000)
				   {
				     yuzhi_high+=500;
				   }
				 }
				 else if(volt_sec_count==0)
				 {
				   if(yuzhi_high<5000)
				   {
				     yuzhi_low+=500;
				   }
				 }
			   }
		       break;
	  }
	}
  }
}



void timer1() interrupt 3
{
  static unsigned int key_count=0,smg_count=0,i=0,smg_blink_count=0,fre_count=0,volt_count=0;
  key_count++;
  smg_count++;
  fre_count++;
  volt_count++;
  if(volt_count==200)
  {
    volt_count=0;
	volt_flag=1;
  }
  if(fre_count==200)
  {
    fre_count=0;
	fre_flag=1;
  }
  if(menu_index==1||menu_index==3||menu_index==5)       //time display --volt 阈值display
  {
    smg_blink_count++;
	if(smg_blink_count==1000)
	{
	  smg_blink_count=0;
	  smg_blink_flag=~smg_blink_flag;
	}
  }
  if(key_count==10)
  {
    key_count=0;
	key_flag=1;
  }
  if(smg_count==3)
  {
    smg_count=0;
	P2=0xc0;P0=0;P2=0;
	if(menu_index==0)
	{
	  P2=0xe0;P0=~time_display[i];P2=0;
	}
	if(menu_index==1)
	{
	  P2=0xe0;P0=~shezhi_display[i];P2=0;
	}
	if(menu_index==2)           //s6 input
	{
	  P2=0xe0;P0=~volt_display[i];P2=0;
	}
	if(menu_index==3)
	{
      P2=0xe0;P0=~volt_yuzhi_display[i];P2=0;
	}
	if(menu_index==4)
	{
	  if(F_T_flag==0)
	  {
	    P2=0xe0;P0=~fre_display[i];P2=0;
  	  }
	  if(F_T_flag==1)
	  {
	    P2=0xe0;P0=~zhouqi_display[i];P2=0;
	  }
	}
	if(menu_index==5)
	{
	  if(smg_blink_flag)
	  {
	    P2=0xe0;P0=~leixing_display[i];P2=0;
  	  }
	  else
	  {
	    P2=0xe0;P0=~shijian_time[i];P2=0;
	  }
	}
//	if(menu_index==6)
//	{
//	  P2=0xe0;P0=~shijian_time[i];P2=0;
//	}
	P2=0xc0;P0=smg_wei[i];P2=0;
	i++;
	if(i==8)i=0;
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值