Ðề tài: Pic 18f452
View Single Post
Old 20-04-2006, 03:43 AM   #5
ahchu
Đệ tử 1 túi
 
Tham gia ngày: Mar 2006
Bài gửi: 23
:
hê hê, xin lỗi mọi người nghe... Đoạn code của em
Code:
	org		0x0000
	goto	start

;	case1	equ	0x20	;PORTA value = 000 -> the robot is in the air
;	case2	equ	0x21	;PORTA value = 001 -> left hand side
;	case3	equ	0x22	;PORTA value = 011 -> completely in left hand side 
;	case4	equ	0x23	;PORTA value = 100 -> right hand side
;	case5	equ	0x24	;PORTA value = 101 -> center the line
;	case6	equ	0x25	;PORTA value = 110 -> completely in right hand side
;	case7	equ	0x26	;PORTA value = 111 -> 

start
	movlw	d'124'			;set period for PWM=1000Hz
	movwf	PR2
	movlw	b'00000111'		;set prescale = 1:16 and enable TIMER2
	movwf	T2CON

	bcf		TRISC, CCP1
	bcf		TRISB, CCP2

	clrf	PORTB		;initialize PORTB, PORTA
	clrf	TRISB		
	clrf 	PORTA
	clrf	PORTD
	clrf	TRISD

;R_led off <=> PORTB = 11111110
;C_led off <=> PORTB = 11111101
;L_led off <=> PORTB = 11111011

	;configure PORTA	
	movlw	b'00000110'	;set all PORTAs as digital input
	movwf	ADCON1
	
	movlw	0xff
	movwf	TRISA		;set portA as input
	
	movf	PORTA,0		;read from PORTA and store it to W_reg
	movwf	PORTB	

loop
	movf	PORTA,0
	movwf	PORTB
	goto	case2

s1							;special case of robot when it turning right and get out of the line
	movlw	b'00000000'
	cpfseq	PORTA				;if equal skip
	goto	loop				;if equal goto case1

	call	RIGHT_WHEEL_FAST
	call	LEFT_WHEEL_SLOW
	goto	loop	
	
s2							;special case of robot when it turning left and get out of the line
	movlw	b'00000000'
	cpfseq	PORTA				;if equal skip
	goto	loop				;if equal goto case1

	call	LEFT_WHEEL_FAST
	call	RIGHT_WHEEL_SLOW
	goto	loop

;s3							;robot will stop while it follow the center and suddenly did not sence anything
;	movlw	b'00000111'
;	cpfseq	PORTA				;if equal skip
;	goto	loop				;if equal goto case1
	
;	call	LEFT_WHEEL_STOP	
;	call	RIGHT_WHEEL_STOP


case2							;in left hand side
	movlw	b'00000001'
	cpfseq	PORTA				;if equal skip
	goto	case3
	
	call	LEFT_WHEEL_FAST
	call	RIGHT_WHEEL_NORM
	goto	loop

case3							;completely in left hand side
	movlw	b'00000011'
	cpfseq	PORTA
	goto	case4
	
	call	LEFT_WHEEL_FAST
	call	RIGHT_WHEEL_SLOW
	
	goto	s2
	
case4							;right hand side
	movlw	b'00000100'
	cpfseq	PORTA
	goto	case5
	
	call	RIGHT_WHEEL_FAST
	call	LEFT_WHEEL_NORM
	goto	loop

case5							;center
	movlw	b'00000101'
	cpfseq	PORTA
	goto	case6
	
	call	RIGHT_WHEEL_NORM
	call	LEFT_WHEEL_NORM
	goto	loop

case6							;completely in right hand side
	movlw	b'00000110'
	cpfseq	PORTA
	goto	loop

	call	RIGHT_WHEEL_FAST
	call	LEFT_WHEEL_SLOW
	goto	s1
ahchu vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn