SpiderElectron

Electronic Engineering Blog

Reading 1-Wire iButtons using Cypress PSoC 3 and 5/5LP

Previously I posted about using a standard UART to read and write to 1-Wire devices. In this post I look at how to read and write to these devices using Cypress PSoC devices.

The Cypress PSoC 3, 5 and 5LP devices can all easily communicate with 1-Wire devices such as iButtons. The PSoC devices are quite unique, they consist of a processor core (Such as 80C51 or ARM) surrounded by a whole load of universal device blocks (UDBs) which can be configured to perform many different tasks. It’s a bit like CPLD or FPGA but much more accessible to the end user and programmer, through their software, PSoC Creator, which embeds and graphical hardware designer and programming IDE in one.

Hardware

There are various UART devices available on PSoC 3/5/5LP, but because they are hardware designed at design-time, their baud rate cannot easily by changed at run-time. (It can be done by providing a custom clock and divider, but that’s another story). So to read and write from 1-Wire devices using PSoC 3/5/5LP we need another approach. It turns out to be pretty simple.

We need nothing more than a single bi-directional I/O pin to fully communicate with 1-Wire devices.
In this project I added a single bi-direction pin to the design, along with an LCD Display and two more pins to control LEDs for some nice user feedback.

PSoC Creator Schematic

PSoC Creator Schematic

The full schematic for PSoC Creator is shown here. Click on the image for a full size view. There’s very little to it. I will link to a download of the full completed project below. In my case I am using the CY8CKIT-050 development prototype board from Cypress to build and test my code. So the iButton IO Pin is connected to Port P0[0], the LCD is connected to P2[6:0], LED 3 is  P6[2] and LED 4 is on P6[3].

When running, LED3 flashes every quarter second to indicate that it is checking for a 1-Wire device, and LED4 flashes for 3/4 of s second if a device is found and successfully read.

Software

CY8CKIT-050Since we are using a Bi-directional I/O pin, we can read and write to the same pin.  The natural, idle state of the pin should be pulled up to VCC, so before we do anything else, we write a ‘1’ to the pin. The 1-Wire devices leech power from the ping while it is ‘high’ and store it in order to be able to drive the signal back to GND again. To detect a 1-Wire device we simply take the pin low (0V) for approx 480 micro seconds and then let the line go high again, and sample (read from) the pin after a further 70 µS. If there is a 1-Wire device connected, it will respond by driving the line low for about 6 micro seconds.

This code will detect a 1-wire device. It returns 1 if there is no device present, and 0 if there is a device present. Note that I am using the ‘CyPins’ functions to read and write to/from the IO pin as these are quicker and give much more deterministic timing.

int OW_Detect(unsigned int pin)
{
 owpin=pin;
 
 CyPins_ClearPin(owpin);
 CyDelayUs(480);
 
 CyPins_SetPin(owpin);
 CyDelayUs(70);
 
 return CyPins_ReadPin(owpin);
}

 

Ones and Zeros

In order to write bytes to the device, we start a ‘write’ timeslot by taking the line low again. If we hold the line low for just 6 micro seconds and then drive it high for 64 microseconds the 1-Wire device reads this as a ‘1’. To write a ‘0’ to the device we drive the line low for 60 micro seconds. To write full bytes to the device we simply repeat the writing of 1’s or 0’s as necessary.

The following code will write a byte to the 1-wire device

void OW_WriteByte(unsigned char u8)
{
 int i=0;
 for(i=0; i<8; i++)
 {
 if(u8 & 0x01)
 {
 CyPins_ClearPin(owpin);
 CyDelayUs(6);
 CyPins_SetPin(owpin);
 CyDelayUs(64);
 }
 else
 {
 CyPins_ClearPin(owpin);
 CyDelayUs(60);
 CyPins_SetPin(owpin);
 CyDelayUs(10); 
 }
 
 u8>>=1;
 }
}

And the following code will read a byte from the device.

unsigned char OW_ReadByte()
{
 unsigned char b=0;
 int i=0;
 
 while(i<8)
 {
 b >>= 1;
 CyPins_ClearPin(owpin);
 CyDelayUs(6);
 CyPins_SetPin(owpin);
 CyDelayUs(9);
 
 b |= (CyPins_ReadPin(owpin)>0) ? 0x80 : 0x00;
 CyDelayUs(55);
 
 i++;
 }
 
 return b;
 
}

That’s all it takes to detect, write to and read from a 1-Wire device.

The full project, suitable for running on CY8CKIT-050 with a PSoC 5LP device is here. If your dev kit uses a different device simply replace it using the ‘Device Selector’ in the project manager window in PSoC Creator.

iButton Key Fobs

One thought on “Reading 1-Wire iButtons using Cypress PSoC 3 and 5/5LP

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.