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

Migration problem
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Apr 10, 2008 6:24 pm     Reply with quote

If you want to disable the temperature displaying then do it like:
Code:
   disable_interrupts(INT_EXT);
   if (fs_flag==0)
   {
 //     show_temp();
   }
   enable_interrupts(INT_EXT);
or
Code:
   disable_interrupts(INT_EXT);
 //  if (fs_flag==0)
 //  {
 //     show_temp();
 //  }
   enable_interrupts(INT_EXT);
Your choice was in between with a different result.

Quote:
On a similar note, I'm using a JDM Enhanced programmer to write to the chip, There MUST be an easier way than to swap it in and out with the old chip pliers.

Will an ICD or other device aid in all of this? suggestions?
Most developers will program the chip on the board, Microchip calls this in-circuit-serial-programming (ICSP). It requires a 5-pin connector on your board and sacrificing two I/O-pins. The ICD2 is a versatile device for both programming and debugging but will set you back about $159. There are cheaper clones available. Another possibility is the PICkit2 programmer for $35, this does also support debugging but I've never come to try that yet.

Check your mailbox for a new DS18B20 driver. If it works we can post it in the Code Library.
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Thu Apr 10, 2008 8:12 pm     Reply with quote

Thanks again for the assistance

I've got the code running for the night, getting wierd temps but it could be a bad sensor.

I know for use one sensor is shot. it locks up the display after about 2 minutes. the others are all jumping around in 0,8,16,32,48,96 increments.

Cheers
Steve
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 4:02 pm     Reply with quote

Spent the last few hours tinkering, tossed out 2 of my 4 ds18b20's.

I had to split the code into two groups, 1 the clock, and 2 the temperature.

I've made sense out of the clock code, and its running fine, so take a look and let me know if I'm picking up bad coding habits please.

While not an original of my own making, I'm happy to see it works.

Code:
//========================================
//========================================
//          24 hour Clock
//       using Dallas ds1307
//           TPIC6B595N
//              2008
//========================================
//========================================
#include <16F628A.h>
#use delay(clock=4000000)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#define RTC_SDA         PIN_A2               
#define RTC_SCL         PIN_A3               
#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)   

#define DEC             PIN_B1
#define INC             PIN_B2
#define EXP_OUT_CLOCK   PIN_B3
#define colon           PIN_B4
#define EXP_OUT_DO      PIN_B5
#define EXP_OUT_CLEAR   PIN_B6
#define EXP_OUT_LATCH   PIN_B7

BYTE CONST MAP1[10]={0xFA,0x30,0xD9,0x79,0X33,0x6B,0xEB,0x38,0xFB,0x7B};
BYTE CONST MAP2[10]={0xDE,0x18,0xCD,0x5D,0X1B,0x57,0xD7,0x1c,0xDF,0x5F};
BYTE sec,min,hour;
BYTE fs_flag;
BYTE RES;
BYTE D_SHIFT;

//========================================
// initial DS1307
//========================================
void init_DS1307()
{
output_float(RTC_SCL);
output_float(RTC_SDA);
}

//========================================
// write data one byte to DS1307
//========================================
void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
   i2c_start();
   status=i2c_write(0xd0);
}
}

//========================================
// read data one byte from DS1307
//========================================
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}

//=================================
// WRITE DATA TO 6B595
//=================================
void write_expanded_outputs(BYTE D)
{
BYTE i;
for(i=1;i<=8;++i)
{  // Clock out bits from the eo array
 if ((D & 0x80)==0)
   output_low(EXP_OUT_DO);
 else
   output_high(EXP_OUT_DO);
D=D<<1;
output_high(EXP_OUT_CLOCK);
output_low(EXP_OUT_CLOCK);
}
}

//=================================
// show time 1
//=================================
void show_time1()
{
byte m;
      output_low(EXP_OUT_LATCH);
      output_low(colon);
      min=read_ds1307(1);
      hour=read_ds1307(2);
      m=min & 0x0F;
      D_SHIFT=MAP2[m];
      write_expanded_outputs(D_SHIFT);
      swap(min);
      m=min & 0x07;
      D_SHIFT=MAP2[m];
      write_expanded_outputs(D_SHIFT);
      m=hour & 0x0F;
      D_SHIFT=MAP1[m];
      write_expanded_outputs(D_SHIFT);
      swap(hour);
      m=hour & 0x03;
      D_SHIFT=MAP1[m];
      write_expanded_outputs(D_SHIFT);
      output_high(EXP_OUT_LATCH);
      swap(min);
      swap(hour);
}

