79F162_32 UARTnkz.docx

上传人:zhang****gqing 文档编号:62656786 上传时间:2022-11-22 格式:DOCX 页数:6 大小:17.58KB
返回 下载 相关 举报
79F162_32 UARTnkz.docx_第1页
第1页 / 共6页
79F162_32 UARTnkz.docx_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《79F162_32 UARTnkz.docx》由会员分享,可在线阅读,更多相关《79F162_32 UARTnkz.docx(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、79F162/32 UART 驱动 例子双UART功能Uart0_Txd-P3.1口Uart0_Rxd-P3.0 口Uart1_IRTxd-P2.7口Uart1_IRRxd-P2.6口两路UART,其中Uart1内部有红外硬件载波发生器,可以从Uart1_IRTxd直接发射调制的UART红外载波信号79F32系统寄存器#ifndef_CPU32_H#define_CPU32_HsfrACC=0xE0;sfrB=0xF0;sfrCRE=0xF1;/*C register*/sfrPSW=0xD0;sfrSP=0x81;sfrDPL=0x82;sfrDPH=0x83;sfrDPL1=0x84;/*

2、Data pointer2 Low byte*/sfrDPH1=0x85;/*Data pointer2 High byte*/sfrINSCON=0x86;/*Data pointer select*/*bit0 - 0: DPTR*bit0 - 1: DPTR 1*bit2 - 0: 8 bit mode = (A*B) = B(High Byte) (CA)*B= C(High Byte)16+ B(Middle Byte) (A/B) = A(Quotient low byte) + B(Remainer)*bit3 - 1: 16 bit mode= (CA/B)= C(Quotie

3、nt High Byte)8+ A(Quotient Low Byte) + B(Remainer)*/ sfrXPAGE=0xF7;/*Memory page*/sfrPCON=0x87;/*power control*/sfrSUSLO=0x8E;/*suspend mode control*/sfrLPDCON=0xB3;/*LPD control*/sfrIB_CLK0=0xF9;/* flash programming clock register 0 */sfrIB_CLK1=0xFA;/* flash programming clock register 1 */sfrIB_OF

4、FSET = 0xFB;/* Low byte offset of flash memory for programming */sfrIB_DATA= 0xFC;/* Data register for programming flash memory */sfrFOCON=0xFD;/*flash option control*/sfrIB_CON1=0xF2;/*Flash memory control register 1*/sfrIB_CON2=0xF3;/*Flash memory control register 2*/sfrIB_CON3=0xF4;/*Flash memory

5、 control register 3*/sfrIB_CON4=0xF5;/*Flash memory control register 4*/sfrIB_CON5=0xF6;/*Flash memory control register 5*/sfrWDT=0xB1;/*watchdog timer control*/sfrSCM=0xB2;/*system clock select and monitor*/sfrIEN0=0xA8;/*External interrupt enable control 1*/sfrIEN1=0xA9;/*External interrupt enable

6、 control 2*/sfrIPL0=0xB8;/*Interrupt priority control Low 0*/sfrIPH0=0xB4;/*Interrupt priority control High 0*/sfrIPL1=0xB9;/*Interrupt priority control Low 1*/sfrIPH1=0xB5;/*Interrupt priority control High 1*/sfrP0=0x80;sfrP1=0x90;sfrP2=0xA0;sfrP3=0xB0;sfrP4=0xC0;sfrP0CR=0xE1;/*port 0 input/output

7、direction control*/sfrP1CR=0xE2;/*port 1 input/output direction control*/sfrP2CR=0xE3;/*port 2 input/output direction control*/sfrP3CR=0xE4;/*port 3 input/output direction control*/sfrP4CR=0xE5;sfrPP0CR=0xE9;/*Internal pull-high enable for port0*/sfrPP1CR=0xEA;/*Internal pull-high enable for port1*/

8、sfrPP2CR=0xEB;/*Internal pull-high enable for port2*/sfrPP3CR=0xEC;/*Internal pull-high enable for port3*/sfrPP4CR=0xED;sfrTCON=0x88;/*Timer/Counter 0 and 1 control register*/sfrTMOD=0x89;/*Timer/Counter 0 and 1 modes*/sfrT0L=0x8A;/*Timer/Counter 0 low byte*/sfrT0H=0x8C;/*Timer/Counter 0 high byte*/

9、sfrT1L=0x8B;/*Timer/Counter 1 low byte*/sfrT1H=0x8D;/*Timer/Counter 1 high byte*/sfrT2CON=0xC8;/*Timer/Counter 2 control*/sfrT2MOD=0xC9;/*Timer/Counter 2 mode*/sfrRCAP2L =0xCA;/*Timer/Counter 2 reload/capture low byte*/sfrRCAP2H =0xCB;/*Timer/Counter 2 reload/capture high byte*/sfrT2L=0xCC;sfrT2H=0x

10、CD;sfrSCON=0x98;/*serial control*/sfrSBUF=0x99;/*serial data buffer*/sfrSADDR=0x9A;/*slave address*/sfrSADEN=0x9B;/*slave address mask*/sfrSCON1=0xD8;/*serial 1 control*/sfrSBUF1=0xD9;sfrSADDR1=0xDA;sfrSADEN1=0xDB;sfrIRCON=0xA1;/*IR control*/sfrSPCON=0xF8;/*SPI control*/sfrSPSTA=0xA2;/*SPI status*/s

11、frSPDAT=0xA3;/*SPI data*/sfrI2CCON=0xA6;sfrI2CDAT=0xA7;sfrADCON=0x93;/*ADC control*/sfrADCT=0x94;/*ADC time select*/sfrADCH=0x95;/*ADC configuration*/sfrADDL=0x96;/*ADC data buffer low nibble*/sfrADDH=0x97;sfrLCDCON=0xAB;/*LCD control*/sfrLCDCLK=0xAC;sfrP1SS=0xAD;/*P1 or segment select*/sfrP0SS=0xAE

12、;/*P0 or segment select*/sfrPLCON=0xAF;/*PWM LCD control*/sfrRTCCON=0xBE;/*RTC control*/sfrRTCDAT=0xBF;/*RTC calibration data*/sfrSECOND=0xC1;/*second & half second*/sfrMINUTE=0xC2;sfrHOUR=0xC3;sfrDAY=0xC4;sfrMONTH=0xC5;sfrYEAR=0xC6;sfrWEEK=0xC7;sfrPWMEN=0xCF;/*PWM timer enable*/sfrPWMLO=0xE7;/*PWM

13、clock*/sfrPWMC=0xD2;sfrPWMPL=0xD3;sfrPWMPH=0xD4;sfrPWMDL=0xD5;sfrPWMDH=0xD6;sfrPWMDT=0xD1;/*PWM dead time control*/sfrEXF0=0xE8;/*external interrupt 2 control*/*Bitaddressable */sbitP0_0=P00;sbitP0_1=P01;sbitP0_2=P02;sbitP0_3=P03;sbitP0_4=P04;sbitP0_5=P05;sbitP0_6=P06;sbitP0_7=P07;sbitP1_0=P10;sbitP

14、1_1=P11;sbitP1_2=P12;sbitP1_3=P13;sbitP1_4=P14;sbitP1_5=P15;sbitP1_6=P16;sbitP1_7=P17;sbitP2_0=P20;sbitP2_1=P21;sbitP2_2=P22;sbitP2_3=P23;sbitP2_4=P24;sbitP2_5=P25;sbitP2_6=P26;sbitP2_7=P27;sbitP3_0=P30;sbitP3_1=P31;sbitP3_2=P32;sbitP3_3=P33;sbitP3_4=P34;sbitP3_5=P35;sbitP3_6=P36;sbitP3_7=P37;sbitP4

15、_0=P40;sbitP4_1=P41;sbitP4_2=P42;sbitP4_3=P43;sbitP4_4=P44;sbitP4_5=P45;sbitP4_6=P46;sbitP4_7=P47;sbit CY = 0xD7;/* carry bit */sbit AC = 0xD6;/*BCD code carry bit*/sbit F0 = 0xD5;/*The F0 flag is used by several arithmetic library routines. */sbit RS1 = 0xD4;/*RS0(1) select the current work registe

16、r set*/sbit RS0 = 0xD3;sbit OV = 0xD2;/*calculate result overflow*/sbitF1=0xD1;/*The UD/F1 flag is not used by any of the Keil development tools and may be used by your software/hardware.*/sbit P = 0xD0;/*ACC odd/Even check bit*/sbit TF1 = 0x8F;/* Timer 1 overflow flag bit*/sbit TR1 = 0x8E;/*Timer 1

17、 start /stop control bit*/sbit TF0 = 0x8D;/*Timer 0 overflow flag bit*/sbit TR0 = 0x8C;/*Timer 0 start /stop control bit*/sbit IE1 = 0x8B;/*External interrupt 1 request flag*/sbit IT1 = 0x8A;/*External interrupt 1 trigger mode: 0- level trigger;1- edge trigger*/sbit IE0 = 0x89;/*External interrupt 0

18、 request flag*/sbit IT0 = 0x88;/*External interrupt 0 trigger mode: 0- level trigger;1- edge trigger*/sbit EA = IEN07;/0xAF;/* all interrupt enable bit*/sbitEADC=0xAE;/*ADC interrupt enable bit*/sbitET2=0xAD;/*Timer 2 interrupt enable bit*/sbit ES0 = 0xAC;/*EUART 0 interrupt enable bit*/sbit ET1 = 0

19、xAB;/*Timer 1 interrupt enable bit*/sbit EX1 = 0xAA;/*External interrupt 1 enable bit */sbit ET0 = 0xA9;/*Timer 0 interrupt enable bit*/sbit EX0 = 0xA8;/*External 0 interrupt enable bit*/sbit SM0 = 0x9F;/* select UART working mode*/sbit SM1 = 0x9E;/*select UART working mode*/sbit SM2 = 0x9D;/*0:sign

20、al machine point to signal machine; 1:N point N */sbit REN = 0x9C;/*0:disable to receive; 1: enable to receive*/sbit TB8 = 0x9B;/*Transmit the 9th bit data*/sbit RB8 = 0x9A;/*Receive the 9th bit data*/sbit TI = 0x99;/*Transmit data interrupt flag*/sbit RI = 0x98;/*receive data interrupt flag*/sbitSM

21、10=0xDF;sbitSM11=0xDE;sbitSM12=0xDD;sbitREN1=0xDC;sbitTB81=0xDB;sbitRB81=0xDA;sbitTI1=0xD9;sbitRI1=0xD8;sbitTF2 =0xCF;/*T2CON*/sbitEXF2=0xCE;sbitRCLK=0xCD;sbitTCLK=0xCC;sbitEXEN2=0xCB;sbitTR2=0xCA;sbitCT2=0xC9;sbitCPRL2=0xC8;sbitPADCL=0xAE;sbitPT2L=0xAD;sbitPSL=0xAC;sbitPT1L=0xAB;sbitPX1L=0xAA;sbitP

22、T0L=0xA9;sbitPX0L=0xA8;sbitSPEN=0xFF;/*SPSTA*/sbitSPIF=0xFE;sbitMODF=0xFD;sbitWCOL=0xFC;sbitRXOV=0xFB;sbitIE2=0xE9;/*EXF0*/sbitIT2=0xE8;#endifUART的初始化#defineBAUDRATE_6000/baud rate index value set:#defineBAUDRATE_12001#defineBAUDRATE_24002#defineBAUDRATE_48003#defineBAUDRATE_96004#defineFSYSCLK80000

23、00/系统跑8M时钟#defineBAUDRATE_TIME_600(65536-FSYSCLK/32/600)/Set BaudRate with Timer 2 ,Mode 2.#defineBAUDRATE_TIME_1200(65536-FSYSCLK/32/1200)/Set BaudRate with Timer 2 ,Mode 2.#defineBAUDRATE_TIME_2400(65536-FSYSCLK/32/2400)/Set BaudRate with Timer 2 ,Mode 2.#defineBAUDRATE_TIME_4800(65536-FSYSCLK/32/

24、4800)/Set BaudRate with Timer 2 ,Mode 2.#defineBAUDRATE_TIME_9600(65536-FSYSCLK/32/9600)/Set BaudRate with Timer 2 ,Mode 2.#defineBAUDRATE_TIME_19200(65536-FSYSCLK/32/19200)/Set BaudRate with Timer 2 ,Mode 2.#defineOpCurrentUARTSpeedBAUDRATE_1200/default selection is 1200BPS.voidSetUARTMode3(void)un

