Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Reading DS1990a ibutton with 16F Microcontroller

Status
Not open for further replies.

konzen

New Member
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.
 
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=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);
}
 
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'.
 
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");
 
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.
 
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!!!
 
Can you post the code. Put
Code:
 before the code and [/co[U][/U]de] after it to preserve the formating. It would help if you identified the error lines.

Mike.
 
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:
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)[COLOR="Blue"]{[/COLOR]
        buffer[i]=touch_read_byte();
        [COLOR="blue"]write_eeprom(i,buffer[i]);
    }[/COLOR]
}
Blue bits added.

Mike.
 
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
 
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.
 
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?
 
oh one more thing, when i declare variables as byte, it doesn't turn blue the way int, for, if and while turns blue. i'm starting to believe that byte is not the correct choice of variable type. perhaps i'll try unsigned char.
 
Do you realise that your code is waiting until the button is missing?

I indented and commented your code,
Code:
void main()
{
BYTE buffer[8];
BYTE i;
    while(TRUE){
        //printf("\r\nWaiting for a touch device...\r\n");

        while(!touch_present());	//wait for button to be missing
        output_high(PIN_B4);		//light LED
        delay_ms(200);			

        if(touch_present()) {		//if button is present
            output_low(PIN_B4);		//extinguish LED
            touch_write_byte(0x33);	//write command ReadRom
            for(i=0;i<8;++i)		//read the 8 byte ID
            	buffer[i]=touch_read_byte();
        }
        for(i=0; i<8; i++)
           putc(buffer[i]);

    }
}

The write_eeprom routine I used above is for the CCS compiler. I found it on page 243 of this document.

Mike.
 
Last edited:
Really?

Isn't that line saying 'as long as the ibutton is missing?'

Code:
void main()
{
BYTE buffer[8];
BYTE i;
    while(TRUE){
        //printf("\r\nWaiting for a touch device...\r\n");

        while(!touch_present());	//wait for button to be missing...'as long as ibutton is     
                                                   missing'
        output_high(PIN_B4);		//light LED
        delay_ms(200);			

        if(touch_present()) {		//if button is present
            output_low(PIN_B4);		//extinguish LED
            touch_write_byte(0x33);	//write command ReadRom
            for(i=0;i<8;++i)		//read the 8 byte ID
            	buffer[i]=touch_read_byte();
        }
        for(i=0; i<8; i++)
           putc(buffer[i]);

    }
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top