STC 51モノリシックシリアル通信実験


これからは高望みをしないで、着実にすべてのことをして、着実に単片機をマスターしなければなりません.
最初のブログとして、まず私が書いた51単片機シリアル通信プログラムについて話します.
私は強化版51単片機を使っています.型番はSTC 12 LE 5 A 60 S 2です.
1、クエリー方式、以下は私のソースコードです.
/*======================================
 :STC12LE5A60S2
 1, 1, 1 8 
Author:fighting
Date:2013-07-21
=======================================*/

#include 

sfr AUXR = 0x8e;

void uart_init(void)
{
    TMOD &= 0x0f;
    TMOD |= 0x20;//8-bit,auto-reload
    PCON &=0x7f;    //Don't double baund rate
    SCON = 0x50;    //8-bit data, changeable baund rate
    AUXR |= 0x40;   //Choose 1T mode
    AUXR &= 0xfe;   //UART 1 chooses timer 1 as baund rate generator
    TL1 = 0x98;     //in order to operate on 9600 when the clock is 32MHz
    TH1 = 0x98;
    ET1 = 0;
    TR1 = 1;
}

void main(void)
{
    unsigned char response;
    uart_init();
    while(1)
    {
		if(RI == 1)
		{
			RI = 0;
			response = SBUF;
			SBUF = response;
		}
    }
}

2、中断方式
/*======================================
 :STC12LE5A60S2
 1, 1, 1 8 
 
Author:fighting
Date:2013-07-21
=======================================*/

#include 

sfr AUXR = 0x8e;
unsigned char response;

void uart_init(void)
{
    TMOD &= 0x0f;
    TMOD |= 0x20;//8-bit,auto-reload
    PCON &=0x7f;    //Don't double baund rate
    SCON = 0x50;    //8-bit data, changeable baund rate
    AUXR |= 0x40;   //Choose 1T mode
    AUXR &= 0xfe;   //UART 1 chooses timer 1 as baund rate generator
    TL1 = 0x98;     //in order to operate on 9600 when the clock is 32MHz
    TH1 = 0x98;
    ET1 = 0;        //Disable timer 1 interrupt
    TR1 = 1;        //Turn on timer 1
    ES = 1;         //Enable serial interrupt
    EA = 1;         //Enable global interrupt
}

void serial() interrupt 4 using 3
{
    if(RI == 1)
    {
        RI = 0;
        response = SBUF;
		TI = 0;
		SBUF = response;
		while(TI == 0);
    }
}
void main(void)
{
    uart_init();
    while(1);
}