We wrote some pretty ugly code to do all this which worked reasonably well. We also saw an application note from Microchip which was in fact, more complex and horrendous than our own code – har! State machines are awesome, but in teensy micro source code? har!
Long story short we also happened upon some awesome PicBasic source by Dave Houston. he very cleverly only measured the space pulses which cut down on the number of conditionals in the source code by about a factor of 2. We dusted off our copy of PicBasic Pro and fired away – success! It worked very well and was not susceptible to a couple of bugs that our original code had – basically losing sync and not doing what it was supposed to. Thanks, Dave!
'**************************************************************** '* Name : OSTV_PBP1BAS * '* Author : Openschemes, using stack from davehouston.org * '* Notice : Copyright (c) 2010 Openschemes.com * '* : All Rights Reserved * '* Date : 8/30/2010 * '* Version : 1.0 * '* Notes : * '**************************************************************** ' PINOUT ' GPIO,0 <= IR_DATA ' GPIO,1 => DEBUG FLAG ' GPIO,2 => ON ' GPIO,3 <= UNUSED ' GPIO,4 => TX ' GPIO,5 <= RX '12F629 ;@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF DEFINE PULSIN_MAX 1140 '>1140 RETURNS 0 DEFINE DEBUG_REG GPIO DEFINE DEBUG_BIT 5 DEFINE DEBUG_MODE 0 'Normal, for pickup with modified USB-Serial DEFINE DEBUG_BAUD 19200 DEFINE OSCCAL_1K 1 IR VAR byte[4] pulse VAR byte i VAR byte stx VAR word 'start of transmission ISON VAR byte INPWR VAR BYTE ' Marks that we're currently in a power command repeat OFFCTR VAR BYTE ' A sentinel for turning off with repeat byte CMCON = 7 'comparators off 'Debug "IR NEC PROTOCOL" low GPIO.2 ISON=0 irloop: IR[0]=0:IR[1]=0:IR[2]=0:IR[3]=0:i=0 PulsIn GPIO.0, 0, stx If (stx<760) Then irloop 'debug #stx While GPIO.0=1:Wend 'wait space Repeat PulsIn GPIO.0, 1, pulse If (pulse>100) Then IR.0(i)=1 'set bit EndIf i=i+1 Until (i>31) '$84$79$12$ED$00$01$02$04$00$01$02$04 if ison==0 then ; If we're off and found a power command, turn on if (ir[1]==$79 and ir[3]==$ED) then HIGH GPIO.2 ison=1 INPWR=0 OFFCTR=10 endif else ; If we're on and found a power command, mark repeat if (ir[1]==$79 and ir[3]==$ED) then INPWR=1 OFFCTR=10 ELSE ;If we're on and already in a power on repeat mode, decfsz IF (INPWR==1 and IR[0]==$00 and IR[1]==$01 and IR[2]==$02 and IR[3]==$04) then OFFCTR=offctr - 1 else ;But if we're in power cmd repeat and get something new, quit inpwr=0 offctr=10 endif endif endif ;If the repeat command repeated 10x, shut down WDTV. IF OFFCTR <= 0 then LOW GPIO.2 ISON=0 OFFCTR=10 endif ; Bit bang the received data to GPIO.5 For i = 0 To 3 Debug IHEX2(IR[i]) Next ;Loop GoTo IRLOOP
The coolest thing about this code is that it’s got bit-banged RS232 output on GPIO 5 so you can actually use it to read in IR codes from all your remotes if you’d like to adapt this project for your own uses. Delicious! We’ve inverted the polarity of the RS232 debug so we could simply plug in a USB->Serial converter. If you’re using a MAX232 then perhaps you’ll want it back to inverse mode – keep that in mind if you plug it into your PC and it spews garbage instead of nice strings like $84$79$12$ED.
And that’s pretty much it. Compile, flash with internal RC, no external clock, and no WDT. I use the power on reset and brownout with BG=11 just to ensure the voltage is stable by the time we start crankin, but this entire process is so ungodly slow that it probably doesn’t matter.
Attached is the source code in both Picbasic and asm format (for those of you without PBP), as well as a precompiled hex file for PIC12F629. Enjoy, and let us know if this project worked for you!
Openschemes OSTV Source & Hex
Cheers,
-Openschemes






[...] With all the technology around us there will often come a time when you wish it would work in a slightly different way. Well unless you work as one of the design engineers in the company that made the product chances are you will just have to suck it up and live with what was produced. Well when Matt from Openschemes was in this situation with his Western Digital WDTV he decided to whip up a small circuit to make it work as he wanted. Turns out that the WDTV doesn’t really turn off, it just goes into a mode that looks like it is sleeping. Matt made the WDTV Remote Control External Power Circuit shown above to allow him to remotely turn the darn thing off for real. The board design and construction is well documented, a photo etch method is used etch a copper clad board and the coding of the PIC chip microcontroller can be seen here in this follow-up article. [...]