Microcontroller simulation serial communication Notes

The baud rate calculation formula: 1,3 baud rate mode = 1/32 or 1/16 * Timer 2 overflow rate

        Baud rate = 1/16 or 1/32 * (crystal frequency / 12 * (256-TH1))

    

 

 

 

 1 operating in serial mode, respectively T / C1 and T / C2 baud rates common initial value table is as follows

 

 

 

 

 

 About analog serial port baud rate setting method:

9600b / s -> 104us -> mode of operation of the timer counter 2, in order to achieve the delay time 104us -> i.e. at a frequency of 104 counts of 12MHZ

-> SCM + 1 are counted, the initial value is set (256-104) -> Since the function needs to consume a certain running time, so setting the initial value

Is (256-99) to reduce the error

 

 

Microcontroller timing counter 51 in the working mode 4:

Way of working Feature
0 Timer / counter T0 operating in mode 0, only the 16-bit counter 13, i.e., TH0 high 8 bits and lower 5 bits of TL0, to form a 13-bit timer / counter.
1 Timer T0 work and work 1 0 similar, the difference lies in the number of bits in which the counter. Work involved in 0 to 13-bit counter counting work places a 16-bit counter counted.
2

2 at T0 timer mode of operation, the 16-bit counter into two separate 8-bit counter TH0 and TL0.

3 3 T0 timer only works effectively. When the timer T0 working mode 3, the 16-bit counter into two separate 8-bit counter TH0 and TL0.

Serial relevant register:

 

 

 

 

Serial communication timing diagram:

 Output Timing Diagram:

 

 Input Timing Diagram:

 

 

REN software set to 1, the receiver 16 to the selected baud rate sampling times RXD pin level, detects RXD pin input level to a negative transition occurs, then the start bit is valid, it shifted into the input shift register, and starts receiving the remaining bits of the frame information. Reception, the data is transferred from the input shift register to the right, when the left start bit shifted into the input shift register, the control circuit last shift. When RI = 0, and SM2 = 0 (or the received stop bit is 1), the first 8-bit data to 9-bit data received is loaded into the receive SBUF, the 9th bit (stop bit) into RB8, juxtaposed RI = 1, an interrupt request to the CPU.
----------------
Disclaimer: This article is the original article CSDN bloggers "To_dreams", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/to_dreams/java/article/details/7716678

 

Microcontroller interrupt signal way:

 Int0 External Interrupt 0, active low. P3.2 pin through.
 INT1 An external interrupt request is active low. By pin P3.3
T0 Timer / Counter 0 overflow interrupt request.
T1 Timer / Counter 1 overflow interrupt request
TXD/RXD Serial port interrupt request. When the completion of a serial port to send or receive data, it requests an interrupt.

 

Form about 51 microcontroller interrupt and C language programming format

void INT0() interrupt  0  using  1
{

}
interrupt   0     indicates external interrupt 0
interrupt   1     indicates the timer interrupt 0
    .
    .
    .
    .

the using  0 0 Group register
 the using  1 is a group of the first register

 

for example:

/ *   External interrupt program * / 
void ISR_Key ( void ) interrupt 0  a using  1
{
    Pl = Pl ~; // S3 once the trigger is pressed, P1 negated once 
}

/ *    Serial interrupt program   * / 
void UART_SER ( void ) interrupt 4  // serial interrupt service routine 
{
    unsigned char the Temp; // definition of temporary variables 
    IF (the RI) // determines an interrupt is received 
    {
        The RI = 0 ; // flag is cleared and 
        the Temp = SBUF; // read into the buffer value 
        P1 = the Temp; // the value P1 to the output port for viewing 
        SBUF = the Temp; // the received value and then back end computer 
    }
     IF (TI) // if the transmission flag is cleared 
    TI = 0 ;
}

            

 

Microcontroller simulation serial communication Code:

#include " reg52.h " 
// transceiver pins byte defines the maximum number of received data
 // #include "stdio.h"
 // the sbit the RXD P3 ^ = 0;


//#define TXD    P3^1

#define RECEIVE_MAX_BYTES    16

#define TIMER_ENABLE()    {TL0=TH0;TR0=1;fTimeouts=0;}//使能T/C
#define TIMER_DISABLE()    {TR0=0;fTimeouts=0;}//禁止T/C
#define TIMER_WAIT()    {while(!fTimeouts)fTimeouts=0;}//等待T/C超时


unsigned char fTimeouts = 0 ; // T / C timeout flag 
unsigned char recvbuf [ 16 ]; // data receiving buffer 
unsigned char RecvCount = 0 ; // receiving data counter

// send byte 
void SendByte (unsigned char B)
{
    unsigned char i=8;
    TXD = 0;
    TIMER_ENABLE();
    TIMER_WAIT();
    while(i--)
    {
            if(b&1)
                TXD=1;
            else 
                TXD=0;
            TIMER_WAIT();
            b>>=1;
    }
    
        TIMER_ENABLE();
    TIMER_DISABLE();
    
}

// receive byte 
unsigned char RecvByte ( void )
{
    unsigned char i;
    unsigned char b=0;

    TIMER_ENABLE();
    TIMER_WAIT();
    for(i=0;i<8;i++)
    {
            if(RXD)
                b|=(1<<i);
            TIMER_WAIT();
            
    }
    
    TIMER_WAIT (); // wait for the end position 
    TIMER_DISABLE ();
     return b;
    
}
// print string 
void PrintfStr ( char * pstr)
{
    while(pstr && *pstr)
    {
        SendByte(*pstr++);
    }
}

// T / C Initialization 
void TimerInit ( void )
{
    TMOD=0X02;
    TR0 = 0 ;
    TF0=0;
    TH0 = ( 256 - 99 );
    TL0=TH0;
    ET0 = 1 ;
    EA = 1 ;
}


// Is there a start bit reaches the 
unsigned char StartBitCome ( void )
{
    return (RXD==0);
}



// main function

void main (void)
{
    unsigned char i;
    TimerInit ();
    //printf("hello 80c52\r\n");
    while(1)
    {
        if(StartBitCome())
        {
                RecvBuf[RecvCount++] = RecvByte();
            if(RecvCount >= RECEIVE_MAX_BYTES)
            {
                RecvCount=0;
                for(i=0;i<RECEIVE_MAX_BYTES;i++)
                {
                    SendByte(RecvBuf[i]);
                }
            }
                
        }
    }
    
}

// timer interrupt service routine 
void Timer0IRQ ( void ) interrupt 1  a using  0
{
    fTimeouts=1;
}

 

Experimental phenomena:

 Since the baud rate has been garbled reasons do not recommend using an analog port

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

Guess you like

Origin www.cnblogs.com/xwtstudio/p/12602745.html