Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*
  2.  * 1_Wire.c
  3.  *
  4.  *  Created on: Mar 3, 2011
  5.  *      Author: Mariusz Poslinski
  6.  */
  7. #include"1_Wire.h"
  8.  
  9. //GPIO_InitTypeDef GPIO_InitStructure;
  10. #include "stm32f10x_conf.h"
  11. #include "stm32f10x_it.h"
  12. //#include "owirelib.h"
  13.  
  14. int i;
  15. void delay1 (int us)
  16. {
  17.  
  18.     u16 i, tus;
  19.         tus = 9*us;
  20.         for(i=0;i<tus;i++);
  21.         asm volatile ("nop");
  22. }
  23.  
  24. unsigned char ow_reset(void)
  25. {
  26.     ow_pin_out();
  27.     u8 presence;
  28.     GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
  29.     delay1(500); // delay 560uS
  30.     GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
  31.     ow_pin_in();
  32.     delay1(30);
  33.  
  34. if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
  35.     {presence=0;}
  36.     else
  37.     {presence=1;}
  38.     delay1(470);
  39. if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
  40.     {presence&=1;}
  41.     else
  42.     {presence&=0;}
  43.     return(presence);
  44.  
  45. }
  46.  
  47. unsigned char ow_read_bit(void)
  48. {
  49.     unsigned char presence=0;
  50.     ow_pin_out();
  51.     GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
  52.     delay1(2);
  53.     GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
  54.     ow_pin_in();
  55.     delay1(13);
  56.     if(GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_13))
  57.     {presence=1;}
  58.     else
  59.     {presence=0;}
  60.     return(presence);
  61. }
  62.  
  63. void ow_write_bit(unsigned char bitval)
  64. {
  65.     GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_RESET);
  66.     delay1(5);
  67.     if(bitval==1)
  68.         GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
  69.  
  70.         delay1(80);
  71.         GPIO_WriteBit(GPIOD, GPIO_Pin_13, Bit_SET);
  72. }
  73.  
  74.  
  75. unsigned char ow_read_byte(void)
  76. {
  77. unsigned char i;
  78. unsigned char value=0;
  79. for (i=0;i<8;i++)
  80. {
  81. if(ow_read_bit()) value |= (0x01<<i);
  82. delay1(15); //was 65
  83. }
  84. return(value);
  85. }
  86.  
  87. void ow_write_byte(unsigned char val)
  88. {
  89.     unsigned char i;
  90.     unsigned char pom;
  91.     ow_pin_out();
  92.     for(i=0; i<8; i++)
  93.     {
  94.         pom = val>>1;
  95.         pom &= 0x01;
  96.         ow_write_bit(pom);
  97.     }
  98.     delay1(100);
  99. }
  100.  
  101. void ow_pin_out(void)
  102. {
  103.  
  104. /* Configure CX_ADC2 as 1-wire port */
  105.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  106.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  107.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  108.    GPIO_Init(GPIOD, &GPIO_InitStructure);
  109. }
  110.  
  111. void ow_pin_in(void)
  112. {
  113.  
  114. /* Configure CX_ADC2 as 1-wire port */
  115.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  116.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  117.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  118.    GPIO_Init(GPIOD, &GPIO_InitStructure);
  119. }
  120.  
  121. void ow_get_rom(unsigned char *romid)
  122. {
  123.     ow_write_byte(0x33);
  124.     for(i = 0; i < 8; i++)
  125.        {
  126.            romid[i] = ow_read_byte();
  127.        }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement