STC12C5A60S2單片機(jī)雙串口通信
下面是一段雙串口通信的程序:
本文引用地址:http://m.butianyuan.cn/article/201611/323566.htm/***********************************************************************時間:2012.11.24芯片:STC12C5A60S2晶振:22.1184MHz 波特率:9600bps引腳定義:串行口1: 發(fā)送————TxD/P3.1; 接收————RxD/P3.0串行口2: 發(fā)送————TxD2/P1.3;接收————RxD2/P1.2功能描述:STC12雙串口通信(中斷方式)當(dāng)串行口1接收數(shù)據(jù)后,將此數(shù)據(jù)由串行口2發(fā)送出去當(dāng)串行口2接收數(shù)據(jù)后,將此數(shù)據(jù)由串行口1發(fā)送出去***********************************************************************/#include<12C5A60S2.h>#define S2RI 0x01 //串口2接收中斷請求標(biāo)志位#define S2TI 0x02 //串口2發(fā)送中斷請求標(biāo)志位unsigned char flag1,flag2,temp1,temp2;/****************串行口初始化函數(shù)****************/void InitUART(void){TMOD = 0x20; //定時器1工作在方式2 8位自動重裝SCON = 0x50; //串口1工作在方式1 10位異步收發(fā) REN=1允許接收TH1 = 0xFA; //定時器1初值TL1 = TH1;TR1 = 1; //定時器1開始計數(shù)EA =1; //開總中斷ES =1; //開串口1中斷S2CON = 0x50; //串口2工作在方式1 10位異步收發(fā) S2REN=1允許接收BRT = 0xFA; //獨(dú)立波特率發(fā)生器初值A(chǔ)UXR = 0x10; //BRTR=1 獨(dú)立波特率發(fā)生器開始計數(shù)IE2 =0x01; //開串口2中斷 ES2=1}/****************串行口1發(fā)送****************/void UART_1SendOneByte(unsigned char c){SBUF = c;while(!TI); //若TI=0,在此等待TI = 0; }/****************串行口2發(fā)送****************/void UART_2SendOneByte(unsigned char c){S2BUF = c;while(!(S2CON&S2TI)); //若S2TI=0,在此等待S2CON&=~S2TI; //S2TI=0}/*****************主函數(shù)******************/void main(void){InitUART(); //串行口初始化while(1){//如果串口1接收到數(shù)據(jù),將此數(shù)據(jù)由串口2發(fā)送if(flag1==1){flag1=0;UART_2SendOneByte(temp1);}//如果串口2接收到數(shù)據(jù),將此數(shù)據(jù)由串口1發(fā)送if(flag2==1){flag2=0;UART_1SendOneByte(temp2);}}}/************串行口1中斷處理函數(shù)*************/void UART_1Interrupt(void) interrupt 4{if(RI==1){RI=0;flag1=1;temp1=SBUF;}}/************串行口2中斷處理函數(shù)*************/void UART_2Interrupt(void) interrupt 8{if(S2CON&S2RI){S2CON&=~S2RI;flag2=1;temp2=S2BUF;} }12C5A60S2.h的頭文件如下://--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機(jī)內(nèi)核特殊功能寄存器 C51 Core SFRs// 7 6 5 4 3 2 1 0 Reset Valuesfr ACC = 0xE0; //Accumulator 0000,0000sfr B = 0xF0; //B Register 0000,0000sfr 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,0111sfr DPL = 0x82; //Data Pointer Low Byte 0000,0000sfr DPH = 0x83; //Data Pointer High Byte 0000,0000//--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機(jī)系統(tǒng)管理特殊功能寄存器// 7 6 5 4 3 2 1 0 Reset Valuesfr PCON = 0x87; //Power Control SMOD SMOD0 LVDF POF GF1 GF0 PD IDL 0001,0000// 7 6 5 4 3 2 1 0 Reset Valuesfr 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, 缺省PCA 在P1 口1,PCA/PWM 從P1 口切換到P4 口: ECI 從P1.2 切換到P4.1 口,PCA0/PWM0 從P1.3 切換到P4.2 口PCA1/PWM1 從P1.4 切換到P4.3 口SPI_P4:0, 缺省SPI 在P1 口1,SPI 從P1 口切換到P4 口: SPICLK 從P1.7 切換到P4.3 口MISO 從P1.6 切換到P4.2 口MOSI 從P1.5 切換到P4.1 口SS 從P1.4 切換到P4.0 口S2_P4: 0, 缺省UART2 在P1 口1,UART2 從P1 口切換到P4 口: TxD2 從P1.3 切換到P4.3 口RxD2 從P1.2 切換到P4.2 口GF2: 通用標(biāo)志位ADRJ:0, 10 位A/D 轉(zhuǎn)換結(jié)果的高8 位放在ADC_RES 寄存器, 低2 位放在ADC_RESL 寄存器1,10 位A/D 轉(zhuǎn)換結(jié)果的最高2 位放在ADC_RES 寄存器的低2 位, 低8 位放在ADC_RESL 寄存器DPS: 0, 使用缺省數(shù)據(jù)指針DPTR01,使用另一個數(shù)據(jù)指針DPTR1*///-----------------------------------sfr WAKE_CLKO = 0x8F; //附加的 SFR WAK1_CLKO/*7 6 5 4 3 2 1 0 Reset ValuePCAWAKEUP RXD_PIN_IE T1_PIN_IE T0_PIN_IE LVD_WAKE _ T1CLKO T0CLKO 0000,0000Bb7 - PCAWAKEUP : PCA 中斷可喚醒 powerdown。b6 - RXD_PIN_IE : 當(dāng) P3.0(RXD) 下降沿置位 RI 時可喚醒 powerdown(必須打開相應(yīng)中斷)。b5 - T1_PIN_IE : 當(dāng) T1 腳下降沿置位 T1 中斷標(biāo)志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b4 - T0_PIN_IE : 當(dāng) T0 腳下降沿置位 T0 中斷標(biāo)志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b3 - LVD_WAKE : 當(dāng) CMPIN 腳低電平置位 LVD 中斷標(biāo)志時可喚醒 powerdown(必須打開相應(yīng)中斷)。b2 - b1 - T1CLKO : 允許 T1CKO(P3.5) 腳輸出 T1 溢出脈沖,F(xiàn)ck1 = 1/2 T1 溢出率b0 - T0CLKO : 允許 T0CKO(P3.4) 腳輸出 T0 溢出脈沖,F(xiàn)ck0 = 1/2 T1 溢出率*///-----------------------------------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 cycle01 : The P0 address setup time and hold time to ALE negative edge is 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.*///--------------------------------------------------------------------------------//新一代 1T 8051系列 單片機(jī)中斷特殊功能寄存器//有的中斷控制、中斷標(biāo)志位散布在其它特殊功能寄存器中,這些位在位地址中定義//其中有的位無位尋址能力,請參閱 新一代 1T 8051系列 單片機(jī)中文指南// 7 6 5 4 3 2 1 0 Reset Valuesfr IE = 0xA8; //中斷控制寄存器 EA ELVD EADC ES ET1 EX1 ET0 EX0 0x00,0000//-----------------------sbit EA = IE^7;sbit ELVD = IE^6; //低壓監(jiān)測中斷允許位sbit EADC = IE^5; //ADC 中斷允許位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 Valuesfr IP = 0xB8; //中斷優(yōu)先級低位 PPCA PLVD PADC PS PT1 PX1 PT0 PX0 0000,0000//--------sbit PPCA = IP^7; //PCA 模塊中斷優(yōu)先級sbit PLVD = IP^6; //低壓監(jiān)測中斷優(yōu)先級sbit PADC = IP^5; //ADC 中斷優(yōu)先級sbit PS = IP^4;sbit PT1 = IP^3;sbit PX1 = IP^2;sbit PT0 = IP^1;sbit PX0 = IP^0;//-----------------------
評論