News:

Let's find out together what makes a PIC Tick!

Main Menu

18f47k42 and hpwm broke

Started by dionbrewington, Sep 25, 2021, 03:08 AM

Previous topic - Next topic

dionbrewington

I have a lcd with the backlighting connected up to portc.2.  I cannot get hpwm to work with it.  I can remove hpwm from the code and turn the pin on/off fine.  I can also take the exact same code, change the declare to a 18f47q83, and flash that chip and hpwm works.  Seems like something isn't getting configured right with the 18f47k42.  Here is the code that will display the text but the backlight receives no power:

Declare Optimiser_Level = 3  'change to 3 and test later


;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F47K42

$ifndef _EEADR 'necessary to fix symbol not previously defined EEADR error
    Symbol EEADR = NVMADRL
$endif

Config_Start
  FEXTOSC = OFF    ;Oscillator not enabled
  RSTOSC = HFINTOSC_64MHZ    ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
  CLKOUTEN = OFF    ;CLKOUT function is disabled
  PR1WAY = OFF    ;PRLOCKED bit can be set and cleared repeatedly
  CSWEN = On    ;Writing to NOSC and NDIV is allowed
  FCMEN = On    ;Fail-Safe Clock Monitor enabled
  MCLRE = INTMCLR    ;If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR
  PWRTS = PWRT_64    ;PWRT set at 64ms
  MVECEN = OFF    ;Interrupt contoller does not use vector table to prioritze interrupts
  IVT1WAY = OFF    ;IVTLOCKED bit can be cleared and set repeatedly
  LPBOREN = On    ;Low-Power BOR enabled
  BOREN = SBORDIS    ;Brown-out Reset enabled , SBOREN bit is ignored
  BORV = VBOR_2P85    ;Brown-out Reset Voltage (VBOR) set to 2.8V
  ZCD = OFF    ;ZCD module is disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
  PPS1WAY = OFF    ;PPSLOCKED bit can be set and cleared repeatedly (subject to the unlock sequence)
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = OFF    ;HV on MCLR/VPP must be used for programming
  XINST = OFF    ;Extended Instruction Set and Indexed Addressing Mode disabled
  WDTCPS = WDTCPS_31    ;Divider ratio 1:65536; software control of WDTPS
  WDTE = OFF    ;WDT Disabled; SWDTEN is ignored
  WDTCWS = WDTCWS_7    ;window always open (100%); software control; keyed access not required
  WDTCCS = SC    ;Software Control
  BBSIZE = BBSIZE_512    ;Boot Block size is 512 words
  BBEN = OFF    ;Boot block disabled
  SAFEN = OFF    ;SAF disabled
  Debug = OFF    ;Background Debugger disabled
  WRTB = OFF    ;Boot Block not Write protected
  WRTC = OFF    ;Configuration registers not Write protected
  WRTD = OFF    ;Data EEPROM not Write protected
  WRTSAF = OFF    ;SAF not Write Protected
  WRTAPP = OFF    ;Application Block not write protected
  Cp = OFF    ;PFM and Data EEPROM code protection disabled
Config_End
;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

'Declare Xtal 64
Declare Xtal 32

OSCCON1=%00000000
'OSCFRQ=%00001000 '64mhz
OSCFRQ=%00000110 '32mhz
oscen.6=1  'HFINTOSC is explicitly enabled, operating as specified by OSCFRQ
WPUB=%11111111

INLVLA=0           'set all to TTL
INLVLB=%00000001      'set b.0 to ST style input
INLVLC=0        'set all to TTL
INLVLD=0        'set all to TTL
INLVLE=0         'set all to TTL
SLRCONA=0
SLRCONB=0
SLRCONC=0
SLRCOND=0
SLRCONE=0
ANSELA=0  'set to digital IO and disable analog input
ANSELB=0
ANSELC=0
ANSELD=0
ANSELE=0
'-----end extra config for 18F47Q43-----------------


Declare Hserial1_Baud = 9600
Declare HRSOut1_Pin = PORTC.6

Declare TRIS_Reverse = On
Declare Watchdog = off
Declare FSR_CONTEXT_SAVE = On
All_Digital=true

Declare LCD_Lines 4
Declare LCD_ENPin PORTD.2
Declare LCD_RSPin PORTD.3
Declare LCD_DTPin PORTD.4

Declare CCP1_Pin PORTC.2

TRISA=%10111010
TRISB=%10101111
TRISC=%10110000
TRISD=%00000000
TRISE=%11101001


delayms 1000

HPWM 1,200,2000
cls
print at 1,1,"hpwm test"


main:
delayms 1000
goto main

Any ideas?  Thanks!

dionbrewington

After much trial and error I was able to get hpwm to work by adding two lines.  Here is what it takes:

'----configure hpwm
Declare CCP1_Pin PORTC.2
T2CLK = 0x01; //clock source = Fosc/4, Fosc = 32MHz = 8MHz*4xPLL  must add this
t2con.7=1  'must add this
HPWM 1,200,2000
'----end configure hpwm

Not sure if the t2clk and t2con are supposed to be handled by proton but this is how I got it working.

John Drew

Perseverance pays off. Well done Dion.
John