DEVICE SX28, OSCHS3, TURBO, STACKX, OPTIONX IRC_CAL IRC_SLOW FREQ 50000000 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- inputpin var RB.0 'data from drive outputbyte var RC 'data to pc byteready var RB.2 'notify pc byte is ready pcack var RB.3 'pc saying it got the byte samplepin var bit 'actual value of pin at interrupt numstoredbits var byte '# of stored bits in shift reg storedata var byte 'actual stored data seenedge var bit 'seen an edge or are we idly? pending var byte 'swapped with wkpnd_b, RTCC or edge? validhigh var byte 'how many high bits have we seen in a row ' ------------------------------------------------------------------------- INTERRUPT ' ------------------------------------------------------------------------- ISR_Start: RTCC = 0 RB.1 = 1 'for debugging samplepin = inputpin 'grab the pin pending = 0 wkpnd_b = pending 'is this an edge or a rollover if pending.0 = 1 then processedge if samplepin = 0 then processedge 'we shouldn't be at an edge here but let's process right anyways 'this might cause us to lose one valid high where we sampled too late 'but at least we dont miss the next edge event which would cause 'us to lose something like 2 bits + if seenedge = 0 then goback 'are we idle? inc validhigh if validhigh > 3 then goidle 'more than 3 highs? go idle ' process a high, 5v, non-edge non-idle signal ' shift left, store a 0, and increase storedbits storedata = storedata << 1 storedata = storedata | 0 inc numstoredbits goto goback goidle: seenedge = 0 'erase the edge we saw before validhigh = 0 'reset and start over goto goback processedge: seenedge = 1 'we've now seen an edge! validhigh = 0 'this should already be zero ' shift left, store a 1, and increase storedbits storedata = storedata << 1 storedata = storedata | 1 inc numstoredbits goback: wkpnd_b = 0 RB.1 = 0 'for debugging returnint 75 PROGRAM start_point ' Program Code ' ------------------------------------------------------------------------- start_point: 'setup port b TRIS_B=%11111001 'direction bits rb.0 input, rb.1 & rb.2 output ST_B = %11111110 'schmitt trigger for our drive input WKPND_B = 0 'clear pending interrupts WKED_B = %11111111 'set falling edge trigger WKEN_B = %11111110 'enable drive input interrupts PLP_B = %11111110 'enable pullups 'setup port c TRIS_C=0 'enable port c for output PLP_C = %00000000 numstoredbits = 0 byteready = 0 seenedge = 0 pending = 0 validhigh = 0 storedata = 0 ' turn on interrupts OPTION = $88 repeat: if byteready = 0 then skipcheck 'don't check to see if pc ack'd if we don't have a byte on the port if pcack = 0 then skipcheck byteready = 0 skipcheck: if numstoredbits < 8 then repeat 'else output data to pc outputbyte = storedata byteready = 1 numstoredbits = 0 storedata = 0 goto repeat