Send data with one wire protocol?

Discussion in 'Embedded Systems and Microcontrollers' started by ArFa, Jan 21, 2013.

  1. ArFa

    Thread Starter New Member

    Apr 24, 2011
    9
    0
    Hi there,
    I want to send data through one wire protocol from stm8.
    So, stm8 will act as one wire slave device, and send those data to another device through one wire protocol.

    Please if you can, tell me how to solve this problem !
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Did you google one wire?
     
  3. ArFa

    Thread Starter New Member

    Apr 24, 2011
    9
    0
    yeah i did, understood tha how it works, but i'm not able to send data .
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Show what you are doing in order to send data.
     
  5. ArFa

    Thread Starter New Member

    Apr 24, 2011
    9
    0
    Code ( (Unknown Language)):
    1.  
    2. #include"stm8s.h"
    3. #include <iostm8S105c6.h>
    4. #include <intrinsics.h>
    5. //#define cli() __asm("cli")
    6. //#define rim() __asm("sim")
    7.  
    8. #define PR 0
    9.  
    10.  
    11. //
    12. //  Setup Timer 2 to generate an interrupt every 480 clock ticks (30 uS).
    13. //
    14.  
    15. char A[64] = "1010101010101010101010101010101010101010101010101010101010101010"; // serial number to send to one wire
    16. //int i ;
    17. void SetupTimer2(char a) //40 us
    18. {
    19.     TIM2_PSCR = 0x00;       //  Prescaler = 1.
    20.     if(a == 'r') //
    21.     {
    22.       TIM2_ARRH = 0x04;       //  Wait for 480.
    23.       TIM2_ARRL = 0x38;      
    24.     }
    25.     else if(a == 'p')
    26.     {
    27.       TIM2_ARRH = 0x01;       //  High byte of 120.
    28.       TIM2_ARRL = 0x18;      
    29.     }
    30.     else if(a == 'b')
    31.     {
    32.       TIM2_ARRH = 0x00;       //  wait for 30us.
    33.       TIM2_ARRL = 0x3c;      
    34.     }
    35.     else if(a == 'c')
    36.     {
    37.       TIM2_ARRH = 0x00;       //  wait for 10us.
    38.       TIM2_ARRL = 0x14;      
    39.     }
    40.  
    41.    // TIM2_IER_UIE = 1;       //  Turn on the interrupts.
    42.     TIM2_CR1_CEN = 1;       //  Finally enable the timer.
    43. }
    44. //
    45. //  Timer 2 Overflow handler.
    46. //
    47.  
    48. #pragma vector = TIM2_OVR_UIF_vector
    49. __interrupt void TIM2_UPD_OVF_IRQHandler(void)
    50. {
    51.     TIM2_SR1_UIF = 0;              //  Reset the interrupt otherwise it will fire again straight away.
    52.     TIM1_IER_UIE = 0;
    53. }
    54.  
    55. int i_count;
    56. char proc_isr;
    57. #pragma vector = 6
    58. __interrupt void EXTI_PORTB_IRQHandler(void) // B interrupt
    59. {
    60.        //sim();
    61.        //__disable_interrupt();
    62.        PB_CR2 = 0x00; // disable EXTI_PORTB
    63.        if(i_count<1)
    64.        {
    65.        SetupTimer2('r'); // delay_us(510);
    66.        __enable_interrupt();
    67.        __wait_for_interrupt();
    68.        //__disable_interrupt();          
    69.        PB_ODR_ODR4 = 0;//low -> presence
    70.        PB_DDR = 0xff;
    71.        
    72.        SetupTimer2('p');//delay_us(140);
    73.        
    74.        __enable_interrupt();
    75.        __wait_for_interrupt();
    76.        }
    77.        else
    78.        if(i_count<9) // receive command from master -> 0xF0 search();
    79.        {}
    80.        else
    81.        if(i_count<73) // go interrupt
    82.        {
    83.          if(A[i_count-8] == 0)
    84.          {
    85.            //send 0 bit
    86.            PB_ODR_ODR4 = 0;//low -> bit
    87.            PB_DDR = 0xff;
    88.            SetupTimer2('b');//delay us = 60 us
    89.            __enable_interrupt();
    90.            __wait_for_interrupt();
    91.            
    92.            // send 1 bit
    93.            PB_ODR_ODR4 = 1;
    94.            PB_DDR = 0x00;
    95.            SetupTimer2('b');//delay us = 60 us
    96.            __enable_interrupt();
    97.            __wait_for_interrupt();
    98.          }
    99.          else
    100.          {
    101.            // send 1 bit
    102.            PB_DDR = 0x00;
    103.            SetupTimer2('b');//delay us = 60 us
    104.            __enable_interrupt();
    105.            __wait_for_interrupt();
    106.  
    107.            // send 0 bit
    108.            PB_DDR = 0xff;
    109.            SetupTimer2('b');//delay us = 60 us
    110.            __enable_interrupt();
    111.            __wait_for_interrupt();
    112.            
    113.          }
    114.          
    115.            PB_DDR = 0x00;
    116.            SetupTimer2('b');//delay 30us for recv signal.
    117.            __enable_interrupt();
    118.            __wait_for_interrupt();
    119.        }
    120.        i_count++;
    121.        if(i_count>73)
    122.          i_count = 0;
    123.        //rim();
    124.     PB_CR2 = 0x00;
    125. }
    126.  
    127. //
    128. //  Main program loop.
    129. //
    130. void main()
    131. {
    132.    i_count = 0;
    133.    
    134.    PB_ODR_ODR4 = 1;
    135.    PB_DDR = 0x00; // porta D pin 4 input
    136.    PB_CR1 = 0xff;         //  PB4 is pull-up input.
    137.    PB_CR2 = 0xff;         //  PB4 is with interrupt
    138.    EXTI_CR1_PBIS = 2;      //  Interrupt on falling edge.
    139.    EXTI_CR2_TLIS = 0;      //  Falling edge only.
    140.    __enable_interrupt();
    141.     i_count = 0;
    142.     while (1)
    143.     {
    144.       __wait_for_interrupt();
    145.        PB_DDR = 0x00; // porta D pin 4 input
    146.        PB_CR1 = 0xff;         //  PB4 is pull-up input.
    147.        PB_CR2 = 0xff;
    148.        EXTI_CR1_PBIS = 2;      //  Interrupt on falling edge.
    149.        EXTI_CR2_TLIS = 0;      //  Falling edge only.
    150.     }
    151. }
    152.  
     
Loading...