Blue Bridge Cup MCU preliminary

Posted by StefanRSA on Thu, 13 Jan 2022 17:35:52 +0100

Blue Bridge Cup MCU preliminary code basic framework

preface

The preliminary competition of Blue Bridge Cup single chip microcomputer gives me the feeling that on the basis of being familiar with the basic modules, combining the basic modules, and using interrupt and timer assistance at the same time, I can complete most functions. Therefore, basic modules such as key, nixie tube and power on initialization are very important and the key to further complete the problem. In some provinces with relatively weak competition, even write all the basic modules, and then realize a small function in the title, you can save three!!! Then go directly to the code!!!!

1, What is the code base framework?

It is a set of code to realize the basic module. This set of code will be used!!! You can even write it down before the exam!

2, Specific code

The code is as follows (example):

#include<STC15F2K60S2.H>

#define uint unsigned int
#define uchar unsigned char

uint ms;
uchar num_key;
uchar temp;//Save key value
uchar yi,er,san,si,wu,liu,qi,ba;
uchar code com[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};//Nixie tube position selection
uchar code tab[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF};//Segment code selection of nixie tube 0-9, and finally all out
void delay(uint ms);//Delay function, about 1ms delay
void allinit();//Power on initialization
void smg_display(uchar yi,uchar er,uchar san,uchar si,uchar wu,uchar liu,uchar qi,uchar ba);
uchar boredscan();
void keyscan()

void main()
{
	allinit();
	
	while(1)
	{
		yi = 1,er = 2,san = 3,si = 4,wu = 5,liu = 6,qi = 7,ba = 8;//It is mainly used to verify whether the nixie tube is normal
		
		smg_display(yi,er,san,si, wu,liu, qi, ba);
	}
}

void allinit()
{
	P2 = 0XA0;
	P0 = 0X00;//Close relay and buzzer
	
	P2 = 0X80;
	P0 = 0XFF;//Turn off the led
	
	P2 = 0XC0;
	P0 = 0XFF;//Select all nixie tubes
	P2 = 0XFF;
	P0 = 0XFF;//Turn off all nixie tubes
}

void delay(uint ms)
{
	uint a;
	int b;
	for(a = ms; a >0; a--)
		for(b = 845; b> 0;b--);
}

void smg_display(uchar yi,uchar er,uchar san,uchar si,uchar wu,uchar liu,uchar qi,uchar ba)
{
	uchar i;
	for(i = 0; i<8; i++)
	{
		P2 = 0xc0;
		P0 = com[i];
		P2 = 0xff;
		switch(i)
		{
			case 0:P0 = tab[yi];break;
			case 1:P0 = tab[er];break;
			case 2:P0 = tab[san];break;
			case 3:P0 = tab[si];break;
			case 4:P0 = tab[wu];break;
			case 5:P0 = tab[liu];break;
			case 6:P0 = tab[qi];break;
			case 7:P0 = tab[ba];break;
		}
		delay(1);
	}
	if(i == 8)
		i = 0;
}

//uchar boredscan()
//{
//		P3=0X7F;
//		P4=0XEF;
//		temp = P3;
//		temp = temp&0x0f;
//		if(temp != 0x0f)
//		{
//			delay(5);
//			temp = P3;
//			temp = temp&0x0f;
//			if(temp != 0x0f)
//			{
//				temp = P3;
//				switch(temp)
//				{
//					case 0x7e: num_key = 0; break;
//					case 0x7d: num_key = 1; break;
//					case 0x7b: num_key = 2; break;
//					case 0x77: num_key = 3; break;
//				}
//				while(temp!=0x0f)
//				{
//					temp=P3;
//					temp=temp&0x0f;
//				}
//			}
//		}
//	
//		P3 = 0XbF;
//		P4=0XFB;
//		temp = P3;
//		temp = temp&0x0f;
//		if(temp != 0x0f)
//		{
//			delay(5);
//			temp = P3;
//			temp = temp&0x0f;
//			if(temp != 0x0f)
//			{
//				temp = P3;
//				switch(temp)
//				{
//					case 0xbe: num_key = 4; break;
//					case 0xbd: num_key = 5; break;
//					case 0xbb: num_key = 6; break;
//					case 0xb7: num_key = 7; break;
//				}
//				while(temp!=0x0f)
//				{
//					temp=P3;
//					temp=temp&0x0f;
//				}
//			}
//		}
//		
//		P4=0XFF;
//		P3 = 0XdF;
//		temp = P3;
//		temp = temp&0x0f;
//		if(temp != 0x0f)
//		{
//			delay(5);
//			temp = P3;
//			temp = temp&0x0f;
//			if(temp != 0x0f)
//			{
//				temp = P3;
//				switch(temp)
//				{
//					case 0xde: num_key = 8; break;
//					case 0xdd: num_key = 9; break;
//					case 0xdb: num_key = 10; break;
//					case 0xd7: num_key = 11; break;
//				}
//				while(temp!=0x0f)
//				{
//					temp=P3;
//					temp=temp&0x0f;
//				}
//			}
//		}
//		P3 = 0Xef;
//		temp = P3;
//		temp = temp&0x0f;
//		if(temp != 0x0f)
//		{
//			delay(5);
//			temp = P3;
//			temp = temp&0x0f;
//			if(temp != 0x0f)
//			{
//				temp = P3;
//				switch(temp)
//				{
//					case 0xee: num_key = 12; break;
//					case 0xed: num_key = 13; break;
//					case 0xeb: num_key = 14; break;
//					case 0xe7: num_key = 15; break;
//				}
//				while(temp!= 0x0f)
//				{
//					temp=P3;
//					temp=temp&0x0f;
//				}
//			}
//		}
//		return num_key;
//}

void keyscan()
{
	if(P30 == 0)
		{
			delay(5);
			if(P30 == 0)
			{
				a = a - 1; 1111 1110
				P0 = a;
			}
			while(!P30); //Release detection
		} 
		else if(P31 == 0)
		{
			delay(5);
			if(P31 == 0)
			{
				P0 = 0XFD;
			}
			while(!P31);
		}
}

The code was written two years ago. If there is any problem, please criticize and correct it!!

3, Code description

1. Use the matrix keyboard or independent keys to select according to the specific requirements of the test questions. Although there are many codes in these two parts, the ideas are repeated, and only some details need to be changed.
2. For basic learning, software delay is used in the code to realize nixie tube dynamic scanning. If the function of the test question is complex, it is recommended to use timer to replace software delay.
3. * * remember this code after all peripheral functions are realized independently and skillfully** Otherwise, it is not conducive to beginners' learning.

summary

Share a little more of my experience. The preliminary test of the Blue Bridge Cup is not difficult. As long as you prepare well, it's really no problem to save two and one! My learning route is: first implement all the basic modules, get familiar with the basic framework, then brush the real problem once (using the most basic method, focusing on the realization of functions), and finally brush the real problem twice (this time not only to realize functions, but also to reduce bug s). My conclusion is that the flag bit is very important. If there are students who have written real questions, they should also have experience.

Topics: Single-Chip Microcomputer