PDA

View Full Version : Pwm settings for 18f47k40



longpole001
- 17th December 2019, 14:17
hi guys have anyone got set an example for this on the 18f47k40

looking to put PWM4 using timer 2 on port c.1


tryied using

define HPWM4_TIMER 2 ' HPWM4 TO USE TIMER 2 - USED FOR HUB_OE DIMMING CONTROL
RC1PPS = $08 ' SET PWM4 TO PORTC.1
tris portc.1 =0
PMD1.2 = 0 ' allow timer 2
PMD3.3 =0 , allow PWM4

hpwm 4 ,PWM_Duty ,1000 'CPP4 Wduty , freq

mpgmike
- 18th December 2019, 21:09
The "HPWM" Command only works with CCPx Modules, not PWMx. This should get you functional:



#CONFIG
CONFIG PPS1WAY = OFF ;PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
#ENDCONFIG


Value VAR BYTE


Init:
; -=- NOTE: This is merely example code. There are no SFRs for OSC and other Important Registers!! -=-


TRISC.2 = 1 ;Initialize PWM4 Output as an Input, will change later
ANSELC.2 = 0 ;Make PWM4 Output Digital
PIE4.1 = 0 ;Ensure TMR2 Interrupt is Disabled
PIR4.1 = 0 ;Clear TMR2 Interrupt Flag

; -=- NOTE: Section 20 of the Data Sheet Covers Pre/Post Scalers for Timer 2 -=-
T2CON = %00100000 ;1:4 Prescaler, note ON = 0
T2CON.7 = 1 ;Turn Timer 2 ON in Separate Operation
WHILE PIR4.1 = 0 ;Wait Until Timer2 is Running
WEND
PIR4.1 = 0 ;Timer 2 is Now Running
TRISC.2 = 0 ;Now Change RC2 to Output
PPSLOCK = $55 ;Peripheral Pin Select
PPSLOCK = $AA ; Unlock Sequence
PPSLOCK.0 = 0 ; PPS = UNLOCKED
RC2PPS = $08 ;PWM4 Output on RC2
; -=- NOTE: You can Lock PPS when you're done (Optional, but Recommended)
PPSLOCK = $55 ;Peripheral Pin Select
PPSLOCK = $AA ; Unlock Sequence
PPSLOCK.0 = 1 ; PPS = LOCKED
PWM4DCL = 0
PWM4DCH = 100 ;THIS IS YOUR DUTY CYCLE REGISTER, 0 >> 255 (8-bit)
PWM4CON = $80 ;Turn PWM4 ON

Main:
DO
'GOSUB Get_PWM4 ; -=- Determine Value for PWM4 DC%
PWM4DCH = Value
LOOP

longpole001
- 19th December 2019, 01:35
thanks mike