25、signed intTimer2Value ; #if (OpCurrentUARTSpeed = BAUDRATE_9600)Timer2Value=BAUDRATE_TIME_9600; #elif (OpCurrentUARTSpeed = BAUDRATE_1200)Timer2Value=BAUDRATE_TIME_1200; #elif (OpCurrentUARTSpeed = BAUDRATE_2400)Timer2Value=BAUDRATE_TIME_2400; #elif (OpCurrentUARTSpeed = BAUDRATE_4800)Timer2Value=BA

26、UDRATE_TIME_4800; #elif (OpCurrentUARTSpeed = BAUDRATE_9600)Timer2Value=BAUDRATE_TIME_9600; #else Timer2Value=BAUDRATE_TIME_19200; #endifT2MOD=Bin(00000000);/Set Timer 2 Baud-Rate generator mode.RCAP2L=LOBYTE(Timer2Value);T2L=LOBYTE(Timer2Value);RCAP2H=HIBYTE(Timer2Value);T2H=HIBYTE(Timer2Value);T2C

27、ON=Bin(00110100);/Timer 2 is used as EUART clock source,and has autoload function.SCON=Bin(11010000);/select mode 3 ,enable receive,SM2=0,TB8=1.SCON1=Bin(11010000);PCON=Bin(00000000);IRCON =128+ 104;/Bit7=1允许红外载波功能,载波频率38K采用Timer2作为UART载波基准时钟,程序跑8M时钟,UART0和UART1设置为1200bps,UART1打开内部38k红外载波发生器,UART模式3

