News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Problem with HPWM in 16F15213

Started by midali, Mar 12, 2025, 08:36 AM

Previous topic - Next topic

midali

Hi to all,

I can't set HPWM , is it a pps configuration problem ? I doing something wrong and I don't realize ?

   Device = 16F15213                              ' Tell the compiler what device to compile for
   Declare Xtal = 4                               ' Tell the compiler what frequency the device is operating at 4 MHz
        OSCCON = $20
        OSCEN = $40
        OSCFRQ = 2
Declare  CCP1_Pin   = PORTA.0         
Declare  CCP2_Pin   = PORTA.2       
       
PORTA  = %00000000
ANSELA = %00000000
TRISA  = %00000000

InitPPS()

Do:
    PORTA.1 = ~ PORTA.1
    DelayMS 100
    HPWM 1 ,127, 2000
    HPWM 2 ,127, 2000
Loop


Proc InitPPS()
PPS_UnLock()
   '-----------------
         CCP2PPS = 0x2  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
         RA2PPS = 0x02  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
       '-----------------
         CCP1PPS = 0x0  'RA0 CCP1(Pin 7) bi-directional to Module CCP1
         RA0PPS = 0x01  'RA0 CCP1(Pin 7) bi-directional to Module CCP1
PPS_Lock()
EndProc


'---------------------------------------------------------------------------------------------------
' Setup the configuration fuses for a PIC16F15213 or PIC16F15214 device to use its internal oscillator at 32MHz
' The OSC pins are set for standard I/O pins
'
    Config1 FEXTOSC_OFF,_                           ' External Oscillator not enabled
            RSTOSC_HFINTOSC_1MHZ,_                  ' HFINTOSC is 32MHz
            CLKOUTEN_OFF,_                          ' CLKOUT function is disabled. I/O function on RA4
            VDDAR_HI                                ' Internal analog systems are calibrated for operation between VDD = 2.3V to 5.5V

    Config2 MCLRE_INTMCLR,_                         ' MCLR pin is MCLR
            PWRTS_PWRT_OFF,_                        ' Power-up Timer is disabled
            WDTE_OFF,_                              ' WDT disabled. SEN is ignored
            BOREN_SBOREN,_                          ' Brown-out Reset enabled according to SBOREN bit
            BORV_LO,_                               ' Brown-out Reset Voltage (VBOR) set to 1.9V
            PPS1WAY_OFF,_                           ' The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)
            STVREN_ON                               ' Stack Overflow or Underflow will cause a reset

    Config4 BBSIZE_BB512,_                          ' Boot Block Size 512 words
            BBEN_OFF,_                              ' Boot Block is disabled
            SAFEN_OFF,_                             ' SAF is disabled
            WRTAPP_OFF,_                            ' Application Block is not write protected
            WRTB_OFF,_                              ' Boot Block is not write protected
            WRTC_OFF,_                              ' Configuration Registers are not write protected
            WRTSAF_OFF,_                            ' Storage Area Flash (SAF) is not write protected
            LVP_OFF                                 ' High Voltage on MCLR/Vpp must be used for programming

    Config5 CP_ON                                  ' User Program Flash Memory code protection is disabled

Thank you !

trastikata

Your PPS initialization looks odd.

top204

#2
The compiler sets up the PPS SFRs for the HPWM command, before the user program starts.

Below is a code listing for a PIC16F15213 device operating at 32MHz, and implementing the HPWM commands. It has not been fully tested because I do not have any PIC16F152xx devices, but the code looks OK.

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Operate the HPWM commands on a PIC16F15213 device (untested)
' Written for the Positron8 BASIC compiler by Les Johnson.
'
    Device = 16F15213                                       ' Tell the compiler what device to compile for
    Declare Xtal = 32                                       ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup CCP1 and CCP2
'
    Declare CCP1_Pin = PORTA.0
    Declare CCP2_Pin = PORTA.5
      
