<root>
<document schema='101' license='1,Jan Lichtenbelt,9HXPGG' title='' description='' guid='' target='PIC.16F.16F1939' >
	<config data='!0,fe2!10,1633%0,2%3,0%5,1%6,1%7,1%8,1%9,3%b,1%c,0%d,0%10,3%14,3%18,0%19,1%1a,1%1c,1%1d,0' clkspd='19660800' simspd='1' usewdt='0' constif='0' />
	<plugins >
		<dll_dbg_icd enabled='0' >
			<data name='debug' value='1' />
			<data name='userpins' value='0' />
			<data name='speed' value='1000' />
			<data name='firmware' value='0000000000000000' />
			<data name='breaks' value='8' />
			<data name='stacks' value='8' />
			<data name='clkport' value='1' />
			<data name='clkbit' value='6' />
			<data name='dataport' value='1' />
			<data name='databit' value='7' />
			<data name='monitor' value='1' />
			<data name='ictloop' value='1' />
			<data name='ictrate' value='1000' />
			<data name='ictmask' value='-1' />
		</dll_dbg_icd>
		<dll_dbg_ict enabled='0' >
			<data name='speed' value='1000' />
			<data name='firmware' value='0000000000000000' />
			<data name='breaks' value='8' />
			<data name='stacks' value='8' />
			<data name='clkport' value='1' />
			<data name='clkbit' value='6' />
			<data name='dataport' value='1' />
			<data name='databit' value='7' />
			<data name='userpins' value='0' />
		</dll_dbg_ict>
		<dll_models enabled='1' />
		<dll_upgrader enabled='1' />
		<dll_webhelp enabled='1' />
	</plugins>
	<supplement use='0' head='' body='' />
	<debug />
	<components >
		<settings autoimg='1' center='1' unitscale='0' fixedscale='0' fixedx='1' fixedy='1' fixedz='1' headcode='0' />
		<definition guid='' vmin='0' vmaj='1' visiblename='' description='' category='' author='' sysinfo='0' keywords='' dynamic='0' iconpath='' />
		<component class_type='root' codename='panel' x='0' y='0' z='0' xsz='1' ysz='1' zsz='1' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' interactive='1' solid='1' layer='0' >
			<resources />
			<properties />
			<values />
			<events />
			<apis />
			<variables >
				<variable public='0' >
					<def class_type='variable' name='Counter' type='u16' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='Address' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='PeriodTime' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='RC5In' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='false' type='b1' description='' isconst='1' isinit='1' usrinit='0' setinit='0' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='Command' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='Toggle' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='Temp' type='u8' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='ReadIn' type='u16' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='InterruptFlag' type='b1' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='TestBit' type='b1' description='' isconst='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='true' type='b1' description='' isconst='1' isinit='1' usrinit='1' setinit='1' />
				</variable>
			</variables>
			<macros >
				<macro >
					<flowline name='Init' description='' >
						<command class_type='call' title='start LCD' component='v5__lcddisplay0' macro='Start' />
						<command class_type='call' title='Show Text' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot;Simple RC5 Rx&quot;' />
						</command>
						<command class_type='call' title='2nd line' component='v5__lcddisplay0' macro='Cursor' >
							<argument exp='0' />
							<argument exp='1' />
						</command>
						<command class_type='call' title='Show Text' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot;Version 2, JHL&quot;' />
						</command>
					</flowline>
				</macro>
				<macro >
					<flowline name='IOC' description='' >
						<command class_type='calculation' title='Set flag' >
							<exp exp='InterruptFlag = true' />
							<exp exp='' />
						</command>
					</flowline>
				</macro>
				<macro >
					<flowline name='Show_Data' description='' >
						<command class_type='call' title='Clear LCD' component='v5__lcddisplay0' macro='Clear' />
						<command class_type='call' title='Show Address' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot;A: &quot;' />
						</command>
						<command class_type='call' title='Show Address' component='v5__lcddisplay0' macro='PrintNumber' >
							<argument exp='Address' />
						</command>
						<command class_type='call' title='Show Command' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot; C: &quot;' />
						</command>
						<command class_type='call' title='Show Command' component='v5__lcddisplay0' macro='PrintNumber' >
							<argument exp='Command' />
						</command>
						<command class_type='call' title='2nd line' component='v5__lcddisplay0' macro='Cursor' >
							<argument exp='0' />
							<argument exp='1' />
						</command>
						<command class_type='call' title='Show Toggle' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot; T: &quot;' />
						</command>
						<command class_type='call' title='Show Toggle' component='v5__lcddisplay0' macro='PrintNumber' >
							<argument exp='Toggle' />
						</command>
						<command class_type='call' title='Show Period time' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot; P: &quot;' />
						</command>
						<command class_type='call' title='Show Toggle' component='v5__lcddisplay0' macro='PrintNumber' >
							<argument exp='ReadIn' />
						</command>
						<command class_type='delay' title='Delay' exp='1' type='2' />
						<command class_type='call' title='Component Macro Aanroepen' component='v5__lcddisplay0' macro='Clear' />
						<command class_type='call' title='Component Macro Aanroepen' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot;Waiting for&quot;' />
						</command>
						<command class_type='call' title='2nd line' component='v5__lcddisplay0' macro='Cursor' >
							<argument exp='0' />
							<argument exp='1' />
						</command>
						<command class_type='call' title='Component Macro Aanroepen' component='v5__lcddisplay0' macro='PrintString' >
							<argument exp='&quot;new data&quot;' />
						</command>
					</flowline>
				</macro>
				<macro >
					<flowline name='Main' description='' >
						<command class_type='comment' title='' comment='Alternative RC5 receiver on one of the PortB selected.

