--- /dev/null
+// 1-wire drv
+
+#include <lpc2xxx.h> /* LPC23xx definitions */
+#include <types.h>
+
+
+
+
+#define SET 1
+#define CLEAR 0
+
+
+
+
+
+typedef uint32_t reg_size;
+static reg_size dir;
+static reg_size set;
+static reg_size clr;
+static reg_size port;
+static uint8_t pin;
+static uint32_t tim_1us;
+
+
+
+unsigned char macro_delay;
+
+/**
+ * Nastavi pin na vystupni a nastavi log. 0
+ *
+ */
+void drive_OW_low (void)
+{
+ *((reg_size *)dir) |= (1<<pin);
+ *((reg_size *)clr) = (1<<pin);
+}
+
+/**********************************************************************
+* 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)
+{
+ *((reg_size *)dir) &= ~(1<<pin); // tohle neni uplne clear
+// *((reg_size *)dir) |= (1<<pin);
+// *((reg_size *)set) = (1<<pin);
+}
+
+/**********************************************************************
+* 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;
+
+ *((reg_size *)dir) &= ~(1<<pin);
+ return (unsigned char) ((*((reg_size *)port) >> pin) & 1);
+
+
+}
+
+/**********************************************************************
+* 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 = 0;;
+
+ drive_OW_low(); // Drive the bus low
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 480));
+ T3TCR = 3;
+
+
+ read_OW();// drive_OW_high (); // Release the bus
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 70));
+ T3TCR = 3;
+
+ presence_detect = read_OW(); //Sample for presence pulse from slave
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 410));
+ T3TCR = 3;
+
+ 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(); // Drive the bus low
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 6));
+ T3TCR = 3;
+ drive_OW_high (); // Release the bus
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 64));
+ T3TCR = 3;
+ }
+ else
+ {
+ //writing a bit '0'
+ drive_OW_low(); // Drive the bus low
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 60)); // delay 60 microsecond (us)
+ T3TCR = 3;
+ drive_OW_high (); // Release the bus
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 10)); // delay 10 microsecond (us)
+ T3TCR = 3;
+ }
+}
+
+
+/**********************************************************************
+* 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
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 6));
+ T3TCR = 3;
+
+ drive_OW_high (); // Release the bus
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 9));
+ T3TCR = 3;
+
+ read_data = read_OW(); //Read the status of OW_PIN
+
+ T3TCR = 1;
+ while(T3TC < ( tim_1us * 55));
+ T3TCR = 3;
+ 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 loop;
+
+ for (loop = 0; loop < 8; loop++)
+ {
+ 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 loop, result=0;
+
+ for (loop = 0; loop < 8; loop++)
+ {
+
+ 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;
+}
+
+
+void init_1_wire(uint8_t port2, uint8_t pin_num, uint32_t f_ahb)
+{
+ dir = (uint32_t) &FIO0DIR + port2 * 0x20;
+ set = (uint32_t) &FIO0SET + port2 * 0x20;
+ clr = (uint32_t) &FIO0CLR + port2 * 0x20;
+ port = (uint32_t) &FIO0PIN + port2 * 0x20;
+ pin = pin_num;
+
+ PCONP |= (1<<23);
+ T3TCR = 3; // Enable and reset, timer is set as free runing
+ T3CTCR = 0;
+ T3PR = 0;
+
+ tim_1us = f_ahb / 1000000;
+
+// PINMODE1 |= 3;
+
+
+}
+
+
+/**********************************************************************
+* 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 1;
+ else
+ return 0;
+}
+
--- /dev/null
+
+
+#include "ds18s20.h"
+#include "1-wire-drv.h"
+//#include <string.h>
+#include <lpc23xx.h> /* LPC23xx definitions */
+
+
+
+
+
+
+/* Update 8-bit CRC value
+ using polynomial X^8 + X^5 + X^4 + 1 */
+
+#define POLYVAL 0x8C
+
+void update_crc(unsigned char new, unsigned char *crc)
+{
+ unsigned char c, i;
+
+ c = *crc;
+ for (i = 0; i < 8; i++)
+ {
+ if ((c ^ new) & 1)
+ c = (c >> 1 ) ^ POLYVAL;
+ else
+ c >>= 1;
+ new >>= 1;
+ }
+ *crc = c;
+}
+
+
+
+uint8_t read_temp(struct ds18s20_dev * dev)
+{
+ uint8_t i, crc =0, data[9];
+ int16_t temp =0;
+
+ init_1_wire(dev->port, dev->pin, 72000000);
+
+ if( Detect_Slave_Device())
+ return DEVICE_NOT_FOUND;
+
+
+ OW_reset_pulse();
+ OW_write_byte (0xCC); // Send a command to prepare read temp
+ OW_write_byte (0x44);
+
+ T3TCR = 1;
+ while(T3TC < ( (72000000/100000) * 6));
+ T3TCR = 3;
+
+ OW_reset_pulse();
+ OW_write_byte (0xCC); // Read temp
+ OW_write_byte (0xBE);
+
+ for(i = 0; i < 9; i++)
+ data[i] = OW_read_byte(); // Read 64-bit registration (48-bit serial number) number from 1-wire Slave Device
+
+ for(i = 0; i < 8; i++)
+ update_crc(data[i], &crc);
+
+
+ if(crc != data[8])
+ return DEVICE_CRC_FAIL;
+
+ temp = ((uint16_t)(data[7] - data[6]) * 100)/data[7] - 25;
+ temp = (temp + 5) / 10;
+ temp += ((data[0] | ((uint16_t)data[1] << 8)) & ~(0x0001)) * 5;
+ dev->temp = temp;
+
+
+
+ return 0;
+}
+
+
+