⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.c

📁 是IC,CH451的处理程序,连接有LED灯.可用.
💻 C
字号:
#include "my51.h"

extern void delay_s(unsigned char);

bit timeout;
unsigned char tcount;
unsigned char bflw;

void timer0_init()
{
	timer0_mode_autoreload;
	TH0 = 0;
	TL0 = 0;
	timer0_stop;
}

void timer0() interrupt int_timer2 
{
    TF0 = 0;
    TL0 = 0xf0;
    TH0 = 0x58;
    if (tcount < 150)
		tcount++;
    else
		timeout = 1;
}



void serial_int() interrupt int_serial
{
	if (RI){
		RI = 0;
		bflw = SBUF;
	}
}

void serial_init()
{
	serial_baud_9600;
	serial_uart_8;
	serial_receive_enable;
}

bit s_in(unsigned char xdata *pbuf)
{
	timeout = 0;
	tcount = 0;
	timer0_run;
	while(1){
		if (RI){
			*pbuf = SBUF;
            RI = 0;
			break;
		}
		else{
			if (timeout)
				break;
		}
	}
	timer0_stop;
	return timeout;
}

void s_out(unsigned char ndata)
{
	TI = 0;
	timeout = 0;
	tcount = 0;
	timer0_run;
	SBUF = ndata;
	while(TI == 0){
		if (timeout)
			break;
	}
	timer0_stop;
}



//发送指令到PC
void serial_data_to_main(unsigned char xdata *pcmd, unsigned char nlen)
{
	unsigned char i;
	unsigned char xdata tmp;
	for (i = 0; i < nlen; i++, pcmd++){
		s_out(*pcmd);
		if (s_in(&tmp))
			return;
		if (tmp != *pcmd)
			return;
	}
}

//返回:接收数据长度
unsigned char serial_data_from_main(unsigned char xdata *pcmd)
{
	unsigned char i, cmdid;
	unsigned char xdata tmp;
	while(1){	//接收数据头0xff,0x00
		if (s_in(&tmp))
			return 0xff;
		if (tmp != 0xff)
			continue;
in_1:
		s_out(tmp);
		if (s_in(&tmp))
			return 0xff;
		if (tmp == 0xff)
			goto in_1;
		if (tmp != 0x00)
			continue;
		s_out(tmp);
		break;
	}
	if (s_in(&tmp))	//接受命令ID
		return 0xff;
	s_out(tmp);
	cmdid = tmp;
	if (s_in(&tmp))	//接受数据长度
		return 0xff;
	s_out(tmp);
	for (i = 0; i < tmp; i++){
		if (s_in(pcmd))
			return 0xff;
		s_out(*pcmd);
		pcmd++;
	}
	return cmdid;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -