当前位置:网站首页>Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition

Blue Bridge Cup provincial road 06 -- the second game of the 12th provincial competition

2022-04-23 16:46:00 The God of C language

One , Code section

1,iic.c

/*
   Program description : IIC Bus driver 
   Software environment : Keil uVision 4.10 
   Hardware environment : CT107 SCM comprehensive training platform  8051,12MHz
   Japan      period : 2011-8-9
*/

#include "reg52.h"
#include "intrins.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

// Bus pin definition 
sbit SDA = P2^1;  /*  cable  */
sbit SCL = P2^0;  /*  Clock line  */

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
// Bus start condition 
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

// Bus stop condition 
void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

// Send reply 
void IIC_SendAck(bit ackbit)
{
    SCL = 0;
    SDA = ackbit;  					// 0: The reply ,1: Non response 
    IIC_Delay(DELAY_TIME);
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SCL = 0; 
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

// Waiting for an answer 
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

// adopt I2C The bus sends data 
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

// from I2C Data is received on the bus 
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}

2,smgshow.c

#include "reg52.h"
void Delay(int t)
{
	while(t--);
}
void HC573(unsigned char channel, unsigned char dat)
{
	P0=dat;
	switch(channel)
	{
		case 4:
			P2=(P2 & 0x1f) | 0x80;// Strobe Y4C,LED
		  break;
		case 5:
			P2=(P2 & 0x1f) | 0xa0;// Strobe Y5C, Buzzer , Relay 
		  break;
		case 6:
			P2=(P2 & 0x1f) | 0xc0;// Strobe Y6C, Digital tube position selection 
		  break;
		case 7:
			P2=(P2 & 0x1f) | 0xe0;// Strobe Y7C, Nixie tube segment code 
		  break;
	}
	P2=(P2 & 0x1f) | 0x00;
}
void Display_SMG(unsigned char pos,unsigned char val)
{

	HC573(6,0x01<<pos);
	HC573(7,val);
	Delay(500);
//	HC573(6,0x01<<pos);
	HC573(7,0xff);// Blanking 
}	
void Display_ALL(unsigned char dat)
{
	HC573(6,0xff);
	HC573(7,dat);
}

3,main.c

