micro basic error with timer1 interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by h.d, May 10, 2008.

  1. h.d

    Thread Starter Active Member

    Oct 22, 2007
    150
    0
    hi all
    am working in the following program and has two problems
    i will talk about what th eprogram do and the problems
    and please help me to solve it
    first it for PIC 16F877A 4Mhz

    its 5 inputs
    RD2 FOR INCREASE SPEED
    RD3 DOR DECREASE SPEED
    RD4 TO START PROGRAM
    RB0 (INTERRUPT)STOP PWM AND WHOLE PROGRAM THEN DISPLAY "STOP"
    RA4 ENXTERNAL COUNTER

    IN ADITION TO TIMER SECOUNDES AND MINUETES START WITH THE PROGRAM AND DISPLAYED ON LCD

    THE SPEED AND DISTANCE DISPALYED ON LCD ..
    THE PROBLEMS
    1. WHEN INTERRUPT RB0 CANT USE THE DISPLAY AND I DONT KNOW HOW TO DO IT
    2. FOR THE TIMER ITS LAG 18SEC FROM THE PROGRAM START AND IF PRESS OUN RD2 AND RD3 THE TIMER STOP AND LAG 18 SEC


    SO PEASE HELP ME TO SOLVE THAT... ​



    Code:


    Code ( (Unknown Language)):
    1. program functiontestpwm
    2. dim z1 as byte
    3. dim cnt as byte
    4.  
    5. sub procedure delay800    'delay 800ms
    6. delay_ms(800)
    7. end sub
    8.  
    9. sub procedure delay100    'delay 100ms
    10. delay_ms(100)
    11. end sub
    12.  
    13.  
    14.  
    15.  
    16. sub procedure interrupt
    17. if INTCON.INTF=1 then
    18. intcon.intf=0
    19. pwm_stop
    20. end if
    21.   cnt = cnt + 1
    22.   pir1.0 = 0      ' Clear TMR1IF
    23. end sub
    24.  
    25.  
    26. sub function upspeed(dim x as byte) as byte  'increase speed
    27. dim j as byte
    28. j=x+10
    29. if j>190 then
    30. j=190
    31. end if
    32. result=j
    33. end sub
    34.  
    35. sub function downspeed(dim r as byte)as byte      'decrease speed
    36. dim f as byte
    37. f=r-10
    38. if f>200 then
    39. f=0 end if
    40. result=f
    41. end sub
    42.  
    43.  
    44.  
    45. dim sec,min,a as byte            'decleration
    46. dim min1,sec1 as string[3]
    47.  
    48. dim y,b,w,q,dist,se as byte
    49. dim y1,q1,dist1 as string[3]
    50.  
    51. main:
    52. b=1
    53. z1=0
    54. q=0
    55. w=0
    56. dist=0
    57. tmr0=0
    58.  
    59. T1CON = 1
    60. PIR1.TMR1IF = 0         ' Clear TMR1IF
    61. PIE1  =   1             ' Enable interrupts
    62.  
    63. cnt =   0               ' Initialize cnt
    64.  
    65.  
    66. option_reg=$68
    67. intcon=$D0    'timer1 and RB0 interrupt
    68.  
    69. trisd=$ff
    70. trisb=$00
    71. trisa=$ff
    72. portc=0
    73.  
    74. lcd_config(portb,7,6,5,4,portb,1,2,3)'display primary parameters
    75. delay800
    76. lcd_cmd(lcd_cursor_off)
    77. lcd_out(1,1,"wait start.")
    78.  
    79.  
    80. start:
    81.  
    82. delay800
    83. lcd_cmd(lcd_clear)
    84. delay800
    85. lcd_out(1,1,"time  spd  dist.")   'dispay promary parameters
    86.  
    87.  
    88. pwm_init(8000)'pwm initiat
    89. delay100
    90. pwm_start 'start pwm D.C=0
    91. run:
    92.  
    93. 'If cnt is 15, then inc sec untill =60 inc min and reset cnt
    94.  
    95. if cnt = 15 then  'timer
    96.       inc(sec)
    97.  
    98.       bytetostr(sec,sec1)
    99.  
    100.       lcd_out(2,3,sec1)
    101.       cnt = 0
    102.  
    103. if sec=59 then
    104.       inc(min)
    105.       sec=0
    106.       end if
    107.     end if
    108.  
    109.     bytetostr(min,min1)
    110.     lcd_out(2,0,min1)
    111.  
    112.  
    113. if portd.2=0 then'up-speed switch
    114. delay800
    115. z1=upspeed(z1)
    116.  
    117. end if
    118.  
    119. if portd.3=0 then   'down speed switch
    120. delay800
    121. z1=downspeed(z1)
    122.  
    123.  
    124. end if
    125.  
    126.  pwm_change_duty(z)'change speed
    127.  
    128.  
    129. bytetostr(z1,y1)
    130. lcd_out(2,8,y1)  'display speed
    131.  
    132.  
    133. if tmr0=5 then   'shaft encoder signal to calvulate distance
    134.  inc(q)
    135.  tmr0=0
    136.  end if
    137.   bytetostr(q,q1)
    138.  lcd_out(2,12,q1)
    139.  
    140.  
    141.  goto run
    142.  
    143.  
    144. end.
     
    Last edited: May 11, 2008
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Code ( (Unknown Language)):
    1.  
    2. Code:
    3.  
    4. program functiontestpwm
    5. dim z1 as byte
    6. dim cnt as byte
    7.  
    8. sub procedure delay800 'delay 800ms
    9.    delay_ms(800)
    10. end sub
    11.  
    12. sub procedure delay100 'delay 100ms
    13.    delay_ms(100)
    14. end sub
    15.  
    16.  
    17.  
    18.  
    19. sub procedure interrupt
    20.    if INTCON.INTF=1 then
    21.       intcon.intf=0
    22.       pwm_stop
    23.    end if
    24.    cnt = cnt + 1
    25.    pir1.0 = 0 ' Clear TMR1IF
    26. end sub
    27.  
    28.  
    29. sub function upspeed(dim x as byte) as byte 'increase speed
    30.    dim j as byte
    31.    j=x+10
    32.    if j>190 then
    33.       j=190
    34.    end if
    35.    result=j
    36. end sub
    37.  
    38. sub function downspeed(dim r as byte)as byte 'decrease speed
    39.    dim f as byte
    40.    f=r-10
    41.    if f>200 then
    42.       f=0
    43.    end if
    44.    result=f
    45. end sub
    46.  
    47.  
    48.  
    49. dim sec,min,a as byte 'decleration
    50. dim min1,sec1 as string[3]
    51.  
    52. dim y,b,w,q,dist,se as byte
    53. dim y1,q1,dist1 as string[3]
    54.  
    55. main:
    56.    b=1
    57.    z1=0
    58.    q=0
    59.    w=0
    60.    dist=0
    61.    tmr0=0
    62.  
    63.    T1CON = 1
    64.    PIR1.TMR1IF = 0 ' Clear TMR1IF
    65.    PIE1 = 1 ' Enable interrupts
    66.  
    67.    cnt = 0 ' Initialize cnt
    68.  
    69.  
    70.    option_reg=$68
    71.    intcon=$D0 'timer1 and RB0 interrupt
    72.  
    73.    trisd=$ff
    74.    trisb=$00
    75.    trisa=$ff
    76.    portc=0
    77.  
    78.    lcd_config(portb,7,6,5,4,portb,1,2,3)'display primary parameters
    79.    delay800
    80.    lcd_cmd(lcd_cursor_off)
    81.    lcd_out(1,1,"wait start.")
    82.  
    83.  
    84. start:
    85.  
    86.    delay800
    87.    lcd_cmd(lcd_clear)
    88.    delay800
    89.    lcd_out(1,1,"time spd dist.") 'dispay promary parameters
    90.  
    91.  
    92.    pwm_init(8000)'pwm initiat  
    93.    delay100
    94.    pwm_start 'start pwm D.C=0
    95.  
    96. run:
    97.  
    98.    'If cnt is 15, then inc sec untill =60 inc min and reset cnt
    99.  
    100.    if cnt = 15 then 'timer
    101.       inc(sec)
    102.  
    103.       bytetostr(sec,sec1)
    104.  
    105.       lcd_out(2,3,sec1)
    106.       cnt = 0
    107.  
    108.       if sec=59 then
    109.          inc(min)
    110.          sec=0
    111.       end if
    112.    end if
    113.  
    114.    bytetostr(min,min1)
    115.    lcd_out(2,0,min1)
    116.  
    117.  
    118.    if portd.2=0 then'up-speed switch
    119.       delay800
    120.       z1=upspeed(z1)
    121.  
    122.    end if
    123.  
    124.    if portd.3=0 then 'down speed switch
    125.       delay800
    126.       z1=downspeed(z1)
    127.  
    128.  
    129.    end if
    130.  
    131.    pwm_change_duty(z)'change speed
    132.  
    133.  
    134.    bytetostr(z1,y1)
    135.    lcd_out(2,8,y1) 'display speed
    136.  
    137.  
    138.    if tmr0=5 then 'shaft encoder signal to calvulate distance
    139.       inc(q)
    140.       tmr0=0
    141.    end if
    142.    bytetostr(q,q1)
    143.    lcd_out(2,12,q1)
    144.  
    145.    goto run
    146.  
    147. end.
    148.  
    Formatted the code from your original post using the "CODE" tags.
     
    Last edited: May 10, 2008
  3. h.d

    Thread Starter Active Member

    Oct 22, 2007
    150
    0
    ok thanks for your note.......
     
Loading...