|
Having the bits and getting to the bytes
should be easy. Single thing to remeber here is communication happens
least significant bit first. This is when you have a byte to send, you
are going to send bit0 first and bit7 last. I would suggest give it a
go alone first, don't look, don't ...
void wTxbyte (u8 data)
{
u8 i;
for (i=0; i<8; i++)
{
wTxbit (data & 0x01);
data>>=1;
}
}
For 8 times we mask the LSB bit and we send it. Each time,
we shift for the next bit.
u8 wRxbyte (void)
{
u8 i, bit, data;
data=0;
for (i=0; i<8; i++)
{
bit=wRxbit();
data|=bit<<i;
}
return data;
}
For 8 times we read a bit and we place it in the data byte.
Ohh, I was about to forget, in case you are in doubt about your 1-wire
Reset routine ....
u8 Reset1Wire (void)
{
u8 present;
wOut;wL;
uDelay (192); uDelay (193); // 481.10us
wIn;
uDelay (46); // 61.82us
present=wR;
uDelay (163); uDelay (164); // 410.34us
// 0 for device present ! Sorry Reisga, Dave ...
return present;
}
To be honest with you, the Reset1Wire routine is the first
you should write cause it gives feedback. By just sending bits over your
1-wire it doesn't really tell you something. You can for example try to
turn on the LED in case there is a thermo present. If you manage to make
this one work you will also be sure about your timing calculations.
Reading this paragraph means that you have your low level 1-wire
stuff up and running and tested. If you just copy/pasted please let me
know cause I am seriously thinking of bugging up my code. Whatever the
case, its time to do the high level stuff. Give it a go, decide what you
need and then go through. It looks pretty much straight forward ...
- First, we need to Reset our 1-wire device and wait for the
Presence signal.
- After we get it, we issue a Skip ROM command, we don't really need to
address our slave.
- Then, we can tell our thermo to capture temperature data, this is the
Convert Temperature command.
- We need again to Reset our 1-wire device and wait for the Presence
signal.
- After we get it, we issue again a Skip ROM command to forse our thermo
talk.
- Eventually, we will request the temperature data, this is the Read RAM
command.
- What we get is a frame of 9 bytes, 2 for temperature, 6 for the thermo
ID and 1 for the cyclic redundancy code.
The Convert Temperature and the Read RAM commands have to
be separated, each one has to be preceded by a Reset and a Skip ROM command.
To me, this was not clear from the beginning, I had to do many tests.
So ... tell me what you think.
u8 Reset1Wire (void)
{
u8 present;
wOut;wL;
uDelay (192); uDelay (193); // 481.10us
wIn;
uDelay (46); // 61.82us
present=wR;
uDelay (163); uDelay (164); // 410.34us
// 0 for device present ! Sorry Reisga, Dave ...
return present;
}
You should know by now your thermo's unique ID and your
room's temperature! You like it? I feel your excitement. Well, it does
work. But, it is a bit crappy. No, not crappy just incomplete. Although
it works? Yes, having something that works is not everything! Would you
monitor your nuclear reactor's temperature with this routine? No? Why
not?
By the way, you might be wondering if someone whispered
to me the Delay values. No, I did many tests, sending bytes via the serial
port with shorter delays.
|