Pokusavam pokrenuti ds 1820 digitalni termometar, navedeni kod mi ne radi pa ako neko ima kakav prijedlog....Inace koristim PIC18f252 i htpicov kompajler
// The following are standard 1-Wire routines.
void _1w_init(int sensor)
{
_1w_pin_hi(sensor);
_1w_pin_low(sensor);
DelayUs(250);
DelayUs(250);
_1w_pin_hi(sensor);
DelayUs(65);
}
int _1w_in_byte(int sensor)
{
int n, i_byte, temp, mask;
i_byte = 0;
mask = 0xff & (~(0x01<<sensor));
for (n=0; n<8; n++)
{
PORTB=0x00;
TRISB=mask;
TRISB=0xff;
#asm
CLRWDT
NOP
NOP
#endasm
temp=PORTB;
if(temp & ~mask)
{
i_byte=(i_byte >> 1) | 0x80; // least sig bit first
}
else
{
i_byte=i_byte >> 1;
}
//i_byte = i_byte | ((temp & ~mask) & (1 << n));
DelayUs(60);
}
return(i_byte);
}
void _1w_out_byte(int d, int sensor)
{
int n, mask;
mask = 0xff & (~(0x01<<sensor));
for(n=0; n<8; n++)
{
if (d&0x01)
{
PORTB=0;
TRISB=mask; // momentary low
TRISB=0xff;
DelayUs(60);
}
else
{
PORTB=0;
TRISB=mask;
DelayUs(60);
TRISB=0xff;
}
d=d>>1;
}
}
void _1w_pin_hi(int sensor)
{
TRISB = 0xff;
}
void _1w_pin_low(int sensor)
{
PORTB = 0x00;
TRISB = 0xff & (~(0x01 << sensor));
}
void _1w_strong_pull_up(int sensor) // bring DQ to strong +5VDC
{
PORTB = 0x01 << sensor;
TRISB = 0xff & (~(0x01 << sensor));
DelayMs(250);
asm("CLRWDT");
DelayMs(250);
asm("CLRWDT");
DelayMs(250);
TRISB = 0xff;
}
a main funkcija izgleda otprilike ovako:
void main(void)
{
MCU_Init();
while(1)
{
asm("CLRWDT");
if(flag_ds1820 == 1)
{
for (sensor=0; sensor<MAX_SENSORS; sensor++)
{
_1w_init(sensor);
_1w_out_byte(0xcc, sensor); // skip ROM
_1w_out_byte(0x44, sensor); // perform temperature conversion
_1w_strong_pull_up(sensor);
_1w_init(sensor);
_1w_out_byte(0xcc, sensor); // skip ROM
_1w_out_byte(0xbe, sensor); // read the result
for (n=0; n<1; n++)
{
buff[n]=_1w_in_byte(sensor);
}
}
mjerena_vrijednost = t;
flag_prikaza = TERMOMETAR;
podjela_cifre_na_4_znamenke(mjerena_vrijednost);
}
}