//=================================
// check switch
//=================================
void check_sw()
{
byte j;
if (fs_flag!=0)
{
   if (!input(INC))
   {
      if (fs_flag==1)
      {
         min=read_ds1307(1);
         min++;
         j=min & 0x0F;
         if (j>=0x0A) min=min+0x06;
         if (min>=0x60) min=0;
         write_ds1307(1,min);
      }
      else
      {
         hour=read_ds1307(2);
         hour++;
         j=hour & 0x0F;
         if (j>=0x0A) hour=hour+0x06;
         if (hour>=0x24) hour=0;
         write_ds1307(2,hour);
      }
    show_time1();
   }
   if (!input(DEC))
   {
      if (fs_flag==1)
      {
         min=read_ds1307(1);
         if (min!=0)
         {
            min--;
            j=min & 0x0F;
            if (j>=0x0A) min=min-0x06;
         }
         else min=0x59;
         write_ds1307(1,min);
      }
      else
      {
         hour=read_ds1307(2);
         if (hour!=0)
         {
            hour--;
            j=hour & 0x0F;
            if (j>=0x0A) hour=hour-0x06;
         }
         else hour=0x23;
         write_ds1307(2,hour);
      }
    show_time1();
   }
}
}

//=================================
// show time
//=================================
void show_time(byte fs)
{
byte m;
      output_low(EXP_OUT_LATCH);
      output_low(colon);
      min=read_ds1307(1);
      hour=read_ds1307(2);
      m=min & 0x0F;
      D_SHIFT=MAP2[m];
      write_expanded_outputs(D_SHIFT);
      swap(min);
      m=min & 0x07;
      D_SHIFT=MAP2[m];
      write_expanded_outputs(D_SHIFT);
      m=hour & 0x0F;
      D_SHIFT=MAP1[m];
      write_expanded_outputs(D_SHIFT);
      swap(hour);
      m=hour & 0x03;
      D_SHIFT=MAP1[m];
      write_expanded_outputs(D_SHIFT);
      output_high(EXP_OUT_LATCH);
      swap(min);
      swap(hour);
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      //delay_ms(500);
      if (fs==0)
      {
         output_high(colon);
      //   delay_ms(500);
      }
      else
      {
         if (fs==1)
         {
            output_high(colon);
            output_low(EXP_OUT_LATCH);
            D_SHIFT=0x00;
            write_expanded_outputs(D_SHIFT);
            D_SHIFT=0x00;
            write_expanded_outputs(D_SHIFT);
            m=hour & 0x0F;
            D_SHIFT=MAP1[m];
            write_expanded_outputs(D_SHIFT);
            swap(hour);
            m=hour & 0x03;
            D_SHIFT=MAP1[m];
            write_expanded_outputs(D_SHIFT);
            output_high(EXP_OUT_LATCH);
            //delay_ms(500);
         }
         else
         {
            output_high(colon);
            output_low(EXP_OUT_LATCH);
            m=min & 0x0F;
            D_SHIFT=MAP2[m];
            write_expanded_outputs(D_SHIFT);
            swap(min);
            m=min & 0x07;
            D_SHIFT=MAP2[m];
            write_expanded_outputs(D_SHIFT);
            D_SHIFT=0x00;
            write_expanded_outputs(D_SHIFT);
            D_SHIFT=0x00;
            write_expanded_outputs(D_SHIFT);
            output_high(EXP_OUT_LATCH);
            //delay_ms(500);
         }
      }
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
      delay_ms(100);
      check_sw();
}
#int_EXT
void EXT_isr()
{
fs_flag++;
if (fs_flag>=3) fs_flag=0;
delay_ms(100);
}


