Need Help to program PIC16F877A stepper motor revolution [Full circuit & 3/4 Code]

Discussion in 'Embedded Systems and Microcontrollers' started by slapdash, Nov 4, 2008.

  1. slapdash

    Thread Starter New Member

    Sep 21, 2008
    2
    0
    Automatic Blind System

    Hi there,

    I'm currently working on project call automatic blind system which is activate depends on outside condition. Before this I tried to build this project MICROCONTROLLER BASED AUTOMATED LIGHT CONTROL SYSTEM which is based on AVR ATMEGA32 but it failed to get what is has supposed to.

    Since that i try to switch to PIC and found out using PICBasic Pro (PBP) is time saving and cost reduced language since the project submission is just around the corner..I hope PBP master here can fix my code since i wrote this just a day ago with zero knowledge of PBP by just reviewing the manual and project book. Here is the circuit diagram and code. PM for Proteus VSM.

    The Circuit

    [​IMG]

    PicBasic Pro code

    Code ( (Unknown Language)):
    1.  
    2. '****************************************************************
    3. '*  Name    : 877A_BLIND.BAS                                    *
    4. '*  Author  : PIJO RAHMAN                                       *
    5. '*  Notice  : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
    6. '*          : All Rights Reserved                               *
    7. '*  Date    : 11/4/2008                                         *
    8. '*  Version : 1.0                                               *
    9. '*  Notes   : Using PIC16F877A to control Vertical Blind Auto   *
    10. '*            and Manual with light sensor                      *
    11. '****************************************************************
    12. DEFINE OSC 4
    13.  
    14. '****************************************************************
    15. CMCON = 7
    16. ADCON1 = 7
    17.  
    18. TRISB = 0 ' Set PortB as output
    19. TRISA = 1 ' Or TRISA = 255, set PortA as input
    20.  
    21. '***************************************************************
    22. Define  LCD_DREG        PORTD 'Based on Rentron 877A LCD Connection
    23. Define  LCD_DBIT        4
    24. Define  LCD_RSREG       PORTE
    25. Define  LCD_RSBIT       0
    26. Define  LCD_EREG        PORTE
    27. Define  LCD_EBIT        1
    28.  
    29. B1 VAR BYTE            'Declared Byte For LCD Splash Screen
    30. B0 VAR BYTE
    31. B2 VAR BYTE
    32. B3 VAR BYTE
    33. B4 VAR BYTE
    34. B5 VAR BYTE
    35.  
    36. '****************************************************************
    37. clear                   'clear all variables
    38. LCDOUT $FE,1            'clear lcd
    39. PAUSE 500               'pause for lcd init
    40. Low PORTE.2             'LCD R/W line low (W)
    41. '**************************************************************
    42.  
    43. '*****************slide text for splash screen**************************
    44. LOOP:
    45. LCDOut $fe,2
    46. For B2= 0 TO 15
    47. LookUp B0, ["               AUTOMATIC BLIND SYSTEM              #"], B1  
    48. IF B1 = "#" Then GoTo reset
    49. LCDOut B1
    50. B0 = B0 + 1
    51. Next B2
    52. pause 125        'character wait time
    53. B0 = B0 - 15
    54. GoTo loop
    55. RESET:
    56. LCDOUT $FE,1
    57. '************************************************************************
    58.  
    59. CREDIT:                     'Display Presenter Credit
    60.   LCDOUT $FE,$80
    61.   LCDOUT "PRESENTER NAME"  'presenter  credit
    62.   'LCDOUT $FE,$C0
    63.   'LCDOUT " GROUP   "   'presenter  credit
    64.   PAUSE 1500
    65.   LCDOUT $FE,1
    66.  
    67. speed var word
    68. speed = 100
    69. ;i var byte
    70.  
    71. Main:                   ' Main Prgm, Check Button for Auto/Manual Op
    72. IF PORTA.0=1 THEN Manual
    73. If PORTA.1=1 Then ForwardM
    74. If PORTA.1=0 Then ReversesM
    75. Goto Main
    76. End
    77.  
    78. Manual:     'FOR MANUAL OPERATION START WITH RA0=1 THEN CONTROL RA2 AND RA3
    79. Pause 500    'Do it about 2 times a second
    80. LCDOUT $FE,1  'Clear LCD
    81. LCDOUT $FE,$80,"MANUAL"   'Display "Manual" 1st Line on LCD
    82. LCDOUT $FE,$C0,"USER INPUT" 'Display "USER INPUT" 2nd Line on LCD
    83. IF PORTA.2=1 THEN FORWARDM   'Manually Opening Blind
    84. IF PORTA.3=1 Then ReversesM   'Manually Closing Blind
    85. Goto Main
    86.  
    87. ForwardM:
    88. LCDOUT $FE,1
    89. LCDOUT $FE,$80,"OPENING BLIND"
    90. LCDOUT $FE,$C0,"DAYLIGHT"
    91. ;FOR i = 1 TO 5 ' one revolution   'Actually Use this Function for Revolution Step But No LUCK! :(
    92. GOto Forward ' rotate clockwise
    93. ;NEXT i
    94. GOTO Main
    95.  
    96. ReversesM:
    97. LCDOUT $FE,1                        'Clear LCD
    98. LCDOUT $FE,$80,"CLOSING BLIND"
    99. LCDOUT $FE,$C0,"SUNSET/DARK"
    100. ;FOR i = 1 TO 5 ' one revolution
    101. Goto Reverses ' rotate counter-clockwise
    102. ;NEXT i
    103. GOTO Main
    104.  
    105. Forward:     'Stepper Motor Clockwise Sequence
    106. portb = %1100
    107. pause speed
    108. portb = %0110
    109. pause speed
    110. portb = %0011
    111. pause speed
    112. portb = %1001
    113. pause speed
    114. Goto Main
    115.  
    116. Reverses:        'Stepper Motor AntiClockwise Sequence
    117. portb = %1001
    118. pause speed
    119. portb = %0011
    120. pause speed
    121. portb = %0110
    122. pause speed
    123. portb = %1100
    124. pause speed
    125. Goto Main
    126. END
    127.  


    The operation


    This project is simple as it shows. The circuit is consist of manual and automatic function. SW1 connected at RA0 is to use to toggle between auto/manual mode. In auto mode, all the manual control switch which is SW3 and SW4 is not activated but only receive the signal from SW2 [which is will be interface to light-dark activated switch, so no ADC will be required]. If SW2 high, motor is rotate clockwise and the blind is open, but if SW2 low, motor is rotate anticlockwise and the blind is close.It need 10 stepper motor revolution [according to my blind] to open and close the blind slat, which is i dunno how to program it. :( ... so even the switch is still in high, the stepper only rotate 10 times and stop and wait till the other state. The stepper motor that I use is common 1.8 degree per step.

    The operation repeated until someone invoke the manual switch.

    Other switch which is SW5-SW6 is to use to control blind vertical position by using simple DC motor interface to L293D which is will be added later on. The LCD 16x2 is only to display operations, since PIC16F877A still have the unused port.

    p/s: Just PM me to get the proteus VSM files. Since the dateline is only a week, I hope someone can help me ASAP...Thanks in advance!
     
  2. yavarp

    New Member

    Nov 5, 2008
    2
    0
    Dear Rahman,

    I have seen some problems in your code, first of all, you can not put END instruction in between the program, which you have written after 'Main' routine, another is a loop of ForwardM, you have written 'GOto Forward ' and in 'forward' sub rouine you have goven instruction 'goto main', instead of this youe have to copy the command and past directly in ForwardM routine, which look like as below

    ForwardM:
    LCDOUT $FE,1
    LCDOUT $FE,$80,"OPENING BLIND"
    LCDOUT $FE,$C0,"DAYLIGHT"
    FOR i = 1 TO 50 ' one revolution
    portb = %1100
    pause speed
    portb = %0110
    pause speed
    portb = %0011
    pause speed
    portb = %1001
    pause speed

    NEXT i
    GOTO Main


    each step of above will ove your stepper motor by 1.8 degree movement so for one revolution you have to give 200 steps, here in the above routine 4 steps are written so now run this rouine for 50 times so 4X50=200 which are required o move one revolution.

    Yavar Pothiawala, Surat, India.
     
  3. russ_hensel

    Well-Known Member

    Jan 11, 2009
    818
    47
    You should debug one bit of functionality at a time.

    Using goto is discouraged in modern ( post 1980 or so ) programming. Subroutines are usually a better choice.

    Try to format your code to indicate its structure, do not left allign so much.
     
Loading...