Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC(16F877A) Code for DC motor

Status
Not open for further replies.

godha89

Newbie level 4
Joined
Nov 5, 2012
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,321
I need to control 15 dc motors using a single PIC controller.
In the circuit I have used KT 450 IC and Toshiba ULN 2803 IC for each motor. The circuit is being used for multi level car parking system in which 2 motors are for central lift and rest are for parking slots.
please help me in generating the code. . . .
 

Do the motors need to be just turned on/off? Or do their speeds and direction need to be controlled as well? If so, for how many?
 
Last edited:

Do the motors need to be just turned on/off? Or do their speeds and direction need to be controlled as well? If so, for how many?

Actually i need to first move the central lift to particular level and then after that i need to rotate it to parking slot which is empty and then move the car to that slot using the conveyor belt attached to the motor.
 

I wrote the code for the above project
But while simulating in MPLAB, I get Stack underflow error at PC=0x00000001
My code is as follows:

Code:
#include <p16f877A.inc>

ORG 0020

	bsf        STATUS, RP0        ;set RP0
        bcf        STATUS, RP1        ;clear RP1


        movlw     0xff            ;set PORTA as input
        movwf    PORTA
	

	movlw     0x00            ;set PORTB as output
        movwf    TRISB    
    
		BCF TRISB,2
		BCF TRISB,3
		BSF TRISA,0
		BSF TRISA,2
		BSF TRISA,3
		
		

H: 		BTFSS PORTA,0
		GOTO H



H2: 		BTFSS PORTA,2
		BTFSS PORTA,3
		GOTO RLY1
		GOTO H

RLY1:	BSF TRISB,2
	BSF TRISB,3
	
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
	S1:	BTFSC PORTA,2
	S2:	BTFSC PORTA,3
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		NOP
		GOTO S1
		GOTO s2
		GOTO H

END



I have also attached the circuit diagrams
 

Attachments

  • Relay.pdf
    79 KB · Views: 59
  • as.pdf
    91.5 KB · Views: 47
Last edited by a moderator:

Re: PIC code for DC motor

You are not using the stack so it can't underflow or overflow!

It's strange code though, can you answer:
1. why start the code at address 20?
2. what assembler and simulator are you using?

You also have the banking bits wrong for addressing PORTA throughout the whole code. That would stop it working but not cause a stack error.

Brian.
 

Re: PIC code for DC motor

Hi,

You first need to set up the pic to operate correctly.

It needs the Config line to tell it the basics and what oscillators you are using , have coded for a XT Crystal at 4 mhz or below.

Also the Input ports are set to Analogue at power on and you must set them to Digital.

You have got your PORTS and TRIS mixed up.
TRIS determines INPUT or OUTPUT
PORT Reads or Writes to the pin.

Have change your code to set it up correctly, but have not done anything with your switching logic which does not seem to do anything when running.

Code:
	title "pic16f877a carparking"
	

	list		p=16f877A	; list directive to define processor
	#include	<p16f877A.inc>	; processor specific variable definitions
	
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF

	
	ORG 0x000
	goto start


	
start  	movlw	0x06		; set all Ports to DIGITAL from Default Analogue
	banksel ADCON1 			;( bank1)
	movwf	ADCON1			
	
	movlw 0xff			    ;set PORTA as input using the TRIS instruction
;	movwf PORTA  ?
	MOVWF  TRISA
	
	movlw 0x00 			   ;set PORTB as output
	movwf TRISB 

	banksel 0  		    	; return to bank0


;?   PORTS ??

;	BCF TRISB,2
;	BCF TRISB,3
;	BSF TRISA,0
;	BSF TRISA,2
;	BSF TRISA,3
	
	
	
H: BTFSS PORTA,0
	GOTO H
	
	
	
H2: BTFSS PORTA,2
	BTFSS PORTA,3
	GOTO RLY1
	GOTO H
	
RLY1:	BSF TRISB,2
	BSF TRISB,3
	
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
S1:	BTFSC PORTA,2
S2:	BTFSC PORTA,3
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	GOTO S1
	GOTO s2
	GOTO H
	
	END
 

Re: PIC code for DC motor

Well thanks for the above code but, in the system, we need to sense the object from parking entry sensor and then move the lift to parking sensor that is empty.
how can we control motor direction for that. This was the code that I figured out.
I have been using MPLAB's MPASM toolsuite for assembly language coding and MPLAB SIM as simulator.
 

Re: PIC code for DC motor

Well thanks for the above code but, in the system, we need to sense the object from parking entry sensor and then move the lift to parking sensor that is empty.
how can we control motor direction for that. This was the code that I figured out.
I have been using MPLAB's MPASM toolsuite for assembly language coding and MPLAB SIM as simulator.

Hi,

There are a couple of ways to change direction of a motor.
The simplest is to use a DP CO relay, plenty of examples on the web; that can be controlled by one pin from the Pic.
You need another pin and relay to act as the on /off control

Will let you work out the program logic for that.

Check your code, particulalry the RLY1 where you are using TRIS. See my eariler comment about TRIS and PORT.
Also check the Datasheet , Registers section, to see that PORT and TRIS regsiters are in different Banks and you must manually change them otherwise your program code goes wrong.

Also check your logic on the use of the BTFss/sc instructions, it not typical ( though possible) to have two together like that as the outcome of each instruction has two options, skip or not to skip.

Put ; comments against you code so you can follow your own logic better and others can also follow it easier.
 

Hi all, i am doing the same project DC motor with PIC16f877A + L293D.

Here also the Source code that I write in mikroC PRO for PIC. bt code.JPG
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top