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 support@ccsinfo.com

Temperature logger

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



Joined: 17 Aug 2011
Posts: 2

View user's profile Send private message

Temperature logger
PostPosted: Wed Aug 17, 2011 4:16 am     Reply with quote

Hi Guys,

Can someone help me please, i am trying to include Cotesta Tolentino's Temperature Logger on one of my project, i have tried modify the code to allow only one Ds18B20 but the simulation does not work accordingly.

help please

Code:

// ************************************************************************************** //
// I use the Temperature Logger - TNT_1.0 - by Cotesta Tolentino - cotestatnt@yahoo.com
 ************************************************************************************** //


#include "Tracker.h"
#include <LCD.C>
#include "onewire.c"
#define PERIOD 10

void init(void);
int16 time = 101;
int1 RST = 0;
//global variable in the " onewire.c" file

void init()
{
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
}


#int_TIMER1
void  TIMER1_isr(void)
{
   time++;
   RST = 1;
}

//main function
void main(void)
{
   int8 i, tmp = 0;
   int16 sensData, celsius, fract;
   int8 scratch[9];     
   unsigned char sign;
   
   init();
   lcd_init();
   output_float(DQ);       // Set as input. 4k7 pullup on bus.
         
  while(TRUE)
   {
      //printf(lcd_putc,("I'm alive!\n"));
      delay_ms(100);
      if (RST){output_toggle(LED); RST = 0;}
     
      if ((!ow_reset())&&(time > PERIOD))     // If a device is present and it's time to send string
      {
      //printf(lcd_putc,("device present and \nready to send \nstrings\n"));
         time = 0;
         write_byte(0xCC); // Skip Rom command
         write_byte(0x44); // Temperature convert command
         output_float(DQ);
         delay_ms(750);    // Max. conv. time is 750mS for 12 bit
         ow_reset();
         
         
            if (!ow_reset())
            {
               //lcd_putc("temp conversion started\n");
               write_byte(0xBE); // Read scratch pad command               
               for(i=0; i<2; i++)
               {
                  scratch[i]= read_byte();
                 
               }
               // raw sensor data (16bit)
               sensData = make16(scratch[1], scratch[0]);
               
               // check negative
               if (bit_test(sensData, 15))
               {
                  sign = '-';
                  sensData = ~sensData + 1;               
               } else
                     sign = '+';
   
               fract = 0;
               tmp = sensData&0xF;        // obtain the fractional part nibble               
               celsius = sensData >> 4 ;  // calculate the whole number part
               
               if (tmp == 0xFF)
                   celsius = celsius + 1;    // Calculate the fractional part           
               else
                  for (i=0; i<tmp; i++)   
                     fract = fract + 0625;   
               lcd_putc("\f");
               printf(lcd_putc,"Temp:%c%03lu.%04lu;", sign, celsius, fract);
             
               
            }     
             
         
         printf ("\n");  // new line when end of all devices on the bus temperature readings is done
       
      }     // if ((!ow_reset())&&(time > PERIOD))
   }     // while (TRUE)
}     // main

Onewire code

// Global variables
int8 dowcrc;            // crc is accumulated in this variable


//calc_CRC - INTERNAL FUNCTION
//Purpose:    To calculate an 8-bit CRC based on a polynomial and the series
//            of data bytes
//Note:       Polynomial used x^8 + x^5 + x^4 + 1 = 10001100
//Inputs:     A pointer to an array of the data bytes and an int saying how many
//            bytes there are in the data array
//Outputs:    An int8 which is the calculated CRC
int8 calc_CRC(int8* data, int8 bytes)
{
   #define CRC_POLY      0x8C
   int8 shift_register = 0, i, datab, bits;

   for(i = 0; i < bytes; ++i)
   {
      datab = *(data + i);

      for(bits = 0; bits < 8; ++bits)
      {
         if(bit_test((shift_register ^ datab), 0))
         {
            shift_register = shift_register >> 1;
            shift_register ^= CRC_POLY;
         }
         else
         {
            shift_register = shift_register >> 1;
         }

         datab = datab >> 1;
      }
   }
   return shift_register;
} //calc_CRC


