PDA

View Full Version : problem with PWM



mimmmis
- 9th September 2008, 21:17
Hi i made a connection computer-->rs232-->pic16f876. it works fine. then i connected an h-bridge and a dc motor to my pic. it works fine. with 1 it moves counterclockwise with 2 clockwise and with 0 it rests.my program

INCLUDE "MODEDEFS.BAS"

B0 VAR WORD
B1 VAR WORD
Pause 500
SerOut2 PORTC.6,84,["Hi!",13,10]

start:

SerIn2 PORTC.7,84,[DEC B0]
IF(B0==1) Then
Low PORTC.1
High PORTC.2
EndIF

IF(B0==0) Then
Low PORTC.1
Low PORTC.2
EndIF


IF(B0==2) Then
Low PORTC.2
High PORTC.1
EndIF

GoTo start
End


I have to problems:
first i want to put some different levels of speed.
so i wrote the following program but it works as the first program!with numbers 3 and 4 i can't see reduce in speed.

INCLUDE "MODEDEFS.BAS"

B0 VAR WORD
B1 VAR WORD
Pause 500
SerOut2 PORTC.6,84,["Hi!",13,10]

start:

SerIn2 PORTC.7,84,[DEC B0]
IF(B0==1) Then
Low PORTC.1
High PORTC.2
EndIF

IF(B0==0) Then
Low PORTC.1
Low PORTC.2
EndIF


IF(B0==2) Then
Low PORTC.2
High PORTC.1
EndIF

IF (B0==3) Then
Low PORTC.2
PWM PORTC.1,128,10
EndIF


IF (B0==4) Then
Low PORTC.2
PWM PORTC.1,64,10
EndIF




GoTo start

End


SECONDLY i want the motor dc to run in the speed i say until a new press from my keyboard.do i need harware pwms? i need 4 (=2 motors). is there a pic with 4 pwms?
Thanx in advance

skimask
- 10th September 2008, 00:33
PWM is a 'blocking' command, synchronous, one-at-a-time...
While a PWM command is executing, nothing else can run. PWM is software driven, bit-banged, etc.

HPWM on the other hand is hardware driven. Set the registers and it runs without further program intervention, aside from changing the frequency and/or duty cycle.

mimmmis
- 12th September 2008, 13:00
thanx skimax!

with further investigation i have made a hardware pwm that sets in the background!
here is the code if anyone needs it

INCLUDE "MODEDEFS.BAS"


' ** Declare Variables **



Pause 500


start:

IF PORTB.7=1 Then

TRISC.2 = 0 ' Enable PORTC.2 (CCP1) as output for PWM
T2CON = %00000100 ' Set the Prescaler for 1:1, and turn on TMR2
PR2 = 255 ' See above calculation for the output period



CCPR1L = 128 ' MSB of the 10-bit duty value
CCP1CON.2 = 1 ' Turn on PWM Module one,
CCP1CON.3 = 1 ' by setting bits 2 and 3 of CCP1CON

EndIF


IF PORTB.7=0 Then
CCPR1L = 0
CCP1CON.2 = 1 ' Turn on PWM Module one,
CCP1CON.3 = 1 ' by setting bits 2 and 3 of CCP1CON
High PORTB.6
EndIF

GoTo start







End

skimask
- 12th September 2008, 13:36
thanx skimax!

with further investigation i have made a hardware pwm that sets in the background!
here is the code if anyone needs it
Or you could just use the HPWM command...