Embedded Development Learning Notes 9 - Make a fun LED flash

Embedded Development Learning Notes 9 - Make a fun LED flash

Preface

The essence of LED small lamp flickering is to control the I/O port on the single chip computer, and control the light of the LED small lamp by cycling the input level to the I/O port.However, since the machine cycle of single-chip computer is 12/11059200 s, and the human eye can not recognize this fast conversion generally, we also need to use timers to achieve delay, reduce the flicker effect recognized by the human eye, so as to achieve LED light flicker, which is the basic principle of LED small light flicker.

Actual operation

Program Functions

What we need to do is make the LED small light 1s flash once, and the number tube shows the number in turn, turn on buzzer alarm after 10 flashes
Here we mainly use P1 port, timer 0 and external interrupt 1. The overall implementation architecture is as follows

Ideas for implementation

The small lamp on the MCU development board is connected to the P1 port on C51, so the P1 port needs to be controlled. Eight LED small lights can be controlled by using the 16-digit number. In order to be simple and complete, the full brightness corresponds to the 16-digit 0xFF, and the reverse is the total extinction, and the cycle will flash the LED small lamp.

Due to the persistence effect of the human eye, we also need to use a timer to delay it. The maximum machine cycle that the timer can time is 65536, which is about 71ms. Although it may allow us to see the LED flickering, the LED flickers too fast, so we need to use multiple cycles to expand.For ease of calculation, a timer timer event of 50ms is selected, corresponding to a machine cycle of 468080, so the initial timer value should be 19456, corresponding to hexadecimal 0x4BFD.

Based on the above data, a function is used to encapsulate the timer initialization process, which is divided into three main steps:
1. Set the working mode of the timer as the second mode, TMOD=0x01;
2. Setting the initial value of the timer, setting TH0=0x4B, TL0=0xFD
3. Turn on the timer and set TR0=1
This completes the timer 0 setting, cycles it 20 times, and then reverses the P1 port, then the LED light 1s flashes once. Note here that since no internal interrupt T0 is used, the software will set TF0 to 0 after the timer overflows, and then reset the initial value.

After achieving the above functions, the task should have been completed, but I want to familiarize myself with interrupts, so I use external interrupt 1 to achieve another function: buzzer alarm after 10 flashes.
The routine for external interruption is similar to that for timers:
1. Turn on interrupt master switch EA
2. Open external interrupt switch EX1
3. Set the trigger mode, set here as the down-edge trigger mode, i.e. IT1=1
This completes the setting of external interrupt 1, and then enters the service program of external interrupt 1 after setting the service program of external interrupt 1, that is, after meeting the trigger conditions, to interrupt the execution service program, that is, to turn on the buzzer, to turn off again after a cycle (or to close after a delay period of time). Here we need to note that in the MCU development board, we need to connect the P37 port to the P33 port, and then generate the P3^7 port.3 descending edges to trigger interruption.
I also added a display function for the digital tube, which is basically the same way, but you need to know how the digital tube works. I won't go into details here, and then I'll have time to start a blog to talk about circuit issues.

Program Source

#include<reg52.h>

sbit DU = P2^6;//Segment selector latch
sbit WE = P2^7;//Bit Selective Lock
sbit beep = P2^3;//Buzzer
sbit flag = P3^7;//Generate drop-off along control into interruption


//Digital tube location address plus code is stored in R0M area and will not be changed during execution
code unsigned char location[]={
0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F};
//Digital tube display content
code unsigned char nums[]={
//0     1     2     3     4    5     6      7     8    9    
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 
//A     b     C     d     E    F     G      H     L     N    -
0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ,0x76, 0x38, 0x37, 0x3E, 0x40 };

//External interrupt 1 initialization
void INIT1()
{
   EA = 1; //Open Total Interrupt
   EX1 = 1; //Open External Interrupt 1
   IT1 = 1;	//External interrupt 1 drop edge trigger
}

//External interrupt 1 service program
//If interruption occurs, buzzer works alternately
void int1() interrupt 2
{
	beep = ~beep;
}

//timer initiated
void TimerInit()
{
	TMOD = 0x01; // Set timer working mode
	TH0 = 0x4B;	//(65536-46082) / 256;
	TL0 = 0xFD; //(65536-46082)%256; Timing 50ms
	TR0 = 1; //Turn on the timer
}

//Digital tube display timer counting time
//LocionInfo -- Select the position of the display tube, starting from 1
//numInfo -- Selects the number to display, defaulting to 0
void Display(unsigned char locationInfo,unsigned char numInfo)
{
	P0 = 0xFF;//Clear break code, single-chip processing effect is faster, naked eye may also mix the value in the last P-port
	 
	WE = 1; //Open Bit Selected Lock
	P0 = location[locationInfo-1]; //Turn on the digital tube 1111 1110
	WE = 0; //Lock data, not affected by IO port
	
	DU = 1; //Open Segment Selector Lock
	P0 = nums[numInfo]; //Display data 0000 0110
	DU = 0;	
}

void main()
{
	unsigned char msec,sec = 0;//Milliseconds and seconds to store variables
	TimerInit();
	P1 = 0x00; //LED light
	while(1)//10s a cycle
	{
	    if(TF0 == 1)//Timer Overflow Flag
		{
		 	TF0 = 0;//Timer Overflow Flag Bit Zero
			TH0 = 0x4B;	//
	        TL0 = 0xFD;
		 	msec++; //50ms to
			if(msec == 20)//1s to
			{
			 	msec = 0;
				P1 = ~P1;//Achieve LED light blinking
				Display(2,sec);//Default second tube display
				sec++;//1s time to		
			}
		}
		if(sec == 10) 
		{
		 	sec = 0;
			INIT1();//External interrupt 1 initialization
			flag = 1;//Entry interruption
			flag = 0;//Transfer to P3^3 port through P3^7 descent along change--Wired connection is required on single chip computer
		}
	}	
}

Implement effect display


---------------------------------------------------------------------------------------------------

40 original articles published, 9 praised and 1789 visited
Private letter follow

Posted on Fri, 13 Mar 2020 18:35:55 -0700 by wreed