当前位置:网站首页>The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup

The intelligent material transmission system of the 6th National Games of the Blue Bridge Cup

2022-07-06 02:07:00 Miseñor

This one is much simpler than previous ones , Finally, I can finish all the tasks smoothly . Among them, we should pay attention to two times eeprom In the process of data saving, we need a small delay, Or you'll make a mistake !

subject :

 Insert picture description here
 Insert picture description here
 Insert picture description here

Code :

main.c

#include <STC12C5A60S2.H>
#include"intrins.h"
#include"iic.h"
sbit TX=P1^0;
sbit RX=P1^1;
int led_work=0xff;
int rb_2;
int L3_mode=0;
int distance;
int Y5_work=0x00;
char last_time_1=2;// Delivery time of class I goods 
char last_time_2=4;// Delivery time of class II goods 
int weight_mode=0;
int smg_mode=1;
int work_mode; // Type of goods 
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
int running=0;// Whether to run 
int last_time;
int L4_work=0;
int set_mode=0;
unsigned int code xianshi[]={
    0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
void choose_573(int i)
{
    
	switch(i)
	{
    
		case(0):P2=(P2&0x1f)|0x00;break;
		case(4):P2=(P2&0x1f)|0x80;break;
		case(5):P2=(P2&0x1f)|0xa0;break;
		case(6):P2=(P2&0x1f)|0xc0;break;
		case(7):P2=(P2&0x1f)|0xe0;break;
	}
}
void init_system()
{
    
	choose_573(4);
	P0=0xff;
	choose_573(5);
	P0=0x00;
	choose_573(0);
}
//============================== Timer 0
void Timer0Init(void)		//5 millisecond @11.0592MHz
{
    
	AUXR |= 0x80;		// Timer clock 1T Pattern 
	TMOD &= 0xF0;		// Set timer mode 
	TL0 = 0x00;		// Set the initial timing value 
	TH0 = 0x28;		// Set the initial timing value 
	TF0 = 0;		// eliminate TF0 sign 
	TR0 = 1;		// Timer 0 Start timing 
	ET0=1;
	EA=1;
}
int count_1=0;int count_2=0;
void service_timer0() interrupt 1
{
    
	count_1++;
	if(count_1>=100)
	{
    
		count_1=0;
		L3_mode++;
		if(L3_mode>=2)
		{
    
			L3_mode=0;
		}
	}
	if(running!=0&&last_time!=0)
	{
    
		count_2++;
		if(count_2>=200)
		{
    
			count_2=0;
			last_time--;
			if(last_time==0)
			{
    
				running=0;
				smg_mode=1;
				Y5_work=(Y5_work&0xef)|0x00;
				choose_573(5);
				P0=Y5_work;
				choose_573(0);
			}
		}
	}
}
//==============================
//==============================led

void rb_2_display()
{
    
	rb_2=get_rb2();
}
void led_display()
{
    
	//L1
	if(rb_2<100)
	{
    
		weight_mode=1;
		led_work=(led_work&0xfe)|0x00;
		choose_573(4);
		P0=led_work;
		choose_573(0);
	}
	else
	{
    
		led_work=(led_work&0xfe)|0x01;
		choose_573(4);
		P0=led_work;
		choose_573(0);
	}
	
	//L2
	if(rb_2>=100&&rb_2<400)
	{
    
		weight_mode=2;
		led_work=(led_work&0xfd)|0x00;
		choose_573(4);
		P0=led_work;
		choose_573(0);
	}
	else
	{
    
		led_work=(led_work&0xfd)|0x02;
		choose_573(4);
		P0=led_work;
		choose_573(0);
	}
	//L3
	if(rb_2>=400&&L3_mode==1)
	{
    
		weight_mode=3;
		led_work=(led_work&0xfb)|0x00;
		Y5_work=(Y5_work&0xbf)|0x40;
		choose_573(4);
		P0=led_work;
		choose_573(5);
		P0=Y5_work;
		choose_573(0);
	}
	else if(rb_2>=400&&L3_mode==0)
	{
    
		weight_mode=3;
		led_work=(led_work&0xfb)|0x04;
		Y5_work=(Y5_work&0xbf)|0x40;
		choose_573(4);
		P0=led_work;
		choose_573(5);
		P0=Y5_work;
		choose_573(0);
	}
	else if(rb_2<400)
	{
    
		led_work=(led_work&0xfb)|0x04;
		Y5_work=(Y5_work&0xbf)|0x00;
		choose_573(4);
		P0=led_work;
		choose_573(5);
		P0=Y5_work;
		choose_573(0);
	}
	
}
//==============================
//============================== ultrasonic 
void Delay12us()		//@12.000MHz
{
    
	unsigned char i;

	_nop_();
	_nop_();
	i = 33;
	while (--i);
}
void send_wave()
{
    
	int i;
	for(i=0;i<8;i++)
	{
    
		TX=1;
		Delay12us();
		TX=0;
		Delay12us();
	}
}
void get_distance()
{
    
	unsigned int time=0;
	AUXR |= 0x40;		// Timer clock 1T Pattern 
	TMOD &= 0x0F;		// Set timer mode 
	TL1 = 0x00;		// Set the initial timing value 
	TH1 = 0x00;		// Set the initial timing value 
	TF1 = 0;		// eliminate TF1 sign 
	TR1 = 0;		// Timer 1 Start timing 
	send_wave();
	TR1=1;
	while(RX==1&&TF1==0);
	TR1=0;
	if(TF1==0)
	{
    
		time=TH1;
		time=(time<<8)|TL1;
		distance=time*0.017;
		distance=distance/12;
	}
	else
	{
    
		distance=999;
		TF1=0;
	}
	TH1=TL1=0;
	if(distance<=30)
	{
    
		work_mode=1;
	}
	else if(distance>30)
	{
    
		work_mode=2;
	}
}
//==============================
//============================== Key 
void Delay10ms()		//@11.0592MHz
{
    
	unsigned char i, j;

	i = 108;
	j = 145;
	do
	{
    
		while (--j);
	} while (--i);
}
void key_board()
{
    
	//S4
	if(S4==0&&weight_mode==2)
	{
    
		Delay10ms();
		if(S4==0&&weight_mode==2)
		{
    
			smg_mode=2;
			Y5_work=(Y5_work&0xef)|0x10;
			choose_573(5);
			P0=Y5_work;
			choose_573(0);
			running=1;
			if(work_mode==1)
			{
    
				last_time=last_time_1;
			}
			if(work_mode==2)
			{
    
				last_time=last_time_2;
			}
		}
		while(!S4);
	}
	//S5
	if(S5==0&&smg_mode==2)
	{
    
		Delay10ms();
		if(S5==0&&smg_mode==2)
		{
    
			if(running==1)
			{
    
				running=0;
				Y5_work=(Y5_work&0xef)|0x00;
				choose_573(5);
				P0=Y5_work;
				choose_573(0);
				L4_work=1;
			}
			else
			{
    
				running=1;
				Y5_work=(Y5_work&0xef)|0x10;
				choose_573(5);
				P0=Y5_work;
				choose_573(0);
				L4_work=0;
			}
		}
		while(!S5);
	}
	
	//S6
	if(S6==0&&weight_mode==1)
	{
    
		Delay10ms();
		if(S6==0&&weight_mode==1)
		{
    	
			smg_mode=3;
			set_mode++;
			if(set_mode>=3)
			{
    
				set_mode=0;
				smg_mode=1;
			}
		}
		while(!S6);
	}
	//S7
	if(S7==0&&smg_mode==3)
	{
    
		Delay10ms();
		if(S7==0&&smg_mode==3)
		{
    
			if(set_mode==1)
			{
    
				last_time_1++;
				if(last_time_1>10)
				{
    
					last_time_1=1;
				}
			}
			if(set_mode==2)
			{
    
				last_time_2++;
				if(last_time_2>10)
				{
    
					last_time_2=1;
				}
			}
		}
		while(!S7);
	}
}
//==============================
//==============================L4
void L4_display()
{
    
	if(L4_work==1)
	{
    
		if(L3_mode==1)
		{
    
			led_work=(led_work&0xf7)|0x00;
			choose_573(4);
			P0=led_work;
			choose_573(0);
		}
		else
		{
    
			led_work=(led_work&0xf7)|0x08;
			choose_573(4);
			P0=led_work;
			choose_573(0);
		}
	}
	else if(L4_work==0)
	{
    
		led_work=(led_work&0xf7)|0x08;
		choose_573(4);
		P0=led_work;
		choose_573(0);
	}
}
//==============================
//==============================smg
void Delay400us()		//@11.0592MHz
{
    
	unsigned char i, j;

	i = 5;
	j = 74;
	do
	{
    
		while (--j);
	} while (--i);
}
void SMG(int wei,int dat)
{
    
	choose_573(6);
	P0=0x80>>(wei-1);
	choose_573(7);
	P0=xianshi[dat];
	choose_573(0);
	Delay400us();
	choose_573(7);
	P0=xianshi[10];
	choose_573(0);
}
void smg_display()
{
    
	// test 
// SMG(1,distance%10);
// SMG(2,(distance%100)/10);
// SMG(3,(distance%1000)/100);
// SMG(4,weight_mode);
// SMG(5,10);
// SMG(6,smg_mode);
// SMG(7,10);
// SMG(8,10);
	if(smg_mode==1)
	{
    
		SMG(1,work_mode);
		SMG(2,10);
		SMG(3,10);
		SMG(4,distance%10);
		SMG(5,(distance%100)/10);
		SMG(6,10);
		SMG(7,10);
		SMG(8,smg_mode);
	}
	if(smg_mode==2)
	{
    
		SMG(1,last_time%10);
		SMG(2,(last_time%100)/10);
		SMG(3,10);
		SMG(4,10);
		SMG(5,10);
		SMG(6,10);
		SMG(7,10);
		SMG(8,smg_mode);
	}
	if(smg_mode==3)
	{
    
		if(set_mode==2&&L3_mode==1)
		{
    
			SMG(1,last_time_2%10);
			SMG(2,(last_time_2%100)/10);
		}
		else if(set_mode==2&&L3_mode==0)
		{
    
			SMG(1,10);
			SMG(2,10);
		}
		else if(set_mode!=2)
		{
    
			SMG(1,last_time_2%10);
			SMG(2,(last_time_2%100)/10);
		}
		SMG(3,10);
		if(set_mode==1&&L3_mode==1)
		{
    
			SMG(4,last_time_1%10);
			SMG(5,(last_time_1%100)/10);
		}
		else if(set_mode==1&&L3_mode==0)
		{
    
			SMG(4,10);
			SMG(5,10);
		}
		else if(set_mode!=1)
		{
    
			SMG(4,last_time_1%10);
			SMG(5,(last_time_1%100)/10);
		}
		SMG(6,10);
		SMG(7,10);
		SMG(8,smg_mode);
	}
}
//==============================
//==============================eeprom
void eeprom_work()
{
    
	if(set_mode==0)
	{
    
		write_EEPROM(0x01,last_time_1);
		Delay10ms();
		write_EEPROM(0x33,last_time_2);
	}
}
//==============================
void main()
{
    
	init_system();
	Timer0Init();
	last_time_1=read_EEPROM(0x01);
	Delay10ms();
	last_time_2=read_EEPROM(0x33);
	while(1)
	{
    
		rb_2_display();
		led_display();
		smg_display();
		get_distance();
		key_board();
		L4_display();
		eeprom_work();
	}
}

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;    
}
int temp_1;
int get_rb2()
{
    
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x03);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	temp_1=IIC_RecByte();
	IIC_SendAck(1);                     
	IIC_Stop();
	temp_1=temp_1*1.96+0.5;
	return temp_1;
}
void write_EEPROM(int add,int dat)
{
    
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}
int read_EEPROM(int add)
{
    
	int temp;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	temp=IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	return temp;
}

iic.h

#ifndef _IIC_H
#define _IIC_H

void IIC_Start(void); 
void IIC_Stop(void);  
bit IIC_WaitAck(void);  
void IIC_SendAck(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
unsigned char IIC_RecByte(void); 
int get_rb2(); 
read_EEPROM(int add);
write_EEPROM(int add,int dat);
#endif
原网站

版权声明
本文为[Miseñor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140041083777.html