Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help needed for 18f452

Status
Not open for further replies.

ichigo

New Member
hi all ,

I badly need advise from you guys . Hope you guys could help me as i am not sure whether what is wrong with my coding . Thxs a million in advance . Currently i am writing a program to run and control direction of my 2 motors through the use H bridge circuit. I am using the PICDEM 2 plus demo board and trying with their built on buttons ,with the 18f452 chip.


After setting and pressing the button that is on the PICDEM board as the input port, the output port which is the CCP1 and CCP2 port(port C, 1 and 2) will cause my 2 motors to run.Beside , I make use of the port D, 0 and 1 as output port to set the motor forward and reverse direction by doing a bsf or a bcf giving a 1 / 0 to my H bridge circuit.

The problem now i faced is that when i pressed the first button (PORTA,4), the 2 motors could move in the same direction. But when i pressed the second button (PORTB,0), only one motor will move while the other motor wont move.I tried compiling the code , but no error is being found. I tried playing around with the code but it still couldnt work. Below will be my code . Pls help and advise me ! Thanks once again .


list p=18f452
#include p18f452.inc
;STARTUP CODE
;************************************************************
; declare variables here
Count2 equ 0x000
Count1 equ 0x001

;************************************************************
STARTUP CODE
org 00000h ; Reset Vector
goto start

PROG1 CODE
;************************************************************
;program code starts here
start

;*****************initialisation*****************
clrf PORTC ;initialise PORTC as output port and clear the port.
movlw b'00000000'
movwf TRISC
clrf PORTD ;initialise PORTD as output port and clear the port.
movlw b'00000000'
movwf TRISD
setf TRISA ;init PORTA as input
clrf PORTB ;initialise PORTB as input port.
movlw b'11111111' ;initialise PORTB as input port.
movwf TRISB ;initialise PORTB as input port.
;*****************main body****************************
clrf T2CON
bcf T2CON,TMR2ON
setf PR2 ;PR2=oxFF
movlw 0x00
movwf CCPR1L
movlw 0x00
movwf CCPR2L
clrf CCP1CON
bsf CCP1CON,CCP1M3
bsf CCP1CON,CCP1M2
bsf CCP1CON,CCP1M0 ;set CCP1 PWM mode


clrf CCP2CON
bsf CCP2CON,CCP2M3
bsf CCP2CON,CCP2M2
bsf CCP2CON,CCP2M1
bsf CCP2CON,CCP2M0 ;set CCP2 PWM mode

;Switch control
;********************Switch 1 **********************
checkSW
bcf PORTD,1 ;set forward direction
bcf PORTD,0 ;set forward direction
btfsc PORTB,0 ;check for sw2.it's normally high, low when pressed
goto wait ;check for sw if sw2 is not pressed.
goto turnon
wait
bcf T2CON,TMR2ON
movlw 0x00
movwf CCPR1L
movlw 0x00
movwf CCPR2L
clrf CCP2CON
clrf CCP1CON
call police
;**************************POLICE*******************************
police
btfsc PORTA,4
goto checkSW2
goto checkSW3

;********************Switch 2 **********************
checkSW2
btfss PORTB,3 ;check for sw.it's normally high, low when pressed
goto wait2 ;check for sw2 if sw is not pressed.
goto turnon2
wait2
bcf T2CON,TMR2ON
movlw 0x00
movwf CCPR1L
movlw 0x00
movwf CCPR2L
clrf CCP2CON
clrf CCP1CON
call police2

;****************************************************************
police2
btfsc PORTB,0
goto checkSW3
goto checkSW

;********************Switch 3 **********************
checkSW3

btfsc PORTA,4 ;check for sw2.it's normally high, low when pressed
goto wait3 ;check for sw if sw2 is not pressed.
goto turnon3
wait3
bcf T2CON,TMR2ON
movlw 0x00
movwf CCPR1L
movlw 0x00
movwf CCPR2L
clrf CCP2CON
clrf CCP1CON
call police3
;*********************Police 3 *****************************************
police3
btfss PORTB,3
goto checkSW
goto checkSW2

;**************************thief*******************************
thief
btfss PORTB,3
goto continue
goto stop

