PDA

View Full Version : Using PWM built in Module 16F628



earltyso
- 29th August 2007, 16:16
Hey there,
I would like to use the internal PWM module in my PIC16F628 chip instead of the PWM software statement. I have heard this will allow me to use a seperate clock speed (rc=3?) so I can run a motor speed control while monitoring several other switches with my Xtal. Where I am really stuck is... How to set up the loops such that I am really doing 2 things at once.... , checking conditions of switches and fetching POT value for PWM on a seperate clock speed. I would appreciate any help.
thanks!

'general idea, no coding as I do not want to be mocked just yet
example....

loop 1: 'internal RC constant or something
' fetch POT value...store as B0
' use value for PWM output to motor



loop2: 'external 4Mhz crystal
'if then else statements looking at 5 switch values
'branching to subroutines

mat janssen
- 29th August 2007, 20:28
Yust use the hpwm instruction and put in the define part:
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3

nothing else more !!

sayzer
- 30th August 2007, 08:34
Yust use the hpwm instruction and put in the define part:
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3

nothing else more !!

For PICs that have onboard PWM modules, like 628, CCP DEFINEs are not required.


Reading POT value and using it to drive a motor, try the following demo code.




@ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F628, MCLR_OFF
@ DEVICE PIC16F628, WDT_OFF
@ DEVICE PIC16F628, PROTECT_OFF
@ DEVICE PIC16F628, BOD_OFF
@ DEVICE PIC16F628, CPD_OFF

TRISA = 255 'All pins are input.
TRISB = 0 'All pins are output.

'=====Timer2 settings - 8Bit ====================
T2CON = %00000000 ' Required for accurate timing for RPM.
INTCON.7 = 0 ' Disable interrupts.

'===================PWM Part===============
CMCON = 7 ' PortA = digital I/O
VRCON = 0 ' Disable A/D Voltage reference.
PR2 = 255 ' PWM frequency.
CCP1CON = %00001100 ' PWM Mode selected.
T2CON = %00000100 ' Timer2 ON + 1:1 prescale
CCPR1L = 0 ' Set PWM Duty-Cycle

PotPin VAR PORTB.0

Begin:

POT PotPin , 255, CCPR1L 'Read Pot value and put on PWM Module. From 0 to 255. From zero to full speed.
'... have your switch readings here.
'......
'........




Goto Begin


END

earltyso
- 30th August 2007, 15:33
thanks for the help

RodSTAR
- 5th October 2007, 18:46
For PICs that have onboard PWM modules, like 628, CCP DEFINEs are not required.


Reading POT value and using it to drive a motor, try the following demo code.




@ DEVICE PIC16F628, INTRC_OSC_NOCLKOUT
@ DEVICE PIC16F628, MCLR_OFF
@ DEVICE PIC16F628, WDT_OFF
@ DEVICE PIC16F628, PROTECT_OFF
@ DEVICE PIC16F628, BOD_OFF
@ DEVICE PIC16F628, CPD_OFF

TRISA = 255 'All pins are input.
TRISB = 0 'All pins are output.

'=====Timer2 settings - 8Bit ====================
T2CON = %00000000 ' Required for accurate timing for RPM.
INTCON.7 = 0 ' Disable interrupts.

'===================PWM Part===============
CMCON = 7 ' PortA = digital I/O
VRCON = 0 ' Disable A/D Voltage reference.
PR2 = 255 ' PWM frequency.
CCP1CON = %00001100 ' PWM Mode selected.
T2CON = %00000100 ' Timer2 ON + 1:1 prescale
CCPR1L = 0 ' Set PWM Duty-Cycle

PotPin VAR PORTB.0

Begin:

POT PotPin , 255, CCPR1L 'Read Pot value and put on PWM Module. From 0 to 255. From zero to full speed.
'... have your switch readings here.
'......
'........




Goto Begin


END


hi Sayzer;
looking at your really cool sample code i think instead of
"PotPin VAR PORTB.0"

should be
"PotPin VAR PORTA.0"
because as PIC's datasheet says, analog inputs are only in PORTA, and also you configured PORTA as inputs and PORTB as outputs.


anyway i found that your POT & CCPR1L line is amazing !

mister_e
- 6th October 2007, 16:34
yeah but CMCON=7 disable all analog feature, there's no analog-to-digital converter on this one, and POT don't even need any analog stuff to work, it just measure the RC constant or whatever else useless/inacurate method.

sayzer
- 6th October 2007, 18:35
...

anyway i found that your POT & CCPR1L line is amazing !

Thanks.

If you like to use a PIC with on board ADC and PWM modules, like 16F877 or 12F683, then you can also use a single line as below.




ADCIN 0, CCPR1L

'This will read Analog channel 0
'and put on PWM register right away.





If you like to use F628 with POT command, then make sure that the pot is not connected to "input only" pins. Pin must be a bi-directional pin.

RodSTAR
- 7th October 2007, 18:28
good morning sayzer, mister_e,

About the PWM thing, I'm clear and OK,

but... reading your last posts I don't know if understood correctly:

-> what I understood is that I can measure the value of a potentiometer into any I/O pin without any necessary A/D conversion feature? In other words, in a 16F628A: I could use 16 potentiometers?? wow!! that would be crazyly cool!

(sorry my english)

nomad
- 9th October 2007, 06:40
-> what I understood is that I can measure the value of a potentiometer into any I/O pin without any necessary A/D conversion feature? In other words, in a 16F628A: I could use 16 potentiometers?? wow!! that would be crazyly cool!


From what I've read, that's sort of correct. it can use any bidirectional digital pin.

It can not be used on an open collector output pin, or an input only pin.