📄 1-wire.c
字号:
/********************** 1-WIRE 总线基本操作函数******************************
[文件名] 1-WIRE.c+1-WIRE.H
[版本] TV0.1
[修正] hxc
hxc last change time 2006.03.03 x:x
hxc last check time 2006.4.14 16:46
[功能说明]:实现总线复位、读1位、8位;写1位、8位和搜索1个外设标识码
***************************************************************************/
#include "ASMselect.h"
#if ASM1wire
#include <stdio.h>
#include <reg51x.h>
#include <tab.h>
#include <1-wire.h>
unsigned char idata ROM_ID[8];//rom_id[0] is 8bit family,rom_id[1~6]48bit ID,rom_id[8] 8 bit CRC
unsigned char code 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};
//=================================read one rom_id ===================================
//=======================right ret 1,error and search to last ret 0=====================
bit WIRE_id_read(uchar searchor_if)//read one ROM_ID,right ret 1,error ret 0,must read to 0 stop
{
bit id_bit; //read wire on first data
bit cmp_id_bit; //read wire on secondly data -first data ID reverse
bit search_direction; //ID search direction
static bit last_device_flag; //ID search ok
bit ret_flag=1; //ret flag ,ID search ok ret '1'
unsigned char Id_Search_xBit; //now search ID bit count
static unsigned char LastDiscrepancy; //last time at direction change bit
//unsigned char LastFamilyDiscrepancy; //
unsigned char Last_Zero; //now search id change bit
unsigned char i;
unsigned char j;
unsigned char K;
if (WIRE_reset()) return (0);
if (last_device_flag){last_device_flag=0;return (0); } //tx wire reset ok or ROM ID not Last
Id_Search_xBit=1; //search id first start
Last_Zero=0;
WIRE_tx_8(searchor_if); //tx id search command
for (i=0;i<8;i++){
K=0;
for(j=0;j<8;j++){
id_bit=WIRE_rx_1(); //rx 1 bit id and 1 bit |id
cmp_id_bit=WIRE_rx_1();
// printf ("%bx\n ",(char)(id_bit)); //ouput id_bit
// printf ("%bx\n ",(char)(cmp_id_bit)); //ouput cmp_id_bit
if (id_bit & cmp_id_bit) //not 1 wire bus device
{ret_flag=0;
break;
}
else if((id_bit ==0)&&(cmp_id_bit==0)) //wire id have 1 and 0
{
printf("id_bit = cmp_id_bit=0\n");
if(Id_Search_xBit==LastDiscrepancy) //id search = Discrepancy bit select 1 path
{search_direction=1;} //last path is 0
else if (Id_Search_xBit>LastDiscrepancy) //id search> Discrepancy bit select 0 path
{search_direction=0;} //last Discrepancy after have Discrepancy
else
{search_direction=(bit)((ROM_ID[i]>>j)&0x01);//rom_id 中的Id_Search_xBit 位->search_direction
}//endif
if (search_direction==0){
Last_Zero=Id_Search_xBit;//at ID have 1 or 0, select 0 path,
} //show have naw Discrepancy bit ,annal at Last_Zero .
}// else if
else {
search_direction=id_bit; // on wire not 0 and 1,path search wire read bit path
}//endif
K=1;
K=K<<j;
if (search_direction) //if path is 1 ,ROM_ID 第Id_Search_xBit or 1
{
ROM_ID[i]=ROM_ID[i]| K;
}
else { //if path is 1 ,ROM_ID 第Id_Search_xBit and 0
K=~K;
ROM_ID[i]=ROM_ID[i] & K ;
}//endif
WIRE_tx_1(search_direction); //on wire write search_direction bit select path
// printf ("tx,%bx\n ",(char)(search_direction)); //ouput select bit
Id_Search_xBit++; //id amount +1
// printf("bit,%bx\n",Id_Search_xBit); //ouput search bit
}//next j
}//next i
LastDiscrepancy=Last_Zero; //discrepancy bit save ,last use .
if (LastDiscrepancy==0) {last_device_flag=1;} //not discrepancy,ROM_ID is last device
if((~ret_flag)|WIER_crc(ROM_ID,8)){ //if ret 0 (error) CRC <>0(error),reset register,check 8 byte
LastDiscrepancy=0;
//LastFamilyDiscrepancy=0;
last_device_flag=0;
}
return (ret_flag); //not error,ret 1.
}
//==================================ROM_ID _CRC===================================
//========================ok ret 0 array *crc_array CRC check ,chach byte is i2 pcs ===========
unsigned char WIER_crc(unsigned char *crc_array,unsigned char i2)
{
unsigned char i1;
unsigned char dowcrc;
dowcrc=0;
for (i1=0;i1<i2;i1++){
dowcrc=dscrc_table[dowcrc^*crc_array];
crc_array++;}
//ROM_ID[i1]];}
return dowcrc;
}
//==================================WIRE_reset====================================
//=================================ok ret 0 not device ret 1======================
bit WIRE_reset(void){ //H480~960us 15~60uS 60~240us
bit flag; //|___________/--------|__________/-------
wire_pin=0; //| >480us | >480us |
DELAY_X(35);//delay 550us //| 566us | 200us | 300us |
wire_pin=1;
DELAY_X(5);
flag=wire_pin;
DELAY_X(25);
return (flag);}
//==================================WIRE_tx_1===================================
//============================Txbit wirte on wire ===============================
WIRE_tx_1(bit Txbit){ //|60us<write'0'<120us| write'1'
wire_pin=0; //|15us|15us| 30us |>1us|15us|15us|30us|
wire_pin=0; //|__________________/----|__/--------------
wire_pin=0; //| 77us | |4us| 72us |
wire_pin=0;
wire_pin=Txbit;
DELAY_X(4);
wire_pin=1;}
//==================================WIRE_rx_1=====================================
//===============================read wire one bit ret ============================
bit WIRE_rx_1(void){
bit flag; //H read '0' H read '1'
uchar i; //|1us|12us| 72us |1us|12us| 72us |
wire_pin=0; //|___________/---------|___/----------------
wire_pin=1; //D TX '0'15us | 45us |TX '1'15us | 45us |
for(i=0;i<2;i++);//delay12us
flag=wire_pin;
DELAY_X(3);
return (flag);}
//==================================WIRE_rx_8======================================
//===============================read wire 8 bit ret ===============================
unsigned char WIRE_rx_8(void){
uchar i1;
uchar bytex=0;
for (i1=0;i1<8;i1++){
if (WIRE_rx_1())bytex=bytex|(0x01<<i1);
}
return (bytex);
}
//==================================WIRE_tx_8=======================================
//===============================write wire 8 bit ret ===============================
WIRE_tx_8(unsigned char Txbyte){
uchar i1;
for (i1=0;i1<8;i1++){
WIRE_tx_1((bit)(Txbyte&(0x01<<i1)));
}
}
//==================================DELAY_X=======================================
//============================delay=16*x us+8us(f=12M)=============================
void DELAY_X(int xs){
int s1;
for (s1=1;s1<xs;s1++){
}}
//==================================DELAY_nh======================================
//============================delay time =xs1*xs2*15+xs1*18========================
/*void DELAY_nh( int xs1, int xs2){ //delay time =xs1*xs2*15+xs1*18 last long 4.5 hour
int s1;
int s2;
for (s1=1;s1<xs1;s1++){
for(s2=1;s2<xs2;s2++);
}}*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -