普通IO模拟串口

上传者: q452197900 | 上传时间: 2022-06-21 15:14:28 | 文件大小: 61KB | 文件类型: RAR
外部中断模拟串口,波特率不能超过65536 实验测试:发送57600可以正常,但接收只能 <= 38400 #include "Uart_EXT0.h" #include "MAIN.h" #define FOCS 22114800ul bit Over; bit bRxflag; unsigned char IEN0_NOW,IEN1_NOW; //中断临时变量 unsigned char idata bRxstate=0; //接收状态 unsigned char idata tmp_Len=0; //缓存数组下标 unsigned char idata bRxlen; //接收字节数 unsigned char xdata EX_buf[64]; //接收存放区 /*************************************************** baud = 56000 接收一字节 =178.6us ,接收会出现错误 baud = 38400 接收一字节 =260.4us //快速接收都容易出现错误 baud = 19200 接收一字节 =520us | baud = 14400 接收一字节 =694.5us | baud = 9600 接收一字节 =1041.6us | baud = 4800 接收一字节 =2.083ms 接收过长,定时器MODE2无法满足 //主机发送的数据最好加上校验字 发送:最大57600,发送还能正常。 ****************************************************/ void Time0_Uart(unsigned int baud) { TMOD &= 0XF0; TMOD |= 0X02; //使用定时器0方式2,使用方式1则2400波特也能有 TH0 = 256-(FOCS/12)/baud; TL0 = TH0; IP0 |= 0x02; //设置为最高优先级 IP1 |= 0x02; TR0 = 1; ET0 = 1; EA = 1; } void Time0_isr() interrupt 1 { //方式1则重装值 Over = 1; } /* 发送数据,1起始位,8数据,1停止位 发送波特率实验57600都不会错误,大量发送数据待测试 */ void Uart_set(unsigned char dat) { unsigned char i; Over = 0; TL0 = TH0; //防止发送数据开始时不知道TL0是多少 T_uart = 0; //起始位 while(Over==0); Over = 0; for(i=0;i<8;i++) //8数据位 {// Over = 0; //起始位时清零了 if(dat&0x01) //先发最低位 { T_uart = 1; } else { T_uart = 0; } while(Over==0); Over = 0; dat >>= 1; } // Over = 0; //数据位清零了 T_uart = 1; //停止位,此后如果没有数据则一直为高 while(Over==0); } unsigned char get_Uart() { unsigned char I=0,get_dat=0; Over = 0; TL0 = TH0; //重新赋值,防止出错 while(Over==0); Over = 0; for(I=0;I<8;I++) { get_dat >>= 1; if(Get_ex == 1) get_dat |= 0x80; else get_dat &= 0x7f; while(Over==0); Over = 0; } if(Get_ex == 1) { Over = 0; } return get_dat; } void EXuart_isr() interrupt 0 { unsigned char _chr; IEN0_NOW = IEN0; //进入接收1字节,关闭中断 IEN1_NOW = IEN1; IEN0 = 0X82; IEN1 = 0; _chr=get_Uart(); switch(bRxstate) { case 0: if(_chr==0x55) { bRxstate=1; } else bRxstate=0; break; case 1: if(_chr==0xaa) { bRxstate=2; } else bRxstate=0; break; case 2: //取得数据长度 bRxlen=_chr; bRxstate=3; break; case 3: EX_buf[tmp_Len]=_chr; tmp_Len++; if(tmp_Len==bRxlen) { bRxstate=0; tmp_Len=0; bRxflag=1; //一帧数据接收完毕,置位完成标志位 } break; default: break; } IE0 = 0; //清外部0中断标志,防止下次接收不到 IEN0 = IEN0_NOW; IEN1 = IEN1_NOW; } void main() { Time0_Uart(9600); EX_uart(); if(bRxflag==1) { bRxflag = 0; for(i=0;i<10;i++) RevBuffer[i] = EX_buf[i]; T0Uart_TX(10,RevBuffer); } }

文件下载

资源详情

[{"title":"( 30 个子文件 61KB ) 普通IO模拟串口","children":[{"title":"652_EX_uart","children":[{"title":"UART.c <span style='color:#111;'> 2.23KB </span>","children":null,"spread":false},{"title":"MAIN.H <span style='color:#111;'> 850B </span>","children":null,"spread":false},{"title":"652_EX_uart_uvproj.bak <span style='color:#111;'> 13.30KB </span>","children":null,"spread":false},{"title":"SM59R16G6.h <span style='color:#111;'> 5.64KB </span>","children":null,"spread":false},{"title":"uart.h <span style='color:#111;'> 362B </span>","children":null,"spread":false},{"title":"EEPROM.h <span style='color:#111;'> 1.87KB </span>","children":null,"spread":false},{"title":"XL652Show.h <span style='color:#111;'> 780B </span>","children":null,"spread":false},{"title":"EEPROM.c <span style='color:#111;'> 2.61KB </span>","children":null,"spread":false},{"title":"STARTUP.A51 <span style='color:#111;'> 6.23KB </span>","children":null,"spread":false},{"title":"652_EX_uart.uvopt <span style='color:#111;'> 58.45KB </span>","children":null,"spread":false},{"title":"out","children":[{"title":"652_EX_uart.lnp <span style='color:#111;'> 160B </span>","children":null,"spread":false},{"title":"MAIN.obj <span style='color:#111;'> 8.88KB </span>","children":null,"spread":false},{"title":"Uart_EXT0.obj <span style='color:#111;'> 8.81KB </span>","children":null,"spread":false},{"title":"652_EX_uart.hex <span style='color:#111;'> 4.59KB </span>","children":null,"spread":false},{"title":"XL652Show.obj <span style='color:#111;'> 8.32KB </span>","children":null,"spread":false},{"title":"STARTUP.obj <span style='color:#111;'> 806B </span>","children":null,"spread":false},{"title":"652_EX_uart <span style='color:#111;'> 23.62KB </span>","children":null,"spread":false},{"title":"652_EX_uart.plg <span style='color:#111;'> 484B </span>","children":null,"spread":false}],"spread":true},{"title":"652_EX_uart_uvopt.bak <span style='color:#111;'> 58.48KB </span>","children":null,"spread":false},{"title":"652_EX_uart.uvproj <span style='color:#111;'> 13.46KB </span>","children":null,"spread":false},{"title":"XL652Show.c <span style='color:#111;'> 4.12KB </span>","children":null,"spread":false},{"title":"MAIN.C <span style='color:#111;'> 2.58KB </span>","children":null,"spread":false},{"title":"Lis","children":[{"title":"MAIN.lst <span style='color:#111;'> 6.96KB </span>","children":null,"spread":false},{"title":"XL652Show.lst <span style='color:#111;'> 9.04KB </span>","children":null,"spread":false},{"title":"STARTUP.lst <span style='color:#111;'> 13.82KB </span>","children":null,"spread":false},{"title":"Uart_EXT0.lst <span style='color:#111;'> 9.72KB </span>","children":null,"spread":false},{"title":"652_EX_uart.m51 <span style='color:#111;'> 25.40KB </span>","children":null,"spread":false}],"spread":false},{"title":"Uart_EXT0.h <span style='color:#111;'> 522B </span>","children":null,"spread":false},{"title":"Uart_EXT0.c <span style='color:#111;'> 3.75KB </span>","children":null,"spread":false},{"title":"SM39R08A5.h <span style='color:#111;'> 3.83KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明