STC12C5A60S2 dual serial port communication

Publisher:cheng1984Latest update time:2019-05-21 Source: eefocusKeywords:STC12C5A60S2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The STC12C5A60S2 microcontroller is a powerful microcontroller. It has two full-duplex serial communication interfaces. The functions and operations of serial port 1 are the same as those of the traditional 51 microcontroller serial port. What is special is that there is an independent baud rate generator inside the STC12C5A60S2 microcontroller. Serial port 1 can use timer 1 as the baud rate generator, or use an independent baud rate generator as the baud rate generator; while serial port 2 can only use an independent baud rate generator as the baud rate generator.

The following is a dual serial port communication program:



/****************************************************** ************************

Date: 2012.11.24

Chip: STC12C5A60S2

Crystal: 22.1184MHz  Baud rate: 9600bps

Pin definition: Serial port 1: Send————TxD/P3.1; Receive————RxD/P3.0

    Serial port 2: Send————TxD2/P1.3; Receive————RxD2/P1.2

Function description: STC12 dual serial port communication (interrupt mode)

          When serial port 1 receives data, it sends the data out through serial port 2

          When serial port 2 receives data, it sends the data out through serial port 1

*************************************************** *********************/

#include

#define S2RI 0x01 //Serial port 2 receive interrupt request flag

#define S2TI 0x02 //Serial port 2 sends interrupt request flag

unsigned char flag1,flag2,temp1,temp2;

/****************Serial port initialization function****************/

void InitUART(void)

{

    TMOD = 0x20; //Timer 1 works in mode 2 8-bit automatic reload

    SCON = 0x50; //Serial port 1 works in mode 1 10-bit asynchronous transmission REN = 1 allows reception

    TH1 = 0xFA;    //Timer 1 initial value

    TL1 = TH1;

    TR1 = 1;    //Timer 1 starts counting

    EA =1; //Open the general interrupt

    ES =1; //Open serial port 1 interrupt

 

    S2CON = 0x50; //Serial port 2 works in mode 1 10-bit asynchronous transmission S2REN = 1 allows reception

    BRT = 0xFA;    //Independent baud rate generator initial value

    AUXR = 0x10; //BRTR=1 independent baud rate generator starts counting

    IE2 = 0x01; //Open serial port 2 interrupt ES2 = 1

}

/********************Serial port 1 sends********************/

void UART_1SendOneByte(unsigned char c)

{

    SBUF = c;

    while(!TI);    //If TI=0, wait here

    TI = 0;  

}

/********************Serial port 2 sends********************/

void UART_2SendOneByte(unsigned char c)

{

    S2BUF = c;

    while(!(S2CON&S2TI)); //If S2TI=0, wait here

    S2CON&=~S2TI;   //S2TI=0

}

/*********************Main function******************/

void main(void)

{

    InitUART(); //Serial port initialization

while(1)

{

//If serial port 1 receives data, send this data through serial port 2

if(flag1==1)

{

flag1=0;

UART_2SendOneByte(temp1);

}

//If serial port 2 receives data, send this data from serial port 1

if(flag2==1)

{

flag2=0;

UART_2SendOneByte(temp2);

}

}

}

/************Serial port 1 interrupt processing function****************/

void UART_1Interrupt(void) interrupt 4

{

if(RI==1)

{

RI=0;

flag1=1;

temp1=SBUF;

}

}

/************Serial port 2 interrupt processing function*************/

void UART_2Interrupt(void) interrupt 8

