STC89C52 controls the on and off of the running light through the serial port

Publisher:逍遥游侠Latest update time:2016-05-12 Source: eefocusKeywords:STC89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This note contains two aspects:

1. Just control the LED on and off, no value is returned

2. Control the LED on and off and return the value

I watched several lectures, all of which were about serial port mode 1. I haven't touched on the others yet, so here I will only use serial port mode 1 to implement these two functions. The part that needs to be calculated in the serial port is to find the initial value of the timer based on the baud rate to be used. The timer uses mode 2, which can automatically load the initial value to avoid errors when the assignment statement loads the initial value.

At 9800bps, timer mode 2, serial port mode 1, and crystal oscillator baud rate of 11.0592MHZ, calculate the values ​​of TH1 and TL1.

The baud rate of mode 1 = (2^SMOD/32) x T1 overflow rate. After the microcontroller is reset, all the power management registers PCON are cleared, and SMOD is also cleared as one of the bits.

The baud rate is already known. So the only thing left is the T1 overflow rate.

Assuming the initial value is X, the timer overflows every time it counts 256-X numbers (the timer is 8 bits, and the maximum is 255. Overflow occurs when 256). The time it takes to count one number is one machine cycle, and the machine cycle = T clock cycle x 12. So the overflow time is = number x each time = (256 - X) * 12/Fosc. Then the base rate is the reciprocal of the overflow time.

So combined with the formula "mode 1 baud rate = (2^SMOD/32)xT1 overflow rate", the formula can be summarized as:

9600 = 2^0 /32 * Fosc / (256 - X)*12 Substitute all known data to get 9600 = 2^0 /32 * 11059200/ (256 - X)*12 =====》》》》 The obtained X is: 253 .

On this basis, if SMOD is set to 1, the baud rate is:

Baud rate = (2^1/32) * 11059200 / (256 - 253) = 2 * [1/32 * 11059200 / (256 - 253)] = 2 * 9600 = 19200. That is, it becomes twice the original.

If the crystal oscillator is replaced with 12MHZ and the initial value is calculated, the obtained X is: 252.744792... infinite decimal. This will cause errors. I always felt that integer crystal oscillators were good before, but now I know why there is a crystal oscillator like 11.0592MHZ.

The initial value is calculated in this way, and the code is posted below.

It only controls the LED on and off, and does not return a value.

There are two ways to achieve this: query and interrupt.

A. Use query first. It feels better to call it judgment because it is implemented by if judgment.

 

#include 

void main()
{  
		//Setting parameters
		TMOD = 0x20; //Set the working mode of timer 1 to mode 2
		TH1 = 0xfd;
		TL1 = 0xfd; //Load TH1, TL1
		TR1 = 1; //Start timer 1

		REN = 1; //Enable serial reception bit
		SM0 = 0;
		SM1 = 1; //Set the serial port working mode to mode 1
/*
* EA = 1; //Global interrupt enable bit
* ES = 1; //Serial port interrupt enable bit
* The query method is used here to determine the receive interrupt flag, so even if the interrupt enable bit is not enabled,
*
*/
	while(1)
	{	 	
		//Query method to detect RI
		if(RI == 1) //RI is the receive interrupt flag. Set to 1 by hardware and cleared to 0 by software
		{
			P1 = SBUF;
			RI = 0;
		} 		
	}

}
B. Interruption method

 

 

#include 
void main()
{  
		//Setting parameters
		TMOD = 0x20; //Set the working mode of timer 1 to mode 2
		TH1 = 0xfd;
		TL1 = 0xfd; //Load TH1, TL1
		TR1 = 1; //Start timer 1

		REN = 1; //Enable serial reception bit
		SM0 = 0;
		SM1 = 1; //Set the serial port working mode to mode 1

		EA = 1; //Global interrupt enable bit
		ES = 1; //Serial port interrupt enable bit

		while(1); //Wait for the interrupt to occur
		
}

//Interrupt detection RI
void ser() interrupt 4
{
	   P1 = SBUF;
	   RI = 0;
}

In addition to the code, it seems that the interrupt enable bit is turned on or not. Because RI is set to 1 automatically by the hardware. Even if the interrupt enable bit is not turned on, it can still be judged by if.

 

 

The above two are one-way, here is a two-way one.

 

/*
 *Send data to the lower computer through the serial port and display it on the running light of port P1.
 *At the same time, the MCU returns the received data and displays it on the serial port assistant
 */
#include 

unsigned char flag;