Principal:
1) Detect the period time for 1 bit  (about 1900 usec)
2) Synchronise on the level change half way each bit
3) Read input status directly after this half-way pulse
4) Wait 3/4 of a period to start at 2)
5) Repeat for 12 time with:
   Toggle (1 bit), Address (5 bits) and Command (6 bits)

Interrupts used:
1) Timer2 (76800 Hz / 13 usec), 
   but any other timer fits which measure the 900 usec accurate enough
2) Interrupt On Change (IOC) on portB on any change (positive and negative)
   In this version port B0, but all other ports can be used
   (change the IOC interrupt and inputs of port B0)

Remark: There are no requirements for the chip oscillator frequency

Version V2
November 2013
Jan Lichtenbelt, NL
' textarea='12,0,12,0' />
						<command class_type='call' title='Init' macro='Init' />
						<command class_type='loop' title='endless loop' exp='1' type='0' start='1' count='1' breakend='0' >
							<flowline >
								<command class_type='calculation' title='Calculation' >
									<exp exp='Toggle = 0' />
									<exp exp='Address = 0' />
									<exp exp='Command = 0' />
									<exp exp='ReadIn = 0 // combination of toggle, address and Ccommand' />
								</command>
								<command class_type='interrupt' title='76800 Hz or 13 usec' enable='1' name='TMR2' call='TimerInterrupt' >
									<settings timer='1' prescale='1' postscale='1' rollover='256' bitcount='-1' trigger='3' maskhi='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' masklo='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' >
										<option value='0' />
										<option value='0' />
										<option value='256' />
										<option value='-1' />
									</settings>
								</command>
								<command class_type='native' title='Increase the timer2 frequency with a factor 4' language='C' />
								<command class_type='interrupt' title='Interrupt on  change (IOC) of port B0' enable='1' name='IOC0' call='IOC' >
									<settings timer='0' prescale='-1' postscale='-1' rollover='-1' bitcount='-1' trigger='3' maskhi='-wEAAAEAAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' masklo='-wEAAAEAAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' >
										<option value='-1' />
										<option value='-1' />
										<option value='-1' />
										<option value='-1' />
									</settings>
								</command>
								<command class_type='comment' title='' comment='1. Check no pulse detected for 2 msec' textarea='12,0,12,0' />
								<command class_type='loop' title='Loop for 2 msec with no pulses found' exp='NOT InterruptFlag' type='1' start='0' count='1' breakend='0' >
									<flowline >
										<command class_type='calculation' title='reset interrupt flag and counter' >
											<exp exp='InterruptFlag = false' />
											<exp exp='counter = 0' />
											<exp exp='' />
										</command>
										<command class_type='loop' title='Loop for 2 msec' exp='(counter &gt;= 154)' type='1' start='0' count='1' breakend='0' >
											<flowline />
										</command>
									</flowline>
								</command>
								<command class_type='comment' title='' comment='2. Get first pulse (half way first start bit)' textarea='12,0,12,0' />
								<command class_type='calculation' title='Reset interrupt flag' >
									<exp exp='InterruptFlag = false' />
								</command>
								<command class_type='loop' title='Get first start pulse' exp='InterruptFlag' type='1' start='0' count='1' breakend='0' >
									<flowline />
								</command>
								<command class_type='comment' title='' comment='3. Get second pulse (end first start bit)' textarea='12,0,12,0' />
								<command class_type='calculation' title='Reset timer counter and interrupt flag' >
									<exp exp='counter = 0' />
									<exp exp='InterruptFlag = false' />
								</command>
								<command class_type='loop' title='Get second start pulse ' exp='InterruptFlag' type='1' start='0' count='1' breakend='0' >
									<flowline />
								</command>
								<command class_type='comment' title='' comment='4. Period time defined as the time for one bit