28、,开始位数据位基偶位+停止位UART中断UART0 中断允许位是IEN0的Bit4,设置为1就开启UART0中断UART1 中断允许位是IEN1的Bit2,设置为1就开启UART1中断/ Bit7Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 /IEN0 EA EADC ET2 ES0 ET1 EX1 ET0 EX0 /IEN1 EFCOF EI2C EPWM ESCM EHSEC ES1 EX2 ESPI IEN0=Bin(00010000);IEN1=Bin(00000100);EA=1;/开全局中断voidEUART0_ISP(void) interrupt 4EA

29、=0;/disable all interruptif(TI)/transmit work end event.TI=0;ACC=gbUartBuffgbUartLen;/初始化被发送的数据TB81=P;/初始化校验位SBUF1=ACC;./指向下一个数据if(RI)/receive one byte event.ACC=SBUF1;if( RB81= P)/ Even check!/校验正确,保存数据else/校验位错误,错误处理EA=1;voidEUART1_ISP(void) interrupt 9EA=0;/disable all interruptif(TI1)/transmit work end event.TI1 = 0;if(RI1)/receive one byte event.EA=1;/enable all interrupt如何发送数据:开始从UART0发送数据,例如先发送一个0xFESBUF=0xFE;发送数据后,将会发生UART0中断,然后在中断后继续发送后续数据就可以了如何接收数据:允许接收REN = 1;/允许UART0 RxdREN1= 1;/允许UART1 Rxd允许接收后,有收到数据将会发生UART中断,然后在中断程序中检查数据校验位和保存数据就可以了

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 技术方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