// Returns 0 for one wire device presence, 1 for none
int8 ow_reset(void)
{
   int8 presence;
   
   output_low(DQ);
   delay_us(488);          // Min. 480uS
   output_float(DQ);
   delay_us(72);           // Takes 15 to 60uS for devices to respond
   presence = input(DQ);
   delay_us(424);          // Wait for end of timeslot
   return(presence);
}

//******************************************************************************
// Read bit on one wire bus
int8 read_bit(void)
{
   output_low(DQ);
   delay_us(1);         // Added, 1uS min. Code relied on 8051 being slow.
   output_float(DQ);
   delay_us(12);        // Read within 15uS from start of time slot
   return(input(DQ));   
}   

//******************************************************************************
void write_bit(int8 bitval)
{
   output_low(DQ);

   if(bitval == 1) {
      delay_us(1);      // 1uS min. Code relied on 8051 being slow.
      output_float(DQ);
   }
   delay_us(105);       // Wait for end of timeslot
   output_float(DQ);
}

//******************************************************************************
int8 read_byte(void)
{
   int8 i;
   int8 val = 0;

   for(i=0;i<8;i++)
   {
      if(read_bit()) val |= (0x01 << i);
      delay_us(120);  // To finish time slot
   }

   return val;
}

//******************************************************************************
void write_byte(int8 val)
{
   int8 i;
   int8 temp;

   for (i=0;i<8;i++)
   {
      temp = val >> i;
      temp &= 0x01;
      write_bit(temp);
   }

   delay_us(105);
}

//******************************************************************************
// One wire crc
int8 ow_crc(int8 x)
{
   dowcrc = calc_CRC(x,8);
   return dowcrc;
}

//******************************************************************************
// Sends Match ROM command to bus then device address
int8 Send_SkipRom (void)//This code was changed from MATCH ROM to skip ROM
{
   
   if (ow_reset()) return FALSE;          // 0 if device present
   write_byte(0xCC);                      // Match ROM
   return TRUE;
}


Code:

#include <16F628A.h>
#device *=16
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading

#use delay(clock=4000000,RESTART_WDT)

#define DQ PIN_B3      // One Wire Bus pin assignment
#define LED PIN_A1     // Status LED


// Software usart will be used with "invert" option.
// so direct connect without interfaces to PC is available
#use rs232(baud=9600,parity=N,xmit=PIN_A4,rcv=PIN_A5,bits=8, invert)



Code:

///////////////////////////////////////////////////////////////////////////
////                             LCDD.C                                ////
////                 Driver for common LCD modules                     ////
////                                                                   ////
////  lcd_init()   Must be called before any other function.           ////
////                                                                   ////
////  lcd_putc(c)  Will display c on the next position of the LCD.     ////
////                     The following have special meaning:           ////
////                      \f  Clear display                            ////
////                      \n  Go to start of second line               ////
////                      \b  Move back one position                   ////
////                                                                   ////
////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)    ////
////                                                                   ////
////  lcd_getc(x,y)   Returns character at position x,y on LCD         ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2007 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                               ////
///////////////////////////////////////////////////////////////////////////

// As defined in the following structure the pin connection is as follows:
//     D0  enable
//     D1  rs
//     D2  rw
//     D4  D4
//     D5  D5
//     D6  D6
//     D7  D7
//
//   LCD pins D0-D3 are not used and PIC D3 is not used.

// Un-comment the following define to use port B
#define use_portB_lcd TRUE


struct lcd_pin_map {                 // This structure is overlayed
           BOOLEAN enable;           // on to an I/O port to gain
           BOOLEAN rs;               // access to the LCD pins.
           BOOLEAN rw;               // The bits are allocated from
           BOOLEAN unused;           // low order up.  ENABLE will
           int     data : 4;         // be pin B0.
        } lcd;


