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.

Motor Controllers and serial comm?

Status
Not open for further replies.

RedCore

New Member
Hey guys..

I'm using a PIC16F877A to control a Pololu serial motor controller. I'm coding in ASM and sometimes it just refuses to work..

Sometimes the motor controllers LEDs do not light(and nothing happens) and sometimes is works perfectly.. I can usually get it working by unplugging the motor controller and plugging it back in several times(with the pic still running) or by resetting the pic but its very annoying and sometimes is still doesnt work after that.

I'm also using an EasyPic4 to program the PIC and I leave the PIC in the Dev board and hook the motor controller up to the same supply and such using the headers on the board.. (Hacky / wires running everywhere)

I really do not know much about serial communication at all..
I read about how to setup the baud generator and use the serial line on the PIC all from the datasheet and I used the motor controllers datasheet to get the protocol the controller expects but thats about all I know so far..


My code is quite hacked up but here it is..

Code:
;***********************************************;
;	First PIC program			;
;	   					;
;	Implented a Motor Controller		;
;						;
;						;
;						;
;************************************************
    					;Testing and Simulation
	LIST	P=PIC16F877A		;tell assembler what chip we are using
	include "P16F877A.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings 
					;(oscillator type etc.)


;****** Variables **************************************************************

	TEMPW	EQU     H'20'   	;This is the start of the General Registers.

	Motor0	EQU	H'21'		; Motor0 speed
	Motor1	EQU	H'22'		; Motor1 speed

	CurMtr0	EQU	H'23'		; Current Motor0 speed
	CurMtr1	EQU	H'24'		; Current Motor1 speed

	count1	EQU	H'25'		; Delay Variables.
	counta	EQU	H'26'
	countb	EQU	H'27'		



	reverse	EQU	H'28'		;Reverse flag


	org	0x0000			;org sets the origin, 0x0000
	goto 	main			;this is where the program starts running	



	org	0x0004			;org sets the origin, 0x0004
	goto	IntH			;this is where interrupts are vectored	



main
   	bsf 	STATUS,		RP0	;set   RP0
	bcf 	STATUS,		RP1	;clear RP1	This selects Bank 1

   	movlw 	b'00000000'		;Set all ports to output
   	movwf 	TRISE
   	movwf 	TRISD
   	movwf 	TRISC
   	movwf 	TRISB
	movwf	TRISA
	bsf	TRISB,		0	; Using RB0/INT as interrupt here.

	bcf	OPTION_REG,	T0CS	; Use interal CS for T0
	bsf	OPTION_REG,	INTEDG	; Interrupt Edge trigger.

	bsf	INTCON, 	GIE	;Global Interrupt Enable   (set)
	bsf	INTCON, 	INTE	;External Interrupt Enabel (set)
	bcf	INTCON, 	INTF	;External Intterupt flag

	movlw	d'20'
	movwf	SPBRG			;Set the baud rate.

   	bcf	TXSTA,		TX9	;8bit transmission
	bsf	TXSTA,		TXEN	;Transmit Enable bit
	bcf	TXSTA,		SYNC	;Asynchro mode   idk?
	bcf	TXSTA,		BRGH	;low speed baud rate

   	bcf 	STATUS,		RP0	;clear RP0
	bcf 	STATUS,		RP1	;clear RP1	This will select bank 0

   	bsf	RCSTA,		SPEN	;Serial Port Enable. Do I need this? sure..
	bcf	RCSTA,		RX9	;8bit receive
	bcf	RCSTA,		CREN	;Continous Receive disable

	movlw 	0x00			;Set the current speed for M0
   	movwf 	CurMtr0	 		;Set the current speed for M1
   	movwf 	CurMtr1			

	movlw 	0x30			;Set the default speed for M0
   	movwf 	Motor0	 		;Set the default speed for M1
   	movwf 	Motor1			

	movlw 	0x00			;Set the reverse flag (defualt: forward)
   	movwf 	reverse		

	movlw	0x00
	movwf	PORTA
	movwf	PORTB
	movwf	PORTC
	movwf	PORTD
	movwf	PORTE


	call 	Delay			; Let motor controller initialize
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay

   	movlw 	b'00000001'		;Set A0 on
   	movwf 	PORTA



