# step by step learning 5 microcontroller#Interrupt and digital tube dynamic display#not.5

1. Master the concepts, definitions and applications of C language arrays.

1) An array is a set of variables. This set of variables needs to meet three conditions:

have the same array type

have the same name

are continuous in memory

2) Declaration and initialization

Array type array name [array length]

Array type array name [array length] = {initial value list}

3) Pay attention to the phenomenon of use and assignment

Distinguish the concepts of array subscript and number of array elements

The array element subscript can be a constant, variable or expression, and the initialization must be a constant

Array integer assignment can only be performed during initialization, and the program can only assign a single element.


2. Master the usage and difference between if statements and switch statements, and be able to correctly choose which statement to use when programming.

The most detailed summary of if statements and switch statements: https://blog.csdn.net/2301_77479336/article/details/130087491?spm=1001.2014.3001.5501


3. Thoroughly understand the principles and application methods of interrupts, close the tutorial, write the program in this chapter independently, download and practice.

Introduction to interruptions

For example, I am using gas to boil a pot of water in the kitchen, so I have to stay in the kitchen and wait for the water to boil - if the water overflows and extinguishes the
gas, a disaster may occur. Suddenly, sonorous and powerful singing came from outside the door. My favorite Tian Long Ba Bu was about to start. Listening to the "gurgling" sound of the kettle, I knew clearly: Unless I wait until the water boils, I will not have time to enjoy life.
What does this scene have to do with interruption?


In this scene, I am the only subject with processing power. Whether it is boiling water or watching TV, I can only do one thing at the same time . However, when I concentrate on doing one thing, there are always many urgent or not urgent things that suddenly appear in front of
me, all of which require attention. Some of them require me to stop the work at hand and deal with them immediately. Only after processing is completed can you go back and
complete the previous task. The interrupt mechanism not only gives me the ability to deal with unexpected situations, but if I can give full play to the magic of this mechanism, I can complete multiple tasks "simultaneously".

In fact, it takes 10 minutes to boil water, but it only takes me a few seconds to lift the kettle and turn off the gas. For these few seconds, I
need to wait in the kitchen for 10 minutes. If I use the alarm clock to set a timer of 10 minutes, when 10 minutes is up, the alarm clock will remind me that it is time to turn off the gas
, and then I can watch TV with peace of mind. In fact, I used an interrupt signal like an alarm clock to remind me to complete the task of carrying the kettle and turning off the gas.

2)


4. Try to modify the program so that our digital tube only displays the valid bits, that is, the high-order 0s are not displayed.

#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { 
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned int flag1s = 0;
unsigned int cnt = 0,i = 0;
void main()
{
 char arr;
	unsigned long sec = 0;
	unsigned int buf[6];
	EA = 1;
	ENLED = 0;
	addr3 = 1;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	ET0 = 1;
	TR0 = 1;
  while(1) 
{
 if (flag1s == 1)  //ÅжÏ1Ã붨ʱ±êÖ¾
        {
            flag1s = 0;   
            sec++;        
            buf[0] = sec%10;
            buf[1] = sec/10%10;
            buf[2] = sec/100%10;
            buf[3] = sec/1000%10;
            buf[4] = sec/10000%10;
            buf[5] = sec/100000%10;			
			for(arr = 5;arr >=1;arr--)
				{
				if(buf[arr] == 0)			
				LedBuff[arr] = 0xff;
				else
				     break;
				}
				for(;arr >=0;arr--)
				{
				LedBuff[arr] = LedChar[buf[arr]];
				}
			}
}	
}
void InterruptTimer0() interrupt 1
{
	TH0 = 0xfc;
	TL0 = 0x67;
	cnt++;
	if(cnt >= 1000)
	{
		cnt = 0;
	flag1s = 1;
	}
	P0 = 0xff;
	switch (i)
	{
		    case 0 :addr0 = 0;addr1 = 0;addr2 = 0;P0 = LedBuff[0];i++;break;
				case 1 :addr0 = 1;addr1 = 0;addr2 = 0;P0 = LedBuff[1];i++;break;
				case 2 :addr0 = 0;addr1 = 1;addr2 = 0;P0 = LedBuff[2];i++;break;
				case 3 :addr0 = 1;addr1 = 1;addr2 = 0;P0 = LedBuff[3];i++;break;
				case 4 :addr0 = 0;addr1 = 0;addr2 = 1;P0 = LedBuff[4];i++;break;
				case 5 :addr0 = 1;addr1 = 0;addr2 = 1;P0 = LedBuff[5];i=0;break;
		default:break;
	}
	}


5. Try to write a program that counts down from 999999, and use the interrupt of timer T1 to complete it. By writing this program, you can master the application of timers and interrupts.

#include <REGX52.H>
sbit addr0 = P1^0;
sbit addr1 = P1^1;
sbit addr2 = P1^2;
sbit addr3 = P1^3;
sbit ENLED = P1^4;
unsigned char code LedChar[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { 
    0x90, 0x90, 0x90, 0x90, 0x90, 0x90
};
unsigned int flag1s = 0;
unsigned int cnt = 0,i = 0;

void main()
{
 char arr;
	unsigned long sec = 0;
	unsigned int buf[6];
	EA = 1;
	ENLED = 0;
	addr3 = 1;
	TMOD = 0x01;
	TH0 = 0xfc;
	TL0 = 0x67;
	ET0 = 1;
	TR0 = 1;
  while(1) 
{
 if (flag1s == 1)  //ÅжÏ1Ã붨ʱ±êÖ¾
        {
            flag1s = 0;  
            sec++;        
            
            buf[0] = sec%10;
            buf[1] = sec/10%10;
            buf[2] = sec/100%10;
            buf[3] = sec/1000%10;
            buf[4] = sec/10000%10;
            buf[5] = sec/100000%10;
				
				
				for(arr = 5;arr >=0;arr--)
				{
				LedBuff[arr] = LedChar[9-buf[arr]];
				}
			}
}	
}
void InterruptTimer0() interrupt 1
{
	TH0 = 0xfc;
	TL0 = 0x67;
	cnt++;
	if(cnt >= 1000)
	{
		cnt = 0;
	flag1s = 1;
	}
	P0 = 0xff;
	switch (i)
	{
		    case 0 :addr0 = 0;addr1 = 0;addr2 = 0;P0 = LedBuff[0];i++;break;
				case 1 :addr0 = 1;addr1 = 0;addr2 = 0;P0 = LedBuff[1];i++;break;
				case 2 :addr0 = 0;addr1 = 1;addr2 = 0;P0 = LedBuff[2];i++;break;
				case 3 :addr0 = 1;addr1 = 1;addr2 = 0;P0 = LedBuff[3];i++;break;
				case 4 :addr0 = 0;addr1 = 0;addr2 = 1;P0 = LedBuff[4];i++;break;
				case 5 :addr0 = 1;addr1 = 0;addr2 = 1;P0 = LedBuff[5];i=0;break;
		default:break;
	}
	}

Guess you like

Origin blog.csdn.net/2301_77479336/article/details/132841655