#include "reg52.h"
#include "smgshow.h"
#include "iic.h"
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
unsigned char sta_smg=0;
unsigned char u_sta=0;
unsigned char switch_LED=0;
void Init_Timer()
{
	 TR1=1;
	 TR0=1;
	 TMOD=0x16;
	 TH0=0xff;
	 TL0=0xff;
	 TH1=(0-10000)/256;
	 TL1=(0-10000)%256;
	 EA=1;
	 ET0=1;
	 ET1=1;
} 
unsigned int t;
void Service_T0() interrupt 1
{
	t=t+1;
}
unsigned int count=0;
unsigned int count_key=0;
unsigned F_key=0;
unsigned int count_t=0;
unsigned int count_f=0;
unsigned int t_mem=65535;
float f;
void Service_T1() interrupt 3
{
	TH1=(0-10000)/256;
	TL1=(0-10000)%256;
	count+=1;
	if(count==100)
	{
		count=0;
		count_t=t;
		f=1.0/t;
		count_f=f*1000000;
		f=0;
		t=0;
	}
	if(F_key==1)
	{
		count_key+=1;
	}
}
float u_guanming=0; 
unsigned int u_guanming_smg;
void Read_guanming()
{
	unsigned char temp1;
	
	IIC_Start();
  IIC_SendByte(0x90);
	IIC_WaitAck();
  IIC_SendByte(0x01);
	IIC_WaitAck();

  IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp1=IIC_RecByte();//0~255
	IIC_SendAck(1); 
	IIC_Stop();  
	u_guanming=temp1*5/255.0;
	u_guanming_smg=u_guanming*100;
}
float u_dianweiqi=0; 
unsigned int u_dianweiqi_smg;
float u_mem=99;
void Read_dianweiqi()
{
	unsigned char temp2;
	
	IIC_Start();
  IIC_SendByte(0x90);
	IIC_WaitAck();
  IIC_SendByte(0x03);
	IIC_WaitAck();

  IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp2=IIC_RecByte();//0~255
	IIC_SendAck(1); 
	IIC_Stop();  
	u_dianweiqi=temp2*5/255.0;
	u_dianweiqi_smg=u_dianweiqi*100;
}
void SMGWork()
{
	switch(sta_smg)
	{
		case 0:
			Display_SMG(0,0x8e);
		  if(count_t>999999)
			{
				Display_SMG(1,SMGnotdotduanma[count_t/1000000%10]);
			}
			if(count_t>99999)
			{
				Display_SMG(2,SMGnotdotduanma[count_t/100000%10]);
			}
			if(count_t>9999)
			{
				Display_SMG(3,SMGnotdotduanma[count_t/10000%10]);
			}
			if(count_t>999)
			{
				Display_SMG(4,SMGnotdotduanma[count_t/1000%10]);
			}
			if(count_t>99)
			{
				Display_SMG(5,SMGnotdotduanma[count_t/100%10]);
			}
			if(count_t>9)
			{
				Display_SMG(6,SMGnotdotduanma[count_t/10%10]);
			}
		  	Display_SMG(7,SMGnotdotduanma[count_t%10]);  
		  break;
			case 1:
			  Display_SMG(0,0xc8);
		    if(count_f>999999)
				{
					Display_SMG(1,SMGnotdotduanma[count_f/1000000]);
				}
				if(count_f>99999)
				{
					Display_SMG(2,SMGnotdotduanma[count_f/100000%10]);
				}
				if(count_f>9999)
				{
					Display_SMG(3,SMGnotdotduanma[count_f/10000%10]);
				}
				if(count_f>999)
				{
					Display_SMG(4,SMGnotdotduanma[count_f/1000%10]);
				}
				if(count_f>99)
				{
					Display_SMG(5,SMGnotdotduanma[count_f/100%10]);
				}
				if(count_f>9)
				{
					Display_SMG(6,SMGnotdotduanma[count_f/10%10]);
				}
				Display_SMG(7,SMGnotdotduanma[count_f%10]);
			break;
			case 2:
				Display_SMG(0,0xc1);
			  Display_SMG(1,~0x40);
			if(u_sta==0)
			{
			  Display_SMG(2,SMGnotdotduanma[1]);
			  Display_SMG(5,SMGdotduanma[u_guanming_smg/100%10]);
			  Display_SMG(6,SMGnotdotduanma[u_guanming_smg/10%10]);
        Display_SMG(7,SMGnotdotduanma[u_guanming_smg%10]);
			}
			else
			{
				Display_SMG(2,SMGnotdotduanma[3]);
			  Display_SMG(5,SMGdotduanma[u_dianweiqi_smg/100%10]);
			  Display_SMG(6,SMGnotdotduanma[u_dianweiqi_smg/10%10]);
        Display_SMG(7,SMGnotdotduanma[u_dianweiqi_smg%10]);
			}
		  break;	
	}
}
void Scan_key()
{
	if(S4==0)
	{
		Delay(500);
		if(S4==0)
		{
			if(sta_smg==0)
			{
				sta_smg=1;
				
			}
			else if(sta_smg==1)
			{
				sta_smg=2;
				u_sta=0;
			}
			else 
			{
				sta_smg=0;
			}
			while(S4==0)
			{
				SMGWork();
			}
		}
	}
	if(S5==0)
	{
		Delay(500);
		if(S5==0)
		{
			if(sta_smg==2 && u_sta==0)
			{
			    u_sta=1;
			}
			else if(sta_smg==2 && u_sta==1)
			{
				  u_sta=0;
			}
			while(S5==0)
			{
				SMGWork();
			}
		}
	}
	if(S6==0)
	{
		Delay(500);
		if(S6==0)
		{
			 Read_dianweiqi();
			 u_mem=u_dianweiqi;
		}
		while(S6==0)
		{
			SMGWork();
		}
	}
	if(S7==0)
	{
		Delay(500);
		if(S7==0)
		{
			F_key=1;
			count_key=0;
	
			while(S7==0)
		{
			SMGWork();
		}
		  F_key=0;
		if(count_key>100)
		{
			switch_LED =! switch_LED;
		}
			else
			{
				t_mem=count_t;
			}
			
		}
	}
}
unsigned char sta_LED=0xff;
void LED()
{
	if(switch_LED==0)
	{	
		if(sta_smg != 2)
		Read_dianweiqi();
		
	if(u_dianweiqi>u_mem)
	{
		sta_LED &= ~0x01;
		HC573(4,sta_LED);
	}
	else
	{
		sta_LED |= 0x01;
		HC573(4,sta_LED);
	}
	
	if(count_t>t_mem)
	{
		sta_LED &= ~0x02;
		HC573(4,sta_LED);
	}
	else
	{
		sta_LED |= 0x02;
		HC573(4,sta_LED);
	}
	if(sta_smg==0)
	{
		sta_LED &= ~0x04;
		HC573(4,sta_LED);
	}
	else
	{
		sta_LED |= 0x04;
		HC573(4,sta_LED);
	}
	if(sta_smg==1)
	{
		sta_LED &= ~0x08;
		HC573(4,sta_LED);
	}
	else
	{
		sta_LED |= 0x08;
		HC573(4,sta_LED);
	}
	if(sta_smg==2)
	{
		sta_LED &= ~0x10;
		HC573(4,sta_LED);
	}
	else
	{
		sta_LED |= 0x10;
		HC573(4,sta_LED);
	}
}
	else
	{
		sta_LED=0xff;
		HC573(4,sta_LED);
	}
}
void Init_system()
{
	HC573(4,0xff);
	HC573(5,0x00);
	Display_ALL(0xff);
}
void main()
{
	Init_system();
	Init_Timer();
	while(1)
	{
	 if(u_sta==0)
		{
		Read_guanming();
		Delay(100);
		}
	 else
		{
		Read_dianweiqi();
			Delay(100);
		}
		SMGWork();
		Scan_key();
		LED();
	}
}