' textarea='12,0,12,0' />
								<command class_type='calculation' title='Calculate 1/2 and  3/4 period time' >
									<exp exp='Periodtime = counter' />
									<exp exp='Temp = PeriodTime &gt;&gt; 1' />
									<exp exp='periodtime = periodtime &lt;&lt; 1' />
									<exp exp='Periodtime = Periodtime - Temp // 3/4 period time' />
									<exp exp='Temp = Temp &lt;&lt; 1 // 1/2 period time' />
								</command>
								<command class_type='comment' title='' comment='5. Wait 1/2 period time up to half way 2nd start bit
' textarea='12,0,12,0' />
								<command class_type='calculation' title='Reset counter' >
									<exp exp='counter = 0' />
								</command>
								<command class_type='loop' title='Wait 1/2 period times' exp='counter &gt;= Temp' type='1' start='1' count='1' breakend='0' >
									<flowline />
								</command>
								<command class_type='comment' title='' comment='6. Get all data (12 bits)' textarea='12,0,12,0' />
								<command class_type='loop' title='Get toggle (1 bit) address (5 bits) and command (6 bits)' exp='12' type='2' start='1' count='12' breakend='0' >
									<flowline >
										<command class_type='comment' title='' comment='7. Go to 1/4 of a bit' textarea='12,0,12,0' />
										<command class_type='calculation' title='Reset counter' >
											<exp exp='counter = 0' />
										</command>
										<command class_type='loop' title='Wait 3/4 periodtime' exp='counter &gt;= periodtime' type='1' start='0' count='1' breakend='0' >
											<flowline />
										</command>
										<command class_type='comment' title='' comment='8. Synchonise to half way a bit (always a change of the input)' textarea='12,0,12,0' />
										<command class_type='calculation' title='Calculation' >
											<exp exp='InterruptFlag = false' />
										</command>
										<command class_type='loop' title='Wait until pulse half way bit' exp='InterruptFlag' type='1' start='0' count='1' breakend='0' >
											<flowline />
										</command>
										<command class_type='comment' title='' comment='8. Read directly after ahlf way a pulse the input status' textarea='12,0,12,0' />
										<command class_type='input' title='Get status' mode='1' port='1048577' mask='1' exp='Temp' />
										<command class_type='comment' title='' comment='9. And save the input status
(The bit = Not input_status)' textarea='12,0,12,0' />
										<command class_type='calculation' title='Save bit' >
											<exp exp='Readin = Readin &lt;&lt; 1' />
											<exp exp='ReadIn = ReadIn + 1 - Temp' />
											<exp exp='' />
										</command>
									</flowline>
								</command>
								<command class_type='interrupt' title='Disable Timer intterupt' enable='0' name='TMR2' >
									<settings timer='1' prescale='1' postscale='1' rollover='0' bitcount='-1' trigger='3' maskhi='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' masklo='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' >
										<option value='0' />
										<option value='0' />
										<option value='0' />
										<option value='0' />
									</settings>
								</command>
								<command class_type='interrupt' title='Disable Port B intterupt' enable='0' name='IOC0' >
									<settings timer='0' prescale='-1' postscale='-1' rollover='-1' bitcount='-1' trigger='3' maskhi='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' masklo='-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAD-AAAA-wAAAP8AAAA' >
										<option value='0' />
										<option value='0' />
										<option value='0' />
										<option value='0' />
									</settings>
								</command>
								<command class_type='comment' title='' comment='10. The data read in ReadIn are:
