Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 14th January 2008, 11:19 PM   (permalink)
Default JuneBug SIRC RX - Complete

Hi All,

Thanks to the great advice given from this site I utilised the IR module of my JuneBug to detect SIRC codes. I have included the completed the code below - Thanks to all who helped!

How it works
The IR receiver has its output put into RB0, I configured INT0 on this port to trigger on the falling edge - or the beginning of a pulse.

When this happens the code clears TMR0L and enables the timer, the INT0 interrupt is then altered to fire upon the rising edge.

When this occurs TIMER0 is disabled and a reading is taken from TMR0L.

Taking TMR0L readings from as far away as 10 meters, gives me.
0x12-0x14 = for a StartBit
0x9 - 0xB = for a "one" bit
0x4 - 0x6 = for a "zero" Bit

What the code doesnt do
1. Nigel Goodwin's tutorials indicate the possibility of "extended" SIRC codes, these have not been accomodated into this code (but could be).

2. does not have any output routines, sorry I just wanted to check the IR functionality and so I havent patched this into any LCD code etc.

How to get it working
Best to run from MPLAB IDE in debug mode, when first run LED1 will light indicating it is ready to receive a code, when you fire a SIRC code at it LED2 should light indicating it has received a valid code.

At this stage, stop the debugger and inspect variables CommandCode and DeviceCode you should see the relevant 7 and 5 bit values.

To start again, run the debugger and press button 2 on the JuneBug to begin again.



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

	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
	bsf	OSCCON, IRCF2	;Set Internal 8MHz
	bsf	OSCCON, IRCF1
	bsf	OSCCON, IRCF0

	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'10111110'	
	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
	
WaitForRB2


	btfsc	PORTB, RB2
	goto	WaitForRB2
	call	ClearVariables
	goto	Main
	end

Last edited by UTMonkey; 15th January 2008 at 01:05 PM.
UTMonkey is offline  
Old 15th January 2008, 02:16 AM   (permalink)
Default

Wow, you beat me to it UTMonkey. Nice coding.

Where does one get "Macros.inc"?
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com

Last edited by blueroomelectronics; 15th January 2008 at 06:21 AM.
blueroomelectronics is offline  
Old 15th January 2008, 01:03 PM   (permalink)
Default

Hi Bill,

You can remove that line, it links to the charlie plexing macros (you wrote actually) but I didnt get to use them.

Regards

Mark
UTMonkey is offline  
Old 15th January 2008, 03:58 PM   (permalink)
Default

May I make a couple of observations
1. Change INTIO1 to INTIO2
2. Use "bra" instead of "goto" (short 1K branch)

Nice use of the compare instructions, so handy to have.
I noticed you had fun with 18F interrupts too, they can be a little confusing at first. "retfie fast" is a sneaky one for anyone used to 16F PICs.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com

Last edited by blueroomelectronics; 15th January 2008 at 04:01 PM.
blueroomelectronics is offline  
Old 15th January 2008, 07:45 PM   (permalink)
Default

Okeydoke, thanks.

Its been an enjoyable learning curve.
UTMonkey is offline  
Old 15th January 2008, 09:32 PM   (permalink)
Default

Quote:
Originally Posted by UTMonkey
Okeydoke, thanks.

Its been an enjoyable learning curve.
I'm learning the 18F too, always something new to learn. A real nice PIC family to work with.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Old 29th January 2008, 01:58 AM   (permalink)
Question Would it work here

Hi UTMonkey,

I have also purchased a Junebug. Great little Tutor/Programmer.

I bought it for the exact same purpose. To receive SIRC codes (or very near SIRC code).

I have a Neuros OSD digital recorder that uses what looks like Sony SIRC protocol. See here, pages 5 & 6 of the link below:

http://open.neurostechnology.com/fil...E%20060711.pdf

The IR is fairly close to Sony Sirc.

I wonder if your code would work?
ex-navy is offline  
Old 29th January 2008, 04:37 AM   (permalink)
Default Would your SIRC work here

Well done on your SIRC RX and well done to Bill on the Junebug!!!

I just bought a Junebug with the 18F1320 so I could maybe develop a SIRC
rxer for the Neuros OSD (www.neurostechnology.com)

The OSD remote uses an IR protocol very close to what you have designed.

Could your code be used:

Check out pages 5 & 6 of the link here:

http://open.neurostechnology.com/fil...E%20060711.pdf
ex-navy is offline  
Old 29th January 2008, 08:02 AM   (permalink)
Default

