龙陵电工培训班,龙陵电工培训学校
龙陵电工培训班,龙陵电工培训学校简介:STC12C5A60S2单片机是一款功能比较强大的单片机,它拥有两个全双工串行通信接口,串口1的功能及操作与传统51单片机串行口相同;特殊的是STC12C5A60S2单片机内部
龙陵电工培训学校信息内容:,
STC12C5A60S2单片机是一款功能比较强大的单片机,它拥有两个全双工串行通信接口,串口1的功能及操作与传统51单片机串行口相同;特殊的是STC12C5A60S2单片机内部有一个独立波特率发生器,串口1可以使用定时器1作为波特率发生器,也可以使用独立波特率发生器作为波特率发生器;而串口2只能使用独立波特率发生器作为波特率发生器。
下面是一段双串口通信的程序:
/***********************************************************************时间:2012.11.24芯片:STC12C5A60S2晶振:22.1184MHz波特率:9600bps引脚定义:串行口1:发送————TxD/P3.1;接收————RxD/P3.0串行口2:发送————TxD2/P1.3;接收————RxD2/P1.2功能描述:STC12双串口通信(中断方式)当串行口1接收数据后,将此数据由串行口2发送出去当串行口2接收数据后,将此数据由串行口1发送出去***********************************************************************/#include#defineS2RI0x01//串口2接收中断请求标志位#defineS2TI0x02//串口2发送中断请求标志位unsignedcharflag1,flag2,temp1,temp2;/****************串行口初始化函数****************/voidInitUART(void){TMOD=0x20;//定时器1工作在方式28位自动重装SCON=0x50;//串口1工作在方式110位异步收发REN=1允许接收TH1=0xFA;//定时器1初值TL1=TH1;TR1=1;//定时器1开始计数EA=1;//开总中断ES=1;//开串口1中断S2CON=0x50;//串口2工作在方式110位异步收发S2REN=1允许接收BRT=0xFA;//独立波特率发生器初值AUXR=0x10;//BRTR=1独立波特率发生器开始计数IE2=0x01;//开串口2中断ES2=1}/****************串行口1发送****************/voidUART_1SendOneByte(unsignedcharc){SBUF=c;while(!TI);//若TI=0,在此等待TI=0;}/****************串行口2发送****************/voidUART_2SendOneByte(unsignedcharc){S2BUF=c;while(!(S2CON&S2TI));//若S2TI=0,在此等待S2CON&=~S2TI;//S2TI=0}/*****************主函数******************/voidmain(void){InitUART();//串行口初始化while(1){//如果串口1接收到数据,将此数据由串口2发送if(flag1==1){flag1=0;UART_2SendOneByte(temp1);}//如果串口2接收到数据,将此数据由串口1发送if(flag2==1){flag2=0;UART_2SendOneByte(temp2);}}}/************串行口1中断处理函数*************/voidUART_1Interrupt(void)interrupt4{if(RI==1){RI=0;flag1=1;temp1=SBUF;}}/************串行口2中断处理函数*************/voidUART_2Interrupt(void)interrupt8{if(S2CON&S2RI){S2CON&=~S2RI;flag2=1;temp2=S2BUF;}}12C5A60S2.h的头文件如下://--------------------------------------------------------------------------------//新一代1T8051系列单片机内核特殊功能寄存器C51CoreSFRs//76543210ResetValuesfrACC=0xE0;//Accumulator0000,0000sfrB=0xF0;//BRegister0000,0000sfrPSW=0xD0;//ProgramStatusWordCYACF0RS1RS0OVF1P0000,0000//-----------------------------------sbitCY=PSW^7;sbitAC=PSW^6;sbitF0=PSW^5;sbitRS1=PSW^4;sbitRS0=PSW^3;sbitOV=PSW^2;sbitP=PSW^0;//-----------------------------------sfrSP=0x81;//StackPointer0000,0111sfrDPL=0x82;//DataPointerLowByte0000,0000sfrDPH=0x83;//DataPointerHighByte0000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机系统管理特殊功能寄存器//76543210ResetValuesfrPCON=0x87;//PowerControlSMODSMOD0LVDFPOFGF1GF0PDIDL0001,0000//76543210ResetValuesfrAUXR=0x8E;//AuxiliaryRegisterT0x12T1x12UART_M0x6BRTRS2SMODBRTx12EXTRAMS1BRS0000,0000//-----------------------------------sfrAUXR1=0xA2;//AuxiliaryRegister1-PCA_P4SPI_P4S2_P4GF2ADRJ-DPS0000,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:通用标志位ADRJ:0,10位A/D转换结果的高8位放在ADC_RES寄存器,低2位放在ADC_RESL寄存器1,10位A/D转换结果的 高2位放在ADC_RES寄存器的低2位,低8位放在ADC_RESL寄存器DPS:0,使用缺省数据指针DPTR01,使用另一个数据指针DPTR1*///-----------------------------------sfrWAKE_CLKO=0x8F;//附加的SFRWAK1_CLKO/*76543210ResetValuePCAWAKEUPRXD_PIN_IET1_PIN_IET0_PIN_IELVD_WAKE_T1CLKOT0CLKO0000,0000Bb7-PCAWAKEUP:PCA中断可唤醒powerdown。b6-RXD_PIN_IE:当P3.0(RXD)下降沿置位RI时可唤醒powerdown(必须打开相应中断)。b5-T1_PIN_IE:当T1脚下降沿置位T1中断标志时可唤醒powerdown(必须打开相应中断)。b4-T0_PIN_IE:当T0脚下降沿置位T0中断标志时可唤醒powerdown(必须打开相应中断)。b3-LVD_WAKE:当CMPIN脚低电平置位LVD中断标志时可唤醒powerdown(必须打开相应中断)。b2-b1-T1CLKO:允许T1CKO(P3.5)脚输出T1溢出脉冲,Fck1=1/2T1溢出率b0-T0CLKO:允许T0CKO(P3.4)脚输出T0溢出脉冲,Fck0=1/2T1溢出率*///-----------------------------------sfrCLK_DIV=0x97;//ClockDivder-----CLKS2CLKS1CLKS0xxxx,x000//-----------------------------------sfrBUS_SPEED=0xA1;//Stretchregister--ALES1ALES0-RWS2RWS1RWS0xx10,x011/*ALES1andALES0:00:TheP0addresssetuptimeandholdtimetoALEnegativeedgeisoneclockcycle01:TheP0addresssetuptimeandholdtimetoALEnegativeedgeistwoclockcycles.10:TheP0addresssetuptimeandholdtimetoALEnegativeedgeisthreeclockcycles.(default)11:TheP0addresssetuptimeandholdtimetoALEnegativeedgeisfourclockcycles.RWS2,RWS1,RWS0:000:TheMOVXread/writepulseis1clockcycle.001:TheMOVXread/writepulseis2clockcycles.010:TheMOVXread/writepulseis3clockcycles.011:TheMOVXread/writepulseis4clockcycles.(default)100:TheMOVXread/writepulseis5clockcycles.101:TheMOVXread/writepulseis6clockcycles.110:TheMOVXread/writepulseis7clockcycles.111:TheMOVXread/writepulseis8clockcycles.*///--------------------------------------------------------------------------------//新一代1T8051系列单片机中断特殊功能寄存器//有的中断控制、中断标志位散布在其它特殊功能寄存器中,这些位在位地址中定义//其中有的位无位寻址能力,请参阅新一代1T8051系列单片机中文指南//76543210ResetValuesfrIE=0xA8;//中断控制寄存器EAELVDEADCESET1EX1ET0EX00x00,0000//-----------------------sbitEA=IE^7;sbitELVD=IE^6;//低压监测中断允许位sbitEADC=IE^5;//ADC中断允许位sbitES=IE^4;sbitET1=IE^3;sbitEX1=IE^2;sbitET0=IE^1;sbitEX0=IE^0;//-----------------------sfrIE2=0xAF;//AuxiliaryInterrupt------ESPIES20000,0000B//-----------------------//76543210ResetValuesfrIP=0xB8;//中断优先级低位PPCAPLVDPADCPSPT1PX1PT0PX00000,0000//--------sbitPPCA=IP^7;//PCA模块中断优先级sbitPLVD=IP^6;//低压监测中断优先级sbitPADC=IP^5;//ADC中断优先级sbitPS=IP^4;sbitPT1=IP^3;sbitPX1=IP^2;sbitPT0=IP^1;sbitPX0=IP^0;//-----------------------//76543210ResetValuesfrIPH=0xB7;//中断优先级高位PPCAHPLVDHPADCHPSHPT1HPX1HPT0HPX0H0000,0000sfrIP2=0xB5;//------PSPIPS2xxxx,xx00sfrIPH2=0xB6;//------PSPIHPS2Hxxxx,xx00//-----------------------//新一代1T8051系列单片机I/O口特殊功能寄存器//76543210ResetValuesfrP0=0x80;//8bitPort0P0.7P0.6P0.5P0.4P0.3P0.2P0.1P0.01111,1111sfrP0M0=0x94;//0000,0000sfrP0M1=0x93;//0000,0000sfrP1=0x90;//8bitPort1P1.7P1.6P1.5P1.4P1.3P1.2P1.1P1.01111,1111sfrP1M0=0x92;//0000,0000sfrP1M1=0x91;//0000,0000sfrP1ASF=0x9D;//P1analogspecialfunctionsfrP2=0xA0;//8bitPort2P2.7P2.6P2.5P2.4P2.3P2.2P2.1P2.01111,1111sfrP2M0=0x96;//0000,0000sfrP2M1=0x95;//0000,0000sfrP3=0xB0;//8bitPort3P3.7P3.6P3.5P3.4P3.3P3.2P3.1P3.01111,1111sfrP3M0=0xB2;//0000,0000sfrP3M1=0xB1;//0000,0000sfrP4=0xC0;//8bitPort4P4.7P4.6P4.5P4.4P4.3P4.2P4.1P4.01111,1111sfrP4M0=0xB4;//0000,0000sfrP4M1=0xB3;//0000,0000//76543210ResetValuesfrP4SW=0xBB;//Port-4switch-LVD_P4.6ALE_P4.5NA_P4.4----x000,xxxxsfrP5=0xC8;//8bitPort5----P5.3P5.2P5.1P5.0xxxx,1111sfrP5M0=0xCA;//0000,0000sfrP5M1=0xC9;//0000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机定时器特殊功能寄存器//76543210ResetValuesfrTCON=0x88;//T0/T1ControlTF1TR1TF0TR0IE1IT1IE0IT00000,0000//-----------------------------------sbitTF1=TCON^7;sbitTR1=TCON^6;sbitTF0=TCON^5;sbitTR0=TCON^4;sbitIE1=TCON^3;sbitIT1=TCON^2;sbitIE0=TCON^1;sbitIT0=TCON^0;//-----------------------------------sfrTMOD=0x89;//T0/T1ModesGATE1C/T1M1_1M1_0GATE0C/T0M0_1M0_00000,0000sfrTL0=0x8A;//T0LowByte0000,0000sfrTH0=0x8C;//T0HighByte0000,0000sfrTL1=0x8B;//T1LowByte0000,0000sfrTH1=0x8D;//T1HighByte0000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机串行口特殊功能寄存器//76543210ResetValuesfrSCON=0x98;//SerialControlSM0/FESM1SM2RENTB8RB8TIRI0000,0000//-----------------------------------sbitSM0=SCON^7;//SM0/FEsbitSM1=SCON^6;sbitSM2=SCON^5;sbitREN=SCON^4;sbitTB8=SCON^3;sbitRB8=SCON^2;sbitTI=SCON^1;sbitRI=SCON^0;//-----------------------------------sfrSBUF=0x99;//SerialDataBufferxxxx,xxxxsfrSADEN=0xB9;//SlaveAddressMask0000,0000sfrSADDR=0xA9;//SlaveAddress0000,0000//-----------------------------------//76543210ResetValuesfrS2CON=0x9A;//S2ControlS2SM0S2SM1S2SM2S2RENS2TB8S2RB8S2TIS2RI00000000BsfrS2BUF=0x9B;//S2SerialBufferxxxx,xxxxsfrBRT=0x9C;//S2Baud-RateTimer0000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机看门狗定时器特殊功能寄存器sfrWDT_CONTR=0xC1;//Watch-Dog-TimerControlregister//76543210ResetValue//WDT_FLAG-EN_WDTCLR_WDTIDLE_WDTPS2PS1PS0xx00,0000//-----------------------//--------------------------------------------------------------------------------//新一代1T8051系列单片机PCA/PWM特殊功能寄存器//76543210ResetValuesfrCCON=0xD8;//PCA控制寄存器。CFCR----CCF1CCF000xx,xx00//-----------------------sbitCF=CCON^7;//PCA计数器溢出标志,由硬件或软件置位,必须由软件清0。sbitCR=CCON^6;//1:允许PCA计数器计数,必须由软件清0。//-//-sbitCCF1=CCON^1;//PCA模块1中断标志,由硬件置位,必须由软件清0。sbitCCF0=CCON^0;//PCA模块0中断标志,由硬件置位,必须由软件清0。//-----------------------sfrCMOD=0xD9;//PCA工作模式寄存器。CIDL---CPS2CPS1CPS0ECF0xxx,x000/*CIDL:idle状态时PCA计数器是否继续计数,0:继续计数,1:停止计数。CPS2:PCA计数器脉冲源选择位2。CPS1:PCA计数器脉冲源选择位1。CPS0:PCA计数器脉冲源选择位0。CPS2CPS1CPS0000系统时钟频率fosc/12。001系统时钟频率fosc/2。010Timer0溢出。011由ECI/P3.4脚输入的外部时钟, 大fosc/2。100系统时钟频率,Fosc/1101系统时钟频率/4,Fosc/4110系统时钟频率/6,Fosc/6111系统时钟频率/8,Fosc/8ECF:PCA计数器溢出中断允许位,1--允许CF(CCON.7)产生中断。*///-----------------------sfrCL=0xE9;//PCA计数器低位0000,0000sfrCH=0xF9;//PCA计数器高位0000,0000//-----------------------//76543210ResetValuesfrCCAPM0=0xDA;//PCA模块0PWM寄存器-ECOM0CAPP0CAPN0MAT0TOG0PWM0ECCF0x000,0000sfrCCAPM1=0xDB;//PCA模块1PWM寄存器-ECOM1CAPP1CAPN1MAT1TOG1PWM1ECCF1x000,0000//ECOMn=1:允许比较功能。//CAPPn=1:允许上升沿触发捕捉功能。//CAPNn=1:允许下降沿触发捕捉功能。//MATn=1:当匹配情况发生时,允许CCON中的CCFn置位。//TOGn=1:当匹配情况发生时,CEXn将翻转。//PWMn=1:将CEXn设置为PWM输出。//ECCFn=1:允许CCON中的CCFn触发中断。//ECOMnCAPPnCAPNnMATnTOGnPWMnECCFn//00000000x00未启用任何功能。//x10000x0x2116位CEXn上升沿触发捕捉功能。//x01000x0x1116位CEXn下降沿触发捕捉功能。//x11000x0x3116位CEXn边沿(上、下沿)触发捕捉功能。//100100x0x4916位软件定时器。//100110x0x4d16位高速脉冲输出。//10000100x428位PWM。//ECOMnCAPPnCAPNnMATnTOGnPWMnECCFn//00000000x00无此操作//10000100x42普通8位PWM,无中断//11000110x63PWM输出由低变高可产生中断//10100110x53PWM输出由高变低可产生中断//11100110x73PWM输出由低变高或由高变低都可产生中断//-----------------------sfrCCAP0L=0xEA;//PCA模块0的捕捉/比较寄存器低8位。0000,0000sfrCCAP0H=0xFA;//PCA模块0的捕捉/比较寄存器高8位。0000,0000sfrCCAP1L=0xEB;//PCA模块1的捕捉/比较寄存器低8位。0000,0000sfrCCAP1H=0xFB;//PCA模块1的捕捉/比较寄存器高8位。0000,0000//-----------------------//76543210ResetValuesfrPCA_PWM0=0xF2;//PCA模块0PWM寄存器。------EPC0HEPC0Lxxxx,xx00sfrPCA_PWM1=0xF3;//PCA模块1PWM寄存器。------EPC1HEPC1Lxxxx,xx00//PCA_PWMn:76543210//------EPCnHEPCnL//B7-B2:保留//B1(EPCnH):在PWM模式下,与CCAPnH组成9位数。//B0(EPCnL):在PWM模式下,与CCAPnL组成9位数。//--------------------------------------------------------------------------------//新一代1T8051系列单片机ADC特殊功能寄存器//76543210ResetValuesfrADC_CONTR=0xBC;//A/D转换控制寄存器ADC_POWERSPEED1SPEED0ADC_FLAGADC_STARTCHS2CHS1CHS00000,0000sfrADC_RES=0xBD;//A/D转换结果高8位ADCV.9ADCV.8ADCV.7ADCV.6ADCV.5ADCV.4ADCV.3ADCV.20000,0000sfrADC_RESL=0xBE;//A/D转换结果低2位ADCV.1ADCV.00000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机SPI特殊功能寄存器//76543210ResetValuesfrSPCTL=0xCE;//SPIControlRegisterSSIGSPENDORDMSTRCPOLCPHASPR1SPR00000,0100sfrSPSTAT=0xCD;//SPIStatusRegisterSPIFWCOL------00xx,xxxxsfrSPDAT=0xCF;//SPIDataRegister0000,0000//--------------------------------------------------------------------------------//新一代1T8051系列单片机IAP/ISP特殊功能寄存器sfrIAP_DATA=0xC2;sfrIAP_ADDRH=0xC3;sfrIAP_ADDRL=0xC4;//76543210ResetValuesfrIAP_CMD=0xC5;//IAPModeTable0-----MS1MS00xxx,xx00sfrIAP_TRIG=0xC6;sfrIAP_CONTR=0xC7;//IAPControlRegisterIAPENSWBSSWRSTCFAIL-WT2WT1WT00000,x000//--------------------------------------------------------------------------------.(编辑:龙陵电工培训学校)