Loop	
;	movlw	0xff
;	movwf	PORTA

	movlw 	b'01111111'		;Set the default speed for M0
   	movwf 	Motor0	 		;Set the default speed for M1
   	movwf 	Motor1
	call 	Forward		; Update the motor speeds if needed

	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay


;;	movlw	0x00
;;	movwf	PORTA

	movlw 	b'00000000'		;Set the default speed for M0
   	movwf 	Motor0	 		;Set the default speed for M1
   	movwf 	Motor1
	call 	Forward

	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay
	call 	Delay


	goto	Loop			;go back and do it again




Forward
	movlw 	0x00			;Go forward
 	movwf 	reverse		

	call 	UpdateMotors		; Update the motor speeds if needed
	return
	
;Reverse
;	movlw 	0x01			; Reverse
;   	movwf 	reverse		
;
;	call 	UpdateMotors		; Update the motor speeds if needed
;	return
	


	
UpdateMotors				;Check to see if we want the speed changed. If so, Change it.

updm0
	movfw	CurMtr0			; Current motor speed in W
	xorwf	Motor0,		0	; Xor Current motor speed with wanted motor speed
	btfsc 	STATUS,		Z	;Test if the last operation resulted in 0
	goto	updm1			;Skip to motor1 check if we dont need to update M0
	call	SerStart
	movlw	b'0000001'
	subwf   reverse,0
	movwf	TXREG
	call	SerChk
	movfw	Motor0
	movwf	TXREG
	movwf	CurMtr0

updm1
	movfw	CurMtr1			; Current motor speed in W
	xorwf	Motor1,		0	; Xor Current motor speed with wanted motor speed
	btfsc 	STATUS,		Z	;Test if the last operation resulted in 0
	return				;Return if we dont need to update M1
	call	SerStart
	movlw	b'0000011'
	subwf   reverse,0
	movwf	TXREG
	call	SerChk
	movfw	Motor1
	movwf	TXREG
	movwf	CurMtr1
	return


SerStart
	call 	SerChk
	movlw	0x80
	movwf	TXREG
	call	SerChk
	movlw	0x00
	movwf	TXREG	
	call	SerChk
	return

SerChk
	btfss 	PIR1,		TXIF	;Test if the data has been sent to buffer
	goto	$-1			;Does this work?
	return



Delay
	movlw	d'250'			;delay 250 ms (4 MHz clock)
	movwf	count1
d1	movlw	0xC7
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	return



;----Our Interrupt Handler will go here.----;

IntH
	movwf	TEMPW

	nop	
	nop
	nop
	nop

	bcf	INTCON,		INTF	;Lets set thing back..
	movfw	TEMPW              	;Restore w
	RETFIE
	




	end
 
What config' fuse settings are you using? There's really no easy way for us to tell from your __config 0x3D18 statement.
 
RedCore said:
OSC is HS
Everything else is disabled (watch dog, power up, brown out, etc.)
hi,
Check the least sig bits of the config word.
HS or LP?
Are you using a 20MHz xtal?
 
Rather than using the lazy shorthand way of writing the Config word, as 0x290A,
I would use the method recommended my microchip, in their templates.

It makes it easier for members to identify errors in the Config.

Example:
__CONFIG _CP_OFF & _DEBUG_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
 
The config word is the remanence of using a template. I manually change the OCS to HS before I assemble. I will switch over to the template config word definitions(?). I'm using an 8mhz xtal and I'm pretty sure I've set the correct baud rate for this. Is there any config bits I specifically need for serial comm?
Thanks for the help guys.. Any other tips?
 
RedCore said:
The config word is the remanence of using a template. I manually change the OCS to HS before I assemble. I will switch over to the template config word definitions(?). I'm using an 8mhz xtal and I'm pretty sure I've set the correct baud rate for this. Is there any config bits I specifically need for serial comm?
Thanks for the help guys.. Any other tips?

