CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PROBLEM DS18B20 + PIC 18F4550

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ad



Joined: 24 Mar 2015
Posts: 14

View user's profile Send private message

PROBLEM DS18B20 + PIC 18F4550
PostPosted: Sun Apr 05, 2015 2:01 pm     Reply with quote

I am doing data acquisition unit and I use this sensor. My problem is the following, when I simulate in Proteus and running, but when I load the program through the PICKIT, the sensor reads a temperature of -0.5 ° C. I do not know this happens because my sensor. If someone could advise me. I read the protocol of this sensor, but I can not find solution. Would need to fix this, because I'm doing a project.

Thanks

I use:

-18F4550
-DS18B20
-20MHz
-CHANNEL 0

And my code:

MAIN CODE:
Code:

#include <18F4550.h>
//#device *=18
//#device adc=10

#FUSES NOWDT, HS, PUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT     
#use delay(clock=20000000)

#include "C:\Users\Albert\Desktop\ds18b20(2)\1wire.c"
#include "C:\Users\Albert\Desktop\ds18b20(2)\flex_lcd420.c"
#include "C:\Users\Albert\Desktop\ds18b20(2)\ds1820.c"


void main()
{
float temperature;

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);

lcd_init();
lcd_putc("\f");

while (TRUE)
{
  temperature = ds1820_read();

  lcd_gotoxy(1,1);
  printf(lcd_putc,"TEMP: %3.1f ", temperature);
  lcd_putc(223);
  lcd_putc("C    ");

  lcd_gotoxy(1,2);
  if(temperature >= 29.0)
   printf(lcd_putc,"Hot!    ");
  else if( temperature >= 20 && temperature < 29.0)
   printf(lcd_putc,"Comfort!");
  else
   printf(lcd_putc,"Cold!   ");
 }

}

LCD CODE:
Code:

// Flex_LCD420.c

// These pins are for my Microchip PicDem2-Plus board, which I used to test this driver.
// An external 20x4 LCD is connected to these pins.
// Change these pins to match your own board's connections.
#define LCD_DB7 PIN_C7 // pin 14 LCD // Asignación de pines LCD-PIC en el diseño.
#define LCD_DB6 PIN_C6 // pin 13 LCD
#define LCD_DB5 PIN_C2 // pin 12 LCD
#define LCD_DB4 PIN_C1 // pin 11 LCD
#define LCD_E PIN_E2 // pin 06 LCD
//#define LCD_RW PIN_B0 // pin 05 LCD It is earthed at the display.
#define LCD_RS PIN_E1

// If you want only a 6-pin interface to your LCD, then connect the R/W pin on the LCD to ground,
// and comment out the following line.  Doing so will save one PIC pin,
// but at the cost of losing the ability to read from the LCD. 
// It also makes the write time a little longer because a static delay must be used,
// instead of polling the LCD's busy bit.  Normally a 6-pin interface is only
// used if you are running out of PIC pins, and you need to use as few as possible for the LCD.

//#define USE_RW_PIN   1     

// These are the line addresses for most 4x20 LCDs.
#define LCD_LINE_1_ADDRESS 0x00
#define LCD_LINE_2_ADDRESS 0x40
#define LCD_LINE_3_ADDRESS 0x14
#define LCD_LINE_4_ADDRESS 0x54

// These are the line addresses for LCD's which use the Hitachi HD66712U controller chip.
/*
#define LCD_LINE_1_ADDRESS 0x00
#define LCD_LINE_2_ADDRESS 0x20
#define LCD_LINE_3_ADDRESS 0x40
#define LCD_LINE_4_ADDRESS 0x60
*/

//========================================

#define lcd_type 2   // 0=5x7, 1=5x10, 2=2 lines(or more)

int8 lcd_line;
int8 const LCD_INIT_STRING[4] =
{
 0x20 | (lcd_type << 2),  // Set mode: 4-bit, 2+ lines, 5x8 dots
 0xc,                     // Display on
 1,                       // Clear display
 6                        // Increment cursor
 };
                             
//-------------------------------------
void lcd_send_nibble(int8 nibble)
{
// Note:  !! converts an integer expression to a boolean (1 or 0).
 output_bit(LCD_DB4, !!(nibble & 1));
 output_bit(LCD_DB5, !!(nibble & 2));
 output_bit(LCD_DB6, !!(nibble & 4));   
 output_bit(LCD_DB7, !!(nibble & 8));   

 delay_cycles(1);
 output_high(LCD_E);
 delay_us(2);
 output_low(LCD_E);
}

