Analysis on the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition

Keywords: C Single-Chip Microcomputer

I have just finished this topic. Let's take a look at the topic in 2021. What's wrong? I hope you can correct it.

subject

 

 

 

 

 

First, there are still three modules (nixie tube, LED and key), as well as DS18B20 and DA output. I have written some provincial competition questions. I test these things every time. As long as I have practiced every module, it should not be difficult. I found that from 2020, although four buttons are still used, they have become matrix buttons, which were independent buttons before.

1 nixie tube display

The nixie tube has three interfaces: temperature display, parameter setting and DA output, which can be switched through s4.

2 LED

When in mode 1, L1 is on, L2 is on when the nixie tube is in the temperature display interface, L3 is on when the parameter setting interface, and L4 is on when the DA output interface.

3 key module

The matrix keyboard is used. s4 is the switching of three interfaces. s8 and s9 are the addition and subtraction 1 of temperature parameters. There is a small pit here, which is that the set temperature parameters will only take effect when exiting the parameter setting interface. This requires defining two variables of temperature parameters. One is used for adding and subtracting temperature parameters, and assigned to another variable when exiting the temperature parameter interface, The other is used to compare the size with the real-time temperature. s5 has two modes. Mode 1 outputs 0v when the real-time temperature is less than the temperature parameter DA, otherwise 5v is output. Mode 2 outputs the voltage according to the relationship given in the figure.

4 DS18B20

Is to rewrite the underlying driver code part (onewire), then put it in the timer and read it every other period of time.

5 DAC

Is to rewrite the underlying driver code part (IIC).

onewire.c

#include"onewire.h"

sbit DQ = P1^4;  

void Delay_OneWire(unsigned int t)  
{
	t*=12;
	while(t--);
}

void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

unsigned int Get_temp()
{
	unsigned int result;
	unsigned char high,low;
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low=Read_DS18B20();
	high=Read_DS18B20();
	
	result=(high<<8)|low;
	result*=6.25;
	return result;
}

onewire.h

#ifndef _ONEWIRE_H_
#define _ONEWIRE_H_

#include<stc15f2k60s2.h>

void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
unsigned int Get_temp();

#endif

IIC.c

#include"IIC.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

sbit SDA = P2^1;  
sbit SCL = P2^0; 

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}

void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

void IIC_Stop(void)
{
    SDA = 0;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 1;
    IIC_Delay(DELAY_TIME);
}

bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

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;  
}

void DA_out(unsigned char date)
{
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x40);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();
}

IIC.h

#ifndef _IIC_H_
#define _IIC_H_

#include<stc15f2k60s2.h>
#include<intrins.h>

void IIC_Delay(unsigned char i);
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendByte(unsigned char byt);
void DA_out(unsigned char date);

#endif

init.c

#include"init.h"

#define u8 unsigned char 
#define u16 unsigned int
#define state_0 0
#define state_1 1
#define state_2 2
	
u8 code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff,0xc6,0x8c,0x88};
u8 seg[]={11,11,11,11,11,11,11,11};
static u8 segaddr=0;
u8 value=0;
extern u8 mode;

void all_init()    //Turn off irrelevant peripherals
{
	P2=(P2&0x1f)|0x80;P0=0xff;P2&=0x1f;
	
	P2=(P2&0x1f)|0xa0;P04=0;P06=0;P2&=0x1f;
	
	P2=(P2&0x1f)|0xc0;P0=0x00;P2&=0x1f;
	
	P2=(P2&0x1f)|0xe0;P0=0xff;P2&=0x1f;
}

void display()     //Nixie tube display function
{
	P2=(P2&0x1f)|0xe0;P0=0xff;P2&=0x1f;
	
	P2=(P2&0x1f)|0xc0;P0=1<<segaddr;P2&=0x1f;
	
	if(mode!=2&&segaddr==5)
	{
		P2=(P2&0x1f)|0xe0;P0=tab[seg[segaddr]]&0x7f;P2&=0x1f;
	}
	else
	{
		P2=(P2&0x1f)|0xe0;P0=tab[seg[segaddr]];P2&=0x1f;
	}
	if(++segaddr==8)segaddr=0;
}

u8 Read_key()    //Matrix keyboard
{
	static u8 key_press,key_num=0,key_state=0;
	key_press=P3&0x0f;
	switch(key_state)
	{
		case state_0:
			if(key_press!=0x0f)
				key_state=state_1;
		break;
		case state_1:
			if(key_press!=0x0f)
			{
				if((key_press & 0x08)==0) key_num=4;
				if((key_press & 0x04)==0) key_num=5;
				if((key_press & 0x02)==0) key_num=6;
				if((key_press & 0x01)==0) key_num=7;
				key_state=state_2;
			}
			else
				key_state=state_0;
		break;
		case state_2:
			if(key_press==0x0f)
				key_state=state_0;
		break;
	}
	value=key_num;
	key_num=0;
	return value;
}

