#include #include #include interrupt void isr(void); unsigned char i, j, k, m, n, dat1, dat2, dat3, dat4; interrupt void isr(void) { if(k==255) //counters for timer overflow { if(m==255) n=n+1; m=m+1; } k=k+1; INTCON=0b10100000; // resets the TMR0 interupt flag } void main(void) { RCSTA=0b10010000; // enable serial port, enable continuous receive TXSTA=0b00100100; // transmit enabled, asynchronous, mode high speed SPBRG=20; // Set Baud Rate to 56K OPTION=0b00000000; // Fastest timer rate INTCON=0b10100000; // enables all interupts, enables the TMR0 interupt TRISB=0b00110000; // port B bits 4 and 5 as input TRISD=0b00000000; // all port D bits as output i=0; k=0; m=0; n=0; while(1) { PORTD = 0b11110000; // turn LED on while ((PORTB & 0b00010000)==16) // check if detector reads light {} // wait till light is off dat1=TMR0; // get the time value dat2=k; dat3=m; dat4=n; PORTD = 0b11010000; // turn LED off while((PIR1 & 0b00010000)==0){}; // Do the serial transfer of ON time TXREG=dat4; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat3; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat2; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat1; while ((PORTB & 0b00010000) == 0) // check if detector reads light {} // wait till light is on dat1=TMR0; // get the time value dat2=k; dat3=m; dat4=n; while((PIR1 & 0b00010000)==0){}; // Do the serial transfer of OFF time TXREG=dat4; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat3; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat2; for(i=1;i<3;i++) {} while((PIR1 & 0b00010000)==0){}; TXREG=dat1; } }