C实现单总线从机

本文详细介绍了基于单总线协议的初始化、读写时序,以及从机如何响应主机的ACT_INIT、READ和WRITE操作。在主机写入0x99后,从机会返回8个字节的数据。代码示例展示了具体的操作过程和延时参数,包括DQ_PIN的输入输出配置、延时函数和数据传输逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由某个单排序器件总线协议:
初始化时序
在这里插入图片描述
读时序
在这里插入图片描述

写1时序
在这里插入图片描述

写0时序
在这里插入图片描述
从机要实现ACT_INIT,READ和WRITE几个动作,另外当主机初始化后写入0x99,从机就回复8个byte数据;

#define    DQ_PIN 	(xx)
#define	   DQ_OUT	//(xx)设置输出模式
#define    DQ_IN	//(xx)设置输入模式

#define tRSTL	480//-
#define tRSTH	480//-
#define tPDH	15//-60
#define tPDL	60//-240

#define tSLOT	60//60-120
#define tLOW1	1//1-15
#define tREC	1//1-
#define tLOW0	60//60-120<tSLOT

#define tRELEASE	0//0-45
#define tLOWR		1//1-15
#define tRDV	15
#define tSU		1//<1

void ACT_INIT(){
	DQ_OUT;
	delay_us(tPDH);
	DQ_PIN=0;
	delay_us(tPDL);
	delay_us(tPDL);
	DQ_PIN=1;
}

void WRITE_BYTES(const unsigned char * src, unsigned char len){
	unsigned char i = 0;
	
    DQ_IN;
    while(len>0){
        data=*src;
		for(i=0;i<8;i++){

			while( DQ_PIN == 1 );
			
			DQ_OUT;
			DQ_PIN = ( data&0x1 );
			data>>=1;
            
			delay_us(tRDV);//max tLOWR
			delay_us(tRDV);//
			
			DQ_PIN=1;
            
			DQ_IN;
			if( DQ_PIN == 0 ){
				while( DQ_PIN == 0 );
                ACT_INIT();
				return;
			}
		}
        src++;
        len--;
    }
}

const unsigned char datas[] = {
0x99,0x0,0xff,0x0,0xff,0x0,0xff,0x0,0xff,
};

void 主函数()
{
	unsigned int lowTime;
	unsigned char cmd = 0, Count = 0;

	while(1)
	{
        DQ_IN;
    	while( DQ_PIN == 1 );
    	
    	lowTime = 0;
    	while( DQ_PIN == 0 ){
			lowTime++;
			delay_us(1);
		}
		
    	if( lowTime > tRSTL )
        {
			Count = 0;
			ACT_INIT();
			continue;
		}

    	Count++;
		cmd>>=1;
    	if( lowTime < tLOW1 ){
			cmd |= 0x80 ;
    	}

		if( Count == 8 ){
			Count = 0;
			if( cmd == datas[0] ){
				WRITE_BYTES( datas + 1, 8 );
			}
		}
	}
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

楠木123456

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

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

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

打赏作者

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

抵扣说明:

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

余额充值