PIC program explanation

Discussion in 'Embedded Systems and Microcontrollers' started by ansh_kumar, Jan 1, 2015.

  1. ansh_kumar

    Thread Starter New Member

    Jun 24, 2013
    2
    0
    I'm not a PIC programmer but I need to understand the following program.
    Actually I want to convert it to an atmel program. Please help.

    Code (Text):
    1.  
    2. [FONT=Courier New]' Coding for PIC16F72
    3. ' Numerical Relay
    4.  
    5. Include "numrly.inc"
    6.  
    7. Declare ADIN_RES 8     ' 10-bit result required
    8. Declare ADIN_TAD FRC   ' RC OSC chosen
    9. Declare ADIN_STIME 50  ' Allow 50us sample time
    10.  
    11. Symbol VOLT_IN PORTA.0
    12. Symbol CURRENT_IN PORTA.1
    13. Symbol FREQ_IN PORTC.2
    14. Symbol SYNC_IN PORTC.1
    15. Symbol FIELD_VOLT_IN PORTC.0
    16.  
    17. Symbol IMP_RELAY PORTB.0
    18. Symbol FREQ_RELAY PORTB.1
    19. Symbol CUR_REV_RELAY PORTB.2
    20. Symbol OVER_VOLT_RELAY PORTB.3
    21. Symbol FIELDFAIL_RELAY PORTB.4
    22.  
    23. Low PORTA
    24. Low PORTB
    25. Low PORTC
    26.  
    27. ' |76543210|
    28. TRISA = %11111111
    29. TRISB = %00000000
    30. TRISC = %11111111
    31.  
    32. ADCON1 = %00000000
    33. ' Set analogue input on PORTA.0
    34.  
    35. Dim VOLT As Word
    36. Dim CURRENT As Word
    37. Dim FREQ As Word
    38. Dim RPM As Word
    39. Dim RPMVal As Float
    40. Dim Err As Byte
    41. Dim CntVal As Byte
    42.  
    43. Low PORTB
    44. DelayMS 3000
    45. Err = 0
    46.  
    47. Loop:
    48. VOLT = ADIn 0
    49. CURRENT = ADIn 1
    50. RPM = ADIn 2
    51.  
    52. If CntVal <= 1 Or CntVal = 4 Then
    53.    FREQ = Counter FREQ_IN, 1000
    54. End If
    55. VOLT = VOLT * 10
    56. CURRENT = CURRENT * 10 / 5
    57. RPMVal = RPM * 16.123
    58.  
    59. If (VOLT / CURRENT) < 5 Then
    60.    FIELDFAIL_RELAY = 1
    61.    IMP_RELAY = 1
    62.    Err 0# = 1
    63. Else
    64.    IMP_RELAY = 0
    65.    Err 0# = 0
    66. End If
    67.  
    68. If SYNC_IN = 1 And FREQ < 475 Then
    69.    FIELDFAIL_RELAY = 1
    70.    FREQ_RELAY = 1
    71.    Err 0.1 = 1
    72. Else
    73.    FREQ_RELAY = 0
    74.    Err 0.1 = 0
    75. End If
    76.  
    77. If VOLT > 1300 Then
    78.    FIELDFAIL_RELAY = 1
    79.    OVER_VOLT_RELAY = 1
    80.    Err 0.2 = 1
    81. Else
    82.    OVER_VOLT_RELAY = 0
    83.    Err 0.2 = 0
    84. End If
    85.  
    86. If RPMVal < 2844 Then ' < 1768 Then 'rpmFIELDFAIL_RELAY = 1
    87.    CUR_REV_RELAY = 1
    88.    Err 0.3 = 1
    89. Else
    90.    CUR_REV_RELAY = 0
    91.    Err 0.3 = 0
    92. End If
    93.  
    94. If FIELD_VOLT_IN = 0 Or Err > 0 Then
    95.    If CntVal >= 1 Then
    96.       FIELDFAIL_RELAY = 1
    97.          If CntVal < 4 Then
    98.            CntVal = CntVal + 1
    99.          End If
    100.    Else
    101.    CntVal = CntVal + 1
    102.    End If
    103. Else
    104.     FIELDFAIL_RELAY = 0
    105.     CntVal = 0
    106. End If
    107. GoTo LOOP
    108. End[/FONT]
    109.  
    Moderators note: Please use code tags for pieces of code
     
    Last edited by a moderator: Jan 4, 2015
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    The program shown was written in the BASIC programming language and has little to do with PICs.
    You can translate this into C one line at a time with little knowledge of what the program is supposed to do.
     
  3. ansh_kumar

    Thread Starter New Member

    Jun 24, 2013
    2
    0
    Okay I've translated much of the program, just few doubts as follows:
    1) what does the line FREQ = Counter FREQ_IN, 1000 do ?
    2) Err 0# = 0
    Err 0.3 = 0
    what do those lines do ?
     
  4. jjw

    Member

    Dec 24, 2013
    173
    31
    My guess: Counter FREQ_IN, 1000 counts number of pulses on pin FREQ_IN during 1000 milliseconds.
     
  5. Art

    Distinguished Member

    Sep 10, 2007
    785
    61
    Err is a byte used as bit flags for errors with each relay.
    You wouldn't do it that way in C.
     
Loading...