Log in

View Full Version : Hwpw



l_gaminde
- 20th June 2025, 17:26
Im running a few chips 16f876a at 20 mhz, 18f26k22 10mhz with 4x speed so 40mhz

Even using timer 1 and timer 2 with two different ports, portc1 and portc2 you can not get two different PW is this correct

I have also noticed at times one pulse is half the duty cycle other times it can be changed this could be because i used the same timer for both

define OSC 40

OSCCON2.7 = 1 ' SYSTEM CLOCK 1=4XPLL 0= OSCILLATOR OTHER THAN 4XPLL
TRISA = %11111010
TRISB = %11111100
ADCON1 = %0110
ADCON0 = %000

T0CON = %00000000
INTCON = %00000000
INTCON2 = %00000000

LED VAR PORTA.5 ' Assign name "LED" to PORTA.5
HIGH LED
PAUSE 2000
LOW led
PWM PORTA.5, 64, 6000

DEFINE CCP1_REG PORTC ' USED FOR HWPW
DEFINE CCP1_BIT 2 ' USED FOR HWPW PORTC PIN 2
DEFINE HPWM2_TIMER1 ' DEFINE TIMER USE FOR PWM

DEFINE CCP2_REG PORTC ' USED FOR HWPW
DEFINE CCP2_BIT 1 ' USED FOR HWPW PORTC PIN 1
DEFINE HPWM1_TIMER2 ' DEFINE TIMER USE FOR PWM

HPWM 1, 128, 5000 ' OUTPUT PORTC.2
'HPWM 2, 64, 5000 ' OUTPUT PORTC.1

' HPWM 1, 128, 39000 ' HIGHEST HPWM @ 50% DUTY 78.06 khz
' HPWM 1, 128, 30000 ' HPWM @ 50% DUTY 60.19 khz
' HPWM 1, 128, 20000 ' HPWM @ 50% DUTY 39.96 khz
' HPWM 1, 128, 10000 ' HPWM @ 50% DUTY 19.98 khz
' HPWM 1, 128, 5000 ' HPWM @ 50% DUTY 9.99 khz
' HPWM 1, 128, 2441 ' LOWEST HPWM @ 50% DUTY 4.88 khz

richard
- 21st June 2025, 07:07
a pic18f26k22 can have 5 different pwm streams with 3 different frequencies all with individual duty cycles, old clunker chips like 16f876a are much less capable of course

here is 3 streams 3 frequencies
i find the hpwm command pretty inflexible and seldom use it [the clock defines are not correct in mho] , pwm is not difficult to setup manually





'************************************************* ***************
'* Name : PWM demo.pbp *
'* Author : Richard *
'* Notice : *
'* : *
'* Date : 21/12/2024 *
'* Version : 1.0 *
'* Notes : TMR2 CCP4 10K, TMR4 CCP5 12.5K, TMR6 CCP1 8K *
'* :18f26k22 @64Mhz *
'************************************************* ***************




#CONFIG
CONFIG FOSC = INTIO67
CONFIG PLLCFG = ON
CONFIG PRICLKEN = ON
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRTEN = ON
CONFIG BOREN = SBORDIS
CONFIG BORV = 190
CONFIG WDTEN = ON
CONFIG WDTPS = 32768
CONFIG CCP2MX = PORTC1
CONFIG PBADEN = OFF
CONFIG CCP3MX = PORTB5
CONFIG T3CMX = PORTC0
CONFIG HFOFST = ON
CONFIG P2BMX = PORTB5
CONFIG MCLRE = EXTMCLR
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG
DEFINE OSC 64
ANSELB=0
ANSELC=0
ANSELA=0
trisa=% 11101111
trisb=% 11111110
trisc=% 11111001
OSCCON=$70
OSCTUNE.6=1

T2CON=6 ;PRESCALE 16 TMR ON
T4CON=6 ;PRESCALE 16 TMR ON
T6CON=6 ;PRESCALE 16 TMR ON
PR2=$63 ;10 KHZ
PR4=$4F ;12.5 KHZ
PR6=$7C ;8 KHZ
CCP4CON = $0C ;PWM MODE + DUTY LSB 2 BITS
CCPR4L = $32 ;DUTY MSB 8 BITS
CCP5CON = $0C ;PWM MODE + DUTY LSB 2 BITS
CCPR5L = $27 ;DUTY MSB 8 BITS
CCP1CON = $0C ;PWM MODE + DUTY LSB 2 BITS
CCPR1L = $3E ;DUTY MSB 8 BITS
CCPTMRS1 = 4 ;TMR2 CCP4, TMR4 CCP5
CCPTMRS0 = 2 ;TMR6 CCP1


main:
goto main

l_gaminde
- 23rd June 2025, 22:34
I ran your program and according to the scope they all are running the same freq. with different pulse width

richard
- 24th June 2025, 00:38
I ran your program and according to the scope they all are running the same freq. with different pulse width

not really they all are running the same pulse width with different freq.

l_gaminde
- 24th June 2025, 00:49
Well, I don't know what to say. I get all the same frequency with different pulse width.
Even the program shows different pulse width.
I even went back to the pick basic plus and i'm running for with four different timers, and they all run the same frequency, and if you set it for a different frequency, they shut down pulse width, I can control

richard
- 24th June 2025, 01:07
Even the program shows different pulse width.

what do you mean ,that makes little sense



Well, I don't know what to say. I get all the same frequency with different pulse width.

my code works perfectly on the simulator and the actual hardware
post your code and schematic .

l_gaminde
- 24th June 2025, 02:25
I ran the code that you posted.

l_gaminde
- 24th June 2025, 02:40
Walking and talking on the phone does not work well.
I ran your program my scope has measure function all 3 channels showed the same frequency. But the duty cycle was the only difference. I used pbp setting ccp1, ccp2, ccp3 and ccp5 port c1, c2, and port b0, b5 there is also no control of individual frequency only duty cycle

richard
- 24th June 2025, 03:03
If you don't get three frequencies with 50% duty cycle then you are not using my code ,chip or schema.


Post your code and schema

l_gaminde
- 24th June 2025, 04:38
I will rerun tomorrow and post scope pictures. Its
definitely possible. I did something wrong. I am only running at 40 MHz. At this time, I have 16 mhz crystal's coming.

richard
- 24th June 2025, 06:55
I have 16 mhz crystal's coming.

totally not needed for this simple test, it uses internal osc

l_gaminde
- 24th June 2025, 16:57
RICHARD
I reloaded your program exactly and it gave me
12.54khz with a duty of 48.75 PW on channel A1
8.02khz with a duty of 49.61 on channel C2
10.02khz with a duty of 50.01 on channel B0

You were absolutely correct and it works perfectly
do you have any documentation of the settings and what to change for different freq and duty cycles Im just getting back into this after years of being away

l_gaminde
- 24th June 2025, 19:11
RICHARD I need to change porta4 hwpw to another port i tried ccp3con and ccpr3l changed the ccptmrs0 but nothing works I have something else on a4 on the boards im using

richard
- 25th June 2025, 00:06
do you have any documentation of the settings and what to change for different freq and duty cycles
https://www.picbasic.co.uk/forum/showthread.php/4994
https://www.picbasic.co.uk/forum/showthread.php/26615
or use MCC


RICHARD I need to change porta4 hwpw to another port i tried ccp3con and ccpr3l changed the ccptmrs0 but nothing works I have something else on a4 on the boards im using

if you need porta.4 for another purpose then you cannot use CCP5

post the code you have tried

l_gaminde
- 25th June 2025, 18:50
first question how do you get the code box to post into


OK I was able to change the port from port a4 to port b5 now I need to figure out this

CCP4CON = $0C ; PWM MODE + DUTY LSB 2 BITS ' PORT B0
CCPR4L =%110010 ; DUTY MSB 8 BITS ' $32

it does not seem to be very straight forward I will look at the posts you sent yesterday and see if I'm able to do the math, shifting and what ever else it takes.
I did change the numbering to BIN so when looking in the manual I could line things up with the proper bits (don't get mad)



#CONFIG
ERRORLEVEL -306 ; turn off crossing page boundary message
CONFIG FOSC = INTIO67
CONFIG PLLCFG = ON
CONFIG PRICLKEN = ON
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRTEN = ON
CONFIG BOREN = SBORDIS
CONFIG BORV = 190
CONFIG WDTEN = ON
CONFIG WDTPS = 32768
CONFIG CCP2MX = PORTC1
CONFIG PBADEN = OFF
CONFIG CCP3MX = PORTB5
CONFIG T3CMX = PORTC0
CONFIG HFOFST = ON
CONFIG P2BMX = PORTB5
CONFIG MCLRE = EXTMCLR
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG

DEFINE OSC 64

ANSELB=0
ANSELC=0
ANSELA=0

trisa = %11011111
trisb = %11011110
trisc = %11111001


OSCCON = %1110000 ; $70
OSCTUNE.6 = 1

T2CON = %0110 ; PRESCALE 16 TMR ON PORT B0 6
T4CON = %0110 ; PRESCALE 16 TMR ON PORT B5 6
T6CON = %0110 ; PRESCALE 16 TMR ON PORT C2 6

PR2 = %1100011 ; 10 KHZ PORT B0 $63
PR4 = %1001111 ; 12.5 KHZ PORT B5 $4F
PR6 = %1111100 ; 8 KHZ PORT C2 $7C

CCP4CON = $0C ; PWM MODE + DUTY LSB 2 BITS ' PORT B0
CCPR4L =%110010 ; DUTY MSB 8 BITS ' $32

CCP3CON = $0C ; PWM MODE + DUTY LSB 2 BITS ' PORT B5
CCPR3L = %10111 ; DUTY MSB 8 BITS ' $27

CCP1CON = $0C ; PWM MODE + DUTY LSB 2 BITS ' PORT C2
CCPR1L = %111110 ; DUTY MSB 8 BITS ' $3E

CCPTMRS1 = %00 : TIMER2 = CCP4, ' TIMER4 CCP5 (USED WITH BELOW)
CCPTMRS0 = %1011010 ; TIMER6 = CCP1, TIMER4 = CCP3

richard
- 26th June 2025, 02:27
first question how do you get the code box to post into


https://www.picbasic.co.uk/forum/showthread.php/19594



I did change the numbering to BIN so when looking in the manual I could line things up with the proper bits
it can be informative for some registers for most its just unnecessary clutter

its certainly not helped you get this correct

CCPTMRS0 = %1011010 ; TIMER6 = CCP1, TIMER4 = CCP3

setting C2TSEL<1:0>: CCP2 Timer Selection bits to %11 "reserved" is not a good practice
CCPTMRS0 = %01000010 ; TIMER6 = CCP1, TIMER4 = CCP3 is preferable

l_gaminde
- 26th June 2025, 03:55
Yes the 11 i wasn't sure how to deal with it. So I put it in there if it didn't work. I would have pulled it right out, but it did work in this case. Remember, it's been 15 years since I've done any programming. I wasn't sure how to deal with some of this. So what button do you use to put code in to the forum messages.

l_gaminde
- 26th June 2025, 04:00
Nevermind, I found it a pound tag.

l_gaminde
- 27th June 2025, 01:29
Where can I get this program

mister e's pic multi-calc

richard
- 27th June 2025, 01:44
https://www.picbasic.co.uk/forum/showthread.php/4994

Last post zip attachment