//=====================================
// main program start here
//=====================================
void main(void)
{
byte u;
delay_ms(5);
port_b_pullups(true);
output_low(EXP_OUT_CLEAR);
delay_us(10);
output_high(EXP_OUT_CLEAR);
for(RES=0;RES<80;RES++)
init_ds1307();
u=read_ds1307(0);
sec=u & 0x7F;// enable RTC
write_ds1307(0,sec);   // set second to 00 and enable clock(bit7=0)
output_low(EXP_OUT_CLOCK);
fs_flag=0;
ext_int_edge(H_TO_L);      // init interrupt triggering for button press
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
while(true)
{
   disable_interrupts(INT_EXT);
 //  if (fs_flag==0)
 //  {
 //     show_temp();
 //  }
   enable_interrupts(INT_EXT);
   for (u=0;u<20;u++)
   {
      show_time(fs_flag);
   }
}
} //end of main program





Now the Temperature code

I again stripped out the clock code to take a clear look at it and test it on its own merits.

Its constantly flashing, cant see the error to stop that, or perhaps it needs to, to update.
It uses 9 bit data only, I'd like to expand it to 12Bit to make use of the minus temps and the .1 degree
with the .1 degree resolution, I'll need to shift the "c" symbol to the right.
I'll work on that part first

Code:

//========================================
//========================================
//          Temperature
//       using Dallas ds18b20
//           TPIC6B595N
//              2008
//========================================
//========================================
#include <16F628A.h>
#include <ds1820.c>
#use delay(clock=4000000)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protecti

#define EXP_OUT_LATCH   PIN_B7
#define EXP_OUT_CLOCK   PIN_B3
#define EXP_OUT_DO      PIN_B5
#define EXP_OUT_CLEAR   PIN_B6
#define colon           PIN_B4

byte CONST MAP1[10]={0xFA,0x30,0xD9,0x79,0X33,0x6B,0xEB,0x38,0xFB,0x7B};

BYTE buff[9], sensor, n,temp,temp_dec,RES;
BYTE D_SHIFT;


//=================================
// WRITE DATA TO 6B595
//=================================
void write_expanded_outputs(BYTE *D)
{
  BYTE i;

  for(i=1;i<=8;++i)
  {  // Clock out bits from the eo array
    if ((D & 0x80)==0)
      output_low(EXP_OUT_DO);
    else
      output_high(EXP_OUT_DO);

   D=D<<1;
   output_high(EXP_OUT_CLOCK);
   output_low(EXP_OUT_CLOCK);
  }

}

//=================================
// show temperature
//=================================
void show_temp()
{
   int8 count;

    output_high(colon);

    for (count=0;count<8;count++)  // blink temperature 8 times
    {
      output_low(EXP_OUT_LATCH);
      sensor=0;   // DS1820 CONNECT TO PIN A0
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0x44, sensor);  // perform temperature conversion
      while (read_ds1820_one_byte(sensor)==0xff); // wait for conversion complete
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0xbe, sensor);  // read the result

      for (n=0; n<9; n++)     // read 9 bytes but, use only one byte
      {
         buff[n]=read_ds1820_one_byte(sensor);  // read DS1820
      }
      temp=buff[0]>>1;

      if ((buff[0] & 0x1)==1)
         temp_dec=5;
      else
         temp_dec=0;

      D_SHIFT=0x00;
      write_expanded_outputs(D_SHIFT);

      D_SHIFT=0xE1;  // show 'C
      write_expanded_outputs(D_SHIFT);

      RES=TEMP%10;
      D_SHIFT=MAP1[RES];
      write_expanded_outputs(D_SHIFT);

      RES=TEMP/10;
      if (RES==0)
        D_SHIFT=0x00;
      else
        D_SHIFT=MAP1[RES];

      write_expanded_outputs(D_SHIFT);
      output_high(EXP_OUT_LATCH);
      delay_ms(500);

      output_low(EXP_OUT_LATCH);
      for (n=1;n<=4;n++)
      {
         D_SHIFT=0x00;
         write_expanded_outputs(D_SHIFT);
      }
      output_high(EXP_OUT_LATCH);
      delay_ms(500);
    }
}


//=====================================
// main program start here
//=====================================
void main(void)
{
//byte u;
   delay_ms(5);
   port_b_pullups(true);
   output_low(EXP_OUT_CLEAR);
   delay_us(10);
   output_high(EXP_OUT_CLEAR);

   for(RES=0;RES<80;RES++)
   {
      sensor=0;   // DS1820 CONNECT TO PIN A0
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0x44, sensor);  // perform temperature conversion
      while (read_ds1820_one_byte(sensor)==0xff); // wait for conversion complete
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0xbe, sensor);  // read the result

      for (n=0; n<9; n++)     // read 9 bytes but, use only one byte
      {
         buff[n]=read_ds1820_one_byte(sensor);  // read DS1820
      }
   {
         show_temp();
    }
        }
}//end of main program


