#define dir_pin DDRB
#define out_pin PORTB
#define in_pin PINB
#define dq_out_set (dir_pin=0xff)
#define dq_in_set (dir_pin=0xfe)
#define clk_conv_low (out_pin&=0xfd)
#define clk_conv_high (out_pin|=0x02)
#define reset_low (out_pin&=0xfb)
#define reset_high (out_pin|=0x04)
#define ds1620_comm_read_temp (0xaa)
#define ds1620_comm_write_th (0x01)
#define ds1620_comm_write_tl (0x02)
#define ds1620_comm_read_th (0xa1)
#define ds1620_comm_read_tl (0xa2)
#define ds1620_comm_read_cnt (0xa0)
#define ds1620_comm_read_slope (0xa9)
#define ds1620_comm_start_conv_t (0xee)
#define ds1620_comm_stop_conv_t (0x22)
#define ds1620_comm_write_config (0x0c)
#define ds1620_comm_read_config (0xac)
#define decimal_temp(x) (signed char)(x>>1)
#define point_temp(x) (x&0x01)
unsigned int temperature;
unsigned char temperature_temp[16]={0};
void ds1620_write_protocol(unsigned char protocol){
unsigned char i;
dq_out_set; reset_high;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(protocol&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
} reset_low;
}
void ds1620_write_8bit(unsigned char protocol,unsigned char data){
unsigned char i;
dq_out_set; reset_high;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(protocol&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
}
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(data&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
} reset_low;
}
void ds1620_write_16bit(unsigned char protocol,unsigned int data){
unsigned char i;
dq_out_set; reset_high;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(protocol&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
}
for(i=0;i<16;i++){
clk_conv_low;
_delay_us(1);
if(data&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
} reset_low;
}
unsigned char ds1620_read_8bit(unsigned char protocol){
unsigned char data=0;
unsigned char i;
dq_out_set; reset_high;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(protocol&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
} dq_in_set;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(in_pin) data|=(1<<i);
_delay_us(1);
clk_conv_high;
} reset_low; dq_out_set; return data;
}
unsigned int ds1620_read_16bit(unsigned char protocol){
unsigned int data=0;
unsigned char i;
dq_out_set; reset_high;
for(i=0;i<8;i++){
clk_conv_low;
_delay_us(1);
if(protocol&(1<<i)) out_pin|=0x01;
else out_pin&=0xfe;
_delay_us(1);
clk_conv_high;
} dq_in_set;
for(i=0;i<16;i++){
clk_conv_low;
_delay_us(1);
if(in_pin&0x01) data|=(1<<i);
_delay_us(1);
clk_conv_high;
} reset_low; dq_out_set; return data;
}
unsigned int get_temperature(void){
ds1620_read_16bit(ds1620_comm_read_temp);
}
void ds1620_init(void){
dir_pin=0xff; reset_low; clk_conv_high; out_pin|=0x01;
}
void ds1620_setup(void){
ds1620_write_8bit(ds1620_comm_write_config,0x00);
ds1620_write_protocol(ds1620_comm_start_conv_t);
}