PIC16F627 IR problem

Discussion in 'Programmer's Corner' started by Amjad1313, Dec 31, 2011.

  1. Amjad1313

    Thread Starter New Member

    Dec 31, 2011
    4
    0
    Hey Everyone!
    I have a small code and a big problem!
    There is a TSOP IR receiver on the port B0, and a LED on the port A1.
    What i want: when i press a button on the remote, the LED must be turned on, and by the next button press, turned off. The problem is, the IR signal is long, and the LED flashes. I want to use the Timer0 for this problem, but doesnt work.
    Please help! Thank You!

    Code ( (Unknown Language)):
    1.  
    2.     LIST P= 16F627
    3.     #INCLUDE "P16F627.INC"
    4.     ERRORLEVEL 0, -302
    5.  
    6.     __CONFIG _CP_OFF&_WDT_OFF&_LVP_OFF&_INTRC_OSC_NOCLKOUT
    7.  
    8.     CBLOCK 0X70
    9.         W_TEMP
    10.         STATUS_TEMP
    11.     ENDC
    12.        
    13.     ORG 0x00
    14.     GOTO MAIN
    15.  
    16.     ORG 0x04
    17.     MOVWF W_TEMP
    18.     SWAPF STATUS,W
    19.     MOVWF STATUS_TEMP
    20.     BTFSC INTCON,T0IF        
    21.     GOTO T0MEGSZ    
    22.     BTFSC INTCON,INTF
    23.     GOTO MEGSZ_RB0
    24.     GOTO MEGSZKILEP
    25.    
    26. MEGSZ_RB0                
    27.     BTFSS PORTA,1    
    28.     GOTO BE
    29.     GOTO KI
    30. BE
    31.     BSF PORTA,1
    32.     GOTO MEGSZKILEP
    33. KI
    34.     BCF PORTA,1
    35.     GOTO MEGSZKILEP
    36.        
    37. MAIN
    38.     MOVLW 0x07            
    39.     MOVWF CMCON
    40.     BANKSEL TRISA
    41.     MOVLW 0x00
    42.     MOVWF TRISA
    43.     MOVLW 0xFF
    44.     MOVWF TRISB
    45.     MOVLW B'00000111'    
    46.     MOVWF OPTION_REG
    47.     BCF PCON,3            
    48.     BANKSEL PORTA        
    49.     BSF INTCON,INTE        
    50.     BSF INTCON,GIE
    51.    
    52. LOOP
    53.     NOP
    54.     GOTO LOOP
    55.    
    56. MEGSZKILEP
    57.     BCF INTCON,INTF
    58.     BCF INTCON,INTE
    59.     BSF INTCON,T0IE
    60.     SWAPF STATUS_TEMP,W
    61.     MOVWF STATUS
    62.     SWAPF W_TEMP,F
    63.     SWAPF W_TEMP,W
    64.     RETFIE
    65.    
    66. T0MEGSZ
    67.     BCF INTCON,INTF
    68.     BSF INTCON,INTE
    69.     BCF INTCON,T0IE
    70.     BCF INTCON,T0IF
    71.     SWAPF STATUS_TEMP,W
    72.     MOVWF STATUS
    73.     SWAPF W_TEMP,F
    74.     SWAPF W_TEMP,W
    75.     RETFIE
    76.        
    77.     END
    78.  
    79.  
     
    Last edited by a moderator: Dec 31, 2011
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    TIMER0 always runs, so after a short time T0IF will be set (regardless of whether T0IE is set). This won't cause an interrupt if T0IE is disabled, but when a pin change interrupt happens, the first thing you test for is T0IF so your functions are happening in the opposite order to what you intended.
    Testing the flags in the opposite order will probably help this.
    Also because TIMER0 always runs, you should clear it <ed> and then T0IF </ed> on a pin change interrupt, because otherwise it will be an essentially random value.
    If the time it takes for TIMER0 to overflow is not long enough for the whole IR signal, then you need to add a variable which counts a few overflows before disabling T0IE and setting INTE.
    Hope this helps.
     
    Last edited: Dec 31, 2011
    Amjad1313 likes this.
  3. Amjad1313

    Thread Starter New Member

    Dec 31, 2011
    4
    0
    Thank You very much! I will try to.
    And Happy New Year!

    But could be better the Timer1!?
     
    Last edited: Dec 31, 2011
  4. Amjad1313

    Thread Starter New Member

    Dec 31, 2011
    4
    0
    Hi!
    I tried it with Timer1, bat i have someting screwed up, because it doesnt work. :confused:
    When You have a little time, please see it. Thank You!

    Code ( (Unknown Language)):
    1.  
    2.     ORG 0x00
    3.     GOTO MAIN
    4.  
    5.     ORG 0x04
    6.     MOVWF W_TEMP
    7.     SWAPF STATUS,W
    8.     MOVWF STATUS_TEMP
    9.     BTFSC PIR1,TMR1IF        
    10.     GOTO MEGSZ_T1
    11.     BTFSC INTCON,INTF
    12.     GOTO MEGSZ_RB0
    13.     GOTO MEGSZKILEP
    14.    
    15. MEGSZ_RB0                ;led  ki/be
    16.     BTFSS PORTA,1    
    17.     GOTO BE
    18.     GOTO KI
    19. BE
    20.     BSF PORTA,1
    21.     GOTO MEGSZKILEP
    22. KI
    23.     BCF PORTA,1
    24.     GOTO MEGSZKILEP
    25.        
    26. MAIN
    27.     MOVLW 0x07            ;PORTA digitális
    28.     MOVWF CMCON
    29.     BANKSEL TRISA
    30.     MOVLW 0x00
    31.     MOVWF TRISA
    32.     MOVLW 0xFF
    33.     MOVWF TRISB
    34.     MOVLW B'00000000'    ;Felhúzó ell.
    35.     MOVWF OPTION_REG
    36.     BCF PCON,3            ;OSC 37KHz *****
    37.     BCF PIE1,TMR1IE
    38.     BANKSEL PORTA
    39.     MOVLW B'00000000'    ;T1 INTOSC, *****
    40.     MOVWF T1CON
    41.     BSF INTCON,INTE        ;RB0
    42.     BSF INTCON,GIE
    43.     BSF INTCON,PEIE
    44.    
    45. LOOP
    46.     NOP
    47.     GOTO LOOP
    48.    
    49. MEGSZKILEP
    50.     CLRF TMR1H
    51.     CLRF TMR1L
    52.     BCF INTCON,INTE
    53.     BSF T1CON,TMR1ON
    54.     BANKSEL PIE1
    55.     BSF PIE1,TMR1IE
    56.     SWAPF STATUS_TEMP,W
    57.     MOVWF STATUS
    58.     SWAPF W_TEMP,F
    59.     SWAPF W_TEMP,W
    60.     RETFIE
    61.    
    62. MEGSZ_T1
    63.     BCF PIR1,TMR1IF
    64.     BCF T1CON,TMR1ON
    65.     BCF INTCON,INTF
    66.     BSF INTCON,INTE
    67.     BANKSEL PIE1
    68.     BCF PIE1,TMR1IE
    69.     SWAPF STATUS_TEMP,W
    70.     MOVWF STATUS
    71.     SWAPF W_TEMP,F
    72.     SWAPF W_TEMP,W
    73.     RETFIE
    74.        
    75.     END
    76.  
    77.  
     
    Last edited by a moderator: Jan 1, 2012
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Timer1 is an option, but most of the things I've mentioned still apply. Timer1 can be stopped, but it is just as easy to leave it running and disable the interrupts.
    Timer1 only has (up to) a 1:8 prescaler so it's maximum overflow time is 8 times as long as Timer0.
     
    Amjad1313 likes this.
  6. Amjad1313

    Thread Starter New Member

    Dec 31, 2011
    4
    0
    Hi!
    It works well!!! :):):)
    My PicKit2 is perhaps bad. I tried it with Pk2 (power), and MCLR is to small.
    ===>Reset.
    With a 10K resistor it is good! (I dont know, why?)
    Excuse me for my english.

    Have a nice day!
     
Loading...