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..
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