PDA

View Full Version : 10-bit HPWM



Peter Oors
- 15th October 2004, 11:09
Hello,
How do I setup a 16F819 for 10-bit HPWM?
8-bit HPWM is no problem, see (a part of) the code.

@ DEVICE PIC16F819,INTRC_OSC_NOCLKOUT
@ DEVICE CCPMX_ON ' RB3 PWM
@ DEVICE PIC16F819,MCLR_OFF
DEFINE OSC 8 ' not in use for 4MHz
OSCCON = $70 ' 70=8 MHz, 60=4 MHz
DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
ADCON1 = %00001110 '0=analog, 1,2,3,4=digital
TRISA = %11111111
TRISB = %00000000
PORTB = %00000000
n var byte ' for 10 bit it should be a word
HPwm 3,n,1000

mister_e
- 15th October 2004, 14:35
the only things that spring to my mind is:

1. PIC16F819 have only one PWM channel so use:
HPwm 1,n,1000

2. maybe manual configuration will have to be done for CCP1CON and CCPR1L registers

or may try to set PR2 manually

see page 71 of datasheet section 9.3.3 SETUP FOR PWM OPERATION. sometimes PicBasic will not do everything we have to setup.

datasheet there:
http://ww1.microchip.com/downloads/en/DeviceDoc/39598d.pdf

App note AN594 use CCP
http://ww1.microchip.com/downloads/en/AppNotes/00594b.pdf

And this beauty CCP tricks 'tip
http://ww1.microchip.com/downloads/en/DeviceDoc/41214a.pdf


these should place you on the way.

many regards

Peter Oors
- 17th October 2004, 08:41
At first, I am not familiar with assembler, PicBasic Pro I can work with.
It seems it's not complete yet.
I think it doesn't "see" 2 bits.
At portb I use a led to see if it works.
The led's intensity increases 4 times like a sawtooth, decreases the same way, 10-bits in 4 times 8 bits.
Would you be so kind to help me with the final part?
OSCCON = $60 ' 4 MHz
CCP1CON = %00111100
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
ADCON1 = %00000111
TRISA = %11111111
TRISB = %00000000
PORTB = %00000000
n VAR WORD
Clear
BEGIN:
For n=1 TO 1023
HPwm 1,n,1000
Pause 4
Next
Pause 2000
For n=1023 TO 1 STEP -1
HPwm 1,n,1000
Pause 4
Next
Pause 2000
GoTo BEGIN

Melanie
- 17th October 2004, 11:19
Always worthwhile doing a search...

http://www.picbasic.co.uk/forum/showthread.php?s=&threadid=170

mister_e
- 18th October 2004, 02:03
That could'nt be more clear than this :)

Peter Oors
- 20th October 2004, 10:37
Hello,

Thanks to all, it works fine now.
We made the next loop with a led, just for testing!

@ DEVICE PIC16F819,INTRC_OSC_NOCLKOUT
@ DEVICE CCPMX_ON ' RB3 PWM
@ DEVICE PIC16F819,MCLR_OFF
OSCCON =$60 ' 4 MHz
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
PR2 =249 ' Set PR2 to get 1kHz out
T2CON =%00000100 ' Values for PWM period
CCP1CON =%00001100 ' Set PWM Mode
ADCON1 =%00001110 ' 0=analog, 1,2,3,4=digital
TRISA =%11111111
TRISB =%00000000
PORTB =%00000000
n VAR WORD
'=====
BEGIN:
For n=1 TO 1023 STEP 4
GoSub loop
Pause 10
Next
For n=1023 TO 1 STEP -4
GoSub loop
Pause 10
Next
GoTo BEGIN
loop:
T2CON=%00000101 ' Turn on Timer2, Prescaler=4
CCP1CON.4 =n.0 ' Store n to registers as
CCP1CON.5 =n.1 ' a 10-bit word
CCPR1L =n>>2
Return

Thanks all,
Peter