模拟串口软串口

        普通IO用作串口,定时器来控制波特率

目前,只是试了发送功能,还没试接收,用这种方法接收和发送还不能同时,以后有空再继续研究。

sbit TXSOFT = P2 ^ 3;
sbit RXSOFT = P3 ^ 2;
u8 tiSoft = 0;

static void system_init()
{
    CKSEL = 0x00;  //IRC
    WDT_CONTR=0X26;

    P2M0 |= 0X08;
    P2M1 = 0x00;

    uart_init(); //串口初始化
    // wifi_init();  //wifi初始化
}
void uart_soft_init()
{
	//TX
		//引脚   推挽输出
	TXSOFT = 1;
	//定时器 T1
	AUXR |= 0x40;		//定时器时钟1T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x07;		//设置定时初始值
	TH1 = 0xB8;		//设置定时初始值
	TF1 = 0;		//清除TF1标志
	TR1 = 0;
	//中断
	EA = 1;    //总中断
	//ET1 = 1;
}

//TX发送
void tx_wait_send()
{
	while(TF1==0)
	{

	}
	TF1 = 0;
    // while(tiSoft==0)
    //     ;
    // tiSoft = 0;
}
//发送字符串
void tx_send_byte(u8 str)
{
	u8 i = 0;
	//起始位
	TXSOFT = 0;
	TR1 = 1;    //开始计时
	tx_wait_send();
	//数据位 先发送低位,再发送高位
	for (; i < 8;i++)
	{
		TXSOFT = (bit)(str & 0x01);
		tx_wait_send();
		str = str >> 1;
	}
	//停止位
	TXSOFT = 1;
	tx_wait_send();
	TR1 = 0;   //停止计时
}
void tx_soft_send(u8 *str)
{
	while(*str!='\0')
	{
		tx_send_byte(*str++);
	}
}
void tx_timer_int() interrupt TIMER1_VECTOR
{
	tiSoft = 1;
}

void main()
{
    system_init();
    uart_soft_init();
    printf("hello world\r\n");
    while(1)
    {
        WDT_CONTR=0X36;  //喂狗
        tx_soft_send("y\r\n");
        delay_ms(1000);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

small_planet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值