{

if(S2CON&S2RI)

{

S2CON&=~S2RI;

flag2=1;

temp2=S2BUF;

}


The header file of 12C5A60S2.h is as follows:

//------------------------------------------------ --------------------------------

//New generation 1T 8051 series microcontroller core special function registers C51 Core SFRs

// 7 6 5 4 3 2 1 0 Reset Value

sfr ACC = 0xE0; //Accumulator 0000,0000

sfr B = 0xF0; //B Register 0000,0000

sfr PSW = 0xD0; //Program Status Word CY AC F0 RS1 RS0 OV F1 P 0000,0000

//----------------------------------

sbit CY = PSW^7;

sbit AC = PSW^6;

sbit F0 = PSW^5;

sbit RS1 = PSW^4;

sbit RS0 = PSW^3;

sbit OV = PSW^2;

sbit P = PSW^0;

//----------------------------------

sfr SP = 0x81; //Stack Pointer 0000,0111

sfr DPL = 0x82; //Data Pointer Low Byte 0000,0000

sfr DPH = 0x83; //Data Pointer High Byte 0000,0000

//------------------------------------------------ --------------------------------

// New generation 1T 8051 series MCU system management special function register

// 7 6 5 4 3 2 1 0 Reset Value

sfr PCON = 0x87; //Power Control SMOD SMOD0 LVDF POF GF1 GF0 PD IDL 0001,0000

// 7 6 5 4 3 2 1 0 Reset Value

sfr AUXR = 0x8E; //Auxiliary Register T0x12 T1x12 UART_M0x6 BRTR S2SMOD BRTx12 EXTRAM S1BRS 0000,0000

//----------------------------------

sfr AUXR1 = 0xA2; //Auxiliary Register 1 - PCA_P4 SPI_P4 S2_P4 GF2 ADRJ - DPS 0000,0000

/*

PCA_P4:

    0, default PCA is at P1 port

    1. PCA/PWM switches from P1 to P4: ECI switches from P1.2 to P4.1.

                                   PCA0/PWM0 switches from P1.3 to P4.2

                                   PCA1/PWM1 switches from P1.4 to P4.3

SPI_P4:

    0, default SPI is on P1 port

    1. SPI switches from P1 to P4: SPICLK switches from P1.7 to P4.3

                               MISO switches from P1.6 to P4.2

                               MOSI switches from P1.5 to P4.1

                               SS switches from P1.4 to P4.0

S2_P4: 

    0, default UART2 is on P1 port

    1. UART2 switches from P1 to P4: TxD2 switches from P1.3 to P4.3

                                 RxD2 switches from P1.2 to P4.2

GF2: General flag bit

ADRJ:

    0, The high 8 bits of the 10-bit A/D conversion result are placed in the ADC_RES register, and the low 2 bits are placed in the ADC_RESL register

    1. The highest 2 bits of the 10-bit A/D conversion result are placed in the lower 2 bits of the ADC_RES register, and the lower 8 bits are placed in the ADC_RESL register.

DPS: 0, use the default data pointer DPTR0

     1. Use another data pointer DPTR1

*/

//----------------------------------

sfr WAKE_CLKO = 0x8F; //Additional SFR WAK1_CLKO

/*

      7 6 5 4 3 2 1 0 Reset Value

   PCAWAKEUP RXD_PIN_IE T1_PIN_IE T0_PIN_IE LVD_WAKE _ T1CLKO T0CLKO 0000,0000B

b7 - PCAWAKEUP : PCA interrupt can wake up powerdown.

b6 - RXD_PIN_IE : When the falling edge of P3.0 (RXD) sets RI, it can wake up the powerdown (the corresponding interrupt must be enabled).

b5 - T1_PIN_IE : When the falling edge of T1 pin sets T1 interrupt flag, it can wake up the powerdown (the corresponding interrupt must be enabled).

b4 - T0_PIN_IE : When the falling edge of T0 pin sets T0 interrupt flag, it can wake up the powerdown (the corresponding interrupt must be enabled).

b3 - LVD_WAKE : When the CMPIN pin is low, the LVD interrupt flag is set to wake up the powerdown (the corresponding interrupt must be enabled).

b2 - 

b1 - T1CLKO: Enable T1CKO (P3.5) pin to output T1 overflow pulse, Fck1 = 1/2 T1 overflow rate

b0 - T0CLKO : Enable T0CKO (P3.4) pin to output T0 overflow pulse, Fck0 = 1/2 T1 overflow rate

*/

//----------------------------------

sfr CLK_DIV = 0x97; //Clock Divder - - - - - CLKS2 CLKS1 CLKS0 xxxx,x000

//----------------------------------

sfr BUS_SPEED = 0xA1; //Stretch register - - ALES1 ALES0 - RWS2 RWS1 RWS0 xx10,x011

/*

ALES1 and ALES0:

00: The P0 address setup time and hold time to ALE negative edge is one clock cycle

01 : The P0 address setup time and hold time to ALE negative edge are two clock cycles.

10: The P0 address setup time and hold time to ALE negative edge is three clock cycles. (default)

11: The P0 address setup time and hold time to ALE negative edge is four clock cycles.

RWS2, RWS1, RWS0:

  000 : The MOVX read/write pulse is 1 clock cycle. 

  001 : The MOVX read/write pulse is 2 clock cycles.

  010 : The MOVX read/write pulse is 3 clock cycles.

  011 : The MOVX read/write pulse is 4 clock cycles. (default)

  100: The MOVX read/write pulse is 5 clock cycles.

  101 : The MOVX read/write pulse is 6 clock cycles.

  110 : The MOVX read/write pulse is 7 clock cycles.

  111: The MOVX read/write pulse is 8 clock cycles.

*/

//------------------------------------------------ --------------------------------

//New generation 1T 8051 series microcontroller interrupt special function register

//Some interrupt control and interrupt flag bits are scattered in other special function registers. These bits are defined in the bit address

//Some of the bits have no bit addressing capability, please refer to the Chinese guide for the new generation 1T 8051 series microcontrollers

// 7 6 5 4 3 2 1 0 Reset Value

sfr IE = 0xA8; //Interrupt control register EA ELVD EADC ES ET1 EX1 ET0 EX0 0x00,0000

//-----------------------

sbit EA = IE^7;

sbit ELVD = IE^6; //Low voltage monitoring interrupt enable bit

sbit EADC = IE^5; //ADC interrupt enable bit

sbit ES = IE^4;

sbit ET1 = IE^3;

sbit EX1 = IE^2;

sbit ET0 = IE^1;

sbit EX0 = IE^0;

//-----------------------

sfr IE2 = 0xAF; //Auxiliary Interrupt - - - - - - ESPI ES2 0000,0000B

//-----------------------

// 7 6 5 4 3 2 1 0 Reset Value

sfr IP = 0xB8; //Interrupt priority low PPCA PLVD PADC PS PT1 PX1 PT0 PX0 0000,0000

//--------

sbit PPCA = IP^7; //PCA module interrupt priority

sbit PLVD = IP^6; //low voltage monitoring interrupt priority

sbit PADC = IP^5; //ADC interrupt priority

sbit PS = IP^4;

sbit PT1 = IP^3;

sbit PX1 = IP^2;

sbit PT0 = IP^1;

sbit PX0 = IP^0;

//-----------------------

// 7 6 5 4 3 2 1 0 Reset Value

sfr IPH = 0xB7; //Interrupt priority high PPCAH PLVDH PADCH PSH PT1H PX1H PT0H PX0H 0000,0000

sfr IP2 = 0xB5; // - - - - - - PSPI PS2 xxxx,xx00

sfr IPH2 = 0xB6; // - - - - - - PSPIH PS2H xxxx,xx00

//-----------------------

//New generation 1T 8051 series MCU I/O port special function register

// 7 6 5 4 3 2 1 0 Reset Value

sfr P0 = 0x80; //8 bitPort0 P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0 1111,1111

sfr P0M0 = 0x94; // 0000,0000

sfr P0M1 = 0x93; // 0000,0000

sfr P1 = 0x90; //8 bitPort1 P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0 1111,1111

sfr P1M0 = 0x92; // 0000,0000

sfr P1M1 = 0x91; // 0000,0000

sfr P1ASF = 0x9D; //P1 analog special function

sfr P2 = 0xA0; //8 bitPort2 P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0 1111,1111

sfr P2M0 = 0x96; // 0000,0000

sfr P2M1 = 0x95; // 0000,0000

sfr P3 = 0xB0; //8 bitPort3 P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0 1111,1111

sfr P3M0 = 0xB2; // 0000,0000

sfr P3M1 = 0xB1; // 0000,0000

sfr P4 = 0xC0; //8 bitPort4 P4.7 P4.6 P4.5 P4.4 P4.3 P4.2 P4.1 P4.0 1111,1111

[1] [2]
Keywords:STC12C5A60S2 Reference address:STC12C5A60S2 dual serial port communication

Previous article:STC12C5A60S2 AD conversion details
Next article:STC12C5A60S2 serial port interrupt receiving program

Latest Microcontroller Articles
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号