- Joined
- Feb 9, 2013
- Messages
- 424
Hi all,
I'm really stuck trying to get my code to work and read temperature from the DS18B20.
After I've parsed the temperature reading I seem to get 0b01111111 in whole degress C.
I used this website to help me: https://www.maximintegrated.com/en/app-notes/index.mvp/id/2420
I'm really stuck trying to get my code to work and read temperature from the DS18B20.
After I've parsed the temperature reading I seem to get 0b01111111 in whole degress C.
I used this website to help me: https://www.maximintegrated.com/en/app-notes/index.mvp/id/2420
Code:
#include <htc.h>
#include <pic.h>
#include "LCD 4-BIT.h"
#define _XTAL_FREQ 4000000
__CONFIG(FOSC_XT & WDTE_OFF & PWRTE_OFF & CP_OFF & BOREN_OFF );
#define DQ PORTCbits.RC0
unsigned char presence;
char temp_lsb,temp_msb,temp_c,temp_sign;
unsigned char ow_reset(void)
{
TRISC0=1;
DQ = 0; //pull DQ line low
TRISC0=0;
__delay_us(480); // leave it low for 480us
TRISC0=1; //Release to return high
DQ = 1; // allow line to return high
__delay_us(60); // wait for presence
presence = DQ; // get presence signal
__delay_us(430); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
unsigned char read_bit(void)
{
DQ = 0; // pull DQ low to start timeslot
TRISC0=0; //Lower the port
TRISC0=1; //Release to return high
DQ = 1; // then return high
__delay_us(15); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}
void write_bit(char bitval)
{
DQ = 0; // pull DQ low to start timeslot
TRISC0=0;
if(bitval==1) DQ =1;// return DQ high if write 1
__delay_us(104); // hold value for remainder of timeslot
TRISC0=1; //Release to return high
DQ = 1;
} // Delay provides 104us
unsigned char read_byte(void)
{
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
// shifts it left
__delay_us(120); // wait for rest of timeslot
}
return(value);
}
void write_byte(char val)
{
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_us(104);
}
void Convert_Temp(void)
{
while(!DQ)
{
read_bit();
}
}
void Read_Temperature(void)
{
char get[10];
int k;
// char temp_f,temp_c;
ow_reset();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
__delay_ms(200);
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++){get[k]=read_byte();}
//printf("\n ScratchPAD DATA = %X%X%X%X%X\n",get[8],get[7],get[6],get[5],get[4],get[3],get[2],get[1],get[0]);
temp_msb = get[1]; // Sign byte + lsbit
temp_sign= get[1];
temp_lsb = get[0]; // Temp data plus lsb
temp_lsb = temp_lsb>>4;
temp_msb = temp_msb<<5;
temp_msb = temp_msb>>1;
temp_sign=temp_sign>>3;
temp_sign=temp_sign<<3;
temp_c = temp_lsb|temp_msb;
/* if (temp_msb <= 0x80){temp_lsb = (temp_lsb/2);} // shift to get whole degree
temp_msb = temp_msb & 0x80; // mask all but the sign bit
if (temp_msb >= 0x80) {temp_lsb = (~temp_lsb)+1;} // twos complement
if (temp_msb >= 0x80) {temp_lsb = (temp_lsb/2);}// shift to get whole degree
if (temp_msb >= 0x80) {temp_lsb = ((-1)*temp_lsb);} // add sign bit
//printf( "\nTempC= %d degrees C\n", (int)temp_lsb ); // print temp. C
temp_c = temp_lsb; // ready for conversion to Fahrenheit
temp_f = (((int)temp_c)* 9)/5 + 32;
//printf( "\nTempF= %d degrees F\n", (int)temp_f ); // print temp. F
*/
}
void Read_ScratchPad(void)
{
int j;
char pad[10];
//printf("\nReading ScratchPad Data\n");
write_byte(0xBE);
for (j=0;j<9;j++){pad[j]=read_byte();}
//printf("\n ScratchPAD DATA = %X%X%X%X%X%X\n",pad[8],pad[7],pad[6],pad[5],pad[4],pad[3],pad[2],pad[1],pad[0]);
}
void Read_ROMCode(void)
{
int n;
char dat[9];
//printf("\nReading ROM Code\n");
ow_reset();
write_byte(0x33);
for (n=0;n<8;n++){dat[n]=read_byte();}
//printf("\n ROM Code = %X%X%X%X\n",dat[7],dat[6],dat[5],dat[4],dat[3],dat[2],dat[1],dat[0]);
}
void main()
{
TRISB = 0x00;
PORTB = 0x00;
TRISC0 = 0;
__delay_ms(1000);
LCD_init();
while(1){
ow_reset(); //Reset Device
write_byte(0xCC); //Skip ROM Command
write_byte(0x4E); //Write Scratchpad command
write_byte(0xFF); //Set TH alarm +125
write_byte(0x7F); //Set TL alarm value -55
write_byte(0x1F); //Set config to 9-bit resolution
ow_reset(); //Reset Device
write_byte(0xCC); //Skip ROM Command
Read_ScratchPad(); //Read Scratchpad Command
ow_reset(); //Reset Device
write_byte(0xCC); //Skip ROM Command
write_byte(0x48); //Copy Scratchpad command */
__delay_ms(10);
Read_Temperature(); //Read Temperature
LCD_cmd(LCD_LINE1);
LCD_string("Temp=");
LCD_data(temp_c);
}
}