nancy88
Newbie level 2

- Joined
- Apr 30, 2011
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- South Africa
- Activity points
- 1,300
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 on 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 message "authorized" should be displayed on the lcd.(which is not the case)
i have followed Microchip's app note AN1199.
I initially assigned the "serial_number" variable to all 0's(see demowire.c).This is the variable that the serial number being read via the 1wire protocol is stored in. I then compared this variable to "key1"(initially assigned to the actual iButton serial no.) The 2 values were not equal which lead me to believe that the serial number being read via the 1 wire protocol was incorrect.
I have now changed "key1" to all 1's and when compared to "serial_number", the variables are equal. I'm not sure why the the serial number being read via 1wire is all 1's.
My code is posted below.If anyone has had a similar problem or has an idea on why this is happening, please let me know.
Thanx guys:smile:
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 on 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 message "authorized" should be displayed on the lcd.(which is not the case)
i have followed Microchip's app note AN1199.
I initially assigned the "serial_number" variable to all 0's(see demowire.c).This is the variable that the serial number being read via the 1wire protocol is stored in. I then compared this variable to "key1"(initially assigned to the actual iButton serial no.) The 2 values were not equal which lead me to believe that the serial number being read via the 1 wire protocol was incorrect.
I have now changed "key1" to all 1's and when compared to "serial_number", the variables are equal. I'm not sure why the the serial number being read via 1wire is all 1's.
My code is posted below.If anyone has had a similar problem or has an idea on why this is happening, please let me know.
Thanx guys:smile:
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 ====================================//
// LCD module connections
sbit LCD_RS at RC0_bit;
sbit LCD_RD at RC3_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_RD_Direction at TRISC3_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections
char txt1[] = "Authorized";
char txt2[] = "Unauthorized";
char txt3[] = "Scan Dallas Key";
//dummy keys
//const unsigned char key1[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //all 0's
const unsigned char key1[8]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; //all 1's
//const unsigned char key1[8]={0x01,0x6B,0xAE,0x2A,0x0F,0x00,0x00,0xA3}; //serial number of iButton
unsigned char serial_number[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char temp,g;
//============================= Prototypes ===================================//
void CompareKey();
unsigned char Detect_Slave_Device();
//========================== 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)
{
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISC=0x00;
PORTC=0x00;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt3);
//elay_ms(500);
if (!Detect_Slave_Device()) // Is slave present???
{
PORTC.RC2=0;
}
else
{
PORTC.RC2=1;
OW_write_byte (0x33); // Send a command to read a serial number
for(temp = 0; temp<8; temp++)
{
serial_number[temp] = OW_read_byte(); // Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device
}
CompareKey();
}
}
//================================ 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;
do{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1);
Delay_ms(500);
} while(1);
}
else
{
//PORTC.RC1=1;
do{
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt2);
Delay_ms(500);
} while(1);
}
}
//============================ End of demowire.c ===================================//
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;
OW_PIN_DIRECTION = 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 1wire.c ==============================//
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:
////////////////////////////////////////////////////////////////////////////////
// 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 TRISB//7_bit
#define OW_WRITE_PIN PORTB.RB7 //fOR pic 18f LATB.RB7
#define OW_READ_PIN PORTB.RB7
#endif
//============================= End of Config.h =============================//