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

📄 ds2401.c

📁 单总线加密芯片ds2401驱动应用源码,适合单片机和嵌入式移植开发
💻 C
字号:
/*
*******************************************************************************
workfile:Ds2401.c
author  :
function:
declare :All rights reserved.
		
		
remarks :
finished:
version :
Modtime : 
*******************************************************************************
*/

//*----------------------------------------------------------------------------
//* Header files
//*----------------------------------------------------------------------------
#include    <string.h>
#include    <stdio.h>

#include "../incl/Ds2401.h"

//*----------------------------------------------------------------------------
//* Defines
//*----------------------------------------------------------------------------


//*----------------------------------------------------------------------------
//* Internal function prototypes
//*----------------------------------------------------------------------------

  
//*----------------------------------------------------------------------------
//* External global variables
//*----------------------------------------------------------------------------

//*----------------------------------------------------------------------------
//* Internal global variables
//*----------------------------------------------------------------------------

///////////////////////////////////////////////////
void delay_us(unsigned short iDelay){

	unsigned short i;
	
	for(i=0;i<iDelay;i++){
		;
	}
	
}
///////////////////////////////////////////////////
void API_1UsTest(void){///delay test

	unsigned char i,h;
	
	for(i=0;i<100;i++){
	
		
		h=0;
		h=0;
		GSM_GPIO10_HIGH();
		h=0;
		h=0;
		GSM_GPIO10_LOW();
		
	}
}

/////////Reset ds2401//////////////////////////
unsigned char API_Ds2401_ResetAck(void){

	
	unsigned short uCounter=0;
	
	GSM_GPIO10_OUTPUT();
	delay_us(10);
	GSM_GPIO10_HIGH();
	delay_us(10);
	GSM_GPIO10_LOW();
	delay_us(480);
	GSM_GPIO10_INPUT();
	//delay_us(1);
	////////////////////////////////
ReadIoStatus:

	if((GSM_GPIO_READ()&0x400)==0x00){//0x400
		delay_us(200);/////reset ok delay 
		delay_us(200);
		return 1;
	}else{
	
		uCounter++;
		if(uCounter>1500){///reset fail
			return 0;
		}else{
			goto ReadIoStatus;//wait rest ack
		}
	}
	
}




//send read rom command on 1-wire/////////////////////
void API_Ds2401_SendCommand(unsigned char uCommand)
{ 
	unsigned char i,j,s,k;
	unsigned char uDelay;
	
	
	
	for (j=0;j<8;j++){
	
		GSM_GPIO10_OUTPUT();
		
		GSM_GPIO10_LOW();
		for(k=0;k<5;k++){//启动总线
			uDelay =0;
			uDelay =0;
			uDelay =0;
			uDelay =0;
			uDelay =0;
		}
		i=uCommand&1;
		if (i==1){////////write 1
			
			GSM_GPIO10_INPUT();
		
		}else{
			////////write 0////////
			GSM_GPIO10_LOW();
			
		}
		
		for(s=0;s<60;s++){////wait slave sample
			uDelay =0;
		}
		GSM_GPIO10_INPUT();
			
		for(s=0;s<10;s++){////wait release bus
			uDelay =0;
			uDelay =0;
			uDelay =0;
			uDelay =0;
		}
		uCommand=uCommand>>1;
	}
	
	////////////////////////////////////
	for(s=0;s<10;s++){////wait read data
			uDelay =0;
			uDelay =0;
			uDelay =0;
			uDelay =0;
	}
	
}//end of read rom command 


//read the 8 bytes of code////////////////////////
unsigned char API_Ds2401_Read(unsigned char* uRetCode)
{

	unsigned char j,f,s,uDelay;
	unsigned char uByte;
	
	
	uByte=0x00;
	
	//////////////////////////////////////////////
	for(f=0;f<8;f++){
	
		for (j=0;j<8;j++){/////sample byte
			
			GSM_GPIO10_OUTPUT();	
			GSM_GPIO10_LOW();
			uByte=uByte>>1;
			
			for(s=0;s<5;s++){////启动总线
				uDelay =0;
				uDelay =0;
				uDelay =0;
				uDelay =0;
				uDelay =0;
			}
			
			GSM_GPIO10_INPUT();
							
			for(s=0;s<5;s++){//wait master sample
				uDelay =0;
				uDelay =0;
				uDelay =0;
				uDelay =0;
			}
					
			if ((GSM_GPIO_READ()&0x400)){///Read GPIO2
				uByte |=0x80;
				
			}else{
				
			}
			
			for(s=0;s<50;s++){////release bus
				uDelay =0;
				uDelay =0;
				uDelay =0;
				uDelay =0;
			}
			
			
		
		}
		*(uRetCode+f)=uByte;
		
	}
	return 1;
}//end of read function


/////////////////////////////////////////////////////////////////
static unsigned char dscrc_table[] = {

	0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
	157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
	35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
	190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
	70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
	219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
	101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
	248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
	140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
	17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
	175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
	50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
	202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
	87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
	233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
	116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
};
//---------------------------------------------------------------
// Calculate the CRC8 of the byte value provided with the current
// Returns current global crc8 value
//

//////////////////////8bit crc////////////////////////////////////
unsigned char API_docrc8(unsigned char* value,unsigned char uLen){

	unsigned char crc8=0,i;
	
	for(i=0;i<uLen;i++){
	
		crc8 = dscrc_table[crc8 ^ *(value+i)];
	
	}
	return crc8;
	
}
////////////////2401 application test////////////////////////////////////////////////////

void main(void){

unsigned char uRet;

	if(API_Ds2401_ResetAck()){
				
		API_Ds2401_SendCommand(0x33);
		API_Ds2401_Read(szDebugMessage);
		uRet=API_docrc8(szDebugMessage,7);
		if(uRet==szDebugMessage[7]){
			#ifdef _DEBUG_		/////pass 
			sprintf((char*)szDebugMessage, "Ds2401 test %d\r\n",szDebugMessage[0]);
			SendDataToUart0(szDebugMessage, strlen((char*)szDebugMessage));
			#endif
		}
	}
}





⌨️ 快捷键说明

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