'---------------------------------------------------------------------------------------------------
' The main program starts here
'
Main:
    Setup()                                                 ' Setup the program and any peripherals

    HPWM 1, 127, 2000
    HPWM 2, 127, 2000
   
'---------------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_Int32MHz()                                          ' Setup the device to use the internal oscillator at 32MHz
EndProc

'---------------------------------------------------------------------------------------------------
' Setup the PIC16F15213 device to use the internal oscillator at 32MHz
' Input     : None
' Output    : None
' Notes     : May not need to be called when using the 'RSTOSC_HFINTOSC_32MHZ' config1 fuse
'
Proc Osc_Int32MHz()
    OSCEN = $00
    OSCFRQ = $05                                    ' FRQ is 32MHz
    OSCTUNE = $00
EndProc

'---------------------------------------------------------------------------------------------------
' Setup the configuration fuses for s PIC16F15213 or PIC16F15214 device to use its internal oscillator at 32MHz
' The OSC pins are set for standard I/O pins
'
    Config1 FEXTOSC_OFF,_                           ' External Oscillator not enabled
            RSTOSC_HFINTOSC_32MHZ,_                 ' HFINTOSC is 32MHz
            CLKOUTEN_OFF,_                          ' CLKOUT function is disabled; I/O function on RA4
            VDDAR_HI                                ' Internal analog systems are calibrated for operation between VDD = 2.3V - 5.5V

    Config2 MCLRE_EXTMCLR,_                         ' MCLR pin is MCLR
            PWRTS_PWRT_OFF,_                        ' Power-up Timer is disabled
            WDTE_OFF,_                              ' WDT disabled. SEN is ignored
            BOREN_SBOREN,_                          ' Brown-out Reset enabled according to SBOREN bit
            BORV_LO,_                               ' Brown-out Reset Voltage (VBOR) set to 1.9V
            PPS1WAY_OFF,_                           ' The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)
            STVREN_ON                               ' Stack Overflow or Underflow will cause a reset

    Config4 BBSIZE_BB512,_                          ' Boot Block Size 512 words
            BBEN_OFF,_                              ' Boot Block is disabled
            SAFEN_OFF,_                             ' SAF is disabled
            WRTAPP_OFF,_                            ' Application Block is not write-protected
            WRTB_OFF,_                              ' Boot Block is not write-protected
            WRTC_OFF,_                              ' Configuration Registers are not write-protected
            WRTSAF_OFF,_                            ' Storage Area Flash (SAF) is not write-protected
            LVP_OFF                                 ' High Voltage on MCLR/Vpp must be used for programming

    Config5 CP_OFF                                  ' User Program Flash Memory code protection is disabled

If you look at the assembler code listing (press the F2 button in the IDE), you will see the PPS setup for CCP1 and CCP2, based upon the CCP1_Pin and CCP2_Pin declares:

_compiler_main_start_
; CONFIGURE CCP1 PPS
    movlw _PPS_FN_CCP1
    movlb 0x3E
    movwf RA0PPS
; CONFIGURE CCP2 PPS
    movlw _PPS_FN_CCP2
    movwf RA5PPS
;---------------------------------------------
; START OF THE USER'S PROGRAM CODE
F1_SOF equ $ ; TEST_16F15213.BAS
    movlb 0x3E
    clrf ANSELA
    clrf SLRCONA
    clrf INLVLA
Main

midali

Quote from: top204 on Mar 12, 2025, 11:18 AMThe compiler sets up the PPS SFRs for the HPWM command, before the user program starts.

HPWM doesn't work Mr Les ! :(

Quote from: trastikata on Mar 12, 2025, 10:26 AMYour PPS initialization looks odd.

The most my writed codes looks odd :))

trastikata

Quote from: midali on Mar 12, 2025, 04:27 PMThe most my writed codes looks odd :))

Hi,

what I meant is I don't understand the purpose of this:

