HII在1线
通信中有一些问题。我的观点是:MPLAB X IDE V4.00 xC32 V1.44和声V2.3BICD3A定制板与PIC32 MZ2048 EC144和24MHz外部时钟C24001引脚2连接到ECH144引脚34(RB2)。DS2401引脚1和4连接到GND。我想读取DS24的序列号。01、在数据表(这里)的帮助下,我编写了这些函数:在程序开始时,我投下了iNITS.S.(),为了读取序列号,我写了WrdEyByTeTysn(0x0f),把读值放在一个char数组中,用RealthyByTysS~()来表示。我不知道我是否选择了错误的时间号。其他的设置,但是当我读取字符数组时,里面总是有0个。请告诉我,如果你对我的代码中的错误有什么想法的话?谢谢
以上来自于百度翻译
以下为原文
Hi
I have some problems with an 1 wire communica
tion.
My spects :
MPLAB X IDE V4.00
XC32 v1.44
H
ARMONY V2.3b
ICD3
a custom board with PIC32MZ2048ECH144 and a 24Mhz external clock
DS2401 pin 2 linked to the ECH144 pin 34 (RB2).
DS2401 pin 1 and 4 linked to GND.
I would like to read the serial number of a DS2401.
With the help from the datasheet (here), I wrote these functions :
bool Init_SSN(void) //initilisation Silicon Serial Number
{
int wait;
PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin in ouput
PLIB_PORTS_ChangeNoticePullUpEnable(PORTS_ID_0, 34); //pull up enable
PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin Higth
PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin set Low
wait = 19205; while(wait > 0) { Nop(); wait--; } //wait at least 480us => at 24Mhz (3+1+2)*42ns*1905=480us)
PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin set hight
PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin in input
wait = 397; while(wait > 0) { Nop(); wait--; } //wait 60 to 240us => 6*42ns*397=100us
return SYS_PORTS_PinRead(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //return value send by the DS2401
}
void write_bit_SSN(bool value)
{
int wait;
PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin output
PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin set Low
wait = 59; while(wait > 0) { Nop(); wait--; } //wait 1 to 15us => 6*42ns*59=15us
if(value) //change the value if necessary
{ PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); }
wait = 317; while(wait > 0) { Nop(); wait--; } //wait 45 to 105us => 80us
}
bool read_bit_SSN(void)
{
int wait;
PLIB_PORTS_PinDirectionOutputSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin output
PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin set Low
PLIB_PORTS_PinDirectionInputSet(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //pin input
wait = 8; while(wait > 0) { Nop(); wait--; } //wait 1us => 6*42ns*8=2us
return SYS_PORTS_PinRead(PORTS_ID_0, PORT_CHANNEL_B,PORTS_BIT_POS_2); //return value send by the DS2401
}
unsigned char read_byte_SSN(void)
{
unsigned char valueIn = 0;
unsigned char coeff2 = 1;
int i;
for(i=0 ; i<8 ; i++)
{
valueIn = valueIn + read_bit_SSN() * coeff2;
coeff2 = coeff2 * 2;
}
return valueIn;
}
void write_byte_SSN(unsigned char valueOut)
{
unsigned char masque = 0b00000001;
int i;
for(i=0;i<8;i++)
{
write_bit_SSN((valueOut & masque) >> i);
masque = masque << 1; //*2
}
}
I cast
Init_SSN()at the beginning of my program.
And in order to read the serial numer, I cast
write_byte_SSN(0x0F) and I put the reading values in a char array with
read_byte_SSN().
I don't know if I chose the wrong time number or some other setting, but when I read the char array there are always 0 inside.
Could you tell me please, if you have any idea about what is wrong in my code ?
Thanks
0