Changing "n<9" to "n<12" hangs the display
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 4:43 pm     Reply with quote

You wanted to know about bad coding practices, here is one:
Quote:
Changing "n<9" to "n<12" hangs the display
The use of a hard coded value like this is sometimes referred to as using a 'Magic number' because from the value itself you can not relate it's purpose. Your error is that you didn't make the change in all 3 locations where the buffer size is used.
The solution to this is to use a #define like:
Code:
#define DS1820_SCRATCH_BUFFER_SIZE    9
...
BYTE buff[DS1820_SCRATCH_BUFFER_SIZE];
...
for (n=0; n<DS1820_SCRATCH_BUFFER_SIZE; n++)     // read 9 bytes but, use only one byte
Now you see the two advantages directly:
1) The code is self explanatory.
2) If you want to change the buffer size than you have to make this change at only one location. No risk of forgetting to change a line.

Now for the change of 9 to 12 bits: The buffer size 9 above has nothing to do with the 9 bit resolution in the DS1820. On the 12 bit DS18B20 this buffer is still 9 bytes but the format of the first two bytes has changed.

For the 12-bit code to work instead of the 9-bit version, replace:
Code:
      temp=buff[0]>>1;

      if ((buff[0] & 0x1)==1)
         temp_dec=5;
      else
         temp_dec=0;
by:
Code:
temp = make16(buff[0], buff[1]) >> 4;
temp_dec = (buff[1] & 0x0F) / 16;


Last remarks:
- Why do you read the temperature in the main() function and again in the show_temp() function? This is a waste of time and memory.
- Instead of looping 80 times in the main function you should change this to an forever loop (while(1)). Now after 80 loops the processor executes the sleep instruction at the end of main() and stops working.


Last edited by ckielstra on Fri Apr 11, 2008 4:59 pm; edited 1 time in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 4:57 pm     Reply with quote

Quote:
Its constantly flashing, cant see the error to stop that, or perhaps it needs to, to update.
Code:
      delay_ms(500);

      output_low(EXP_OUT_LATCH);
      for (n=1;n<=4;n++)
      {
         D_SHIFT=0x00;
         write_expanded_outputs(D_SHIFT);
      }
      output_high(EXP_OUT_LATCH);
      delay_ms(500);
This is the part that clears the display. I didn't understand why you wanted to have a flashing display but I thought it was there on purpose. If you don't want the display to flash than remove the shown lines.
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 5:11 pm     Reply with quote

Quote:
Code:
delay_ms(500);

output_low(EXP_OUT_LATCH);
for (n=1;n<=4;n++)
{
D_SHIFT=0x00;
write_expanded_outputs(D_SHIFT);
}
output_high(EXP_OUT_LATCH);
delay_ms(500);

This is the part that clears the display.


Done, thanks.. it no longer flashes.





Quote:
Code:
#define DS1820_SCRATCH_BUFFER_SIZE 9
...
BYTE buff[DS1820_SCRATCH_BUFFER_SIZE];
...
for (n=0; n<DS1820_SCRATCH_BUFFER_SIZE; n++) // read 9 bytes but, use only one byte
Now you see the two advantages directly:
1) The code is self explanatory.
2) If you want to change the buffer size than you have to make this change at only one location. No risk of forgetting to change a line.


Done, as well, Thanks as well =)

Quote:
- Instead of looping 80 times in the main function you should change this to an forever loop (while(1)). Now after 80 loops the processor executes the sleep instruction at the end of main() and stops working.
<-- working on this one.
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 6:31 pm     Reply with quote

Ok, I think I corrected and modified everything mentioned.

Here is the new code.

Code:

//========================================
//========================================
//          Temperature
//       using Dallas ds18b20
//           TPIC6B595N
//              2008
//========================================
//========================================
#include <16F628A.h>
#include <ds1820.c>
#use delay(clock=4000000)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protecti

