void main(void) { uart1_init(); //串口1初始化 //uart2_init(); //串口2初始化 Uart1RxCallBack((void *)Ut1Recv); //Uart2RxCallBack((void *)Ut2Recv); GIE=0X1; //开放全局中断 PEIE=0X1; //使能外设中断 RCIE=0X1; //使能接收中断 TRISC2 = 0; //Uart1PollPutc(&aa, 5); RC2 = 0; delay(500); RC2 = 1; delay(500); RC2 = 0; while(1) { //Uart2PollPutc(&aa, 5); //delay(5000); NetCmdExe(); } } static srlrxcexe uart1callback = NULL; static srlrxcexe uart2callback = NULL; void Uart1RxCallBack(void *fun) { uart1callback = (srlrxcexe)fun; } void Uart2RxCallBack(void *fun) { uart2callback = (srlrxcexe)fun; } void uart1_init() { BAUDCONbits.ABDEN =0; //关闭自动波特率检测 BAUDCONbits.BRG16 = 1; //选择波特率生成器为16方式 TXSTA1bits.BRGH = 1; //选择高速波特率方式 TXSTA1bits.SYNC=0; //选择异步方式 SPBRGH1=0; SPBRG=0x44; //设置波特率为115200BPS, 115200 = 32M/4(0x44+1)) TRISC7 = 1; TRISC6 = 1; TXSTA1bits.TXEN=1; RC1IE = 1; RCSTA1=0x90; //串口工作使能,连续接收 } void uart2_init() { INTCONbits.RBIE = 0; BAUDCON2bits.ABDEN =0; //关闭自动波特率检测 BAUDCON2bits.BRG16 = 1; //选择波特率生成器为16方式 TXSTA2bits.BRGH = 1; //选择高速波特率方式 TXSTA2bits.SYNC=0; //选择异步方式 SPBRGH2=0; SPBRG2=0x44; //设置波特率为115200BPS, 115200 = 32M/4(0x44+1)) TRISB7 = 1; TRISB6 = 1; TXSTA2bits.TXEN=1; RC2IE = 1; RCSTA2=0x90; //串口工作使能,连续接收 } void uart1_send(unsigned char data) { int index; for (index = 0; index < 1000; index++)//检测发送器是否为空 { if (TXSTA1bits.TRMT == 1) // 发送移位寄存器状态位 1 = TSR 空 0 = TSR 满 break; } TXREG1 = data; //把数据写入缓冲区 for (index = 0; index < 1000; index++)//等待发送完毕 { if (TX1IF == 1) break; } //TX1IF=0; //发送中断清零 } void uart2_send(unsigned char data) { int index; for (index = 0; index < 1000; index++)//检测发送器是否为空 { if (TXSTA2bits.TRMT == 1) // 发送移位寄存器状态位 1 = TSR 空 0 = TSR 满 break; } TXREG2 = data; //把数据写入缓冲区 for (index = 0; index < 1000; index++)//等待发送完毕 { if (TX2IF == 1) break; } //TX1IF=0; //发送中断清零 // TXREG2 = data; // while(TXSTA2bits.TRMT==0);// loop } unsigned int Uart1PollPutc(unsigned char *buf, unsigned int size) { unsigned char i; for (i = 0; i < size; i++) { uart1_send(*buf); buf++; } return 1; } unsigned int Uart2PollPutc(unsigned char *buf, unsigned int size) { unsigned char i; for (i = 0; i < size; i++) { uart2_send(*buf); buf++; } return 1; } void interrupt usart(void) { if(RC1IE&&RC1IF) //判断是否为串口1接收中断 { //TXREG=RCREG; //把接收到的数据发送回去 if (uart1callback) { uart1callback(RCREG1); } } if(RC2IE&&RC2IF) //判断是否为串口2接收中断 { //TXREG=RCREG; //把接收到的数据发送回去 if (uart2callback) { uart2callback(RCREG2); } } }
举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
人才类勋章
时间类勋章
发帖类勋章
1
2
0
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号