Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 16th May 2008, 03:11 PM   (permalink)
Default Reading DS1990a ibutton with 16F Microcontroller

Hey guys,

Has anyone successfully read the address of a DS1990a ibutton with a 16f series PIC microcontroller before? I've got the codes in assembly but I have to do it in C. My compiler is CCS 4.057 btw.

Can anyone here help me with the codes? thanks in advance.
konzen is offline  
Reply With Quote
Old 17th May 2008, 08:10 AM   (permalink)
Default

hey,

I got this from MELabs PICBASIC Compiler - UK Distributor, Worldwide shipping on how to read the address of an ibutton but I cannot seem to get it working.
I have saved the library and invoked it using #include "lib1.h" but I keep getting 6 errors.
the errors have something to do with BYTE, i and data not being valid identifiers. Can someone help me with the code and explain what does BYTE actually mean?


btw big thanks to oscar from chile for providing this piece of code.

#include


void main() {
BYTE buffer[8];
BYTE i;

printf("\r\nWaiting for a touch device...\r\n");
while (TRUE) {
while(!touch_present()) ;
delay_ms(200);
if(touch_present()) {
touch_write_byte(0x33);
for(i=0;i<8;++i)
buffer[i]=touch_read_byte();


}
}
}

//you need to use this lib

///////////////////////////////////////////////////////////////////////////
//// Dallas Touch Driver ////
//// ////
//// present = touch_present() Issues a reset and returns TRUE ////
//// if the touch device is there. ////
//// ////
//// data = touch_read_BYTE() Reads one BYTE from a touch device. ////
//// ////
//// ok = touch_write_BYTE(data) Writes one BYTE to a touch device ////
//// and returns TRUE if all went OK. ////
//// A FALSE indicates a collision with ////
//// another device. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////

#ifndef TOUCH_PIN
#define TOUCH_PIN PIN_B0
#if defined(__PCH__)
#bit TOUCH_PIN_BIT = 0xF8A.0
#else
#bit TOUCH_PIN_BIT = 6.0
#endif
#endif


BYTE touch_read_byte() {
BYTE i,data;

for(i=1;i<=8;++i) {
output_low(TOUCH_PIN);
delay_us(14);
output_float(TOUCH_PIN);
delay_us(5);
shift_right(&data,1,input(TOUCH_PIN));
delay_us(100);
}
return(data);
}