#if defined use_portB_lcd
   #locate lcd = getenv("sfr:PORTB")    // This puts the entire structure over the port
   #ifdef __pch__
    #locate lcd = 0xf81
   #else
    #locate lcd = 6
   #endif
   #define set_tris_lcd(x) set_tris_B(x)
#else
   //#locate lcd = getenv("sfr:PORTD")    // This puts the entire structure over the port
   #ifdef __pch__
    #locate lcd = 0xf83
   #else
    #locate lcd = 8
   #endif
   #define set_tris_lcd(x) set_tris_d(x)
#endif

#ifndef lcd_type
#define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
#endif

#define lcd_line_two 0x40    // LCD RAM address for the second line


BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
                             // These bytes need to be sent to the LCD
                             // to start it up.


                             // The following are used for setting
                             // the I/O port direction register.

struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in



BYTE lcd_read_byte() {
      BYTE low,high;
      set_tris_lcd(LCD_READ);
      lcd.rw = 1;
      delay_cycles(1);
      lcd.enable = 1;
      delay_cycles(1);
      high = lcd.data;
      lcd.enable = 0;
      delay_cycles(1);
      lcd.enable = 1;
      //delay_us(1);
      low = lcd.data;
      lcd.enable = 0;
      set_tris_lcd(LCD_WRITE);
      return( (high<<4) | low);
}


void lcd_send_nibble( BYTE n ) {
      lcd.data = n;
      delay_cycles(1);
      lcd.enable = 1;
      //delay_us(2);
      lcd.enable = 0;
}


void lcd_send_byte( BYTE address, BYTE n ) {

      lcd.rs = 0;
      while ( bit_test(lcd_read_byte(),7) ) ;
      lcd.rs = address;
      delay_cycles(1);
      lcd.rw = 0;
      delay_cycles(1);
      lcd.enable = 0;
      lcd_send_nibble(n >> 4);
      lcd_send_nibble(n & 0xf);
}


void lcd_init() {
    BYTE i;
    set_tris_lcd(LCD_WRITE);
    lcd.rs = 0;
    lcd.rw = 0;
    lcd.enable = 0;
    //delay_ms(15);
    for(i=1;i<=3;++i) {
       lcd_send_nibble(3);
      // delay_ms(5);
    }
    lcd_send_nibble(2);
    for(i=0;i<=3;++i)
       lcd_send_byte(0,LCD_INIT_STRING[i]);
}


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

   if(y!=1)
     address=lcd_line_two;
   else
     address=0;
   address+=x-1;
   lcd_send_byte(0,0x80|address);
}

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

char lcd_getc( BYTE x, BYTE y) {
   char value;

    lcd_gotoxy(x,y);
    while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
    lcd.rs=1;
    value = lcd_read_byte();
    lcd.rs=0;
    return(value);
}
temtronic



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

View user's profile Send private message

PostPosted: Wed Aug 17, 2011 4:27 pm     Reply with quote

Have you got his code to work 'asis '? before you modified it ??

If you're using Proteus as the simulator, I can't help you, as I ONLY deal with real chips in the real world. Proteus is FULL of bugs, errors, flawed DRCs, etc.

I can tell you that there is working code, in the code library. I've modified it for 2 sensors and it seems reliable.
Colin_Kyungu



Joined: 17 Aug 2011
Posts: 2

View user's profile Send private message

PostPosted: Thu Aug 18, 2011 2:35 am     Reply with quote

Temtronic-I have only tried simulate it using Proteus and the value display on the LCD are wrong- i have got your point, let me tried it on the chips and see.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Aug 18, 2011 2:57 pm     Reply with quote

I think you will find a warm reception for all who actually work with hardware - andthen come to a problem spot.

But with the Proteus? In terms of a real working system?

To steal from Willam Gibson - "There is no 'there' , there" ;-))
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