brucelee2
Member level 2
Hi ,
Ive been trying to get temperature from a DS1620 for a while now. Can any body help me? My code is below---Its driving me crazy THANKS Guys!
Ive been trying to get temperature from a DS1620 for a while now. Can any body help me? My code is below---Its driving me crazy THANKS Guys!
Code:
char GLCD_DataPort at PORTD;
sbit GLCD_CS1 at RB0_bit;
sbit GLCD_CS2 at RB1_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;
sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections
sfr sbit DS1620_DQ at RE0_bit; // DQ is bit zero of portE
sfr sbit DS1620_CLK at RE1_bit; // CLK is channel 1 of portE //checkED
sfr sbit DS1620_RST at RE2_bit; // RST is channel 2 of portE
sfr sbit DS1620_DQ_Dir at TRISE0_bit;
sfr sbit DS1620_CLK_Dir at TRISE1_bit;
sfr sbit DS1620_RST_Dir at TRISE2_bit;
unsigned char Get1620byte(void)
{
unsigned char j,k,b;
k=0;
b=1;
DS1620_DQ_Dir=1;
DS1620_CLK_Dir=0;
DS1620_RST_Dir=0;
DS1620_RST=0;
DS1620_RST=1;
for (j=0; j < 8; j++)
{
DS1620_CLK=0;
if (DS1620_DQ)
k=k|b; //if high then 1 @DQ---->mask shift
DS1620_CLK =1;
b = (b << 1); //shifting b to mask k
Delay_1us();
}
DS1620_RST=0;
return k;
}
void Put1620byte(char m)
{
char k;
char b=1;
DS1620_DQ_Dir=0;
DS1620_CLK_Dir=0;
DS1620_RST_Dir=0; // Make bit 0 to 7 output
DS1620_RST=0;
DS1620_RST=1;
for (k = 0; k < 8; k++)
{
DS1620_CLK = 0;
if ((m & b) > 0) //
{
DS1620_DQ = 1;
}
else
{
DS1620_DQ = 0;
}
DS1620_CLK = 1;
b = (b<<1);
Delay_1us();
}
DS1620_RST = 0;
}
int calculate_tens(int value)
{
int tens = 0;
do
{
if (value > 10)
{
value = value - 10;
tens = tens + 1;
}
}while(value > 10);
return tens;
}
int calculate_ones(int value)
{
int ones = 0;
do
{
if (value > 1)
{
value = value - 1;
ones = ones + 1;
}
}while(value > 1);
return ones;
}
void main()
{
int to_calc=0;
int to_calc2=0;
unsigned char get=0;
int t=0;
ADCON1 = 0xFF;
Glcd_Init();
Delay_ms(20);
Put1620byte(0x0C); // write to contro reg...
Delay_ms(20);
Put1620byte(0b00000011);//CPU mode + 1shot conversion
Glcd_Fill(0x00);
while(1){
Put1620byte(0xEE); //start convert
Delay_ms(1000);
get =Get1620byte();
to_calc2 =(int)get; //maybe this is not good??
to_calc=(to_calc2/2);
//to_calc=22;//TO CHECK MY CONVERT TO NUMBER PART -good
t = calculate_tens(to_calc);
Glcd_Write_Char((t+48),1,1,1);
to_calc= to_calc - (10 * t);
t=0;
t= calculate_ones(to_calc);
Glcd_Write_Char((t+48),10,2,1);
to_calc = to_calc - (1 * t);
}
}