hi,
Whats the expected baud rate of the USART?
You are setting BRGH=0 and SPBRG as decimal 20, with a 8MHz xtal?

Looking at page 114 of the datasheet I cannot find a correlation between the 4MHz xtal table and 8MHz that gives s SPBRG of d20?

EDIT: If I am doing the sums correctly, for an assumed baud rate of 9600, SPBRG should be d12 ???
 
Last edited:
Using the formula given on page 115 in the datasheet

"(Asynchronous) Baud Rate = FOSC/(64 (X + 1))"

8/(64(20+1)) = 5952

8/(64(12+1)) = 9615

So yeah. Your right :p

I'm not sure how I messed that up.. I used the formula last time but oh well..
I've tried several different baud rates.. and the result was no better but I will try this and see how things go and report back.. Thanks eric
 
I cant even get it to work at all now..
SPBRG = 12d and the PIC is working..

I can see the led light up when the delay for the motor controller is initialized.. If I hook LEDs up to the serial lines.. C7 flashes slowly (Recieve line for serial) and C6(TX line) stays constant..

If I place a multimeter to the serial line that goes to the motor controller I can see a constant 5.5v with slight drops to 5v that seems to be timed when the bytes are sent to the controller..

I'm not sure if that helps you guys at all.. thats all I know.. and the controller isnt working at all now..
 
RedCore said:
I cant even get it to work at all now..
SPBRG = 12d and the PIC is working..

I can see the led light up when the delay for the motor controller is initialized.. If I hook LEDs up to the serial lines.. C7 flashes slowly (Recieve line for serial) and C6(TX line) stays constant..

If I place a multimeter to the serial line that goes to the motor controller I can see a constant 5.5v with slight drops to 5v that seems to be timed when the bytes are sent to the controller..

I'm not sure if that helps you guys at all.. thats all I know.. and the controller isnt working at all now..

hi Red,
Whats baud rate and other settings is the controller expecting?
eg: 9600 baud, no parity, 8 data bits, 1stop bit etc............

Do you have a web link to pololu controller?
Eric
 
RedCore said:
**broken link removed**

Page 6 is the serial format the motor controller expects..

hi,
On page #3 of the spec sheet, which type of connection are you using between the 877A and the controller for the serial connection.??
Is it pin #4 of the group of 9 or the 3 pins at the top of the pcb?

Page #6, say any baud rate within the range specified will be autodetected.
8 bit data, no parity, 1 stop bit.
 
RedCore said:
I'm using the serial input(pin 4).
hi Red,
Thats the option without a MAX232 line driver you are using. I will re-run your program tomorrow in the PIC sim and try to program a PIC and hang my laptop on the RS232 line. I'll have to use a 16F877, dont have an 'A' version.
Will change the config to suit.

If you crack the problem. let me know and I get on with something else.

Eric
 
I really appreciate it.. I'm not try to take the easy way out or anything and have you work on it.. I've just been trying so long to get this work..
 
RedCore said:
I really appreciate it.. I'm not try to take the easy way out or anything and have you work on it.. I've just been trying so long to get this work..
hi Red,

Attached a zip file of motor controller drive program.
It works OK thru the Sim.
Used Oshonsoft Basic compiler and Assembler.
Using a 4MHz xtal.

You can get a free 30 day trial of the PIC Simulator program from www.oshonsoft.com

Note:
The controller RESET line is PORTA.0 on the PIC, requires connecting.
As you are using the group of 9 pins for the serial, no MAX232 used.

Lets know how it goes.
Eric
 
Last edited:
hi Red,

You can use MPASMWIN assembler for the attached motor2.asm.

Its modified for 8MHz Xtal.

Eric
 
Last edited:
Wow, All went well. It worked perfectly. Now what is wrong with my code!!
I'm going to go through both sets of code and maybe integrate a little of yours and see how it all goes.. I think you have helped enough and I'll see what i can get going on my own.. Thanks again eric.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top