elecgirl88
New Member
Hi every1
Im trying to program a dallas key reader to read the 64 bit serial number of a DS1990A Dallas iButton. Im using MikroC and a PIC16F690.
For testing purposes im using an led which should turn off when the Dallas key is detected.This is working correctly, but the serial number that is being read is incorrect.
i declared the serial number of the key/iButton that i have as array and i compared it to the serial number being read via the 1 wire protocol, if they are the same, then a 2nd led should turn on.(which is not the case)
i have followed source code from microchip app note AN1199. im not sure why the serial number is not being read correctly.
Ive attached my code below, any suggestions will be appreciated. []
thanx in advance
Im trying to program a dallas key reader to read the 64 bit serial number of a DS1990A Dallas iButton. Im using MikroC and a PIC16F690.
For testing purposes im using an led which should turn off when the Dallas key is detected.This is working correctly, but the serial number that is being read is incorrect.
i declared the serial number of the key/iButton that i have as array and i compared it to the serial number being read via the 1 wire protocol, if they are the same, then a 2nd led should turn on.(which is not the case)
i have followed source code from microchip app note AN1199. im not sure why the serial number is not being read correctly.
Ive attached my code below, any suggestions will be appreciated. []
thanx in advance
Code:
////////////////////////////////////////////////////////////////////////////////
// Config.h //
////////////////////////////////////////////////////////////////////////////////
#ifndef _Config_H
#define _Config_H
//========================= Generic Definitions ==============================//
#define HIGH 1
#define LOW 0
#define OUTPUT 0
#define INPUT 1
#define SET 1
#define CLEAR 0
//======================== 1-Wire Port Pin Definition ========================//
// This Configuration is required to make any PIC MicroController
// I/O pin as Open drain to drive 1-wire.
//============================================================================//
#define OW_PIN_DIRECTION TRISB7_bit
#define OW_WRITE_PIN PORTB.RB7 //fOR pic 18f LATB.RB7
#define OW_READ_PIN PORTB.RB7
#endif
//============================= End of Config.h =============================//
Code:
////////////////////////////////////////////////////////////////////////////////
// 1wire.h //
////////////////////////////////////////////////////////////////////////////////
#ifndef _1wire_H
#define _1wire_H
//============================== Includes ==================================//
//#include <p18f4580.h>
//============================= Prototypes ===================================//
void drive_one_wire_low (void);
void drive_one_wire_high (void);
unsigned char read__one_wire (void);
void OW_write_bit (unsigned char write_data);
unsigned char OW_read_bit (void);
unsigned char OW_reset_pulse(void);
void OW_write_byte (unsigned char write_data);
unsigned char OW_read_byte (void);
#endif
//============================= End of 1wire.h =============================//
Code:
////////////////////////////////////////////////////////////////////////////////
// 1-WIRE COMMUNICATION PROTOCOL //
// 1wire.c //
////////////////////////////////////////////////////////////////////////////////
//============================== Includes ====================================//
#include "Config.h"
#include "1wire.h"
//============================= Variables ====================================//
//unsigned char macro_delay;
//----------------------------------------------------------------------------//
// Function: void drive_OW_low (void)
// PreCondition: None
// Input: None
// Output: None
// Overview: Configure the OW_PIN as Output and drive the OW_PIN LOW.
//----------------------------------------------------------------------------//
void drive_OW_low (void)
{
OW_PIN_DIRECTION = OUTPUT;
OW_WRITE_PIN=LOW;
}
//----------------------------------------------------------------------------//
// Function: void drive_OW_high (void)
// PreCondition: None
// Input: None
// Output: None
// Overview: Configure the OW_PIN as Output and drive the OW_PIN HIGH.
//----------------------------------------------------------------------------//
void drive_OW_high (void)
{
OW_PIN_DIRECTION = OUTPUT;
OW_WRITE_PIN = HIGH;
}
//----------------------------------------------------------------------------//
// Function: unsigned char read_OW (void)
// PreCondition: None
// Input: None
// Output: Return the status of OW pin.
// Overview: Configure as Input pin and Read the status of OW_PIN
//----------------------------------------------------------------------------//
unsigned char read_OW (void)
{
unsigned char read_data=0;
OW_WRITE_PIN = INPUT;
if (HIGH == OW_READ_PIN)
read_data = SET;
else
read_data = CLEAR;
return read_data;
}
//----------------------------------------------------------------------------//
// Function: unsigned char OW_reset_pulse(void)
// PreCondition: None
// Input: None
// Output: Return the Presense Pulse from the slave.
// Overview: Initialization sequence start with reset pulse.
// This code generates reset sequence as per the protocol
//----------------------------------------------------------------------------//
unsigned char OW_reset_pulse(void)
{
unsigned char presence_detect;
drive_OW_low(); // Drive the bus low
Delay_us(480); // delay 480 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(70); // delay 70 microsecond (us)
presence_detect = read_OW(); //Sample for presence pulse from slave
Delay_us(410); // delay 410 microsecond (us)
drive_OW_high (); // Release the bus
return presence_detect;
}
//----------------------------------------------------------------------------//
// Function: void OW_write_bit (unsigned char write_data)
// PreCondition: None
// Input: Write a bit to 1-wire slave device.
// Output: None
// Overview: This function used to transmit a single bit to slave device.
//
//----------------------------------------------------------------------------//
void OW_write_bit (unsigned char write_bit)
{
if (write_bit)
{
//writing a bit '1'
drive_OW_low();
Delay_us(6); // Drive the bus low
// delay 6 microsecond (us)
drive_OW_high ();
Delay_us(64); // Release the bus
// delay 64 microsecond (us)
}
else
{
//writing a bit '0'
drive_OW_low(); // Drive the bus low
Delay_us(60);
// delay 60 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(10);
// delay 10 microsecond for recovery (us)
}
}
//----------------------------------------------------------------------------//
// Function: unsigned char OW_read_bit (void)
// PreCondition: None
// Input: None
// Output: Return the status of the OW PIN
// Overview: This function used to read a single bit from the slave device.
//
//----------------------------------------------------------------------------//
unsigned char OW_read_bit (void)
{
unsigned char read_data; //reading a bit
drive_OW_low(); // Drive the bus low
Delay_us(6);
// delay 6 microsecond (us)
drive_OW_high (); // Release the bus
Delay_us(9); // delay 9 microsecond (us)
read_data = read_OW(); //Read the status of OW_PIN
Delay_us(55); // delay 55 microsecond (us)
return read_data;
}
//----------------------------------------------------------------------------//
// Function: void OW_write_byte (unsigned char write_data)
// PreCondition: None
// Input: Send byte to 1-wire slave device
// Output: None
// Overview: This function used to transmit a complete byte to slave device.
//
//----------------------------------------------------------------------------//
void OW_write_byte (unsigned char write_data)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
OW_write_bit(write_data & 0x01); //Sending LS-bit first
write_data >>= 1; // shift the data byte for the next bit to send
}
}
//----------------------------------------------------------------------------//
// Function: unsigned char OW_read_byte (void)
// PreCondition: None
// Input: None
// Output: Return the read byte from slave device
// Overview: This function used to read a complete byte from the slave device.
//
//----------------------------------------------------------------------------//
unsigned char OW_read_byte (void)
{
unsigned char i, result=0;
for (i = 0; i < 8; i++)
{
result >>= 1; // shift the result to get it ready for the next bit to receive
if (OW_read_bit())
result |= 0x80; // if result is one, then set MS-bit
}
return result;
}
//======================== End of 1-Wire.c ===================================//
Code:
////////////////////////////////////////////////////////////////////////////////
// 1-WIRE COMMUNICATION PROTOCOL //
// demowire.c //
////////////////////////////////////////////////////////////////////////////////
//============================== Includes ====================================//
//#include <p18f4580.h>
#include "Config.h"
#include "1wire.h"
//========================= Generic Definitions ==============================//
#define BAUD_CONTROL 00
// Command to read the 64-bit serial number from 1-wire slave device.
#define READ_COMMAND_DS1990A 0x33
//============================= Variables ====================================//
//dummy keys
const unsigned char key1[8]={0xA3,0x00,0x00,0x0F,0x2A,0xAE,0x6B,0x01};
unsigned char serial_number[8];
unsigned char temp;
//============================= Prototypes ===================================//
//Prototypes
void CompareKey();
//========================== Interrupt Vectors ===============================//
/*#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm goto high_isr _endasm
}
#pragma code
#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
_asm goto low_isr _endasm
}
#pragma code
*/
//----------------------------------------------------------------------------//
// Function: void high_isr(void)
// PreCondition: None
// Input: None
// Output: None
// Side Effects: None
// Overview: None
//----------------------------------------------------------------------------//
/*#pragma interrupt high_isr
void high_isr(void)
{
}
*/
//----------------------------------------------------------------------------//
// Function: void low_isr(void)
// PreCondition: None
// Input: None
// Output: None
// Side Effects: None
// Overview: None
//----------------------------------------------------------------------------//
/*#pragma interruptlow low_isr
void low_isr(void)
{
}
#pragma code
*/
//============================================================================//
// Function: Main Function.
// PreCondition: None
// Input: None
// Output: None
// Overview: 1-Wire protocol develop for DS1990A
//============================================================================//
void main(void)
{
//unsigned char serial_number[8];
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISC=0x00;
PORTC=0x00;
if (!Detect_Slave_Device()) // Is slave present???
{
PORTC.RC2=0;
}
else
{
PORTC.RC2=1;
OW_write_byte (READ_COMMAND_DS1990A); // Send a command to read a serial number
for(temp = 0; temp<8; temp++)
{
serial_number[temp] = OW_read_byte();
}
CompareKey();
}
// Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device
} // END OF MAIN
//----------------------------------------------------------------------------//
// Function: unsigned char Detect_Slave_Device(void)
// PreCondition: None
// Input: None
// Output: 1 - Not Present 0 - Present
// Overview: To check the presence of slave device.
//----------------------------------------------------------------------------//
unsigned char Detect_Slave_Device(void)
{
if (!OW_reset_pulse())
{ return HIGH;
}
else
{ return LOW;
}
}
//----------------------------------------------------------------------------//
// Function: unsigned char Comparekey(void)
// PreCondition: None
// Input: None
// Output: 1 - Not Present 0 - Present
// Overview: To check the presence of slave device.
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////////////////////////
//CompareKey- Compares scanned Dallas key serial number to pre-defined keys
void CompareKey(void)
{
if ((serial_number[0]==key1[0])&&(serial_number[1]==key1[1])&&(serial_number[2]==key1[2])&& (serial_number[3]==key1[3])&&(serial_number[4]==key1[4])&&(serial_number[5]==key1[5])&&(serial_number[6]==key1[6])&&(serial_number[7]==key1[7]))
{
PORTC.RC0=1;
}
else PORTC.RC1=1;
}
//============================ End of Main ===================================//
Last edited: