Blue Bridge Cup MCU learning notes updated on January 6, 2022

Posted by ThEoNeTrUeAcE on Thu, 06 Jan 2022 06:32:13 +0100

About the program I wrote, there's nothing to upload

I've been taking notes with txt before, but later I thought of writing it on csdn because there was too much code
The following is the text, in no order, where you want to write.

Some basic

-In the 138 decoder, the first three bits represent 5, 6 and 7

P2=0xA0 corresponds to 1010 0000, then 5 6 7 is 101, 1111 1011, and Y5 is 0, then Y5C is configured to control P00-P07

P2=0x80 corresponds to 10 million, then 5 6 7 is 100, 1111 0111, Y4 is 0, corresponding to Y4C, which is led lamp

P2=0xC0 corresponds to 11 million, then 5, 6, 7 is 110, 1111, 1101, Y6 is 0, corresponding to Y6C. This is to turn on the chip selection end of the nixie tube (the control is the first display). Why is P0 written later indicates which one to select? For example, 0x80 is 10 million. The first display can also be written as P2 = (P2 & 0x1f) |0x80. The one in front of 0x80 indicates P2 0-P2. 4 set to 0

P2=0xFF corresponds to 1111 1111, then 5, 6, 7 is 111, 1111 1110, Y7 is 0, corresponding to Y7C. This is to open the data end of the nixie tube (control what number is displayed)

P2=0xE0 corresponds to 1110 1111, then 5, 6, 7 bits 111 is 1111 1110, Y7 is 0, corresponding to Y7C. This is to open the data end of the nixie tube (control what number is displayed)

sfr is a register that defines special functions. There are two kinds of registers in the single chip microcomputer. The first is ROM and the second is RAM.
1 function of ROM: the data of ROM cannot be changed when the program is running. Unless you burn the program again, it will change. Just like our books, it can't be changed when printed. Unless printed again, this is the principle of ROM.
2 RAM function: RAM is that the data will change at any time during program operation. Just like our blackboard, it can be erased after writing. It is equivalent to calling the data in ROM for various operations when the program is running.

Data sheet of common anode nixie tube:

unsigned char code num[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6};

Indicates the displayed number, from 0 to 9, and the last three are -, all off and C respectively

Nixie tube chip selection position table (from left to right 0-7):

unsigned char code weizhi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

The nixie tube calls the function. The front is the position and the back is the display

void shumaguan_control(uchar a,uchar b)
{
	delay_ms(1);
	P2=(P2&0X1f)|0xC0;P0=weizhi[a];//
	P2=(P2&0X1f)|0xFF;P0=shuzi[b];
	delay_ms(1);
}

timer

Timer 0, setting 1s:

void Init_timer0()      
{
	TMOD = 0x01;      
	TH0 = (65535 - 50000) / 256;  //50000 microseconds
	TL0 = (65535 - 50000) % 256;	
	EA  = 1;     
	ET0 = 1;     
	TR0 = 1;     
}

The following is an example of writing the execution contents of the clock

void Service_timer0() interrupt 1
{
	TH0 = (65535 - 50000) / 256;    
	TL0 = (65535 - 50000) % 256;	
count++;
	if(count == 20)   //20*50ms is 1s
	{
		second++;
		count = 0;
	}	
	if(second == 60)
	{
		minute++;
		second=0;
	}
	if(minute==60)
	{
		hour++;
		minute=0;
		if(hour==24) hour=0;
	}
	
}

Matrix key

Definition pin

sbit R1=P3^0;//Initialization of the first row (if the jumper cap is on the right, only the leftmost column can be controlled. There is no problem writing this)
sbit R2=P3^1;//Second row initialization
sbit R3=P3^2;//Third row initialization
sbit R4=P3^3;//Fourth row initialization

sbit C1=P3^4;//Initialization of the first column (this is from right to left)
sbit C2=P3^5;//Initialization of the second column
sbit C3=P4^2;//Third column initialization
sbit C4=P4^4;//Fourth column initialization

Initialize matrix keyboard

void Init()//Initialize matrix keyboard
{
	R1=R2=R3=R4=1;
	C1=C2=C3=C4=1;
}

DS18B20 configuration

First, look at the schematic diagram, find DQ and connect P14
So define DQ first

sbit DQ=P1^4;
void dsinit()
{
  DQ=0;
	Delay500us();
  DQ=1;
	Delay500us();
}

Anti shake

Press the key to execute the statement only once:
Take R1 as an example

if(R1==0)
	{
		delay_ms(50);
		if(R1==0)
		{
			//Execute statement
			while(R1==0);
		}
	}

Here are a bunch of delay functions

There are different times. It should be easy to use. I've tried it myself

void Delay100us()		//@12.000MHz
{
	unsigned char i, j;

	i = 2;
	j = 39;
	do
	{
		while (--j);
	} while (--i);
}

void Delay500us()		//@12.000MHz
{
	unsigned char i, j;

	i = 6;
	j = 211;
	do
	{
		while (--j);
	} while (--i);
}


void delay_ms(unsigned int n)
{
    unsigned int i=0,j=0;
    for(i=0;i<n;i++)
        for(j=0;j<123;j++);
}

nop() means an empty statement to delay

PCF8591

It was said in class, but I didn't listen at all. After reading this article, I understood it. It can be said that it was well written
About iic and pcf8591

Steps: start the bus ----- send address + Write ----- send control byte ----- wait for PCF8591 response ----- stop the bus ----- restart the bus ----- send address + read ----- read data ----- host sends non response signal ----- stop the bus

The device address of PCF8591 includes fixed part and programmable part. The programmable part needs to be set according to hardware pins A0, A1 and A2. The last bit of the device address is used to set the direction of data transmission, i.e. read / write bit. In IIC bus protocol, the device address is the first byte sent after the start signal. If the hardware address pins A0, A1 and A2 are grounded, the read operation address of the PCF8591 device is 0x91; The write address is 0x90. Read is 1 and write is 0

The photosensitive sensor is connected to AIN1, channel 1; The control register should write: 0x01.
Potentiometer Rb2 is connected to AIN3, channel 3; The control register should write: 0x03.

Three digits are displayed on the nixie tube:
Hundredth: dat / 100
Ten digits: (DAT% 100) / 10
Bits: DAT% 10

Write so much first, and then come back after I finish the 12th provincial competition

Topics: Single-Chip Microcomputer