This template is suitable for 51 Introduction to single chip microcomputer , It's easier to understand , Including timer and serial port operation .
You can use the timer T0 Interrupt service function and main loop to achieve the main function , stay UART Interrupt service function (T1 timer ) The function of receiving command through serial communication is realized in .
notes : This template STC89C52RC Use of single chip microcomputer 11.0592MHz Crystal oscillator , If used 12MHz Crystal oscillator can modify the relevant position of timer
<>STC89C52RC schematic diagram
<> Template and analysis
<>1. Main structure
#include <reg52.h> unsigned char T0RH = 0; //T0 Overload value high byte unsigned char T0RL = 0;
//T0 Overload value low byte unsigned char RxdByte = 0; // Bytes received by serial port void ConfigTimer0(unsigned int
ms); void ConfigUART(unsigned int baud); void main() { EA = 1; // Enable total interrupt
ConfigTimer0(1); // Configure as 1ms ConfigUART(9600); // Configure baud rate to 9600 while(1) { } }
/* Configure and start T0,ms-T0 Timing time */ void ConfigTimer0(unsigned int ms) { }
/* Serial port configuration function ,baud- Communication baud rate */ void ConfigUART(unsigned int baud) { } /*T0 Interrupt service function */ void
InterruptTimer0() interrupt 1 { } /*UART Interrupt service function */ void InterruptUART() interrupt
4 { }
<>2. Configuration and Implementation T0 interrupt
Write the configuration function first ConfigTimer0. It is necessary to realize the single-chip microcomputer entering every certain time T0 The interrupt service function performs its function , First, determine how often to enter the interrupt .
The parameters we enter into this function are ms, It needs to be converted into the actual working mode of MCU in the function to calculate .
STC89C52RC Of 1 Two machine cycles equal to 12 Clock cycles . The working cycle of single chip microcomputer should be calculated by machine cycle , Every working cycle of single chip microcomputer , The value of the timer increases from the initial value 1, stay 16 Bit timer mode until 65536 overflow , Trigger corresponding interrupt , Enter the corresponding interrupt service function .
with 11.0592MHz For example, the crystal oscillator , First, convert the clock cycle to the machine cycle ,ms Convert to s Calculate the required count . Because the timer is up count overflow , So we need to subtract the count value from the overflow value to get the overload value .( If the timer counts down, this step is not used )
unsigned long tmp; tmp = 11059200 / 12; // Calculate the required count value tmp = (tmp * ms) / 1000; tmp
= 65536 - tmp; // Calculate timer overload value
T0 It's a 16 Bit timer , Divided into TH0 high 8 Bit sum TL0 low 8 position , The corresponding overload values should also be calculated separately .
unsigned char T0RH = 0; //T0 Overload value high byte unsigned char T0RL = 0; //T0 Overload value low byte T0RH = (
unsigned char)(tmp>>8); // The timer overload value is split into high and low bytes T0RL = tmp;
TCON:
TMOD:
IE:
Next, configure TCON,TMOD and IE These registers .
First, register the working mode TMOD Configure as 16 Bit timer mode , Then give it to me TH0,TL0 Two 8 Initial value of bit register , Reconfiguration of bit addressable interrupt allow register IE, Namely ET0 = 1, Final configuration
timer / Controller control register TCON:TR0 = 1 start-up T0 timer .
TMOD &= 0xF0; // Clearing T0 Control bit TMOD |= 0x01; // to configure T0 As a model 1 TH0 = T0RH; // load T0 Overload value TL0 =
T0RL; ET0 = 1; // Enable T0 interrupt TR0 = 1; // start-up T0
At the end of the day T0 Interrupt service function InterruptTimer0 The value of the timer is reinitialized each time the overflow enters the interrupt , performing function .
TH0 = T0RH; // Overload initial value TL0 = T0RL; ...
<>3. Configuration and implementation of serial port interrupt
The serial port interrupt is different from the above interrupt . first , It can only be controlled by T1 or T2 Timer as “ Baud rate generator ”,T0 may not ; secondly , The function of timer is to detect data regularly , Instead of timing into the interrupt ; And then the serial port is generally controlled by the P3.0(RXD) and P3.1(TXD) to bear , Because the specific communication protocol has been realized by the hardware circuit of MCU , We only need to know how to configure registers .
(STC89C52RC There is only one serial port , If want this monolithic integrated circuit to use serial port to receive some data of sensor already , At the same time, the data is sent back to the host computer through serial communication , One serial port is not enough , You can use it IO Analog serial port , Write about the implementation of serial communication protocol , The most convenient is to use 2 A single chip microcomputer with two serial ports )
SCON:( Serial port control register )
First configure the serial port as the mode 1( One byte reception 8 position ), Then configure the timer T1 by 8 Bit reload mode , Namely TL1 Every time 256 Load after overflow TH1 Value of . default Power controller and baud rate selection register
PCON = 1, One bit detection 32 second , In the same way , use 256 Get by subtracting T1 Overload value , That is to say, when the overflow control time is the agreed baud rate (9600), One bit takes up time 1 32 \frac
{1}{32}321.
SCON = 0x50; // Configure serial port as mode 1 TMOD &= 0x0F; // Clearing T1 Control bit TMOD |= 0x20; // to configure T1 As a model 2 TH1 =
256 - (11059200/12/32)/baud; // calculation T1 Overload value TL1 = TH1; // The initial value is equal to the overload value ET1 = 0; // prohibit T1 interrupt ES
= 1; // Enable serial port interrupt TR1 = 1; // start-up T1
SBUF Is the serial data buffer register , Size is 8 byte .
At the end of receiving a byte of data ,RI Auto set 1, To manually return to the 0;TI In the same way . Then receive the data , Transfer information of executive function .
/*UART Interrupt service function */ void InterruptUART() interrupt 4 { if (RI) // Bytes received { RI = 0;
// Manual reset receive interrupt flag bit RxdByte = SBUF; // Received data (8 position ) Remains in the receive byte variable SBUF = RxdByte;
// The received data is sent back directly , It is used to prompt whether the input information is received correctly ... } if (TI) // Byte send complete { TI = 0; // Manual reset send interrupt flag bit } }
<> Complete template code
/*********************************************************************** *
Interrupted 4 Registers ( All for 8 position ) * Interrupt allowed Interrupt priority timer / Counter working mode timer / Counter control * IE IP TMOD TCON * Bit addressing fixed
Non bitable addressing Bit addressing * EA|ET1|ES1 ---- T1|T0 TR0|TR1 * Serial port control register SCON Non bitable addressing Place 0x50 *
Interrupt priority (0~5):INT0 T0 INT1 T1 TX/RX T2 * * Specific transfer in reg52.h View in header file
************************************************************************/ #
include <reg52.h>
/****************************** Define pin *********************************/
/****************************** Define global variables *****************************/
/************************* timer ********************************/ unsigned char
T0RH= 0; //T0 Overload value high byte unsigned char T0RL = 0; //T0 Overload value low byte
/************************ Serial communication *******************************/ unsigned char
RxdByte= 0; // Bytes received by serial port
/**************************** Function declaration ***********************************/ void
ConfigTimer0(unsigned int ms); void ConfigUART(unsigned int baud); /* Principal function */ void
main() { EA = 1; // Enable total interrupt ConfigTimer0(1); // Configure as 1ms ConfigUART(9600);
// Configure baud rate to 9600 while(1) { } } /* Configure and start T0,ms-T0 Timing time */ void ConfigTimer0(unsigned int
ms) { unsigned long tmp; tmp = 11059200 / 12; // Calculate the required count value (ms Convert to s) tmp = (tmp *
ms) / 1000; tmp = 65536 - tmp; // Calculate timer overload value //tmp = tmp + 13; // Compensating the error caused by interrupt response delay T0RH
= (unsigned char)(tmp>>8); // The timer overload value is split into high and low bytes T0RL = (unsigned char)tmp; TMOD &=
0xF0; // Clearing T0 Control bit TMOD |= 0x01; // to configure T0 As a model 1 TH0 = T0RH; // load T0 Overload value TL0 = T0RL; ET0 =
1; // Enable T0 interrupt TR0 = 1; // start-up T0 } /* Serial port configuration function ,baud- Communication baud rate */ void ConfigUART(unsigned int
baud) { SCON = 0x50; // Configure serial port as mode 1 TMOD &= 0x0F; // Clearing T1 Control bit TMOD |= 0x20;
// to configure T1 As a model 2 TH1 = 256 - (11059200/12/32)/baud;
// calculation T1 Overload value (32:PCON Configure as 1, A data monitor 32 second , Ensure accuracy ) TL1 = TH1; // The initial value is equal to the overload value ET1 = 0; // prohibit T1 interrupt ES
= 1; // Enable serial port interrupt TR1 = 1; // start-up T1 }
/****************************** Custom function *******************************/
/****************************** Custom function END****************************/
/*T0 Interrupt service function */ void InterruptTimer0() interrupt 1 { TH0 = T0RH; // Overload initial value TL0 = T0RL;
/************************** timer 0 Function realization ***************************/
/*******************************END**********************************/ }
/*UART Interrupt service function */ void InterruptUART() interrupt 4 { if (RI) // Bytes received { RI = 0;
// Manual reset receive interrupt flag bit RxdByte = SBUF; // Received data (8 position ) Remains in the receive byte variable SBUF = RxdByte;
// The received data is sent back directly // It is used to prompt whether the input information is received correctly
/************************** Realization of serial communication function ***************************/
/********************************END**********************************/ } if (TI
) // Byte send complete { TI = 0; // Manual reset send interrupt flag bit } }
Technology