PIC16F627 IR problem

Thread Starter

Amjad1313

Joined Dec 31, 2011
4
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!

Rich (BB code):
    LIST P= 16F627
    #INCLUDE "P16F627.INC"
    ERRORLEVEL 0, -302

    __CONFIG _CP_OFF&_WDT_OFF&_LVP_OFF&_INTRC_OSC_NOCLKOUT

    CBLOCK 0X70
        W_TEMP
        STATUS_TEMP
    ENDC
        
    ORG 0x00
    GOTO MAIN

    ORG 0x04
    MOVWF W_TEMP
    SWAPF STATUS,W
    MOVWF STATUS_TEMP
    BTFSC INTCON,T0IF        
    GOTO T0MEGSZ    
    BTFSC INTCON,INTF
    GOTO MEGSZ_RB0
    GOTO MEGSZKILEP
    
MEGSZ_RB0                
    BTFSS PORTA,1    
    GOTO BE
    GOTO KI
BE
    BSF PORTA,1
    GOTO MEGSZKILEP
KI
    BCF PORTA,1
    GOTO MEGSZKILEP
        
MAIN 
    MOVLW 0x07            
    MOVWF CMCON
    BANKSEL TRISA
    MOVLW 0x00
    MOVWF TRISA
    MOVLW 0xFF
    MOVWF TRISB
    MOVLW B'00000111'    
    MOVWF OPTION_REG
    BCF PCON,3            
    BANKSEL PORTA        
    BSF INTCON,INTE        
    BSF INTCON,GIE
    
LOOP
    NOP
    GOTO LOOP
    
MEGSZKILEP
    BCF INTCON,INTF
    BCF INTCON,INTE
    BSF INTCON,T0IE
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE
    
T0MEGSZ
    BCF INTCON,INTF
    BSF INTCON,INTE
    BCF INTCON,T0IE
    BCF INTCON,T0IF
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE
        
    END
 
Last edited by a moderator:

Markd77

Joined Sep 7, 2009
2,806
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:

Thread Starter

Amjad1313

Joined Dec 31, 2011
4
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!

Rich (BB code):
    ORG 0x00
    GOTO MAIN

    ORG 0x04
    MOVWF W_TEMP
    SWAPF STATUS,W
    MOVWF STATUS_TEMP
    BTFSC PIR1,TMR1IF        
    GOTO MEGSZ_T1
    BTFSC INTCON,INTF
    GOTO MEGSZ_RB0
    GOTO MEGSZKILEP
    
MEGSZ_RB0                ;led  ki/be
    BTFSS PORTA,1    
    GOTO BE
    GOTO KI
BE
    BSF PORTA,1
    GOTO MEGSZKILEP
KI
    BCF PORTA,1
    GOTO MEGSZKILEP
        
MAIN 
    MOVLW 0x07            ;PORTA digitális
    MOVWF CMCON
    BANKSEL TRISA
    MOVLW 0x00
    MOVWF TRISA
    MOVLW 0xFF
    MOVWF TRISB
    MOVLW B'00000000'    ;Felhúzó ell.
    MOVWF OPTION_REG
    BCF PCON,3            ;OSC 37KHz *****
    BCF PIE1,TMR1IE
    BANKSEL PORTA
    MOVLW B'00000000'    ;T1 INTOSC, *****
    MOVWF T1CON
    BSF INTCON,INTE        ;RB0 
    BSF INTCON,GIE
    BSF INTCON,PEIE
    
LOOP
    NOP
    GOTO LOOP
    
MEGSZKILEP
    CLRF TMR1H
    CLRF TMR1L
    BCF INTCON,INTE
    BSF T1CON,TMR1ON
    BANKSEL PIE1
    BSF PIE1,TMR1IE
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE
    
MEGSZ_T1
    BCF PIR1,TMR1IF
    BCF T1CON,TMR1ON
    BCF INTCON,INTF
    BSF INTCON,INTE
    BANKSEL PIE1
    BCF PIE1,TMR1IE
    SWAPF STATUS_TEMP,W
    MOVWF STATUS
    SWAPF W_TEMP,F
    SWAPF W_TEMP,W
    RETFIE
        
    END
 
Last edited by a moderator:

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

Amjad1313

Joined Dec 31, 2011
4
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!
 
Top