CCP2PPS = 0x2  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
RA2PPS = 0x02  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
'-----------------
CCP1PPS = 0x0  'RA0 CCP1(Pin 7) bi-directional to Module CCP1
RA0PPS = 0x01  'RA0 CCP1(Pin 7) bi-directional to Module CCP1

Can you comment on what do you intend by each line? Take a look at my post here:
https://protoncompiler.com/index.php?msg=19699

midali

Quote from: trastikata on Mar 12, 2025, 04:55 PM
Quote from: midali on Mar 12, 2025, 04:27 PMThe most my writed codes looks odd :))

Hi,

what I meant is I don't understand the purpose of this:

CCP2PPS = 0x2  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
RA2PPS = 0x02  'RA2 CCP2(Pin 5) bi-directional to Module CCP2
'-----------------
CCP1PPS = 0x0  'RA0 CCP1(Pin 7) bi-directional to Module CCP1
RA0PPS = 0x01  'RA0 CCP1(Pin 7) bi-directional to Module CCP1

Can you comment on what do you intend by each line? Take a look at my post here:
https://protoncompiler.com/index.php?msg=19699


The lines was generates with PPS Wizard from Positron Studio . First time I tried only with Declare CCP and doesn't work . Later I tried to manually PPS set .

trastikata

Quote from: midali on Mar 12, 2025, 05:15 PMThe lines was generates with PPS Wizard from Positron Studio . First time I tried only with Declare CCP and doesn't work . Later I tried to manually PPS set .

Take a look at the link in the previous post. I was trying to point you to the fact taht your PPS set-up is incorrect.

trastikata

Also I'd change the PORTA.1 = ~ PORTA.1 to LATA.1 = ~ LATA.1

midali

  Usual the compiler sets up the PPS SFRs for the HPWM command, but for this device something is wrong . Then I tried to set PPS in the code .
You are right Trastikata , my PPS initialisation is wrong.
  @top204 can you check if it really is a compiler problem ?
 

midali

Quote from: trastikata on Mar 12, 2025, 05:51 PMAlso I'd change the PORTA.1 = ~ PORTA.1 to LATA.1 = ~ LATA.1

The toggle on Porta.1 ( with a LED connected in hardware) is only for verify if oscillator work well . Usually I use LATA instead PORTA

trastikata

Quote from: midali on Mar 12, 2025, 05:53 PMUsual the compiler sets up the PPS SFRs for the HPWM command, but for this device something is wrong . Then I tried to set PPS in the code .

In case of doubt, instead of using the HPWM command, try setting the registers yourself for test purposes.

top204

The problem is, is that microchip cannot keep things the same, even when they "are" actually the same. So SFRs and SFR bits and positions change for no reason from device to device.

I do not have one of those devices, or one of those family of devices. But they might operate the same as some other enhanced 14-bit core devices, and I have created 10 library assembler code versions within the compiler for the different versions of the same thing, because microchip have changed things around so much.

So try the code with:

Declare HPWM_Type = 6

And if that does not work, try the code:

Declare HPWM_Type = 10

And if that does not work, I will be changing the PPI files for the 16F152xx devices to state that the HPWM command is not suitable for that device.

However, making procedures to setup and operate the CCP peripherals for PWM is relatively easy to do, but not as a generic compiler library.

midali

It's working with

Declare HPWM_Type = 10

If I declare CCP_pin on PORTA.2

    Declare CCP1_Pin = PORTA.0
    Declare CCP2_Pin = PORTA.2   
    Declare HPWM_Type = 10
       
    PORTA  = %00000000
    ANSELA = %00000000
    TRISA  = %00000000

Do:
    HPWM 1, 127, 2000
    HPWM 2, 235, 2000
Loop


PORT.5 does not change to digital pin, the PWM signal is still there, so CCP2 is on PORT.2 and PORT.5 at the same time

midali

Back with another tests..