#define EXP_OUT_LATCH   PIN_B7
#define EXP_OUT_CLOCK   PIN_B3
#define EXP_OUT_DO      PIN_B5
#define EXP_OUT_CLEAR   PIN_B6
#define colon           PIN_B4
#define DS1820_SCRATCH_BUFFER_SIZE  9

BYTE CONST MAP1[10]={0xFA,0x30,0xD9,0x79,0X33,0x6B,0xEB,0x38,0xFB,0x7B};
BYTE buff[DS1820_SCRATCH_BUFFER_SIZE];
BYTE sensor;
BYTE n;
BYTE temp;
BYTE temp_dec;
BYTE RES;
BYTE D_SHIFT;

//=================================
// WRITE DATA TO 6B595
//=================================
void write_expanded_outputs(BYTE *D)
{
  BYTE i;
  for(i=1;i<=8;++i)
  {  // Clock out bits from the eo array
    if ((D & 0x80)==0)
      output_low(EXP_OUT_DO);
    else
      output_high(EXP_OUT_DO);
   D=D<<1;
   output_high(EXP_OUT_CLOCK);
   output_low(EXP_OUT_CLOCK);
  }
}

//=================================
// show temperature
//=================================
void show_temp()
{
      output_low(colon);
  {
      output_low(EXP_OUT_LATCH);
      sensor=0;   // DS1820 CONNECT TO PIN A0
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0x44, sensor);  // perform temperature conversion
      while (read_ds1820_one_byte(sensor)==0xff); // wait for conversion complete
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);  // skip ROM
      write_ds1820_one_byte(0xbe, sensor);  // read the result
     
      for (n=0; n<DS1820_SCRATCH_BUFFER_SIZE; n++)     // read 9 bytes but, use only one byte
      {
         buff[n]=read_ds1820_one_byte(sensor);  // read DS1820
      }
      temp = make16(buff[0], buff[1]) >> 4;
      temp_dec = (buff[1] & 0x0F) / 16;
     
      D_SHIFT=0x00;
      write_expanded_outputs(D_SHIFT);

      D_SHIFT=0xE1;  // show 'C
      write_expanded_outputs(D_SHIFT);

      RES=TEMP%10;
      D_SHIFT=MAP1[RES];
      write_expanded_outputs(D_SHIFT);

      RES=TEMP/10;
      if (RES==0)
        D_SHIFT=0x00;
      else
        D_SHIFT=MAP1[RES];

      write_expanded_outputs(D_SHIFT);
      output_high(EXP_OUT_LATCH);
  }
}

//=====================================
// main program start here
//=====================================
void main(void)
{
//byte u;
   delay_ms(5);
   port_b_pullups(true);
   output_low(EXP_OUT_CLEAR);
   delay_us(10);
   output_high(EXP_OUT_CLEAR);
   {
   for(RES=0;RES<80;RES++)
   show_temp();
   }
}
//end of main program


My attempts to change the RES<80 routine to (While(1)) kept breaking the code, so I will need to research more on that.

Only thing remains is the Temperature value, It begins on power up as 0, 8, 16, 32, 64, 80, 96 and cycles through those values evenutally settling to 0 for a while.. Touching the sensor will jump the value from 0 to 8 or 16 immediately and increase to 96 and stay there until you remove your finger as a heat source. then slowly steps back down to 0 through the 8x table.

All multiples of 8 , a bit odd even if one sensor is fried, but swapping out the 4 remaining sensors in my collection all result in exactly the same values.


I'll include the ds1820.c here as well, maybe someone can spot what I can't see at this point.

ds1820.c
Code:


#BYTE TRISA=0x85
#BYTE PORTA=0x5
#BYTE STATUS=0x3
#define RP0  5
#define C 0


// The following are standard 1-Wire routines.
void make_ds1820_high_pin(int sensor)
{
   TRISA = 0xff;
}

void make_ds1820_low_pin(int sensor)
{
   PORTA = 0x00;
   TRISA = 0xff & (~(0x01 << sensor));
}


// delay routines
void delay_10us(int t)
{
#asm
            BCF STATUS, RP0
 DELAY_10US_X:
            CLRWDT
            NOP
            NOP
            NOP
            NOP
            NOP
            NOP
            DECFSZ t, F
            GOTO DELAY_10US_X
#endasm
}

