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

12f675 and ds18b20

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



Joined: 03 Jun 2005
Posts: 21
Location: TURKEY

View user's profile Send private message Send e-mail MSN Messenger

12f675 and ds18b20
PostPosted: Sun Oct 15, 2006 5:32 am     Reply with quote

Hi Dear Friends
I need ds18b20 and 12f675 working sample code. I tested a sample code.( below page) But I gets only "TEMP:127 C" on hyper terminal. Would you help us please. Thanks...

Code:

#include <12F675.h>
#use delay(clock=4000000)
#fuses NOWDT, XT//,INTRC
#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A3, INVERT)
#define Sensor PIN_A0

int Sensor_Reset(void);
int Sensor_Write(int data);
void Sensor_read(void);
int Temp_H[8],Temp_L[8],i;

void main(){
   int temp;
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   printf("\r\nWaiting for a Sensor...\r\n");
   while (TRUE) {
                  while(!Sensor_Reset()) ;
                  Sensor_Write(0xCC);
                  Sensor_Write(0x44);
                  delay_ms(200);


                  if(Sensor_Reset())
                  {
                     Sensor_Write(0xCC);
                     Sensor_Write(0xBE);
                     Sensor_read();

temp=((Temp_H[0]*64)+(Temp_H[1]*32)+(Temp_H[2]*16)+(Temp_H[3]*8)+(Temp_H[4]*4)+(Temp_H[5]*2)+(Temp_H[6]*1)+(Temp_H[7]?0.51:0.01));
printf("\n\rTemp: %d C\n\r",Temp);
// printf("\n\rTemp: %2.1f C%d%d%d%d%d%d%d%d\n\r",(Temp_L[7]?(-1*Temp):Temp),Temp_L[0],Temp_L[0],Temp_L[2],Temp_L[3],Temp_L[4],Temp_L[5],Temp_L[6],Temp_L[7]);
delay_ms(1000);
}
}
}

int Sensor_Reset(void){
int present;
output_low(Sensor);
delay_us(500);
output_float(Sensor);
delay_us(5);
if(!input(Sensor))
return(FALSE);
delay_us(65);
present=!input(Sensor);
delay_us(240);
if(present)
return(TRUE);
else
return(FALSE);
}


int Sensor_Write(int data){
for(i=1;i<=8;++i){
output_low(Sensor);
delay_us(10);
if(shift_right(&data,1,0)){
output_high(Sensor);
delay_us(10);
}
else{
output_low(Sensor);
delay_us(10);
}
delay_us(50);
output_high(Sensor);
delay_us(50);
}
return(TRUE);
}

void Sensor_read(void){
for(i=0;i<8;i++) {
output_low(Sensor);
delay_us(14);
output_float(Sensor);
delay_us(5);
Temp_H[7-i]=input(Sensor);
delay_us(100);
}
}

C Turner



Joined: 10 Nov 2003
Posts: 40
Location: Utah

View user's profile Send private message

PostPosted: Sun Oct 15, 2006 11:29 am     Reply with quote

When trying to read from the 18b20, do you have a pullup resistor anywhere?

During conversion, the 18b20 needs a "strong" pullup - something that may be done by setting the pin HIGH, but while reading, the device has only an open drain output: At the very least, the device's weak pullups need to be enabled - but I prefer an external (stronger) pullup - especially if moderate line length is involved.

As for the sensor read/write code, I haven't examined it too closely - except to note that the input code is quite a bit more complex than it need be in that bits are mapped into bytes and then awkwardly converted into decimal using the "temp" variable in the main() function: It is quite easy to simply shift the bit into a byte, then mask, shift, and concatentate as necessary to yield the temperature in C - plenty of examples abound on the web.

CT
oyhan



Joined: 03 Jun 2005
Posts: 21
Location: TURKEY

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Sun Oct 15, 2006 1:38 pm     Reply with quote

I use a pullup resistor in my circuit for I/O port. What can I do for this code Because it doesn't work. Thanks...
onurdo



Joined: 16 Oct 2006
Posts: 8

View user's profile Send private message

PostPosted: Mon Oct 16, 2006 7:21 pm     Reply with quote

I made termometer with ds18b20. You must connect this device parasite power mode
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