Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 1_Wire.c
- *
- * Created on: Mar 3, 2011
- * Author: Mariusz Poslinski
- */
- #include"1_Wire.h"
- //GPIO_InitTypeDef GPIO_InitStructure;
- #include "stm32f10x_conf.h"
- #include "stm32f10x_it.h"
- //#include "owirelib.h"
- int i;
- void delay1 (int us)
- {
- u16 i, tus;
- tus = 9*us;
- for(i=0;i<tus;i++);
- asm volatile ("nop");
- }
- unsigned char ow_reset(void)
- {
- ow_pin_out();
- u8 presence;
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
- delay1(500); // delay 560uS
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
- ow_pin_in();
- delay1(30);
- if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
- {presence=0;}
- else
- {presence=1;}
- delay1(470);
- if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
- {presence&=1;}
- else
- {presence&=0;}
- return(presence);
- }
- unsigned char ow_read_bit(void)
- {
- unsigned char presence=0;
- ow_pin_out();
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
- delay1(2);
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
- ow_pin_in();
- delay1(13);
- if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
- {presence=1;}
- else
- {presence=0;}
- return(presence);
- }
- void ow_write_bit(unsigned char bitval)
- {
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
- delay1(5);
- if(bitval==1)
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
- delay1(80);
- GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
- }
- unsigned char ow_read_byte(void)
- {
- unsigned char i;
- unsigned char value=0;
- for (i=0;i<8;i++)
- {
- if(ow_read_bit()) value |= (0x01<<i);
- delay1(15); //was 65
- }
- return(value);
- }
- void ow_write_byte(unsigned char val)
- {
- unsigned char i;
- unsigned char pom;
- ow_pin_out();
- for(i=0; i<8; i++)
- {
- pom = val>>1;
- pom &= 0x01;
- ow_write_bit(pom);
- }
- delay1(100);
- }
- void ow_pin_out(void)
- {
- /* Configure CX_ADC2 as 1-wire port */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- void ow_pin_in(void)
- {
- /* Configure CX_ADC2 as 1-wire port */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- void ow_get_rom(unsigned char *romid)
- {
- ow_write_byte(0x33);
- for(i = 0; i < 8; i++)
- {
- romid[i] = ow_read_byte();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement