Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

IR with junebug

Status
Not open for further replies.

AtomSoft

Well-Known Member
Of course i am using a 18F1320 and a junebug....
I found code from Here and edited it. It works well but not as expected really. I wanted when i press a button (1 or Vol Up) LED #1 lights and it does but doesnt stay lit for long. The same for button (2 or Vol Down) but for some reason it lights for like 1 sec then turns off. Its supposed to stay ON until a new command is recieved . Any help would be great.

Code:
	list    p=18F1320
	include	<p18F1320.inc>
;	include	<Macros.inc>  - Dont need
	CONFIG	OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON

btDone	equ	0x0
btStart	equ	0x1
btCommand	equ	0x2


	cblock	0x00
		Time2
		BitCount
		DeviceCode
		CommandCode
		IRStatus
		Bit
		ScratchCode
		d1
		d2
		d3

	endc		
	org	0x0000
	goto	Init

;****************************
;* Interrupt Service        *
;****************************

	org	0x0008

ISR	
	bcf	LATA, RA0
	bcf	LATA, RA6
	btfsc	IRStatus, btDone	;Has a code already been read?
	goto	ISR_Done
	btfsc	INTCON2, INTEDG0	;Check if falling or rising signal
	goto	Rise
	btg	INTCON2, INTEDG0

	clrf	TMR0L		;Reset Timer

	movlw	b'11000111'	;Enable timer 0, use 8bit counter, use 256 prescaler
	movwf	T0CON		;Do it
	

	goto	ISR_Done
Rise

	clrf	T0CON		;Disable timer 0
	movff	TMR0L, Time2	;Put timer 0 value into storage
	btg	INTCON2, INTEDG0

	nop	
GetPulseWidth
	movlw	0x11		;Check for start pulse,
				;should be between 0x12 and 0x14
	cpfsgt	Time2		;Greater than 0x11?
	goto	CheckZero
	movlw	0x15
	cpfslt	Time2		;Less than 0x15?
	goto	IRError		;Start Bit exceeds tolerance
	
StartBit				;We have a Start Bit
	btfsc	IRStatus, btStart	;Check if already set (ERROR)
	goto	IRError
	bsf	IRStatus, btStart	;Flag IRStatus 

	movlw	0x7
	movwf	BitCount
	goto	ISR_Done
CheckZero
	movlw	0x3		;Check for zero pulse
				;Should be between 0x4 and 0x6
	cpfsgt	Time2		;Greater than 0x3?
	goto	IRError
	movlw	0x7
	cpfslt	Time2		;Less than 0x7?
	goto	CheckOne
ZeroBit				;We have a zero bit
	btfss	IRStatus, btStart	;Check if we have previously had Start Bit
	goto	IRError
	movlw	0x00
	movwf	Bit
	call	ProcessBit
	goto	ISR_Done
CheckOne
	movlw	0x8		;Check for One Pulse
				;Should be between 0x9 and 0xB
	cpfsgt	Time2		;Greater than 0x8?
	goto	IRError
	movlw	0xC
	cpfslt	Time2		;Less than 0xC
	goto	IRError

OneBit				;We have a One bit
	btfss	IRStatus, btStart	;Check if we have previously had Start Bit
	goto	IRError
	movlw	0x1
	movwf	Bit
	call	ProcessBit
	goto	ISR_Done


IRError
	call ClearVariables
	bcf	INTCON2, INTEDG0	; Interrupt on falling edge

ISR_Done	
	clrf	LATA
	bcf	INTCON, INT0IF

	retfie	FAST	


;****************************
;* End of Interrupt Service *
;****************************

;*******************
;* Routines        *
;*******************
ProcessBit
	btfss	Bit,0		;Check if bit set
	goto	WriteZero
WriteOne
	bsf	ScratchCode, 0x7
	goto	RotateRight
WriteZero
	bcf	ScratchCode, 0x7
RotateRight
	rrncf	ScratchCode
	decfsz	BitCount
	return
WhichCode
	btfss	IRStatus, btCommand
	goto	CommandCodeDone
DeviceCodeDone
	rrncf	ScratchCode	;a couple of more rotations
	rrncf	ScratchCode
	movff	ScratchCode, DeviceCode
	bsf	IRStatus, btDone
	return
CommandCodeDone
	movff	ScratchCode, CommandCode
	bsf	IRStatus, btCommand
	movlw	0x5
	movwf	BitCount
	clrf	ScratchCode
	return



ClearVariables
	clrf	IRStatus
	clrf	DeviceCode
	clrf	CommandCode
	clrf	ScratchCode
	return


;****************************
;* End of Routines          *
;****************************



Init	
	call	ClearVariables
	movlw    0x72 		; 8MHz clock select
    	movwf    OSCCON

	bcf	ADCON1, PCFG0	; Set RA0 as digital
		
	bsf	TRISB, RB0	; Make RB0 Input
	bsf	TRISB, RB2	; Make RB5 Input
	bcf	INTCON2, RBPU

	bsf	ADCON1, PCFG4	; Make RB0 digital

	
	movlw	b'10111000'	
	movwf	TRISA

	bcf	LATA, RA0
	bcf	LATA, RA6
	
	clrf	T0CON
	
InitISR	
	movlw	0x13
	sublw	0x13


	bcf	INTCON2, INTEDG0	; Interrupt on falling edge

	bsf	INTCON, INT0IE	; Enable INT0 interrupt	
	bcf	RCON, IPEN	; Disable priority interrupts
	bcf	INTCON, INT0IF	; Clear INT0 flag - This stumped me at first
	bcf	INTCON, PEIE	; Disable peripheral interrupts

	bsf	INTCON, GIE	; Enable global Interrupt
	
	
Main
	bsf	LATA, RA0		;Light LED 1
	bcf	LATA, RA6
	;BCF	LATA, RA1
	;BCF	LATA, RA2
	btfss	IRStatus, btDone
	goto	Main
	bcf	LATA, RA0		;Light LED 2
	BSF	LATA, RA6
	[b]
WaitForRB2


;	btfsc	PORTB, RB2
	;goto	WaitForRB2
	;AtomSoft Code

	MOVLW	0x10	;My Vol Device
	CPFSEQ	DeviceCode
	goto 	NumCode
	goto	IsOurs		
NumCode	MOVLW	0x0D	;My Number Device
	CPFSEQ	DeviceCode
	goto	NotOurs
	goto	IsOurs2
NotOurs	
	call	ClearVariables
	goto	Main
IsOurs	
	MOVLW	0x12	;My Volume Up Button
	CPFSEQ	CommandCode
	goto	Check2 	;Not Volume Up
	goto	VolUp
Check2	
	MOVLW	0x13	;Volume Down
	CPFSEQ	CommandCode
	goto	NotOurs	;Not in our commands so clear all
	goto	VolDwn
VolUp
No1	call	Delay
	BCF	LATA, RA1
	BSF	LATA, RA2	;High on RA2 my TOP LED

	call	Delay
	call	Delay
	call	Delay
	goto	NotOurs
VolDwn
No2	call	Delay
	BCF	LATA, RA2
	BSF	LATA, RA1	;High on RA1 my BOTTOM LED
	call	Delay
	call	Delay
	call	Delay
	goto	NotOurs
IsOurs2
	MOVLW	0x00	;My # 1 button
	CPFSEQ	CommandCode
	goto	CheckN2
	goto	No1
CheckN2
	MOVLW	0x01	;My # 1 button
	CPFSEQ	CommandCode
	goto	NotOurs
	goto	No2

Delay
	movlw	0x07
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
Delay_0
	decfsz	d1, f
	bra	$+6
	decfsz	d2, f
	bra	$+6
	decfsz	d3, f
	goto	Delay_0
	bra	$+2
	bra	$+2
	bra	$+2
	return
	end
[/b]
 
This code looks familiar ;-), what happens when you put a breakpoint at the "CPFSEQ DeviceCode"?.

can you establish that it has correctly interpretted the the VOLUP code?
 
yes because it lights up but the issue is it doesnt stay lit. it turns off and it should only turn off if the other one is on and both are off
 
Found it! It was here
Code:
ISR_Done	
	;clrf	LATA ; This was your code clearing the whole LATA i altered it for 6 and 0 only
	BCF	LATA, 0
	BCF	LATA, 6
	bcf	INTCON, INT0IF
 
Final Code:
Code:
	list    p=18F1320
	include	<p18F1320.inc>

	CONFIG	OSC = INTIO1, WDT = OFF, LVP = OFF, DEBUG = ON

btDone	equ	0x0
btStart	equ	0x1
btCommand	equ	0x2

	cblock	0x00
		Time2
		BitCount
		DeviceCode
		CommandCode
		IRStatus
		Bit
		ScratchCode
		d1
		d2
		d3
	endc		
	org	0x0000
	goto	Init
;****************************
;* Interrupt Service        *
;****************************
	org	0x0008
ISR	
	bcf	LATA, RA0
	bcf	LATA, RA6
	btfsc	IRStatus, btDone		;Has a code already been read?
	goto	ISR_Done
	btfsc	INTCON2, INTEDG0		;Check if falling or rising signal
	goto	Rise
	btg	INTCON2, INTEDG0
	clrf	TMR0L				;Reset Timer
	movlw	b'11000111'			;Enable timer 0, use 8bit counter, use 256 prescaler
	movwf	T0CON				;Do it
	goto	ISR_Done
Rise
	clrf	T0CON				;Disable timer 0
	movff	TMR0L, Time2			;Put timer 0 value into storage
	btg	INTCON2, INTEDG0
	nop	
GetPulseWidth
	movlw	0x11				;Check for start pulse, should be between 0x12 and 0x14
	cpfsgt	Time2				;Greater than 0x11?
	goto	CheckZero
	movlw	0x15
	cpfslt	Time2				;Less than 0x15?
	goto	IRError				;Start Bit exceeds tolerance
StartBit					;We have a Start Bit
	btfsc	IRStatus, btStart		;Check if already set (ERROR)
	goto	IRError
	bsf	IRStatus, btStart		;Flag IRStatus 
	movlw	0x7
	movwf	BitCount
	goto	ISR_Done
CheckZero
	movlw	0x3				;Check for zero pulse Should be between 0x4 and 0x6
	cpfsgt	Time2				;Greater than 0x3?
	goto	IRError
	movlw	0x7
	cpfslt	Time2				;Less than 0x7?
	goto	CheckOne
ZeroBit						;We have a zero bit
	btfss	IRStatus, btStart		;Check if we have previously had Start Bit
	goto	IRError
	movlw	0x00
	movwf	Bit
	call	ProcessBit
	goto	ISR_Done
CheckOne
	movlw	0x8				;Check for One Pulse Should be between 0x9 and 0xB
	cpfsgt	Time2				;Greater than 0x8?
	goto	IRError
	movlw	0xC
	cpfslt	Time2				;Less than 0xC
	goto	IRError
OneBit						;We have a One bit
	btfss	IRStatus, btStart		;Check if we have previously had Start Bit
	goto	IRError
	movlw	0x1
	movwf	Bit
	call	ProcessBit
	goto	ISR_Done
IRError
	call ClearVariables
	bcf	INTCON2, INTEDG0		; Interrupt on falling edge
ISR_Done	
	;clrf	LATA
	BCF	LATA, 0
	BCF	LATA, 6
	bcf	INTCON, INT0IF
	retfie	FAST	
;****************************
;* End of Interrupt Service *
;****************************
;*******************
;* Routines        *
;*******************
ProcessBit
	btfss	Bit,0				;Check if bit set
	goto	WriteZero
WriteOne
	bsf	ScratchCode, 0x7
	goto	RotateRight
WriteZero
	bcf	ScratchCode, 0x7
RotateRight
	rrncf	ScratchCode
	decfsz	BitCount
	return
WhichCode
	btfss	IRStatus, btCommand
	goto	CommandCodeDone
DeviceCodeDone
	rrncf	ScratchCode			;a couple of more rotations
	rrncf	ScratchCode
	movff	ScratchCode, DeviceCode
	bsf	IRStatus, btDone
	return
CommandCodeDone
	movff	ScratchCode, CommandCode
	bsf	IRStatus, btCommand
	movlw	0x5
	movwf	BitCount
	clrf	ScratchCode
	return



ClearVariables
	clrf	IRStatus
	clrf	DeviceCode
	clrf	CommandCode
	clrf	ScratchCode
	return
;****************************
;* End of Routines          *
;****************************
Init	
	call	ClearVariables
	movlw    0x72 				; 8MHz clock select
    	movwf    OSCCON
	bcf	ADCON1, PCFG0			; Set RA0 as digital
	bsf	TRISB, RB0			; Make RB0 Input
	bsf	TRISB, RB2			; Make RB5 Input
	bcf	INTCON2, RBPU
	bsf	ADCON1, PCFG4			; Make RB0 digital
	movlw	b'10111000'	
	movwf	TRISA
	bcf	LATA, RA0
	bcf	LATA, RA6
	clrf	T0CON
	
InitISR	
	movlw	0x13
	sublw	0x13
	bcf	INTCON2, INTEDG0		; Interrupt on falling edge
	bsf	INTCON, INT0IE			; Enable INT0 interrupt	
	bcf	RCON, IPEN			; Disable priority interrupts
	bcf	INTCON, INT0IF			; Clear INT0 flag - This stumped me at first
	bcf	INTCON, PEIE			; Disable peripheral interrupts
	bsf	INTCON, GIE			; Enable global Interrupt
Main
	bsf	LATA, RA0			;Light LED 1
	bcf	LATA, RA6
	btfss	IRStatus, btDone
	goto	Main
	bcf	LATA, RA0			;Light LED 2
	BSF	LATA, RA6
NumCode	MOVLW	0x0D				;My Number Device
	CPFSEQ	DeviceCode
	goto	NotOurs
	goto	IsOurs
NotOurs	
	call	ClearVariables
	goto	Main
IsOurs
	MOVLW	0x00				;My # 1 button
	CPFSEQ	CommandCode
	goto	CheckN2
	goto	No1
CheckN2
	MOVLW	0x01				;My # 2 button
	CPFSEQ	CommandCode
	goto	NotOurs
	goto	No2
No1	
	;BCF	LATA, 1
	BTG	LATA, 2				;High on RA2 my TOP LED
	goto	NotOurs
No2	
	;BCF	LATA, 2
	BTG	LATA, 1				;High on RA1 my BOTTOM LED
	goto	NotOurs
	end
 
Here look for some reason i like making videos :D : (press play)
[embed]http://www.youtube.com/v/inDxDjhzeWA[/embed]
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top