void Timer0Init(void)		
{
	AUXR |= 0x80;		
	TMOD &= 0xF0;		
	TL0 = 0xCD;		
	TH0 = 0xD4;		
	TF0 = 0;		
	TR0 = 1;
  ET0 = 1;
  EA = 1;	
}

init.h

#ifndef _INIT_H_
#define _INIT_H_

#include<stc15f2k60s2.h>

void all_init();
void display();
unsigned char Read_key();
void Timer0Init(void);

#endif

jm.c

#include"jm.h"
#include"IIC.h"

#define u8 unsigned char 
#define u16 unsigned int
	
u16 TT=2500,TF=2500;
	
extern u8 mode,seg[];             // extern indicates that this variable is defined elsewhere and should be referenced here
extern u16 temp,RB2;
extern bit MS;

void jm4()
{
	if(mode==1)               //Temperature display interface
	{
		seg[0]=12;
		seg[1]=11;
		seg[2]=11;
		seg[3]=11;
		seg[4]=temp/1000;
		seg[5]=temp/100%10;
		seg[6]=temp/10%10;
		seg[7]=temp%10;
	}
	else if(mode==2)          //Parameter setting interface
	{
		seg[0]=13;
		seg[1]=11;
		seg[2]=11;
		seg[3]=11;
		seg[4]=11;
		seg[5]=11;
		seg[6]=TF/1000;
		seg[7]=TF/100%10;
	}
	else if(mode==3)        //DA output interface
	{
		seg[0]=14;
		seg[1]=11;
	  seg[2]=11;
		seg[3]=11;
		seg[4]=11;
		seg[5]=RB2/100;
		seg[6]=RB2/10%10;
		seg[7]=RB2%10;
	}
}

void jm5()            //Mode switching function
{
	if(MS==0)
	{
		if(temp<TT)
		{
			DA_out(0);
			RB2=0;
		}
		else
		{
			DA_out(255);
			RB2=500;
		}
	}
	else
	{
		if(temp<=2000)
		{
			DA_out(196);
			RB2=100;
		}
		else if(temp>2000&&temp<=4000)
		{
			DA_out((0.15*temp-200)*51.0);
			RB2=(u16)(0.15*temp-200+0.5);
		}
		else
		{
			DA_out(204);
			RB2=400;
		}
	}
}

void jm8()
{
	if(mode==2)TF-=100;
}

void jm9()
{
	if(mode==2)TF+=100;
}

void LED()           //LED display function
{
	u8 i=0xff;
	if(MS==0)
	{
		P2=(P2&0x1f)|0x80;P0=i&0xfe;P2&=0x1f;
	}
	else
	{
		P2=(P2&0x1f)|0x80;P0=i|0x01;P2&=0x1f;
	}
	if(mode==1)
	{
		P2=(P2&0x1f)|0x80;P0=i&0xfd;P2&=0x1f;
	}
	else if(mode==2)
	{
		P2=(P2&0x1f)|0x80;P0=i&0xfb;P2&=0x1f;
	}
	else if(mode==3)
	{
		P2=(P2&0x1f)|0x80;P0=i&0xf7;P2&=0x1f;
	}
}

jm.h

#ifndef _JM_H_
#define _JM_H_

#include<stc15f2k60s2.h>

void jm4();
void jm5();
void jm8();
void jm9();
void LED();

#endif

main.c

#include"init.h"
#include"jm.h"
#include"onewire.h"
#include"IIC.h"

#define u8 unsigned char 
#define u16 unsigned int
	
u8 num=0,temp_count=0;
u8 mode=1;
u16 temp,RB2=325;
bit MS=0,temp_flag=0;
extern TT,TF;
	
void main()
{
	all_init();
	Timer0Init();
	while(1)
	{
		if(temp_flag==1)      //Read the temperature value every 200ms
		{
			temp_flag=0;
			temp=Get_temp();
		}
		num=Read_key();
		switch(num)
		{
			case  4:
				if(++mode==4)mode=1;
			break;
			case  5:
				MS^=1;
			break;
		  case  6:
				jm8();
				TT=TF;
			break;
			case  7:
				jm9();
				TT=TF;
			break;
		}
		jm4();
		jm5();
		LED();
	}
}

void Timer0() interrupt 1
{
	display();
	if(++temp_count==200)
	{
		temp_count=0;
		temp_flag=1;
	}
}

Finally, once again, each module is relatively simple and the most important programming logic.

Posted by Rai_de on Thu, 02 Sep 2021 23:39:55 -0700