void delay_ms(long t)   // delays t millisecs
{
   do
   {
     delay_10us(100);
   } while(--t);
}

void init_ds1820(int sensor)
{
   make_ds1820_high_pin(sensor);
   make_ds1820_low_pin(sensor);
   delay_10us(50);

   make_ds1820_high_pin(sensor);
   delay_10us(50);
}

int read_ds1820_one_byte(int sensor)
{
   int n, i_byte, temp, mask;
   mask = 0xff & (~(0x01<<sensor));
   for (n=0; n<8; n++)
   {
 
      PORTA=0x00;
      TRISA=mask;
      TRISA=0xff;
#asm
      CLRWDT
      NOP
      NOP
#endasm
      temp=PORTA;
      if (temp & ~mask)
      {
        i_byte=(i_byte>>1) | 0x80;   // least sig bit first
      }
      else
      {
        i_byte=i_byte >> 1;
      }
      delay_10us(6);
   }
   return(i_byte);
}

void write_ds1820_one_byte(int d, int sensor)
{
   int n, mask;
   mask = 0xff & (~(0x01<<sensor));
   for(n=0; n<8; n++)
   {
      if (d&0x01)
      {
         PORTA=0;
         TRISA=mask;      // momentary low
         TRISA=0xff;
         delay_10us(6);
      }

      else
      {
          PORTA=0;
          TRISA=mask;
     delay_10us(6);
          TRISA=0xff;
      }
      d=d>>1;
   }
}

int16 read_sensor(byte sensor)
{     
   int16 t1;
   int buff[9], n,temp,temp_dec;
   
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);
      write_ds1820_one_byte(0x44, sensor);
      while (read_ds1820_one_byte(sensor)==0xff);
      init_ds1820(sensor);
      write_ds1820_one_byte(0xcc, sensor);
      write_ds1820_one_byte(0xbe, sensor);
      for (n=0; n<9; n++) {
         buff[n]=read_ds1820_one_byte(sensor);  // read DS1820
      }
      temp=buff[0]>>1;
      if ((buff[0] & 0x1)==1)
         temp_dec=5;
      else
         temp_dec=0;
     
     t1=temp;
     t1=(t1<<8) | temp_dec;   // 0xTT0D
     return(t1);
}


Last edited by Aurbo on Sat Apr 12, 2008 10:28 am; edited 1 time in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:12 pm     Reply with quote

Code:
void write_expanded_outputs(BYTE *D)
I'm still surprised this is working. The variable D is not a pointer so the '*' should be removed.

What is your compiler version number?


Last edited by ckielstra on Fri Apr 11, 2008 7:20 pm; edited 2 times in total
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:17 pm     Reply with quote

CCS PCH C Compiler, Version 4.038, 18306 09-Apr-08 19:09

I didnt notice the *D crept back in. It's gone again.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:21 pm     Reply with quote

Quote:
My attempts to change the RES<80 routine to (While(1)) kept breaking the code, so I will need to research more on that.
Most common error is to add a ';' after the while(1), then it will loop the empty instruction ';' forever. Try:
Code:
void main(void)
{
   delay_ms(5);
   port_b_pullups(true);
   output_low(EXP_OUT_CLEAR);
   delay_us(10);
   output_high(EXP_OUT_CLEAR);

   while(TRUE)
   {
      show_temp();
   }
}
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:33 pm     Reply with quote

Thanks for that,

Corrected the code for the *D,
Changed the main section as suggested.

Sensor responds to figertip heat but still in scale*8

I'd hate to think I trashed all my sensors over the last week, seems like all electronic suppliers are out of stock in my area.

The temp is now fluctuating to changes rapidly, but still in *8..
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:33 pm     Reply with quote

Found another classical error, now in my own code: the MSB and LSB temperature byte are in the wrong order. Change:
Code:
      temp = make16(buff[0], buff[1]) >> 4;
      temp_dec = (buff[1] & 0x0F) / 16;
to:
Code:
      temp = make16(buff[1], buff[0]) >> 4;
      temp_dec = (buff[0] & 0x0F) / 16;
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 7:53 pm     Reply with quote

WOW!

I've got my fingers crossed, that may have done it.

Temp is sitting at 21C which is my current room temp.

Fingertip heat moves the scale to 24C and slowly returns to 20-21C

