micro timer help plz ASAP :( im having it after 2 few days :(

Discussion in 'Embedded Systems and Microcontrollers' started by goldfish300, Apr 13, 2009.

  1. goldfish300

    Thread Starter Member

    Mar 12, 2009
    12
    0
    well first of all im working on a pic 16f84a

    we need to connect the circuit to an oscilloscope and to fit these conditions :

    if signal on portA,0 < 10 hz ------> motor stop

    else
    if 10 hz < signal < 100 hz -----> motor left
    else
    if signal > 100 ------>motor right

    plz if anyone knows how to write such a program help

    i wrote something about interupt at RB0
    in which its as follow:
    at startup led PORTA, 0 is on for one second then im having two leds instead of motor which turn on after the delay of the first leds and just keep blinking like left >right and right > left until i press on the button and it just turn the right led for one second and then stop for one sec then cont to the left >right and right > left until i press on the button. im askin how to fix that for the timer0 with the conditions i need above

    i don't know how to link this with an oscilloscope and writing the timer function.

    well i the code is as follows


    Code ( (Unknown Language)):
    1. #INCLUDE <P16F84.INC>
    2.      #DEFINE        LED             PORTA, 0
    3.      #DEFINE        RELAY1          PORTB, 1
    4.      #DEFINE        RELAY2          PORTB, 2
    5.      #DEFINE        PB              PORTB, 0
    6.  
    7.      __CONFIG _WDT_OFF  &  _PWRTE_OFF  &  _CP_OFF  &  _XT_OSC
    8.  
    9.  
    10.  
    11.       CBLOCK  0X0C
    12.          W_TEMP, STATUS_TEMP, A1, A2, A3, B1, B2, B3
    13.  
    14.  
    15.      ENDC
    16.  
    17.  
    18.  
    19.  
    20.      ORG     0X000
    21.      GOTO    MAIN
    22.      ORG     0X004
    23.      MOVWF   W_TEMP
    24.      SWAPF   STATUS,W
    25.      MOVWF   STATUS_TEMP
    26.  
    27.  
    28.      BTFSC   INTCON,INTE
    29.      BTFSS   INTCON,INTF
    30.      GOTO    $+4  
    31.      CALL    INTRB0
    32.      BCF     INTCON,INTF
    33.      GOTO    RESTOREREG
    34.  
    35. RESTOREREG
    36.      SWAPF   STATUS_TEMP,W
    37.      MOVWF   STATUS  
    38.      SWAPF   W_TEMP,F  
    39.      SWAPF   W_TEMP,W  
    40.      RETFIE  
    41.  
    42.  
    43. INTRB0
    44.     CALL    MOTOR_RIGHT
    45.     CALL    DELAY_1S_B
    46.     CALL    MOTOR_STOP
    47.     CALL    DELAY_1S_B
    48.     RETURN
    49.  
    50. MAIN
    51.         CALL    SETUP
    52.     BSF     LED
    53.     CALL    DELAY_1S_A
    54.     BCF     LED
    55.     CALL    MOTOR_RIGHT
    56.     CALL    DELAY_1S_A
    57.     CALL    MOTOR_LEFT
    58.     CALL    DELAY_1S_A
    59.     GOTO    $-4
    60.  
    61. MOTOR_RIGHT
    62.             BCF     RELAY1
    63.             BSF     RELAY2
    64.             RETURN
    65.  
    66. MOTOR_LEFT
    67.             BSF     RELAY1
    68.             BCF     RELAY2
    69.             RETURN
    70.  
    71. MOTOR_STOP
    72.             BSF     RELAY1
    73.             BSF     RELAY2
    74.             RETURN
    75.  
    76. DELAY_1S_A
    77.     MOVLW    D'46'
    78.     MOVWF    A3
    79.     MOVLW    D'189'
    80.     MOVWF    A2
    81.     MOVLW    D'37'
    82.     MOVWF    A1
    83.     DECFSZ   A1, F
    84.     GOTO     $-1
    85.     DECFSZ   A2, F
    86.     GOTO     $-5
    87.     DECFSZ   A3, F
    88.     GOTO     $-9
    89.     RETURN
    90.  
    91. DELAY_1S_B
    92.     MOVLW    D'46'
    93.     MOVWF    B3
    94.     MOVLW    D'189'
    95.     MOVWF    B2
    96.     MOVLW    D'37'
    97.     MOVWF    B1
    98.     DECFSZ   B1, F
    99.     GOTO     $-1
    100.     DECFSZ   B2, F
    101.     GOTO     $-5
    102.     DECFSZ   B3, F
    103.     GOTO     $-9
    104.     RETURN
    105.  
    106.  
    107. SETUP
    108.      CLRF    PORTA
    109.      CLRF    PORTB
    110.      BSF     STATUS, RP0
    111.      MOVLW   B'00000000'
    112.      MOVWF   TRISA
    113.      MOVLW   B'00000001'
    114.      MOVWF   TRISB
    115.      MOVLW   B'11000000'
    116.      MOVWF   OPTION_REG
    117.      MOVLW   B'10010000'
    118.      MOVWF   INTCON
    119.      BCF     STATUS, RP0
    120.      RETURN
    121.  
    122.  
    123.      END
     
    Last edited: Apr 16, 2009
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Unable to set up a capture interrupt on port RA0, polling will have to do. Most PICs made before about 2004 didn't have Interrupts anyway...

    Set up Timer0 to timeout around 9mS (110Hz). Or 200-ish Hz to get two samples for reliability (suggested). You will need to use your clock frequency and set prescaler to the right value, 9mS for single pulse is a guess, and since not all prescaler values fit nicely, different math is involved.

    The most important part is to keep the Interrupt Routine as short as possible, only set flags that the main loop processes. Actually, at the speeds you are dealing with, it's not that important, but it's best to not miss a pulse if one can help it...

    Code ( (Unknown Language)):
    1.  
    2. unsigned integer count
    3. char dir (use defines for stop, left, right)
    4.  
    5. Interrupt()
    6. {
    7. If Timer Interrupt{
    8. Save anything that will be changed
    9. test for count < 1 (below 10Hz) dir = stop
    10. Test for count > 12 (over 100Hz) dir = right
    11. else dir = left
    12. count = 0
    13. Reset Timer0
    14. Clear interrupt flag
    15. cleanup
    16. }
    17.  
    18. main(void)
    19. {
    20. porta = 0 ; don't want to accidentally send a signal out;
    21. trisa=0x1 ; Set Porta.0 for input
    22.  
    23. for(; ;) Loop Forever
    24. if (porta.0 = 1)
    25.    {
    26.       while(porta.0 == 1); // Wait for end of pulse
    27.      count++; // count pulse
    28.     }
    29.  
    30. //dir is set in ISR above
    31.  
    32. if (dir == stop) stop_motor();;
    33. if (dir == left) run_left();
    34. if (dir == right) run_right();
    35. } // End main
    36.  
    37. motor direction functions here...
    38.  
     
  3. goldfish300

    Thread Starter Member

    Mar 12, 2009
    12
    0
    10x u very much for that but is diff than writing it in a lang diff then c i mean the assembly code . could u plz translate that to an assembly code if possible or other wise give me the hint for every part . 10x
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    It isn't even good C :eek:

    Interrupts in Assembly for 16F84 Should cover most of it.

    Other than the interrupts, it is just a counter and test, as used for flashing RGB LEDs, etc. Source code for those applications are all over google.
     
  5. goldfish300

    Thread Starter Member

    Mar 12, 2009
    12
    0
    10x ;) that was perfect
    bye
    :D:D
     
  6. goldfish300

    Thread Starter Member

    Mar 12, 2009
    12
    0
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    The code at the bottom is for mid range devices in general. Please post questions here rather than PM, and any code you have, so others can both help, and learn from it.
     
  8. goldfish300

    Thread Starter Member

    Mar 12, 2009
    12
    0
    i have added new info above so u can help me if possible :(
     
  9. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    The two links go to the same site, I guess I don't understand the question?

    That site covers interrupts, not counting. The counting in the pseudo-code above would be the same, and the interrupt would be the same, although all in assembly langauge.
     
Loading...