That sure looks like it'll works. There is tonnes of code for SIRCs and PICs on the net. The Sony transmits at 40kHz but the 38kHz detector on the Junebug will see it fine (digikey sells the 40kHz Vishay IR detector). UTMonkeys code should work fine, do you know how to use the MPLAB debugger?
And thanks for the compliment.
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Old 29th January 2008, 08:27 AM   (permalink)
Default

Looks like fun. Guess I need to get a Digikey order together and get myself a TSOP34838 for my Junebug.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================
futz is online now  
Old 29th January 2008, 06:47 PM   (permalink)
Default

Here's the SIRCs 12bit protocol, more info can be found on this site.
http://www.sbprojects.com/knowledge/ir/sirc.htm
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline  
Old 29th January 2008, 09:12 PM   (permalink)
Default

Hi Ex-Navy,

I agree with Bill, it is SIRC.

If the start bit takes 2.4ms then a "one" bit is half that (1.2ms) and then "zero" is 0.6.

It may be a bit reckless but I was thinking of altering my code to assume the codes it would receive would always be SIRC. That way the first bit could be "measured" and the 1 and 0 bits could be deduced from that, rather than expecting a particular timing.

Good Luck

Mark
UTMonkey is offline  
Old 30th January 2008, 10:37 AM   (permalink)
Default

Quote:
Originally Posted by UTMonkey
Hi Ex-Navy,

I agree with Bill, it is SIRC.

If the start bit takes 2.4ms then a "one" bit is half that (1.2ms) and then "zero" is 0.6.

It may be a bit reckless but I was thinking of altering my code to assume the codes it would receive would always be SIRC. That way the first bit could be "measured" and the 1 and 0 bits could be deduced from that, rather than expecting a particular timing.
I don't really see the point?, if it's SIRC the timing is already known (bearing in mind the width of the pulses isn't accurately transferred by the IR link), and if it's not SIRC then it's completely different anyway.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 30th January 2008, 02:19 PM   (permalink)
Default

There probably is'nt, and I have moved on since I developed that code.

This is only for development, so the only "point" would be for me to enhance my knowledge, and lets face it - the journey involved in enhancing my knowledge is bound to lead me up some blind alleys. but that will have taught me something as well!

All the very best!
UTMonkey is offline  
Old 1st February 2008, 05:10 AM   (permalink)
Default Learning MPLAB, Sirc Rx and Junebug

Hi guys,

My forehead hurts as I have been reading reading and more reading.

I have managed to learn how to create a project in MPLAB 8.0, select
the 18f1320, select the programmer Pickit 2 for the Junebug, etc

I added the lkr file 18f1320.lkr

Added the sirc rx.asm file to the project, saved the project and selected build all.

Here is the output:

Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\my projects\sony ir.o".
Clean Warning: File "C:\my projects\C:\Documents and Settings\Darren\Desktop\sony ir.lst" doesn't exist.
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p18F1320 "C:\Documents and Settings\Darren\Desktop\sony ir.asm" /l"sony ir.lst" /e"sony ir.err" /o"sony ir.o"
Warning[205] C:\DOCUMENTS AND SETTINGS\DARREN\DESKTOP\SONY IR.ASM 1 : Found directive in column 1. (list)
Executing: "C:\Program Files\Microchip\MPASM Suite\MPLink.exe" "C:\Program Files\Microchip\MPASM Suite\LKR\18f1320.lkr" "C:\my projects\sony ir.o" /o"18f1320proj.cof" /M"18f1320proj.map" /W
MPLINK 4.14, Linker
Copyright (c) 2007 Microchip Technology Inc.
Error - section '.org_1' can not fit the absolute section. Section '.org_1' start=0x00000008, length=0x0000011e
Errors : 1

Link step failed.
BUILD FAILED: Thu Jan 31 22:56:49 2008

Probably something simple as my forehead pain is hurting too much.

Any suggestions, kicks.....................

Thanks
ex-navy is offline  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Junebug PORTB header mod futz Micro Controllers 91 15th April 2008 07:06 PM
My JuneBug is Here! UTMonkey Micro Controllers 15 7th December 2007 07:25 PM
Junebug (Pickit 2) autorun? Pommie Micro Controllers 4 1st December 2007 03:36 AM
Junebug kit ready, (PICkit2 & tutor) blueroomelectronics Micro Controllers 37 28th October 2007 03:47 AM
Complete NOOB - needs startup advice SystemFX General Electronics Chat 7 17th December 2006 08:24 AM



All times are GMT. The time now is 01:58 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker