News:

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

Main Menu

Can't get pwm to run on 12f1501 VSM

Started by JonW, Jun 11, 2021, 04:50 PM

Previous topic - Next topic

JonW

Really struggling getting the PWM to run on the 12F1501 in ISIS

Am I missing something in setup?

'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 08/06/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 12F1501

Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, BOREN_ON, CLKOUTEN_OFF
Config2 WRT_OFF, STVREN_ON, BORV_LO, LPBOR_OFF, LVP_ON

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Xtal = 16
Declare RSIn_Timeout = 100 ' Timeout after 100mS seconds
Declare Serial_Baud = 9600
Declare RSIn_Mode = TRUE
Declare RSOut_Mode = TRUE
Declare RSIn_Pin = PORTA.4
Declare RSOut_Pin = PORTA.1
Declare Adin_Tad = FRC ' RC oscillator chosen for the ADC
Declare Adin_Stime = 50 ' Allow 50us sample time
Declare HPWM1_Pin = PORTA.2

Dim ADC As      PORTA.0
Dim NCO As      PORTA.1
Dim UP As       PORTA.2
Dim DOWN As     PORTA.3
Dim RX As       PORTA.4
Dim LED As      PORTA.5



;****************************************************************************
;   
;                                  INITIALISE CHIP
;
;****************************************************************************
       INTCON.7 = 0             ' Disable all interrupts

' Oscillator Block Enable

       OSCCON = %01111000       '16M CLOCK
       TRISA = %00010001   
       ANSELA = %00000001       '
       PWM1CON = %11000000     
       'NCO1CON = %11000001
       'NCO1CLK = %01100000     
       
       
     
       GoTo MAIN

       
MAIN:
       HPWM 1,127,10000
RUN:
        RsOut "HELLO"
        Toggle LED
        DelayMS 10
        GoTo RUN
       
       End

JonW

I think there is an issues with the compiler but I maybe wrong.  I compared the assembly generated with another known working assembled code and the PWM is set up as it should.

here is the generated ASM and the call to _hpwm just writes to pp4H and branches to the exit and returns thus no PWM setup occurs.

    org 4
__hpwm_
    movwf PP4H
    bra __hpwm_exit_
__hpwm_CalcFrequency_
    clrf PP0
    movlw 9
    movwf PP0H
    movlw 61
    movwf PP2
    clrf PP2H
    call __divide_int_u1616_
    movlw 252
    andwf T2CON,F
    movf PP0H,W
    btfsc STATUS,2
    bra __hpwm_start_timer_
    bsf T2CON,PP_T2CKPS0
    call __hpwm_DivideByFour_
    andlw 252
    btfsc STATUS,2
    bra __hpwm_start_timer_
    bcf T2CON,PP_T2CKPS0
    bsf T2CON,PP_T2CKPS1
    call __hpwm_DivideByFour_
    andlw 240
    btfsc STATUS,2
    bra __hpwm_start_timer_
    bsf T2CON,PP_T2CKPS0
    call __hpwm_DivideByFour_
__hpwm_start_timer_
    bsf T2CON,PP_TMR2ON
    decf PP0,W
    movwf PR2
    incf GEN,W
    btfss STATUS,2
    bra __hpwm_notfullon_
    movlw 255
    movwf PP2H
    movwf PP2
    bra __hpwm_loadCCPCONX_
__hpwm_notfullon_
    clrf PP2H
    movf GEN,W
    movwf PP2
    clrf PP3H
    movwf PP3
    incf PR2,W
    btfsc STATUS,Z
    bra __hpwm_loadCCPCONX_
    clrf PP1H
    movwf PP1
    call __multiply_u1616_
__hpwm_loadCCPCONX_
    movf PP2H,W
    movwi INDF0++
    moviw ++INDF0
    andlw 207
    btfsc PP2,7
    bsf WREG,5
    btfsc PP2,6
    bsf WREG,4
    iorlw 12
    movwf INDF0
__hpwm_exit_
    movlb 0
    return

Jon

Giuseppe

I hope an example of Hpwm on the tried and tested 12f1501 will be useful to you.
I set the internal oscillator to 8Mhz

Device = 12F1501

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Config1 FOSC_INTOSC, WDTE_ON, PWRTE_ON, MCLRE_OFF, CP_ON, BOREN_ON, CLKOUTEN_OFF
Config2 WRT_OFF, STVREN_OFF, BORV_LO, LPBOR_OFF, LVP_OFF


Declare CCP1_Pin PORTA.2 ' Pin Hpwm

Xtal 8
OSCCON = %01110000      '
OPTION_REG = %00000111  '
INTCON = %00100000      '
WDTCON = %00010101      '

'---Set Porte----------------------------------------------------
LATA  = %00000000
TRISA = %00001000 '
WPUA = %00001000  '
CM1CON0.7 = 0     '
Clear ANSELA     '
'--------------------------------------------------------------------
Dim x As Byte
Dim freq As Word

Symbol led = PORTA.1

x = 0
freq = 1000
Low led

'--------------------------------------------------------------
main:

Clrwdt

Toggle led

For x = 0 To 28
 HPWM 1,127,freq      'frequency from 1000 Hz to 6600 Hz duty 50%
 DelayMS 10
 freq = freq + 200
Next x
DelayMS 10
freq = 1000
 
GoTo main


top204

#3
The PIC12F1501 device does not have any CCP SFRs, it has the PWM SFRs instead, and I had forgotten to alter the type of PWM for that device within its PPI file.

In the BASIC program, add the line:

Declare HPWM_Type = 2

This will direct the compiler to use another HPWM library subroutine for that particular device. For example:
'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
    Device = 12F1501
    Declare Xtal = 8

    Declare HPWM1_Pin = PORTA.2
    Declare HPWM_Type = 2

    Dim bDuty As Byte
    Dim wFreq As Word

'--------------------------------------------------------------
Main:
    Do
        For wFreq = 1000 To 3000 Step 100
            For bDuty = 0 To 255 Step 5
                HPWM 1, bDuty, wFreq
                DelayMS 5
            Next
        Next
    Loop

I have now added the HPWM_Type to the device's PPI file for the next update. Thanks for finding the anomaly Jon. With the hundreds of .ppi and .def files I have to create, and all the differences in devices, it is hard to keep up with things on my own. I had added the HPWM_TYPE directive to the 12F1503 to 12F1509 device PPI files, but forgot to add it to the 12F1501 device PPI file.

I do not have a PIC12F1501 device to test upon, and cannot, even nearly, afford to buy Proteus, but based upon the datasheet and other similar devices I do have, it should work as expected!

JonW

Many thanks Les, appreciate the effort it takes to create the compiler.  I will give it a whirl

Jon