用i/o口模拟串口在网上看了三种方法,但是我用着都实现不了。。代码复制上去,就是不能实现。 [解决办法]
Quote: 引用:sbit TXD1 = P0^1; //定义模拟输出脚sbit RXD1 = P0^2; //定义模拟输入脚bdata unsigned char SBUF1; //定义一个位操作变量sbit SBUF1_bit0 = SBUF1^0; sbit SBUF1_bit1 = SBUF1^1; sbit SBUF1_bit2 = SBUF1^2; sbit SBUF1_bit3 = SBUF1^3; sbit SBUF1_bit4 = SBUF1^4; sbit SBUF1_bit5 = SBUF1^5; sbit SBUF1_bit6 = SBUF1^6; sbit SBUF1_bit7 = SBUF1^7; void delay_bps() {unsigned char i; for (i = 0; i < 29; i++); _nop_();_nop_();} //波特率9600 模拟一个9600波特率unsigned char getchar2() //模拟接收一个字节数据{while (RXD1); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();//建议延时为delay_bps()的一半 delay_bps(); SBUF1_bit0 = RXD1; //0 delay_bps(); SBUF1_bit1 = RXD1; //1 delay_bps(); SBUF1_bit2 = RXD1; //2 delay_bps(); SBUF1_bit3 = RXD1; //3 delay_bps(); SBUF1_bit4 = RXD1; //4 delay_bps(); SBUF1_bit5 = RXD1; //5 delay_bps(); SBUF1_bit6 = RXD1; //6 delay_bps(); SBUF1_bit7 = RXD1; //7 delay_bps(); return(SBUF1) ; //返回读取的数据}void putchar2(unsigned char input) //模拟发送一个字节数据{ SBUF1 = input; TXD1 = 0; //起始位 delay_bps(); TXD1 = SBUF1_bit0; //0 delay_bps(); TXD1 = SBUF1_bit1; //1 delay_bps(); TXD1 = SBUF1_bit2; //2 delay_bps(); TXD1 = SBUF1_bit3; //3 delay_bps(); TXD1 = SBUF1_bit4; //4 delay_bps(); TXD1 = SBUF1_bit5; //5 delay_bps(); TXD1 = SBUF1_bit6; //6 delay_bps(); TXD1 = SBUF1_bit7; //7 delay_bps(); TXD1 = 1; //停止位 delay_bps(); }不懂"直接用貌似没法用"要个初始化的,主要就是开机时TXD,RXD保持高电平最好弄个逻辑分析仪看下时间到底对不对这个怎么初始化?void SPI_Init( void ){ TMOD = 0x20; //定时器工作方式2,初值自动装入 PCON = 0x00; //波特率不增倍 SCON = 0x50; //串行工作方式设定 TH1 = 0xFD; //定时器初值高位 TL1 = 0xFD; //定时器初值地位 TR1 = 1; //启动定时器 ES = 1; //串口中断开关,当ES置0时,只有串口终端不响应 }这是原来的那个串口初始化,我再定义一个void SPI_Init2( void )?这个串口该怎么初始化?
sbit TXD1 = P0^1; //定义模拟输出脚sbit RXD1 = P0^2; //定义模拟输入脚bdata unsigned char SBUF1; //定义一个位操作变量sbit SBUF1_bit0 = SBUF1^0; sbit SBUF1_bit1 = SBUF1^1; sbit SBUF1_bit2 = SBUF1^2; sbit SBUF1_bit3 = SBUF1^3; sbit SBUF1_bit4 = SBUF1^4; sbit SBUF1_bit5 = SBUF1^5; sbit SBUF1_bit6 = SBUF1^6; sbit SBUF1_bit7 = SBUF1^7; void delay_bps() {unsigned char i; for (i = 0; i < 29; i++); _nop_();_nop_();} //波特率9600 模拟一个9600波特率unsigned char getchar2() //模拟接收一个字节数据{while (RXD1); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();//建议延时为delay_bps()的一半 delay_bps(); SBUF1_bit0 = RXD1; //0 delay_bps(); SBUF1_bit1 = RXD1; //1 delay_bps(); SBUF1_bit2 = RXD1; //2 delay_bps(); SBUF1_bit3 = RXD1; //3 delay_bps(); SBUF1_bit4 = RXD1; //4 delay_bps(); SBUF1_bit5 = RXD1; //5 delay_bps(); SBUF1_bit6 = RXD1; //6 delay_bps(); SBUF1_bit7 = RXD1; //7 delay_bps(); return(SBUF1) ; //返回读取的数据}void putchar2(unsigned char input) //模拟发送一个字节数据{ SBUF1 = input; TXD1 = 0; //起始位 delay_bps(); TXD1 = SBUF1_bit0; //0 delay_bps(); TXD1 = SBUF1_bit1; //1 delay_bps(); TXD1 = SBUF1_bit2; //2 delay_bps(); TXD1 = SBUF1_bit3; //3 delay_bps(); TXD1 = SBUF1_bit4; //4 delay_bps(); TXD1 = SBUF1_bit5; //5 delay_bps(); TXD1 = SBUF1_bit6; //6 delay_bps(); TXD1 = SBUF1_bit7; //7 delay_bps(); TXD1 = 1; //停止位 delay_bps(); }不懂"直接用貌似没法用"要个初始化的,主要就是开机时TXD,RXD保持高电平最好弄个逻辑分析仪看下时间到底对不对
sbit TXD1 = P0^1; //定义模拟输出脚sbit RXD1 = P0^2; //定义模拟输入脚bdata unsigned char SBUF1; //定义一个位操作变量sbit SBUF1_bit0 = SBUF1^0; sbit SBUF1_bit1 = SBUF1^1; sbit SBUF1_bit2 = SBUF1^2; sbit SBUF1_bit3 = SBUF1^3; sbit SBUF1_bit4 = SBUF1^4; sbit SBUF1_bit5 = SBUF1^5; sbit SBUF1_bit6 = SBUF1^6; sbit SBUF1_bit7 = SBUF1^7; void delay_bps() {unsigned char i; for (i = 0; i < 29; i++); _nop_();_nop_();} //波特率9600 模拟一个9600波特率unsigned char getchar2() //模拟接收一个字节数据{while (RXD1); _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();//建议延时为delay_bps()的一半 delay_bps(); SBUF1_bit0 = RXD1; //0 delay_bps(); SBUF1_bit1 = RXD1; //1 delay_bps(); SBUF1_bit2 = RXD1; //2 delay_bps(); SBUF1_bit3 = RXD1; //3 delay_bps(); SBUF1_bit4 = RXD1; //4 delay_bps(); SBUF1_bit5 = RXD1; //5 delay_bps(); SBUF1_bit6 = RXD1; //6 delay_bps(); SBUF1_bit7 = RXD1; //7 delay_bps(); return(SBUF1) ; //返回读取的数据}void putchar2(unsigned char input) //模拟发送一个字节数据{ SBUF1 = input; TXD1 = 0; //起始位 delay_bps(); TXD1 = SBUF1_bit0; //0 delay_bps(); TXD1 = SBUF1_bit1; //1 delay_bps(); TXD1 = SBUF1_bit2; //2 delay_bps(); TXD1 = SBUF1_bit3; //3 delay_bps(); TXD1 = SBUF1_bit4; //4 delay_bps(); TXD1 = SBUF1_bit5; //5 delay_bps(); TXD1 = SBUF1_bit6; //6 delay_bps(); TXD1 = SBUF1_bit7; //7 delay_bps(); TXD1 = 1; //停止位 delay_bps(); }
void init_uart(void){ TXD1=1; RXD1=1;}