I NEVER would have seen that last transposition.

Thanks, if this is working I'll work on changing the display to show a 10th degree scale on the display, right now its showing whole numbers.

Then the negative display scale, which I hope will just be a new

Code:
#define tnegsign PIN_A1


to fire off an

Code:
output_high(tnegsign);


When the negative indicator bit is present
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 8:34 pm     Reply with quote

I'm glad to hear it seems to work now.

The method for the negative sign is almost correct, except that the value returned by the DS18B20 is in 2's complement notation and the currently used arithmetic can't handle that.

I also changed the code to show the tenth degree values, except for the dot, I don't know your hardware connections for that.

Change the show_temp() function to:
Code:
void show_temp()
{
  signed int16 Temp16;
  int1 TempIsPos;
  int8 sensor;
  int8 n;
 
  output_low(colon);
  output_low(EXP_OUT_LATCH);
 
  // Read Temperature from sensor
  sensor=0;   // DS1820 CONNECT TO PIN A0
  init_ds1820(sensor);
  write_ds1820_one_byte(0xcc, sensor);  // skip ROM
  write_ds1820_one_byte(0x44, sensor);  // perform temperature conversion
  while (read_ds1820_one_byte(sensor)==0xff); // wait for conversion complete
  init_ds1820(sensor);
  write_ds1820_one_byte(0xcc, sensor);  // skip ROM
  write_ds1820_one_byte(0xbe, sensor);  // read the result
 
  for (n=0; n<DS1820_SCRATCH_BUFFER_SIZE; n++)     // read 9 bytes but, use only one byte
  {
     buff[n]=read_ds1820_one_byte(sensor);  // read DS1820
  }
  Temp16 = make16(buff[1], buff[0]);

  // Split temperature in a temperature sign, positive degrees and a fraction
  if (Temp16 >= 0)
  {
    TempIsPos = TRUE;
  }
  else
  {
    TempIsPos = FALSE;
    Temp16 = -Temp16;     // Make absolute value
  }
  temp = Temp16 >> 4;
  temp_dec = ((Temp16 & 0x0F) * 10) / 16;

  // Display data
  D_SHIFT=0xE1;                 // Show 'C
  write_expanded_outputs(D_SHIFT);

  D_SHIFT = MAP1[ temp_dec ];  // Show fraction
  write_expanded_outputs(D_SHIFT);

  RES = temp%10;              // Show 1's
  D_SHIFT = MAP1[RES];
  write_expanded_outputs(D_SHIFT);

  RES=temp/10;                // Show 10's
  if (RES==0)
    D_SHIFT=0x00;
  else
    D_SHIFT=MAP1[RES];
  write_expanded_outputs(D_SHIFT);

  // Show sign
  if (TempIsPos)
  {
    // add here your positive sign showing code.
  }
  else
  {
    // add here your negative sign showing code.
  }

  output_high(EXP_OUT_LATCH);
}
Aurbo



Joined: 07 Apr 2008
Posts: 49

View user's profile Send private message

PostPosted: Fri Apr 11, 2008 9:49 pm     Reply with quote

Thanks again for all of this.. its most welcome,

I had to bring back in

Code:
byte CONST MAP2[10]={0xDE,0x18,0xCD,0x5D,0X1B,0x57,0xD7,0x1c,0xDF,0x5F};


and change

Code:
 D_SHIFT = MAP2[ temp_dec ];  // Show fraction
  write_expanded_outputs(D_SHIFT);



due to the TPIC6B595 pin outs being inversed for the right 2 digits.

Otherwise its working beautifuly!

Now I need to remember what the shift register call was to light up the 2 digit dot-point to give me the decimal point.

Code:
#define Tnegsign        PIN_A1


Code:

// Show sign
  if (TempIsPos)
  {
    output_low(Tnegsign); // add here your positive sign showing code.
  }
  else
  {
   output_high(Tnegsign); // add here your negative sign showing code.
  }

  output_high(EXP_OUT_LATCH);
}


This part is not working yet but should be. I may have to look at the ds1820.c file and see if I'm making A1 digital and blocking the led from lighting up.

I'll poke away at this for another hour or so then call it a night,

Thanks again ckielstra and PCM programmer for showing interest in this project and all the assistance offered.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
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