BYTE touch_write_byte(BYTE data) {
BYTE i;

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

BYTE touch_present() {
BOOLEAN present;

output_low(TOUCH_PIN);
delay_us(500);
output_float(TOUCH_PIN);

delay_us(5);

if(!input(TOUCH_PIN))
return(FALSE);
delay_us(65);
present=!input(TOUCH_PIN);
delay_us(240);
if(present)
return(TRUE);
else
return(FALSE);
}
konzen is offline  
Reply With Quote
Old 17th May 2008, 08:18 AM   (permalink)
Default

Quote:
Originally Posted by konzen View Post
I have saved the library and invoked it using #include "lib1.h" but I keep getting 6 errors.
the errors have something to do with BYTE, i and data not being valid identifiers. Can someone help me with the code and explain what does BYTE actually mean?
Probably your compiler doesn't accept 'BYTE' as a type qualifier. Try replacing 'BYTE' with 'unsigned char'.
eng1 is offline  
Reply With Quote
Old 17th May 2008, 08:19 AM   (permalink)
Default

Try changing BYTE to char or unsigned char.

Mike.
Pommie is offline  
Reply With Quote
Old 17th May 2008, 08:24 AM   (permalink)
Default

Will try it. Thanks guys.
konzen is offline  
Reply With Quote
Old 17th May 2008, 08:25 AM   (permalink)
Default

One more thing, since the PIC has no form of graphical output, what does this line actually do? printf("\r\nWaiting for a touch device...\r\n");
konzen is offline  
Reply With Quote
Old 17th May 2008, 08:35 AM   (permalink)
Default

Quote:
Originally Posted by konzen View Post
One more thing, since the PIC has no form of graphical output, what does this line actually do? printf("\r\nWaiting for a touch device...\r\n");
That depends on what it's setup to do. Some compilers send it via RS232, others to an LCD.

Mike.
Pommie is offline  
Reply With Quote
Old 18th May 2008, 10:58 AM   (permalink)
Default

Just a heads up for the OP, you will get a lot more in depth response on compiler specific questions on the PDS Forums, not that it really matters, just a heads up


__________________
Spency.

PIC Micro's - Your mind is the limit

PIC's and interfacing with other devices - a PIC Basic Guide @ digital-diy.net
gramo is offline  
Reply With Quote
Old 23rd May 2008, 07:42 AM   (permalink)
Default

guys, i've tried to change BYTE to unsigned char and char but i still get the same errors.

here's what it says;

*** Error 51 "test1a.c" Line 21(1,9): A numeric expression must appear here
*** Error 51 "test1a.c" Line 22(1,4): A numeric expression must appear here
*** Error 12 "test1a.c" Line 30(5,6): Undefined identifier i
*** Error 12 "test1a.c" Line 30(9,10): Undefined identifier i
*** Error 12 "test1a.c" Line 30(15,16): Undefined identifier i
5 Errors, 1 Warnings.

bah i'm wits end!!!
konzen is offline  
Reply With Quote
Old 23rd May 2008, 08:07 AM   (permalink)
Default

Can you post the code. Put [code] before the code and [/code] after it to preserve the formating. It would help if you identified the error lines.

Mike.
Pommie is offline  
Reply With Quote
Old 23rd May 2008, 08:16 AM   (permalink)
Default

I've got it. I made a fundamental mistake declaring the variables within the while(1) loop.
I've changed it and the thing builds fine.
Now i want to check whether the PIC has successfully read the ibutton. Any ideas?
I've used the read function in MPLAB to read the contents of the PIC but apparently all the registers are still zero. something is not right hmmm

here's the code btw

Code:
#include <16F628.h>
#include <stdio.h>
#use fixed_io(b_outputs=PIN_B4)
#use delay(xtal = 4000000)
#include "lib2.h"


void main()
{

BYTE buffer[8];
BYTE i;

	while(1){

	output_high(PIN_B4);
    delay_ms(500);

	output_low(PIN_B4);
	delay_ms(500);
	

//printf("\r\nWaiting for a touch device...\r\n");

while(!touch_present()) ;
delay_ms(200);
if(touch_present()) {
touch_write_byte(0x33);
for(i=0;i<8;++i)
buffer[i]=touch_read_byte();


}
	}
	}
and the library

Code:
#ifndef TOUCH_PIN
#define TOUCH_PIN PIN_B0
#if defined(__PCH__)
#bit TOUCH_PIN_BIT = 0xF8A.0
#else
#bit TOUCH_PIN_BIT = 6.0
#endif
#endif

BYTE touch_read_byte() {
BYTE i,data;


for(i=1;i<=8;++i) {
output_low(TOUCH_PIN);
delay_us(14);
output_float(TOUCH_PIN);
delay_us(5);
shift_right(&data,1,input(TOUCH_PIN));
delay_us(100);
}
return(data);
}

BYTE touch_write_byte(BYTE data) {
BYTE i;

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

BYTE touch_present() {
BOOLEAN present;

output_low(TOUCH_PIN);
delay_us(500);
output_float(TOUCH_PIN);

delay_us(5);

if(!input(TOUCH_PIN))
return(FALSE);
delay_us(65);
present=!input(TOUCH_PIN);
delay_us(240);
if(present)
return(TRUE);
else
return(FALSE);
}
Thanks

Last edited by konzen; 23rd May 2008 at 08:17 AM.
konzen is offline  
Reply With Quote
Old 23rd May 2008, 08:55 AM   (permalink)
Default

You could write it into EEPROM and read it that way.
Code:
if(touch_present()) {
    touch_write_byte(0x33);
    for(i=0;i<8;++i){
        buffer[i]=touch_read_byte();
        write_eeprom(i,buffer[i]);
    }
}
Blue bits added.

Mike.
Pommie is offline  
Reply With Quote
Old 23rd May 2008, 10:14 AM   (permalink)
Default

I think the DS1990A does not have EEPROM. it's an ID only ibutton. I've tried using an osciloscope to read the voltage levels that the input pin is seeing (pin b0) but everytime i touch the ibutton to the to reader, all i get is minor fluctuations in the mV region.

besides that, I've also entered codes to turn off an led everytime the ibutton is touched,

Code:
while(!touch_present()) ;
output_high(PIN_B4);	// while ibutton not touched....turn on LED
delay_ms(200);
if(touch_present()) {
output_low(PIN_B4);    // when ibutton touched... turn it off
but the LED doesnt even light up in the 1st place leading me to suspect that the PIC is not even stepping into that part of the code.

thanks
konzen is offline  
Reply With Quote
Old 23rd May 2008, 01:38 PM   (permalink)
Default

write_eeprom writes to the EEPROM in the pic. You can then read the EEPROM with your programmer to see the ID.

Can you post more of your LED code.

Mike.
Pommie is offline  
Reply With Quote
Old 24th May 2008, 03:37 AM   (permalink)
Default

I see. perhaps i'm running incompatible code on my computer? My compiler is CCS 4.057 but I do not know what compiler that code is based on.

Code:
#include <16F628.h>
#include <stdio.h>
#use fixed_io(b_outputs=PIN_B4)
#use delay(xtal = 4000000)
#include "lib2.h" 
#use rs232(baud = 9600, xmit=PIN_B2, rcv=PIN_B1,  stop=1,)


void main()
{

BYTE buffer[8];
BYTE i;

	while(TRUE){


//printf("\r\nWaiting for a touch device...\r\n");

while(!touch_present()) ;
output_high(PIN_B4);	
delay_ms(200);

if(touch_present()) {
output_low(PIN_B4);

touch_write_byte(0x33);
for(i=0;i<8;++i)
buffer[i]=touch_read_byte();
}

for(i=0; i<8; i++)
   putc(buffer[i]);

	}
	}
the library code remains untouched.

oh yeah btw sometimes my LED does get turned on indicating that the program has stepped into the while(!touch_present) area. sometimes it doesn't.

btw how do i use the read function on my compiler to read the eeprom?
konzen is offline  
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
interfacing ibutton to PC using USB konzen General Electronics Chat 26 13th May 2008 10:31 AM
8051 microcontroller reading data from serial port godatguitar Micro Controllers 11 17th February 2004 08:03 PM
Specifications Applied Caltech General Electronics Chat 1 25th December 2003 05:00 PM



All times are GMT. The time now is 11:15 PM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.