The previous port does not change its state after programming (remaining PWM active). It reverts to digital pin only if I disconnect and reconnect the PIC power, so then work well .
But I don't understand why. In Config2 I changed MCLR to Input pin ,can that influence? This is the first time I've seen this behavior in a PIC

    Config2 MCLRE_INTMCLR,_                         ' MCLR pin is pin
            PWRTS_PWRT_OFF,_                        ' Power-up Timer is disabled
            WDTE_OFF,_                              ' WDT disabled. SEN is ignored
            BOREN_SBOREN,_                          ' Brown-out Reset enabled according to SBOREN bit
            BORV_LO,_                               ' Brown-out Reset Voltage (VBOR) set to 1.9V
            PPS1WAY_OFF,_                           ' The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)
            STVREN_ON                               ' Stack Overflow or Underflow will cause a reset

Pepe

Chat gpt
' Microcontroller configuration
Device 16F15213
Declare Xtal = 4 ' Oscillator frequency in MHz

' Set pins as output
TRISA.0 = 0 ' RA0 as output (PWM1)
TRISA.2 = 0 ' RA2 as output (PWM2)

' PPS mapping configuration for PWM
PPS_Unlock()
RA0PPS = $09 ' Assign RA0 to PWM1 (CCP1)
RA2PPS = $0A ' Assign RA2 to PWM2 (CCP2)
PPS_Lock()

' Configure CCP1 (PWM1) and CCP2 (PWM2)
CCP1CON = %00001100 ' Set CCP1 to PWM mode
CCP2CON = %00001100 ' Set CCP2 to PWM mode

' Configure Timer2 for 2 kHz PWM frequency
PR2 = 124         ' PR2 value for 2000 Hz PWM
T2CON = %00000110 ' Enable Timer2 with prescaler 1:16

' Set duty cycle for each PWM channel
CCPR1L = 62  ' 50% duty cycle on RA0
CCPR2L = 114 ' 92% duty cycle on RA2

' Infinite loop using Do...Loop
Do
    ' Infinite loop body
Loop

' Configuration fuses
' Config1: External Oscillator off, internal oscillator 1 MHz
Config1 FEXTOSC_OFF,_                          ' External Oscillator not enabled
        RSTOSC_HFINTOSC_1MHZ,_                 ' HFINTOSC is 1 MHz
        CLKOUTEN_OFF,_                         ' CLKOUT function is disabled. I/O function on RA4
        VDDAR_HI                               ' Internal analog systems are calibrated for operation between VDD = 2.3V to 5.5V

' Config2: MCLR enabled, WDT disabled, BOD enabled
Config2 MCLRE_INTMCLR,_                        ' MCLR pin is MCLR
        PWRTS_PWRT_OFF,_                       ' Power-up Timer is disabled
        WDTE_OFF,_                             ' Watchdog Timer disabled
        BOREN_SBOREN,_                         ' Brown-out Reset enabled according to SBOREN bit
        BORV_LO,_                              ' Brown-out Reset Voltage (VBOR) set to 1.9V
        PPS1WAY_OFF,_                          ' The PPSLOCKED bit can be set and cleared as needed (unlocking sequence is required)
        STVREN_ON                              ' Stack Overflow or Underflow will cause a reset

' Config4: Boot block and write protection settings
Config4 BBSIZE_BB512,_                        ' Boot Block Size 512 words
        BBEN_OFF,_                             ' Boot Block is disabled
        SAFEN_OFF,_                            ' SAF is disabled
        WRTAPP_OFF,_                           ' Application Block is not write protected
        WRTB_OFF,_                             ' Boot Block is not write protected
        WRTC_OFF,_                             ' Configuration Registers are not write protected
        WRTSAF_OFF,_                           ' Storage Area Flash (SAF) is not write protected
        LVP_OFF                                ' High Voltage on MCLR/Vpp must be used for programming

' Config5: Code Protection enabled
Config5 CP_ON                                  ' Enable code protection