![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
candyman561
Joined: 22 Mar 2008 Posts: 2 Location: Kitchener Canada
|
PIC thermometer not display neg. temp. |
Posted: Sat Mar 22, 2008 2:59 pm |
|
|
HI.....I am new in the CCS C community and I need help!!!!
I am using a PIC 16f628 for a clock/thermometer with DS1307 and DS18S20....it works OK BUT...the thermometer does not display negative temp. How can I change the codes to display negative temp ? The project was compiled with CCS V 3.222 I do have the name of the site where you can see this program:
Code: |
// DIGITAL thermometer project
// Compiler : CCS V 3.222
//==============================================
#include <16F628.h>
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, PROTECT,NOLVP,NOMCLR,NOLVP // for 628
//#fuses NOWDT,XT, NOPUT, PROTECT // for 84a
#include <ds1820.c>
#define DS1307_SDA PIN_A2
#define DS1307_SCL PIN_A3
#use i2c(master, sda=DS1307_SDA, scl=DS1307_SCL)
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;
#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 DEC PIN_B1
#define INC PIN_B2
BYTE buff[9], sensor, n,temp,temp_dec,RES;
BYTE D_SHIFT;
//==========================
// initial DS1307
//==========================
void init_DS1307()
{
output_float(DS1307_SCL);
output_float(DS1307_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 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);
}
}
//=================================
// 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++)
{
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
}
}
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
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Mar 24, 2008 6:53 pm |
|
|
Hi,
If you really want to receive some help, you've got to break the problem down into a manageable chunk. The code you posted is far too long for someone to wade through, and most of it is irrelevant to your problem. I suggest that you create a small program that includes only the temp. measurement stuff, and then tell us what IS happening vs. what you expect to happen.
Cheers,
George |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Mar 24, 2008 8:43 pm |
|
|
You're reading nine bytes from the 1820, but you're not doing anything with any of them except the first! In reading through the chip's datasheet, you'll find that the second byte contains the sign information for the temperature; if it isn't zero, then you'll need to perform a two's complement on the first byte to get the magnitude.
If byte two is non-zero, invert the bits of byte one and add one to the result. If byte two is zero, then do nothing to byte one. Remember that bit 0 represents half-degrees, so you'll have to shift byte one to the right one place to read out in full degrees... |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|