toggle = bit 11
address= bit 6-10
command= bit 0-5' textarea='12,0,12,0' />
								<command class_type='calculation' title='Calculation' >
									<exp exp='command = readin &amp; 0x003F' />
									<exp exp='readin = readin &gt;&gt; 6' />
									<exp exp='address = readin &amp; 0x001F' />
									<exp exp='readin = readin &gt;&gt; 5' />
									<exp exp='toggle = readin &amp; 0x0001' />
								</command>
								<command class_type='comment' title='' comment='11. Calculate the period time in usec' textarea='12,0,12,0' />
								<command class_type='calculation' title='Calculation' >
									<exp exp='ReadIn = PeriodTime' />
									<exp exp='ReadIn = ReadIn * 52 // timer2 52 usec roll over 256' />
									<exp exp='ReadIn = ReadIn * 4' />
									<exp exp='ReadIn = ReadIn / 3' />
								</command>
								<command class_type='native' title='Timer2 Rollover correction (pr2 register)' language='C' />
								<command class_type='comment' title='' comment='12. Show the data (A)ddress, (C)ommand, (T)oggle and (P)eriod time in usec' textarea='12,0,12,0' />
								<command class_type='call' title='Show Data on LCD' macro='Show_Data' />
							</flowline>
						</command>
					</flowline>
				</macro>
				<macro >
					<flowline name='TimerInterrupt' description='' >
						<command class_type='calculation' title='Increase counter' >
							<exp exp='counter = counter + 1' />
						</command>
					</flowline>
				</macro>
			</macros>
			<component class_type='ref' guid='0953aac0-9c23-4c2f-9843-88954a4191ea' vmin='7' vmaj='1' codename='v5__lcddisplay0' x='0' y='0' z='0' xsz='78.2857' ysz='13.1429' zsz='1.14286' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' interactive='1' solid='1' layer='0' >
				<resources />
				<properties />
				<values >
					<value target='Pin_Data_0' data='$PORTD.0' />
					<value target='Pin_Data_1' data='$PORTD.1' />
					<value target='Pin_Data_2' data='$PORTD.2' />
					<value target='Pin_Data_3' data='$PORTD.3' />
					<value target='Pin_RS' data='$PORTD.4' />
					<value target='Pin_E' data='$PORTD.5' />
					<value target='Rows' data='2' />
					<value target='Columns' data='16' />
					<value target='BackgroundColor' data='0x808080' />
					<value target='LineColor' data='0' />
					<value target='TextColor' data='0x000000' />
					<value target='Font' data='Arial' />
					<value target='CursorPosX' data='11' />
					<value target='CursorPosY' data='0' />
				</values>
				<events />
				<apis />
				<variables />
				<macros />
			</component>
		</component>
	</components>
	<keymap />
	<panel2d shadows='0' lighting='2' brightness='0' perspective='0' >
		<background rgb='9737364' img='' style='0' />
		<camera xe='0' ye='0' ze='300' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
	</panel2d>
	<panel3d shadows='0' lighting='2' brightness='0' perspective='0' >
		<background rgb='8409120' img='' style='0' />
		<table rgb='7360576' img='' style='0' size='25' />
		<camera xe='0' ye='0' ze='300' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
	</panel3d>
	<layout >
		<view type='0' name='Main' mode='0' placement='LAAAAAAAAAABAAAA---------------------wAAAAAUAAAAcwQAAL8CAAA' zoom='75' scrollx='0' scrolly='2969' flags='0' />
		<view type='0' name='TimerInterrupt' mode='0' placement='LAAAAAAAAAABAAAA---------------------wAAAAAUAAAAcwQAAL8CAAA' zoom='75' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='Show_Data' mode='0' placement='LAAAAAAAAAABAAAA---------------------wAAAAAUAAAAcwQAAL8CAAA' zoom='75' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='IOC' mode='0' placement='LAAAAAAAAAABAAAA---------------------wAAAAAUAAAAcwQAAL8CAAA' zoom='75' scrollx='0' scrolly='0' flags='0' />
		<view type='0' name='Init' mode='0' placement='LAAAAAAAAAABAAAA---------------------wAAAAAUAAAAcwQAAL8CAAA' zoom='75' scrollx='0' scrolly='0' flags='0' />
	</layout>
</document>
</root>