//-----------------------------------
// This sub-routine is only called by lcd_read_byte(). It's not a stand-alone routine. 
// For example, the R/W signal is set high by lcd_read_byte() before this routine is called.     

#ifdef USE_RW_PIN
int8 lcd_read_nibble(void)
{
int8 retval;
// Create bit variables so that we can easily set individual bits in the retval variable.
#bit retval_0 = retval.0
#bit retval_1 = retval.1
#bit retval_2 = retval.2
#bit retval_3 = retval.3

retval = 0;
   
output_high(LCD_E);
delay_us(1);

retval_0 = input(LCD_DB4);
retval_1 = input(LCD_DB5);
retval_2 = input(LCD_DB6);
retval_3 = input(LCD_DB7);
 
output_low(LCD_E);
delay_us(1);
   
return(retval);   
}   
#endif

//---------------------------------------
// Read a byte from the LCD and return it.

#ifdef USE_RW_PIN
int8 lcd_read_byte(void)
{
int8 low;
int8 high;

output_high(LCD_RW);
delay_cycles(1);

high = lcd_read_nibble();
low = lcd_read_nibble();

return( (high<<4) | low);
}
#endif

//----------------------------------------
// Send a byte to the LCD.
void lcd_send_byte(int8 address, int8 n)
{
output_low(LCD_RS);

#ifdef USE_RW_PIN
while(bit_test(lcd_read_byte(),7)) ;
#else
delay_us(60);
#endif

if(address)
   output_high(LCD_RS);
else
   output_low(LCD_RS);
     
 delay_cycles(1);

#ifdef USE_RW_PIN
output_low(LCD_RW);
delay_cycles(1);
#endif

output_low(LCD_E);

lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
//----------------------------

void lcd_init(void)
{
int8 i;

lcd_line = 1;

output_low(LCD_RS);

#ifdef USE_RW_PIN
output_low(LCD_RW);
#endif

output_low(LCD_E);
 
// Some LCDs require 15 ms minimum delay after power-up.  Others require 30 ms.
// I'm going to set it to 35 ms, so it should work with all of them.
delay_ms(35);         

for(i=0 ;i < 3; i++)
   {
    lcd_send_nibble(0x03);
    delay_ms(5);
   }

lcd_send_nibble(0x02);

for(i=0; i < sizeof(LCD_INIT_STRING); i++)
   {
    lcd_send_byte(0, LCD_INIT_STRING[i]);
   
    // If the R/W signal is not used, then the busy bit can't be polled. 
    // One of the init commands takes longer than the hard-coded delay of 50 us,
    // so in that case, lets just do a 5 ms delay after all four of them.
    #ifndef USE_RW_PIN
    delay_ms(5);
    #endif
   }
}

//----------------------------

void lcd_gotoxy(int8 x, int8 y)
{
int8 address;


switch(y)
  {
   case 1:
     address = LCD_LINE_1_ADDRESS;
     break;

   case 2:
     address = LCD_LINE_2_ADDRESS;
     break;

   case 3:
     address = LCD_LINE_3_ADDRESS;
     break;

   case 4:
     address = LCD_LINE_4_ADDRESS;
     break;

   default:
     address = LCD_LINE_1_ADDRESS;
     break;     
  }

address += x-1;
lcd_send_byte(0, 0x80 | address);
}

//-----------------------------
void lcd_putc(char c)
{
 switch(c)
   {
    case '\f':
      lcd_send_byte(0,1);
      lcd_line = 1;
      delay_ms(2);
      break;
   
    case '\n':
       lcd_gotoxy(1, ++lcd_line);
       break;
   
    case '\b':
       lcd_send_byte(0,0x10);
       break;
   
    default:
       lcd_send_byte(1,c);
       break;
   }
}

//------------------------------
#ifdef USE_RW_PIN
char lcd_getc(int8 x, int8 y)
{
char value;

lcd_gotoxy(x,y);

// Wait until busy flag is low.
while(bit_test(lcd_read_byte(),7));

output_high(LCD_RS);
value = lcd_read_byte();
output_low(LCD_RS);

return(value);
}
#endif

DS18B20 CODE:
Code:

float ds1820_read()
{
 int8 busy=0, temp1, temp2;
 signed int16 temp3;
 float result;

 onewire_reset();
 onewire_write(0xCC);
 onewire_write(0x44);

 while (busy == 0)
  busy = onewire_read();

 onewire_reset();
 onewire_write(0xCC);
 onewire_write(0xBE);
 temp1 = onewire_read();
 temp2 = onewire_read();
 temp3 = make16(temp2, temp1);
 
 result = (float) temp3 / 2.0;   //Calculation for DS18S20 with 0.5 deg C resolution
// result = (float) temp3 / 16.0;  //Calculation for DS18B20 with 0.1 deg C resolution
 
 delay_ms(200);
 return(result);
}

1WIRE CODE:
Code:

/***********************1Wire Class***********************/
/*Description: This class handles all communication */
/* between the processor and the 1wire */
/* sensors.
/*********************************************************/

/*-------1-wire definitions-------*/
#define ONE_WIRE_PIN PIN_A0

/*******************1-wire communication functions********************/

/************onewire_reset*************************************************/
/*This function initiates the 1wire bus */
/* */
/*PARAMETERS: */
/*RETURNS: */
/*********************************************************************/

void onewire_reset()  // OK if just using a single permanently connected device
{
 output_low(ONE_WIRE_PIN);
 delay_us( 500 ); // pull 1-wire low for reset pulse
 output_float(ONE_WIRE_PIN); // float 1-wire high
 delay_us( 500 ); // wait-out remaining initialisation window.
 output_float(ONE_WIRE_PIN);
}

/*********************** onewire_write() ********************************/
/*This function writes a byte to the sensor.*/
/* */
/*Parameters: byte - the byte to be written to the 1-wire */
/*Returns: */
/*********************************************************************/

void onewire_write(int data)
{
 int count;

 for (count=0; count<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate write time-slot.
  output_bit(ONE_WIRE_PIN, shift_right(&data,1,0)); // set output bit on 1-wire
  delay_us( 60 ); // wait until end of write slot.
  output_float(ONE_WIRE_PIN); // set 1-wire high again,
  delay_us( 2 ); // for more than 1us minimum.
 }
}

/*********************** read1wire() *********************************/
/*This function reads the 8 -bit data via the 1-wire sensor. */
/* */
/*Parameters: */
/*Returns: 8-bit (1-byte) data from sensor */
/*********************************************************************/

int onewire_read()
{
 int count, data;

 for (count=0; count<8; ++count)
 {
  output_low(ONE_WIRE_PIN);
  delay_us( 2 ); // pull 1-wire low to initiate read time-slot.
  output_float(ONE_WIRE_PIN); // now let 1-wire float high,
  delay_us( 8 ); // let device state stabilise,
  shift_right(&data,1,input(ONE_WIRE_PIN)); // and load result.
  delay_us( 120 ); // wait until end of read slot.
 }

 return( data );
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Apr 05, 2015 2:18 pm     Reply with quote

Try following these two links

http://www.ccsinfo.com/forum/viewtopic.php?t=47549
http://www.ccsinfo.com/forum/viewtopic.php?t=26245

As a guide:-

Break your code down into easily managed bite size chunks.
Test and modify each piece until it works.
Then proceed to the next.
Finally put them all together.

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 05, 2015 2:26 pm     Reply with quote

Quote:
the sensor reads a temperature of -0.5 ° C.

According to the ds18B20 data sheet, this reading corresponds to a
hex value of FFF8h. That's mostly '1' bits. Possibly there is a problem
with your connections between the PIC and the ds18b20.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Apr 05, 2015 2:39 pm     Reply with quote

I used both that PIC and temp sensor years ago for a 'data logger' project for a client so I know it will work !

There is a 'test program' to locate all 1-wire devices, you should run it to confirm that the hardware is operational.

I notice you use the DS1820.c driver. That may NOT be 100% compatible with the DS18B20 devices. Please check the datasheets, I recall there's a 'timing' issue, though it has been 3-5 years since I worked with it.

After that, look at the 1-wire code to read the sensor and confirm that the 'math' is right to convert the sensor data into the proper temperature reading.

also...
you should have a delay_ms(500) BEFORE yo call he lcd init funcxtion. This allows the LCD hardware to 'wakeup' and 'self configure' correctly before the PIC tries to access it.


hth
jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group