In the second field of the 12th session, except when the real-time voltage of channel 3 is greater than the buffer voltage L1 Light up , Only in voltage acquisition U-3 Interface implementation ( The title is not very clear , Personal understanding is effective in the whole interface ). The reason for this problem is , I read channel one and channel three 2 Two functions are placed in the loop at the same time to read , I find that the values read will be confused , The voltage result of channel 1 shows channel 3 , Reading channel 3 will display channel 1 , If there is a big guy passing by, you can help answer questions . I think I have tied the voltage reading with the display function , Read the voltage of the channel only when the channel is displayed , I have tried many new ways these days , Not ideal , Subsequent optimization .

In this topic, I also learned how to judge the long press and short press of keys , The idea is to press the switch , Logo location 1( Remember to make the count clear 0, For the next count ), To set up while() loop , Judge switch pressed , It is convenient for the timer to start timing , Every time 10ms Counting digits +1, Judge the time of counting digits to determine the content of execution .

Two , summary

        Friends who read the previous code , You should find that the referenced header file is less , Because recently, when I write code and find that there are too many modularities, I have to define a lot of variables to receive , Modify these values , Too complicated , Moreover, its own programming level is too low to achieve the corresponding modularization and strong portability .

        I finished this problem these days , The most important thing is what you can learn from a topic , Not just one day at a time , Learning new knowledge is the most important thing , Mutual encouragement .

版权声明
本文为[The God of C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231640379804.html