;------------------------------------------------------------------------------ ; EXPLOR1.ASM - Ultimo aggrn. 13-8-2002 ; ; (c) 2002, Claudio Fin ;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------ ; Utilizzo porte ;------------------------------------------------------------------------------ ; RA0 ; RA1 ; RA2 ; RA3 ; RA4 ; RA5 ; ; RB0 ----> Bit0 DAC ; RB1 ----> Bit1 DAC ; RB2 ----> Bit2 DAC ; RB3 ----> Bit3 DAC ; RB4 ----> Bit4 DAC ; RB5 ----> debug led (0=acceso 1=spento) / serial out ; RB6 ----> direzione motore sinistro (1=avanti 0=indietro) ; RB7 ----> direzione motore destro (1=avanti 0=indietro) ; ; RC0 ----> ; RC1 ----> CCP2 PWM motore destro (duty 100%=motore fermo) ; RC2 ----> CCP1 PWM motore sinistro (duty 100%=motore fermo) ; RC3 ----> ; RC4 ----> ; RC5 ----> ; RC6 ----> ; RC7 ----> ; |------------------------| ; RD0 <------------| 3x | ; RD1 <---- 2x 4x | Sensori ; RD2 <---- 1x 5x | IR ; RD3 <---- | ; RD4 <---- | ; RD5 <---- 6x 0x--------| ; RD6 <------------------| ; RD7 <---- ; ; RE0 ----> Clock verso bussola ; RE1 ----> Dati verso bussola ; RE2 <---- Dati da bussola ;------------------------------------------------------------------------------ ; FLAGS1 0 ; FLAGS1 1 ; FLAGS1 2 ; FLAGS1 3 ; FLAGS1 4 ; FLAGS1 5 ; FLAGS1 6 ; FLAGS1 7 ;------------------------------------------------------------------------------ ;------------------------------------------------------------------------------ ; Definizioni ;------------------------------------------------------------------------------ PROCESSOR 16F877 ; CLOCK 20Mhz RADIX DEC INCLUDE "P16F877.INC" INCLUDE "pwrasm.inc" __CONFIG 11111100111010B #define serout PORTB,5 #define cmps_outck PORTE,0 #define cmps_outda PORTE,1 #define cmps_inda PORTE,2 ;valori comando pwm motori sxavanti EQU 37 dxavanti EQU 25 sxindietro EQU 31 dxindietro EQU 37 ;------------------------------------------------------------------------------ ; Variabili del programma ;------------------------------------------------------------------------------ ORG 20H cont RES 1 ; Variabili per lettura bussola (4 bytes) cmps_ang RES 1 ;angolo misurato 0..255 cmps_cont RES 1 ;contatore bit cmps_error RES 1 ;0=ok 1=nok cmps_byte RES 1 ;byte trasm.o ricevuto ; Variabili per IR ranging (12 bytes) flags2 RES 1 ;flag misurazione dac RES 1 ;valore da scrivere sul dac tmp_rang RES 1 ;variabile di lavoro task ranging trang2 RES 7 ;tabella misure racont RES 2 ;conteggio per delay ; Variabili per trasmissione seriale (3 bytes) txdelay RES 1 txbyte RES 1 txcontabit RES 1 ;------------------------------------------------------------------------------ ; Inizio programma all'indirizzo 0. ;------------------------------------------------------------------------------ ORG 0 ;------------------------------------------------------------------------------ ; Inizializzazione hardware ;------------------------------------------------------------------------------ Ldf PORTB,11100000B Ldf PORTC,00000110B Ldf PORTE,00000011B Bank1 clrf TRISB clrf TRISC Ldf ADCON1,00000010B ;PORTA Ainputs PORTE digital Ldf TRISE,00000100B Bank0 Ldf PORTB,11100000B Ldf PORTC,00000110B Ldf PORTE,00000011B Bank1 Ldf PR2,127 Bank0 Ldf CCPR1L,128 Ldf CCPR2L,128 Ldf T2CON,00000110B movlw 00111100B iorwf CCP1CON,f iorwf CCP2CON,f ;------------------------------------------------------------------------------ ; Inizializzazione variabili ;------------------------------------------------------------------------------ bsf flags2,7 ;azzera stato task ranging (1=first level) movlw 5 movwf trang2 ;prima misura fittizia tutto ostacoli movwf trang2+1 movwf trang2+2 movwf trang2+3 movwf trang2+4 movwf trang2+5 movwf trang2+6 ;------------------------------------------------------------------------------ ; Programma principale ;------------------------------------------------------------------------------ main Ldf cont,21 pippo call ranging Djnz cont,pippo call readbussola Jb cmps_error,0,main call trasmetti goto main ;------------------------------------------------------------------------------ ; Lettura angolo dalla bussola in I2C simulato a software da PORTE ;------------------------------------------------------------------------------ readbussola clrf cmps_error call cmps_start ;invia start su bus i2c Ldf cmps_byte,11000000B call cmps_tx ;trasmette address bussola Jb cmps_error,0,cmps_fine ;se non ack goto fine call cmps_del2 Ldf cmps_byte,1 call cmps_tx ;trasmette nr.registro da leggere Jb cmps_error,0,cmps_fine ;se non ack goto fine call cmps_del2 call cmps_rstart ;invia repeated start su bus i2c Ldf cmps_byte,11000001B call cmps_tx ;trasmette address bussola OR 01H Jb cmps_error,0,cmps_fine ;se non ack goto fine call cmps_del2 call cmps_rx ;riceve lettura 1 Movff cmps_byte,cmps_ang call cmps_rx ;riceve e scarta lettura 2 call cmps_rx ;riceve e scarta lettura 3 cmps_fine call cmps_stop ;invia stop su bus i2c return cmps_rstart bsf cmps_outda call cmps_del bsf cmps_outck call cmps_del cmps_start bcf cmps_outda call cmps_del bcf cmps_outck call cmps_del return cmps_stop bsf cmps_outck call cmps_del bsf cmps_outda call cmps_del return cmps_tx Ldf cmps_cont,8 cmps_tx1 Movb cmps_byte,7,cmps_outda rlf cmps_byte,f call cmps_del bsf cmps_outck call cmps_del bcf cmps_outck call cmps_del Djnz cmps_cont,cmps_tx1 bsf cmps_outda call cmps_del Movb cmps_inda,cmps_error,0 bsf cmps_outck call cmps_del bcf cmps_outck call cmps_del return cmps_rx bsf cmps_outda call cmps_del Ldf cmps_cont,8 cmps_rx1 rlf cmps_byte,f Movb cmps_inda,cmps_byte,0 call cmps_del bsf cmps_outck call cmps_del bcf cmps_outck call cmps_del Djnz cmps_cont,cmps_rx1 bcf cmps_outda call cmps_del bsf cmps_outck call cmps_del bcf cmps_outck call cmps_del return cmps_del goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 return cmps_del2 Ldf cmps_cont,15 cmps_del22 call cmps_del Djnz cmps_cont,cmps_del22 return ;------------------------------------------------------------------------------ ; Task di ranging Per una misura completa va chiamato 21 volte. ; I livelli misurati vanno da 5 a 25. I valori vengono posti nei 7 bytes ; di trang2. ; Se non si riceve il segnale riflesso il valore e' 32. ; Una chiamata dura circa 2.5mS (la misura completa avviene in 52.5mS) ;------------------------------------------------------------------------------ ranging Jnb flags2,7,ranging1 ranging0 Ldf dac,5 ;valore di partenza dac clrf flags2 ;imposta over range flags ranging1 movf dac,w iorwf PORTB,f ;scrive il valore dac Delay16 racont,1,94 ;630.6µS movf PORTD,w xorlw 11111111B movwf tmp_rang movlw 11100000B ;azzera DAC andwf PORTB,f Delay16 racont,1,94 ;630.6µS movf PORTD,w andwf tmp_rang,f movf dac,w iorwf PORTB,f ;riscrive valore DAC Delay16 racont,1,94 ;630.6µS movf PORTD,w xorlw 11111111B andwf tmp_rang,f movlw 11100000B andwf PORTB,f ;riazzera DAC Delay16 racont,1,94 ;630.6µS movf PORTD,w andwf tmp_rang,f Jb flags2,0,ranging4_3 Jnb tmp_rang,0,ranging4_3 bsf flags2,0 Movff dac,trang2 ranging4_3 Jb flags2,1,ranging4_4 Jnb tmp_rang,1,ranging4_4 bsf flags2,1 Movff dac,trang2+1 ranging4_4 Jb flags2,2,ranging4_5 Jnb tmp_rang,2,ranging4_5 bsf flags2,2 Movff dac,trang2+2 ranging4_5 Jb flags2,3,ranging4_6 Jnb tmp_rang,3,ranging4_6 bsf flags2,3 Movff dac,trang2+3 ranging4_6 Jb flags2,4,ranging4_7 Jnb tmp_rang,4,ranging4_7 bsf flags2,4 Movff dac,trang2+4 ranging4_7 Jb flags2,5,ranging4_8 Jnb tmp_rang,5,ranging4_8 bsf flags2,5 Movff dac,trang2+5 ranging4_8 Jb flags2,6,ranging4_9 Jnb tmp_rang,6,ranging4_9 bsf flags2,6 Movff dac,trang2+6 ranging4_9 incf dac,f Cpje dac,26,ranging4_2 return ranging4_2 bsf flags2,7 ;setta stato task first level Sknb flags2,0 ;imposta overflow se non ricevuto segnale Skip 2 Ldf trang2,32 Sknb flags2,1 Skip 2 Ldf trang2+1,32 Sknb flags2,2 Skip 2 Ldf trang2+2,32 Sknb flags2,3 Skip 2 Ldf trang2+3,32 Sknb flags2,4 Skip 2 Ldf trang2+4,32 Sknb flags2,5 Skip 2 Ldf trang2+5,32 Sknb flags2,6 Skip 2 Ldf trang2+6,32 return ;------------------------------------------------------------------------------ ; Subroutines controllo motori ;------------------------------------------------------------------------------ stopall Ldf CCPR1L,128 Ldf CCPR2L,128 return avall bsf PORTB,6 bsf PORTB,7 Ldf CCPR1L,sxavanti Ldf CCPR2L,dxavanti return indall bcf PORTB,6 bcf PORTB,7 Ldf CCPR1L,sxindietro Ldf CCPR2L,dxindietro return stopsx Ldf CCPR1L,128 return stopdx Ldf CCPR2L,128 return avsx bsf PORTB,6 Ldf CCPR1L,sxavanti return avdx bsf PORTB,7 Ldf CCPR2L,dxavanti return indsx bcf PORTB,6 Ldf CCPR1L,sxindietro return inddx bcf PORTB,7 Ldf CCPR2L,dxindietro return rotsx bsf PORTB,7 Ldf CCPR2L,dxavanti bcf PORTB,6 Ldf CCPR1L,sxindietro return rotdx bcf PORTB,7 Ldf CCPR2L,dxindietro bsf PORTB,6 Ldf CCPR1L,sxavanti return ;------------------------------------------------------------------------------ ; Trasmette i dati di ranging al pc per debug ;------------------------------------------------------------------------------ trasmetti movlw 65 call sertx movf trang2,w call sertx movf trang2+1,w call sertx movf trang2+2,w call sertx movf trang2+3,w call sertx movf trang2+4,w call sertx movf trang2+5,w call sertx movf trang2+6,w call sertx movf cmps_ang,w call sertx return ;------------------------------------------------------------------------------ ; Subroutine per tx seriale per debug ;------------------------------------------------------------------------------ sertx movwf txbyte ;mette dato in txbyte bcf serout ;inizio start bit a 0 movlw 171 ;imposta la sua durata movwf txdelay decfsz txdelay,f goto $-1 ;ciclo di ritardo movlw 8 ;8 bit da trasmettere movwf txcontabit btfsc txbyte,0 ;testa bit 0 di txbyte goto $+4 nop bcf serout ;se 0 abbassa il bit serout goto $+4 bsf serout ;altrimenti lo alza nop nop rrf txbyte,f ;poi scorre a destra txbyte movlw 170 ;imposta durata del bit movwf txdelay decfsz txdelay,f goto $-1 ;ciclo di ritardo decfsz txcontabit,f goto $-14 ;ciclo di 8 bit goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 bsf serout ;inizio stop bits a 1 movlw 255 ;imposta la loro durata movwf txdelay decfsz txdelay,f goto $-1 ;ciclo di ritardo movlw 255 ;imposta la loro durata movwf txdelay decfsz txdelay,f goto $-1 ;ciclo di ritardo return ;------------------------------------------------------------------------------ END