void main()
{  
		//Setting parameters
		TMOD = 0x20; //Set the working mode of timer 1 to mode 2
		TH1 = 0xfd;
		TL1 = 0xfd; //Load TH1, TL1
		TR1 = 1; //Start timer 1

		
		SM0 = 0;
		SM1 = 1; //Set the serial port working mode to mode 1
		REN = 1; //Enable serial reception bit

    	EA = 1; //Global interrupt enable bit
		ES = 1; //Serial port interrupt enable bit

	while(1)
	{	
		/* At the beginning, the MCU buffer register is empty and no data can be displayed
		 * Receive data from the serial port first, then return the data
		 * Receive data in the interrupt and set the flag to 1. This means that data has been received.
		 * If data is received (flag == 1), it means it is received; otherwise, it means no data is received and no display is made. Continue to determine the flag value
		 */
		
		if(flag == 1)
		{						
			//send data
			ES = 0; //Disable serial port interrupt and send data
			SBUF = P1; //Write data into SBUF register
			while(!TI); //wait
			TI = 0;
			ES = 1;
			flag = 0;
		
		}		
	}

}

void ser() interrupt 4
{
	//Receive data
	P1 = SBUF;
	flag = 1;
	RI = 0;
}

The flag = 0 in the main function must be there. Otherwise, the serial port assistant will get stuck after just a short while.

 

There are two more key statements in this example:

P1 = SBUF; //Assign the value in the SBUF register to P1

SBUF = P1; //Write the value of P1 into SBUF

SBUF is written like this: SBUF serial data buffer register, one send buffer register, one receive buffer register. Both share the same address 99H, but are physically two independent registers. So how to distinguish whether it is sending or receiving? Use statements to distinguish.

To control the running lights, you need to send the hexadecimal format.

For example, if I send FB (1111, 1011), the L2 light will be on on my development board. If I send characters, it will be hard to control. If I use the routine in 2 and send "fb" as a character, the microcontroller will return to the serial assistant and display it as "62" in hexadecimal. This, well, I don't know how to calculate it at the moment:P

 

There are no memorable pictures, just some running lights. However, the running lights now are not the same as before. Now the running lights on the development board can be controlled by me from the computer:D

I just don't know when I will write a host computer next time.

Keywords:STC89C52 Reference address:STC89C52 controls the on and off of the running light through the serial port

Previous article:Stepper Motor Driver Based on 51 Single Chip Microcomputer
Next article:MCU Tour - Interrupt Flowing Light

Recommended ReadingLatest update time:2024-11-16 19:43

Control stepper motor based on stc89c52 microcontroller
The 51 microcontroller is an entry-level development board for many microcontroller beginners. Today I will make a stc89c52 board based on the 51 core to control the stepper motor, which can realize the forward and reverse rotation, acceleration and deceleration of the stepper motor. In addition, the Ds18b20 module th
[Microcontroller]
Control stepper motor based on stc89c52 microcontroller
51 single chip microcomputer STC89C52 matrix keyboard scanning method detection
Program source code /*-----------------------Include header file area-------------------------*/ #include reg52.h   //MCU header file  /*-----------------------Data type definition area-----------------------*/ typedef unsigned char u8; //define type unsigned char alias u8 typedef unsigned int u16; //define type u
[Microcontroller]
51 single chip microcomputer STC89C52 matrix keyboard scanning method detection
Make the buzzer sound
The circuit diagram of the buzzer on the development board schematic is as follows. According to the video, we know that it is a passive buzzer. You can see that one end of the buzzer is connected to the resistor, and the other end is connected to pin BZ. Note that this BZ is not the name of Yuanqi. As yo
[Microcontroller]
Make the buzzer sound
51 single chip microcomputer (STC89C52RC) lcd1602 experiment summary
  LCD1602 is a liquid crystal display module with its own controller, which can display two lines of dot matrix data. Please google its pin parameters.      Phenomena encountered during actual use:   The black block on the first line and the blank on the second line are the signs that the 1602 itself has successfull
[Microcontroller]
Design of Intelligent Thickness Measurement System
The intelligent thickness gauge studied in this project is a complete solution based on a single-chip microcomputer, which consists of a main controller, a measuring sensor, an AD conversion module, a liquid crystal display module and corresponding supporting hardware. Through the use of the measuring sensor, the thic
[Test Measurement]
Design of Intelligent Thickness Measurement System
Temperature-measurable electronic calendar based on STC89C52 single-chip microcomputer
     With the development of science and technology, electronic perpetual calendars are becoming more and more common in life, and their functions are becoming more and more. According to people's daily basic needs, an electronic perpetual calendar is designed, which can realize functions such as temperature measureme
[Microcontroller]
Temperature-measurable electronic calendar based on STC89C52 single-chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号