/////////////////////////////////////////////// czujnik temperatury - funkcje /////////////////////////////
// OW_RESET - performs a reset on the one-wire
// returns the presence detect. Reset is 480us Presence
// another 70us later
unsigned char ow_reset(void) // reset lini one wire
{
unsigned char presence=1;
sbi(DDRD,3); //DQ = 0; //pull DQ line
delay(245); // wystawienie na czas 490us "0" na linię 1w przez uC
cbi(DDRD,3); //DQ = 1; // allow line to //powrót lini 1w do trybu wejścia
delay(35); // wait for presence pulse(pochodzący od DS-a) //czekamy na ustabilizowanie lini
if(bit_is_clear(PIND,3)) presence=0; //odczytujemy co wystawił na linię DS
//get presence
delay(220); // wait for end
return(presence); // presence signal
} // 0=presence, 1 = no part
///---------------------------------------------------------
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
unsigned char read_bit(void)
{
unsigned char presence;
sbi(DDRD,3); //DQ = 0; // pull DQ low to start timeslot
cbi(DDRD,3);//DQ = 1; // then return high
delay(10); // delay 20 us from start of timeslot
if(bit_is_clear(PIND,3)) presence=0;
if(bit_is_set(PIND,3)) presence=1;
return(presence); // return value of DQ line
}
///---------------------------------------------------------
void write_bit(char bitval) //WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
{
sbi(DDRD,3); //DQ = 0; // pull DQ low to start timeslot
if(bitval==1) cbi(DDRD,3); //DQ =1; // return DQ high if write 1
delay(52); // hold value for remainder of timeslot - delay 104us
cbi(DDRD,3);//DQ = 1;
}
///---------------------------------------------------------
unsigned char read_byte(void) // READ_BYTE - reads a byte from the one-wire bus
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
delay(61); // shifts it left wait for rest of timeslot
}
return(value);
}
///---------------------------------------------------------
void write_byte(char val) // WRITE_BYTE - writes a byte to the one-wire bus.
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
delay(52);
}
///---------------------------------------------------------
void Read_Temperature(void)
{
char tmp[10];
char temp_lsbyte,temp_msbyte;
int k;
cli(); //wyłączenie przerwań
if(!ow_reset())
{
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
delay(52);
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++){tmp[k]=read_byte();}
temp_msbyte = tmp[1]; // Sign byte + lsbit
temp_lsbyte = tmp[0]; // Temp data plus lsb
if (temp_msbyte <= 0x80){temp_lsbyte = (temp_lsbyte/2);} // shift to tmp whole degree
temp_msbyte = temp_msbyte & 0x80; // mask all but the sign bit
if (temp_msbyte >= 0x80) {temp_lsbyte = (~temp_lsbyte)+1;} // twos complement
if (temp_msbyte >= 0x80) {temp_lsbyte = (temp_lsbyte/2);}// shift to tmp whole degree
if (temp_msbyte >= 0x80) {temp_lsbyte = ((-1)*temp_lsbyte);} // add sign bit
if(temperature!=temp_lsbyte) {temperature=temp_lsbyte; zmtemp=1;}
}
sei(); //włączenie przerwań
}
Dodano po 4 [minuty]:
aha, oto zmienna globalona w której przechowuje wartosć temperatury:
volatile signed char temperature=0; //aktualna temperatura
i zmienna globalna która mówi czy temperatura sie zmieniła:
char zmtemp; //zmienna mówiące o zmianie temperatury
jesli zmienna ta ma wartość jeden (co oznacza zmianę temperatury w stosunku do ostatniego pomiaru to w petli main :
if(zmtemp==1) //sprawdzenie czy temperatura uległa zmianie od ostatniego pomiaru
{
printf("%d",temperature);
zmtemp--;
pisztekst("oC");
}