PDA

View Full Version : Misbehaving HPWM on 16F1827



J. Mark Wolf
- 20th March 2011, 22:47
The following code generates the proper frequency provided I specify 980 or higher.

If I specify 976 the PWM frequency suddenly divides by 4 to 244Hz.

If I specify 400, the output frequency again divides by 4 and generates 100Hz.

What gives?




DEFINE OSC 4
HPWM 1,127,977 '980 = 980Hz, 977 = 980Hz, 976 = 244Hz,, 400 = 100Hz

Darrel Taylor
- 21st March 2011, 01:13
Download the latest patch.
http://melabs.com/support/patches.htm

The 16F1's have an extra prescaler on Timer2 that wasn't accounted for.
It was fixed in the "2.60B" patch.
Current patch is "C".

J. Mark Wolf
- 21st March 2011, 11:51
Thanks Darrel.

I shouldv'e thought of that!

Do the other PWM channels now work with the PBP direct commands at differing frequencies, or do I still need to "poke" the registers for the other PWM channels? (can't try it until I get home from work, 8 long hours from now).

Darrel Taylor
- 21st March 2011, 13:53
The HPWM command now works with up to 10 CCP modules, on those chips that have that many.

It also recognizes what timer has been assigned to each CCP and uses it to adjust the frequency for that and any other CCP module that is also assigned to it.

The timer assignments must be done manually though, as every family seems to do it differently.

J. Mark Wolf
- 22nd March 2011, 01:14
Sure enough Darrel!

I set up the CCPTMRS register to assign the timers, assigned the desired pins (code below) and away it went!

Now I've got two differing PWM frequencies and the DAC working great!

Thanks.

Somebody pinch me!



CCPTMRS = %00100100 ' CCP1=TMR2,CCP3=TMR6 in PWM Mode
Main:
'
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3 'pin 9
'DEFINE CCP2_REG PORTA
'DEFINE CCP2_BIT 7 'pin 16
DEFINE CCP3_REG PORTA '
DEFINE CCP3_BIT 3 'pin 2
'DEFINE CCP4_REG PORTA
'DEFINE CCP4_BIT 4 'pin 3
'
HPWM 1,127,400 'apparently uses timer2 by default
'HPWM 2,100,100
HPWM 3,127,1000 'assigned to tmr6
'HPWM 4,50,1000
'
goto dac_test

Ioannis
- 22nd March 2011, 21:40
I am trying to use the PWM module directly and have 10 bit resolution, but the output is LOW with this:



DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3
ccptmrs=0
pr2=249
ccp1con=$0C
trisb.3=0
...
main_loop:
...
ccpr1l=duty.lowbyte
...
goto main_loop


If I use the PBP command



HPWM 1,duty,2000


then output follows the duty value.

Any ideas?

Ioannis

Ioannis
- 22nd March 2011, 22:07
OK, I got it.

Forgot to add the T2CON register.

Ioannis

J. Mark Wolf
- 20th January 2012, 00:32
16F1827 code below will allow only 2 of the available 4 PWM channels to run simultaneously. Need 3 Simultaneous PWM channels, all at different frequencies.

Channel 1 & 3 will work provided channel 4 is not enabled.
Channel 2 won't run ever.
Channel 3 always works.
Channel 3 & 4 will work but breaks channel 1.

Compiler is PBPX 3.0.1.4

Sorry couldn't figure out how to wrap "code tags" around the code block.

Can anyone spot what I'm doing wrong?


'************************************************* ******************************
'
DEFINE OSC 4 'inform compiler of "intended" oscillator freq
'
OSCCON = %01101010 '4Mhz internal (%01110000 for 8Mhz)
DACCON0 = %11100000 'DAC config reg
DACCON1 = %00000000 '
ANSELA = %00000000 'All digital. A/D disabled
ANSELB = %00000000
APFCON1 = %00000001 'Alternate pin function config
FVRCON = %00000000
ADCON0 = %00000001
ADCON1 = %10010000
INTCON = %00000000
'
APFCON0 = %00001000 'CCP1=RB3,CCP2=RA7 (CCP3=RA3, CCP4=RA4)
CCPTMRS = %00100100 'CCP1=TMR2,CCP2=TMR4,CCP3=TMR6 in PWM Mode
'
pr2 = 77 '400Hz
t2con = 32
pr4 = 68 '900Hz
t4con = 16
pr6 = 77 '1600Hz
t6con = 8
'
TRISA = %00100001 '
TRISB = %11000010
'
'************************************************* ******************************
'
' HPWM configs
'
'************************************************* ******************************
'
DEFINE CCP1_REG PORTB
DEFINE CCP1_BIT 3 'pin 9
DEFINE CCP2_REG PORTA '
DEFINE CCP2_BIT 7 'pin 16
DEFINE CCP3_REG PORTA '
DEFINE CCP3_BIT 3 'pin 2
DEFINE CCP4_REG PORTA '
DEFINE CCP4_BIT 4 'pin 3
'
HPWM 1,127,400 'works if CCP4 not enabled
HPWM 2,127,1600 'CCP2 doesn't work at all
HPWM 3,127,900 'works always
'HPWM 4,127,1000 'CCP4 works but breaks CCP1
STOP
'
end