LIST P=PIC16F876A #include ; processor specific definitions __CONFIG _CP_OFF & _DEBUG_OFF & _LVP_OFF & _PWRTE_ON & _WDT_OFF & _BODEN_OFF & _HS_OSC errorlevel -302 ;suppress "not in bank 0" message CBLOCK 0x20 TIMER1 TIMER2 HOOG METINGEN SHARP TEMP LAAG BOVENGRENS ONDERGRENS RIJ ENDC CBLOCK 0x30 DATAIN1 DATAIN2 DATAIN3 RESULTAAT SNELHEID VAR TEMP2 DRAAI LAMP ENDC ;---------------------------------------------------------------------------- ;RESET Interrupt Vector ORG 0x0000 ;reset vector GOTO RESET nop nop ;---------------------------------------------------------------------------- ;Other interrupt vector ORG 0x0004 ;interrupt vector GOTO Int ;****************************************************************************** ;****************************************************************************** RESET BSF STATUS,RP0 ; Bank 1 CLRF TRISC ; C poort uitgang MOVLW b'00001111' ; B poort MOVWF TRISB MOVLW b'001011' ; A poort ingang MOVWF TRISA BCF STATUS,RP0 ; Bank 0 MOVLW b'00010111' MOVWF PORTC BSF PORTA, 5 ; ledjes aan BSF PORTA, 2 CALL SetupSerial CALL SetupPWM CALL SetupAnalog CALL SETUPLICHT MOVLW D'255' MOVWF CCPR1L MOVWF CCPR2L MOVLW d'3' MOVWF METINGEN MOVLW d'220' MOVWF VAR BEGIN BCF PORTB,4 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSC RIJ,0 GOTO RIJRECHTS MOVLW B'01001001 MOVWF ADCON0 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSC RIJ,0 GOTO RIJLINKS GOTO GALINKSER GALINKSER BCF STATUS,RP0 BCF PORTC, 4 ; linkerwiel uit CALL DELAYB CALL DELAYC BSF PORTC, 4 ; rij rechtdoor GALINKSER2 MOVLW d'90' MOVWF BOVENGRENS ;3 x 34 of 50 cm MOVLW b'11111111' MOVWF HOOG CALL HAALDATA CALL VERGELIJK BTFSS RIJ,0 GOTO GALINKSER2 BCF PORTC, 0 CALL DELAYB CALL DELAYC BSF PORTC,0 GOTO RIJLINKS HAALDATA BSF ADCON0,GO ; Start AD conv HAALDATA2 BTFSC ADCON0,GO ; Wait until AD conv is done GOTO HAALDATA2 CLRWDT MOVF ADRESH,W ; Read Low Byte : Bank 1 ADDWF TEMP,1 DECFSZ METINGEN GOTO HAALDATA MOVFW TEMP MOVWF SHARP CLRF TEMP MOVLW d'3' MOVWF METINGEN RETURN VERGELIJK CLRWDT BSF RIJ,0 DECF HOOG,1 MOVFW HOOG SUBWF BOVENGRENS,0 BTFSC STATUS,Z BCF RIJ,0 BTFSS RIJ,0 RETURN MOVFW SHARP SUBWF HOOG, 0 BTFSC STATUS,Z RETURN GOTO VERGELIJK RETURN RIJRECHTS BCF PORTC, 0 CALL DELAY BSF PORTC,0 RIJRECHTS2 MOVLW B'01000001 MOVWF ADCON0 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW b'00000000' ; MOVWF LAAG MOVLW d'165' ; MOVWF ONDERGRENS ; MOVLW d'195' MOVWF BOVENGRENS CALL VERGELIJKA BTFSC DRAAI,0 CALL GALINKSR BTFSC DRAAI,1 CALL GARECHTSR MOVLW B'01001001 MOVWF ADCON0 CALL HAALDATA MOVLW b'11111111' ; waarde te bepalen MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSC RIJ,0 GOTO RECHTSVANMUUR GOTO RIJRECHTS2 VERGELIJKA BCF DRAAI,0 BCF DRAAI,1 CLRWDT DECF HOOG,1 ;resultaat in HOOG MOVFW HOOG SUBWF BOVENGRENS,0 BTFSC STATUS,Z GOTO VERGELIJKB ; MOVFW SHARP SUBWF HOOG, 0 BTFSS STATUS,Z ; test if last data was 0 GOTO VERGELIJKA BSF DRAAI,0 ; RETURN VERGELIJKB CLRWDT INCF LAAG,1 ;resultaat in HOOG MOVFW LAAG SUBWF ONDERGRENS,0 BTFSC STATUS,Z RETURN ; MOVFW SHARP SUBWF LAAG, 0 BTFSS STATUS,Z ; test if last data was 0 GOTO VERGELIJKB BSF DRAAI,1 ; RETURN RIJLINKS BCF PORTC, 4 CALL DELAY BSF PORTC,4 RIJLINKS2 MOVLW B'01001001 MOVWF ADCON0 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW b'00000000' ; MOVWF LAAG MOVLW d'165' ; MOVWF ONDERGRENS ; MOVLW d'195' MOVWF BOVENGRENS CALL VERGELIJKA BTFSC DRAAI,0 CALL GARECHTSL BTFSC DRAAI,1 CALL GALINKSL MOVLW B'01000001 MOVWF ADCON0 CALL HAALDATA MOVLW b'11111111' MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSC RIJ,0 GOTO LINKSVANMUUR MOVLW B'01000001 MOVWF ADCON0 GOTO RIJLINKS2 GARECHTSR BCF STATUS,RP0 BCF PORTC, 0 ; rechterwiel uit CALL DELAYC CALL DELAYE ; BSF PORTC, 0 ; rij rechtdoor GARECHTSR2 MOVLW d'171' MOVWF BOVENGRENS MOVLW b'11111111' MOVWF HOOG CALL HAALDATA CALL VERGELIJK BTFSS RIJ,0 GOTO GARECHTSR2 BCF PORTC, 4 CALL DELAYC CALL DELAYE BSF PORTC,4 RETURN GALINKSR BCF STATUS,RP0 BCF PORTC, 4 ; linkerwiel uit CALL DELAYC CALL DELAYE BSF PORTC, 4 ; rij rechtdoor GALINKSR2 MOVLW d'171' MOVWF BOVENGRENS ;3 x 50 of 30 cm MOVLW b'11111111' MOVWF HOOG CALL HAALDATA CALL VERGELIJK BTFSC RIJ,0 GOTO GALINKSR2 BCF PORTC, 0 CALL DELAYC CALL DELAYE BSF PORTC,0 RETURN GARECHTSL BCF STATUS,RP0 BCF PORTC, 0 ; rechterwiel uit CALL DELAYC CALL DELAYE BSF PORTC, 0 ; rij rechtdoor GARECHTSL2 MOVLW d'171' MOVWF BOVENGRENS ;3 x 50 of 30 cm MOVLW b'11111111' MOVWF HOOG CALL HAALDATA CALL VERGELIJK BTFSC RIJ,0 GOTO GARECHTSL2 BCF PORTC, 4 CALL DELAYC CALL DELAYE BSF PORTC,4 RETURN GALINKSL BCF STATUS,RP0 BCF PORTC, 4 ; linkerwiel uit CALL DELAYC CALL DELAYE BSF PORTC, 4 ; rij rechtdoor GALINKSL2 MOVLW d'171' MOVWF BOVENGRENS ;3 x 50 of 30 cm MOVLW b'11111111' MOVWF HOOG CALL HAALDATA CALL VERGELIJK BTFSS RIJ,0 GOTO GALINKSL2 BCF PORTC, 0 CALL DELAYC CALL DELAYE BSF PORTC,0 RETURN RECHTSVANMUUR CLRWDT MOVLW B'01001001 MOVWF ADCON0 CALL DELAYB RECHTSVANMUUR2 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW b'00000000' ; MOVWF LAAG MOVLW b'10010011' ; 49 x 3 of 30 cm MOVWF ONDERGRENS ; MOVLW b'11001100' ; 68 x 3 of 20 cm MOVWF BOVENGRENS CALL VERGELIJKA BTFSC DRAAI,0 CALL DRAAIRECHTS BTFSC DRAAI,1 CALL DRAAILINKS CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSS RIJ,0 CALL DRAAI90L BTFSC PORTB, 0 GOTO VOLGLIJN BTFSC PORTB, 1 GOTO VOLGLIJN BTFSC PORTB, 2 GOTO VOLGLIJN GOTO RECHTSVANMUUR2 LINKSVANMUUR CLRWDT MOVLW B'01000001 MOVWF ADCON0 CALL DELAYB LINKSVANMUUR2 CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW b'00000000' ; MOVWF LAAG MOVLW b'10010011' ; 49 x 3 of 30 cm MOVWF ONDERGRENS ; MOVLW b'11001100' ; 68 x 3 of 20 cm MOVWF BOVENGRENS CALL VERGELIJKA BTFSC DRAAI,1 CALL DRAAIRECHTS BTFSC DRAAI,0 CALL DRAAILINKS CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW d'78' ; = 4 x 26 of 65 cm MOVWF BOVENGRENS CALL VERGELIJK BTFSS RIJ,0 CALL DRAAI90R BTFSC PORTB, 0 GOTO VOLGLIJN BTFSC PORTB, 1 GOTO VOLGLIJN BTFSC PORTB, 2 GOTO VOLGLIJN GOTO LINKSVANMUUR2 DRAAILINKS CLRWDT BCF PORTC, 4 ; linkerwiel uit CALL DELAYB ; BSF PORTC, 4 ; rij rechtdoor CALL DELAYC BCF PORTC,0 CALL DELAYB BSF PORTC,0 RETURN DRAAIRECHTS CLRWDT BCF PORTC, 0 ; rechterwiel uit CALL DELAYB ; BSF PORTC, 0 ; rij rechtdoor CALL DELAYC BCF PORTC,4 CALL DELAYB BSF PORTC,4 RETURN DRAAI90L CLRWDT CALL DELAYC CALL DELAYB CALL DELAYB CALL DELAYB CALL DELAYB CALL DELAYB CALL DELAYB CALL DELAYB CALL DELAYB BCF PORTC, 4 ; linkerwiel uit CALL DELAYB CALL DELAYB BSF PORTC, 4 ; rij rechtdoor MOVLW D'220' MOVWF CCPR1L MOVWF CCPR2L CALL DELAYB CALL DELAYB GOTO VOLGLIJN DRAAI90R CLRWDT CALL DELAYC CALL DELAYB CALL DELAYB BCF PORTC, 0 ; rechterwiel uit CALL DELAYB CALL DELAYB CALL DELAYB BSF PORTC, 0 ; rij rechtdoor MOVLW D'220' MOVWF CCPR1L MOVWF CCPR2L CALL DELAYB CALL DELAYB GOTO VOLGLIJN VOLGLIJN MOVLW B'01011001 MOVWF ADCON0 CALL DELAYB VOLGLIJN2 CLRWDT MOVLW d'2' MOVWF METINGEN CALL HAALDATA MOVLW b'11111111' ; MOVWF HOOG MOVLW d'180' ; MOVWF BOVENGRENS CALL VERGELIJK BTFSC RIJ,0 CALL TRAGER BTFSC PORTB, 0 CALL GALINKS2 BTFSC PORTB, 1 CALL RECHT BTFSC PORTB, 2 CALL GARECHTS2 GOTO VOLGLIJN2 TRAGER MOVLW D'180' MOVWF VAR MOVWF CCPR1L MOVWF CCPR2L RETURN GALINKS2 CLRWDT MOVLW D'50' MOVWF CCPR1L MOVFW VAR MOVWF CCPR2L BTFSC PORTB, 1 GOTO RECHT BTFSC PORTB, 2 GOTO RECHT BTFSC PORTB, 3 GOTO KAARS GOTO GALINKS2 GARECHTS2 CLRWDT MOVLW D'50' MOVWF CCPR2L MOVFW VAR MOVWF CCPR1L BTFSC PORTB, 0 GOTO RECHT BTFSC PORTB, 1 GOTO RECHT BTFSC PORTB, 3 GOTO KAARS GOTO GARECHTS2 RECHT CLRWDT MOVFW VAR MOVWF CCPR1L MOVWF CCPR2L BTFSC PORTB, 3 GOTO KAARS RETURN KAARS CLRWDT BCF PORTC,0 BCF PORTC,4 BSF PORTB,4 CALL DELAYB BTFSC PORTB,3 GOTO KAARS BCF PORTB,4 BCF PORTA,2 BCF PORTA,5 ; **************************************************** ; * Setup Serial routine: * ; **************************************************** SetupSerial Bank1 ;select bank 1 BCF TRISC,6 ; Make RC6 - TX pin an output BSF TRISC,7 ; Make RC7 - RX pin an input BCF TXSTA,BRGH ; MOVLW D'12' MOVWF SPBRG ;Select baud rate 9600 in Low speed (bij 8 Mhz) BCF TXSTA,SYNC ;Select async mode Bank0 ;select bank0 movlw 0x90 ;enable serial port and reception 10010000 movwf RCSTA movlw 0xc0 ;enable global and peripheral ints 11000000 movwf INTCON Bank1 ;select bank1 movlw 0x20 ; (0x30 enable TX) and RX interrupts 0010 0000 movwf PIE1 Bank0 return ; **************************************************** ; * Setup PWM routine: * ; **************************************************** SetupPWM BSF STATUS,RP0 ; reg. Bank 1 MOVLW H'ff' MOVWF PR2 ; periode = 0.128 seconden BCF STATUS,RP0 ; reg. Bank 0 MOVLW D'0' MOVWF CCPR1L ; Set Duty Cycle to 0 MOVLW D'0' MOVWF CCPR2L ; Set Duty Cycle to 0 MOVLW B'00000100' MOVWF T2CON MOVLW B'00001100' ; later op te zetten MOVWF CCP1CON MOVWF CCP2CON RETURN ; **************************************************** ; * Setup Analog routine: * ; **************************************************** SetupAnalog Bank1 MOVLW b'01000100' ; analoge ingang op AN0-AN4 MOVWF ADCON1 Bank0 MOVLW B'01000001 MOVWF ADCON0 RETURN SETUPLICHT BCF STATUS,RP0 ; BSF INTCON, GIE MOVLW b'00000000' MOVWF TMR0 BSF STATUS,RP0 ; reg. Bank 1 BCF OPTION_REG, T0CS BCF OPTION_REG, PSA BSF OPTION_REG, PS2 BSF OPTION_REG, PS1 BSF OPTION_REG, PS0 BCF STATUS,RP0 ; reg. Bank 0 BSF INTCON,5 RETURN ;****************************************************************************** ;****************************************************************************** Int: CLRWDT Bank1 BTFSC INTCON,2 GOTO KNIPPER Intt CLRWDT Bank0 ;select bank0 btfsc PIR1,RCIF ;test RCIF receive interrupt (1 als receive buffer is full) bsf STATUS,RP0 ;change to bank1 if RCIF set btfsc PIE1,RCIE ;test if interrupt enabled if RCIF set goto GetData ;if RCIF and RCIE set, do receive GOTO Intt ; ;****************************************************************************** ;****************************************************************************** ;Get received data GetData: Bank0 btfsc RCSTA,OERR ;test overrun error flag goto ErrOERR ;handle it if error btfsc RCSTA,FERR ;test framing error flag goto ErrFERR ;handle it if error movf DATAIN2,W ;Move last received data movwf DATAIN3 movf DATAIN1,W ;Move last received data movwf DATAIN2 movf RCREG,W ;Get received data movwf DATAIN1 ; Copy to DataIn1 SUBLW D'0' BTFSS STATUS,Z ; test if last data was 0 GOTO Intt Bank0 movf DATAIN3,W ;Move data to ReqPos1 movwf RESULTAAT movf DATAIN2,W ;Move data to ReqPos1 movwf SNELHEID MOVWF CCPR1L ; Set Duty Cycle to 0 MOVWF CCPR2L BTFSS RESULTAAT, 0 BCF PORTC, 0 BTFSC RESULTAAT, 0 BSF PORTC,0 BTFSS RESULTAAT, 1 BCF PORTC, 3 BTFSC RESULTAAT, 1 BSF PORTC,3 BTFSS RESULTAAT, 2 BCF PORTC, 4 BTFSC RESULTAAT, 2 BSF PORTC,4 BTFSS RESULTAAT, 3 BCF PORTC,5 BTFSC RESULTAAT, 3 BSF PORTC,5 BTFSS RESULTAAT, 4 BCF PORTB, 4 BTFSC RESULTAAT, 4 BSF PORTB,4 BTFSS RESULTAAT, 5 CALL CLEARLAMP BTFSC RESULTAAT, 5 CALL SETLAMP goto Intt CLEARLAMP BCF PORTA, 2 BCF PORTA, 5 RETURN SETLAMP BSF PORTA, 2 BSF PORTA, 5 RETURN ;error because OERR overrun error bit is set - clear and continue ErrOERR: bcf RCSTA,CREN ;reset the receiver logic bsf RCSTA,CREN ;enable reception again goto Int ;error because FERR framing error bit is set - clear and continue ErrFERR: movf RCREG,W ;discard received data that has error goto Int KNIPPER Bank0 ;select bank0 MOVWF TEMP2 BTFSS PORTA,2 GOTO Int2 BTFSC PORTA,2 GOTO Int3 Int2 BSF PORTA,2 BCF PORTA,5 Bank1 BCF INTCON, 2 Bank0 MOVFW TEMP2 retfie Int3 BCF PORTA,2 BSF PORTA,5 Bank1 BCF INTCON, 2 Bank0 MOVFW TEMP2 retfie VERANDERLAMP Bank0 BTFSS PORTA,2 GOTO Int2a BTFSC PORTA,2 GOTO Int3a Int2a BSF PORTA,2 BCF PORTA,5 return Int3a BCF PORTA,2 BSF PORTA,5 return ; ************************** ; * delay routine: * ; ************************** DELAY CLRWDT MOVLW D'1' ;* MOVWF TIMER1 ;* ; DELAY2 MOVLW D'50' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAY2 RETLW 0 DELAYB CLRWDT MOVLW D'255' ;* MOVWF TIMER1 ;* ; DELAYB2 MOVLW D'255' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAYB2 ; DELAY3 CLRWDT MOVLW D'255' ;* MOVWF TIMER1 ;* ; DELAY4 MOVLW D'255' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAY4 RETLW 0 DELAYC CLRWDT MOVLW D'250' ;* MOVWF TIMER1 ;* ; DELAYC2 MOVLW D'255' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAYC2 RETLW 0 DELAYD CLRWDT MOVLW D'125' ;* MOVWF TIMER1 ;* ; DELAYD2 MOVLW D'255' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAYD2 RETLW 0 ; DELAYE CLRWDT MOVLW D'80' ;* MOVWF TIMER1 ;* ; DELAYE2 MOVLW D'255' MOVWF TIMER2 DECFSZ TIMER2,F ;* GOTO $-1 ;* DECFSZ TIMER1,F GOTO DELAYE2 RETLW 0 END ; End of source code. ; By Jeroen D'hoker