;******************** when switch 1 is being pressed, speed = 0x9f******************
turnon
btfsc PORTA,4
goto thief
goto stop
continue
setf CCP2CON
setf CCP1CON
bsf T2CON,TMR2ON
bsf PORTD,1 ;set forward direction
bsf PORTD,0 ;set forward direction
movlw 0x9F ;set frequency for CCP1
movwf CCPR1L
movlw 0x9F ;set frequency for CCP1
movwf CCPR2L
btfsc PORTB,0
goto checkSW
goto turnon
;**************************thief2*******************************
thief2
btfsc PORTA,4
goto continue2
goto stop

;******************* when switch 2 is being pressed, speed = 0x7f******************
turnon2
btfsc PORTB,0
goto thief2
goto stop
continue2
setf CCP2CON
setf CCP1CON
bsf T2CON,TMR2ON
movlw 0x2F
movwf CCPR1L
bcf PORTD,0 ;set forward direction
movlw 0x7F
movwf CCPR2L
bsf PORTD,1 ;set forward direction
btfss PORTB,3
goto checkSW2
goto turnon2

;**************************thief3*******************************
thief3
btfsc PORTB,0
goto continue3
goto stop

;************** when switch 3 is being pressed, speed = 0x5f******************
turnon3
btfss PORTB,3
goto thief3
goto stop
continue3
setf CCP2CON
setf CCP1CON
bsf T2CON,TMR2ON
movlw 0x9F
movwf CCPR1L
bcf PORTD,0 ;set forward direction
movlw 0x9F
movwf CCPR2L
bcf PORTD,1 ;set forward direction
btfsc PORTA,4
goto checkSW3
goto turnon3
;********************Stop **********************
stop
bcf PORTD,0
bcf PORTD,1
clrf CCP2CON
clrf CCP1CON
bcf T2CON,TMR2ON
movlw 0x00 ;set frequency for CCP1
movwf CCPR1L
movlw 0x00 ;set frequency for CCP1
movwf CCPR2L
goto start
return

end
 
Hi Ichigo,

please see the continue, continue1 and continue2 subroutines. the comments set forward and the instructions are not similar-- i only presume, the comments are not modified.
Code:
	list p=18f452 
 	include<p18f452.inc>
; *********************STARTUP CODE ************** 
; declare variables here 
Count2 equ 0x000 
Count1 equ 0x001 

;**********************STARTUP CODE ********************
 org   00000h 			; Reset Vector 

 goto  start 
 
;************************************************
;program code starts here 
;*****************initialisation***************** 
start 
 clrf  PORTC 			;initialise PORTC as output port and clear  
 movlw b'00000000' 
 movwf TRISC 
 clrf  PORTD 			;initialise PORTD as output port and clear  
 movlw b'00000000' 
 movwf TRISD 
 setf  TRISA 			;init PORTA as input 
 clrf  PORTB 			;initialise PORTB as input port. 
 movlw b'11111111' 		;initialise PORTB as input port. 
 movwf TRISB 			;initialise PORTB as input port. 
;*****************main body**************************** 
 clrf  T2CON 
 bcf   T2CON,TMR2ON 
 setf  PR2 				;PR2=oxFF 
 movlw 0x00 
 movwf CCPR1L 
 movlw 0x00 
 movwf CCPR2L 
 clrf  CCP1CON 
 bsf   CCP1CON,CCP1M3 
 bsf   CCP1CON,CCP1M2 
 bsf   CCP1CON,CCP1M0 	;set CCP1 PWM mode 

 clrf  CCP2CON 
 bsf   CCP2CON,CCP2M3 
 bsf   CCP2CON,CCP2M2 
 bsf   CCP2CON,CCP2M1 
 bsf   CCP2CON,CCP2M0 	;set CCP2 PWM mode 

;Switch control 
;********************Switch 1 ********************** 
checkSW 
 bcf   PORTD,1 			;set forward direction 
 bcf   PORTD,0 			;set forward direction 
 btfsc PORTB,0 			;check for sw2.it's normally high,low when pressed  
 goto  wait 			;check for sw if sw2 is not pressed. 
 goto  turnon 
wait 
 bcf   T2CON,TMR2ON 
 movlw 0x00 
 movwf CCPR1L 
 movlw 0x00 
 movwf CCPR2L 
 clrf  CCP2CON 
 clrf  CCP1CON 
 call  police 
;**************************POLICE*****************
police 
 btfsc PORTA,4 
 goto  checkSW2 
 goto  checkSW3 

