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.

Controlling motor with bytes?

Status
Not open for further replies.

jacks

New Member
Hello

Is it possible to control DC motor from bytes to PIC16F628? I am struggling with this and had a look at Nigel tutorials. Thanks

Bathini
 
Yes, it needs to control an H-bridge. ANd to do that you will need to generate PWM signals from your microcontroller somehow either with the built-in hardware, or bit blasting if the MCU does not have an output compare or PWM module.
 
As you've checked my tutorials, look at the PWM one - which gives 128 speeds either forwards or backwards for two motors. As dknguyen says, you also need an h-bridge to feed the motor, and if you want to use two motors (like for a robot), a 16F628 isn't enough, and my tutorial uses a 16F876 for that reason.
 
I have designed the following circuit ...**broken link removed** - The last circuit from the bottom and works but when it comes to code?I do not know. I read your tutorial and seem to understand but for my implementation it is tricky. I was going to send the bytes with this utility ..Pololu - Pololu Serial Transmitter utility for Windows - serial transmitter and turn on an LED from one of the output pin e.g RA0 before connecting the entire H-bridge.
 
Are you wanting speed control?, or just forward and reverse?.

It would be easy to use my serial and PWM tutorials to make a project that receives serial data and controls motors accordingly.
 
Hi Nigel

I am trying speed control, forward, reverse and stop. My intent is send byte 1 - preamble all ones, byte 2 - address of motor, byte 3 - direction and speed(crucial one) and byte 4 - error detection.

I am going back to visit your site to study your tutorials again and see what I will come with.

jacks
 
From the sound of it you're wanting to do this via radio?, so my Manchester radio routines (using a two byte packet) and the PWM tutorial could easily be joined together.
 
Wouldn't a half bridge work fine for bidirectional DC motors? What would be the advantage of a full H-bridge?
 
LOL, right. Sometimes it's the simple things that trip me up the most. :p

Hence why I'm here now.
 
Friends

I have been reading and referencing from Nigel tutorials and would like to know where am I making a mistake?

LIST p=16F628 ;tell assembler what chip we are using
include "P16F628.inc" ;include the defaults for the chip
ERRORLEVEL 0, -302 ;suppress bank selection messages
__config 0x3D18 ;sets the configuration settings (oscillator)

cblock 0x20 ;start of general purpose registers
count

KM Equ 0

org 0x0000
movlw 0x07
movwf CMCON ;turn comparators off

Initialise clrf count
clrf PORTA
clrf PORTB


SetPorts bsf STATUS, RP0 ;select bank 1
MOVLW B'00000110'
MOVWF TRISB
bcf STATUS, RP0 ;select bank 0
call SER_INIT

CHECK call Rcv_RS232 ;check receive data
xorlw 'B'
btfss STATUS, Z ;is data 'B'?
goto CHECK ;if not, goto CHECK
goto KM_ON ;if yes. goto KM_ON


KM_ON bsf PORTB, KM ;ON
goto CHECK


:Hardware USART Serial Routines
SER_INIT
BSF STATUS, RP0 ;select bank 1
MOVLW d'25' ;9600 baud @ 4 Mhz Fosc +0.16% err
MOVWF SPBRG
MOVLW b'00100100' ;BRGH = 1
MOVWF TXSTA ;enable Async Transmission, set brgh
BCF STATUS, RP0 ;select bank 0
MOVLW b'10010000'
MOVWF RCSTA ;enable Async Reception
RETURN

XMIT_RS232 btfss PIR1, TXIF ;xmit buffer empty?
GOTO XMIT_RS232 ;no, wait
MOVWF TXREG ;now send
RETURN

Rcv_RS232 BTFSS PIR1, RCIF ;check for received data
GOTO Rcv_RS232
MOVF RCREG, W
RETURN

;End of serial routines


; Activating RA0 pin17 on PIC16F628

LED Equ 0 ;set constant LED = 0
LEDPORT Equ PORTA ;set constant LEDPORT = 'PORTA'

org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07
movwf CMCON ;turn comparators off

bsf STATUS, RP0 ;select bank 1
movlw b'00000010' ;set PortB all outputs except RB1
movwf TRISB
movwf TRISA ;set PortA all outputs
bcf STATUS, RP0 ;select bank 0
clrf PORTA
clrf PORTB ;set all outputs 0

Loop
bsf LEDPORT, LED ;turn on RA0 only!
call Delay ;this waits for a while!
goto Loop ;go back and do it again

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

decfsz count1 ,f
goto d1
retlw 0x00

; PWM implementation via strobe and pin9 of PIC16F628

portb equ 0x06 ; port b equate
duty equ 0x0c ; length of duty cycle
temp equ 0x0d ; length of duty cycle

;---------------------------------------------------------------------

c equ 0 ; status bit to check after subtraction

;---------------------------------------------------------------------

org 0x000

movlw 0x00 ; load W with 0x00 make port B output
tris portb ; copy W tristate to port B outputs
movlw 0x00 ; fill w with zeroes
movwf portb ; set port b outputs to low
rstrt movlw d'0'
movwf portb
movlw d'110' ; Duty cycle length
movwf duty
b0loop movf duty,w
movwf temp
bsf portb,0
pwma nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
decfsz temp
goto pwma
movlw d'255'
movwf temp
movf duty,w
subwf temp,f
bcf portb,0
pwmb nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
decfsz temp
goto pwmb
goto rstrt

;----------------------------------------------------------------------

end
 
For a start, you've got three org 0x000 lines, so the program will be overwriting itself, assuming the assembler will even assemble it?.

Also, when posting code, use the 'code' tags (from 'Go Advanced') to keep the code formatted correctly.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top