Okay, with PIC16F690 with int osc at 8MHz, with PULSIN I have a very long code execution time (about 1S) while it is vital that I have a code execution time of less than 100ms as it is supposed to read IR signals incoming every 100ms.
andCode:IF (a.0(0) == 0) THEN FOR i = 0 to 15 'get a sequence of incoming 16 bits (WITHOUT their prefixes which are 600us pause) PULSIN PORTA.0,0,ir_pulse[i] ' Read 16 low-going pulses on RA.0, see pbp manual NEXT i pin = 0 ' Loop 16 times, at 8MHz record in 5us resolution pulse duration in the word variables array ENDIF
I seem to be unable to use PULSIN the right way.Code:databyte = 0 'reset low byte and high byte of the word variable to %0000000000000000 FOR i = 0 TO 15 ' process all 16 "data" bits out of 16 bits collected ' LCDOUT $fe,1,#i,":",#ir_pulse[i] ' PAUSE 2000 IF ((ir_pulse[i] > 210) && (ir_pulse[i] < 275)) THEN '240*5us resolution at 8MHz = 1200 us (usual length of a zero is about 127 - 145 and 242 to 266 for a one) databyte.0(15-i) = 1 'clear the bit goto slip 'bypass ENDIF IF ((ir_pulse[i] > 105) && (ir_pulse[i] < 160)) THEN databyte.0(15-i) = 0 'clear the bit GOTO slip 'bypass ENDIF 'ELSE: invalid data 'LCDOUT $fe,1,#i," != ",#ir_pulse[i] 'LCDOUT $fe,$c0,"Invalid" 'PAUSE 500 valid = 0 'PAUSE 3000 'GOTO hop_here 'bypass and abort if one of the 14 bits is off limits slip: NEXT i
I've decided not to use PULSIN but use TIMER 1 to measure the pulse length.
I was expecting either 600 or 1200 us pulses but get pretty different values and I'm wondering why...
Code:'the 2400US header that triggered the int when porta.0 went HIGH->LOW is in progress (LOW going pulse because TSOP4840 pulls the pin low when getting 40KHz mod pulse) while (porta.0 == 0) : WEND 'while pin is low because of the incoming header LOW pulse '0->1 (2400US header pulse ended) 'here the signal goes high and will stay high for 600us (spacer between header and first bit) while (porta.0 == 1) : WEND '1->0 (600US spacer between header and first bit ended because first bit is starting) 'timer1: max 65536 increments of minimum 0.5US at prescale of 1:1 T1CON = %00011100 '1:2 prescale for 1US increments, timer 1 is stopped TMR1H = 0 'reset timer high byte TMR1L = 0 'reset timer low byte databyte = 0 'word var 'memo: 'recorded ranges for a 600 us pulse: 608-741 'recorded ranges for a 1200 us pulse: 1217-1346 'Get length for 16 bits (1200 or 600 us followed by 600 us spacer) FOR i = 0 to 15 'Bit has started its LOW going pulse, either 1200 or 600 us long T1CON.0 = 1 'start TMR1 with prescaler = 1:2, we will have 1US increments WHILE (porta.0 == 0): WEND 'wait for bit pulse to end (while TMR2 is incremented every us) '0->1 (spacer) T1CON.0 = 0 'stop TMR2 pulse_length.highbyte = TMR1H pulse_length.lowbyte = TMR1L bit_pulse[i] = pulse_length IF ((pulse_length > 600) && (pulse_length < 750)) THEN 'about 600 us, it is a '0' databyte.0(15-i) = 0 GOTO next_bit ENDIF IF ((pulse_length > 1200) && (pulse_length < 1350)) THEN 'about 1200 us, it is a '1' databyte.0(15-i) = 1 GOTO next_bit ENDIF 'GOTO bad_bit next_bit: TMR1H = 0 TMR1L = 0 WHILE (porta.0 = 1) : WEND 'we don't measure length of the spacer '1->0 the falling edge for the next byte NEXT i
Where am I loosing all these precious clock cycles???




Bookmarks