;********************Switch 2 ********************** 
checkSW2 
 btfss PORTB,3 			;check for sw.it's normally high, low when pressed 
 goto  wait2 			;check for sw2 if sw is not pressed. 
 goto  turnon2 
wait2 
 bcf   T2CON,TMR2ON 
 movlw 0x00 
 movwf CCPR1L 
 movlw 0x00 
 movwf CCPR2L 
 clrf  CCP2CON 
 clrf  CCP1CON 
 call  police2 

;*************************************************  
police2 
 btfsc PORTB,0 
 goto  checkSW3 
 goto  checkSW 

;********************Switch 3 ********************** 
checkSW3 
 btfsc PORTA,4  		;check for sw2.it's normally high, low when pressed 
 goto  wait3 			;check for sw if sw2 is not pressed. 
 goto  turnon3 
wait3 
 bcf   T2CON,TMR2ON 
 movlw 0x00 
 movwf CCPR1L 
 movlw 0x00 
 movwf CCPR2L 
 clrf  CCP2CON 
 clrf  CCP1CON 
 call  police3 
;*********************Police3********************* 
police3 
 btfss PORTB,3 
 goto  checkSW 
 goto  checkSW2 

;**************************thief******************  
thief 
 btfss PORTB,3 
 goto  continue 
 goto  stop 

;*****when switch 1 is being pressed, speed =0x9f**** 
turnon 
 btfsc PORTA,4 
 goto  thief 
 goto  stop 
continue 
 setf  CCP2CON 
 setf  CCP1CON 
 bsf   T2CON,TMR2ON 
 bsf   PORTD,1 			;set forward direction 
 bsf   PORTD,0 			;set forward direction 
 movlw 0x9F 			;set frequency for CCP1 
 movwf CCPR1L 
 movlw 0x9F 			;set frequency for CCP1 
 movwf CCPR2L 
 btfsc PORTB,0 
 goto  checkSW 
 goto  turnon 
;**************************thief2*****************  
thief2 
 btfsc PORTA,4 
 goto  continue2 
 goto  stop 

;*******when switch 2 is being pressed, speed = 0x7f *** 
turnon2 
 btfsc PORTB,0 
 goto  thief2 
 goto  stop 
continue2 
 setf  CCP2CON 
 setf  CCP1CON 
 bsf   T2CON,TMR2ON 
 movlw 0x2F 
 movwf CCPR1L 
 bcf   PORTD,0 			;set forward direction 
 movlw 0x7F 
 movwf CCPR2L 
 bsf   PORTD,1 			;set forward direction 
 btfss PORTB,3 
 goto  checkSW2 
 goto  turnon2 

;**************************thief3*****************  
thief3 
 btfsc PORTB,0 
 goto  continue3 
 goto  stop 

;*****when switch 3 is being pressed, speed = 0x5f **** 
turnon3 
 btfss PORTB,3 
 goto  thief3 
 goto  stop 
continue3 
 setf  CCP2CON 
 setf  CCP1CON 
 bsf   T2CON,TMR2ON 
 movlw 0x9F 
 movwf CCPR1L 
 bcf   PORTD,0 			;set forward direction 
 movlw 0x9F 
 movwf CCPR2L 
 bcf   PORTD,1 			;set forward direction 
 btfsc PORTA,4 
 goto  checkSW3 
 goto  turnon3 
;********************Stop ********************** 
stop 
 bcf   PORTD,0 
 bcf   PORTD,1 
 clrf  CCP2CON 
 clrf  CCP1CON 
 bcf   T2CON,TMR2ON 
 movlw 0x00 			;set frequency for CCP1 
 movwf CCPR1L 
 movlw 0x00 			;set frequency for CCP1 
 movwf CCPR2L 
 goto  start 
 return 

 end

please pay partcular attention to continue2 where portd,0 is BCF and portd.1 is BSF

(ONE THING -- I AM NOT EVEN PREMATURE --JUST TRYING TO LEARN-- AS SOMEONE PUT IT --CRITIC CAN'T BE POET)

REGARDS
 
Last edited:
hi . thanks for ur help . didnt notice that i made an error in the comment . The reason for portd,0 is BCF and portd,1 is BSF is because the h bridge require the 1/0 to control the different direction ( forward /backward ). But this part doesnt seems to work out at all .
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top