News:

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

Main Menu

HPWM problem using the PIC12F1571

Started by laserline, Aug 04, 2022, 04:59 PM

Previous topic - Next topic

top204

#20
Many thanks people. It is always good to know that there are still good people in this dreadful world we now live in.

I have a tendancy to agree with Jonw. We have all seen companies "give freebies" for quite a few years until they know that there are so many people reliant on something, then they start to charge for it. I could make a list of them, but it would fill this post.

And with microchip's reputation, it is just a matter of time before they do that with the dreadful IDE and C compilers, regardless of whether they are based on open source programs or not. Heck.... They charge a huge price for some of "their" compilers, even though they are actually open source that have had a "few" changes made to them!

laserline

I've been a Proton user since 2004. I want to make it clear that I have great admiration for the work Les has done since then.
I just thought that having a function like "HPWM_16" would be a natural evolution for the language.
I just needed a simple operation of multiplying two analog values to generate a proportional PWM signal. Yes, sometimes we don't have time and would be nice to have something ready to solve the problem in a few minutes. Sorry if my message sounded rude or like a criticism, that was not my intention.

top204

#22
If microchip had kept stability with their microcontrollers, new commands would have been straightforward to add to the compilers. However, they have changed and changed and changed again, then gone back a few steps, then changed again, with all of their peripherals on their microcontrollers for many years now, even with devices of the same family and the same peripherals that have been in the devices for decades. So built-in commands constantly need changing for devices that have also changed their peripheral use, and the logistics of it are not possible for me as a sole developer and not having the funds to buy new devices constantly, or the free time to scour datasheets for changes and write new assembler code libraries for "that device only" and test. Or employ other people to do the "dirty" work, as larger companies do. etc...

That is why I finally took the months of work required to add procedures to the, once flat, 8-bit language. It sounds easy: "add procedures", but the method I used for Positron8 and Positron16 I had to conceive of myself and write the masses of code myself, with code and a concept that does not exist anywhere else, so it was not just "copy and paste this bit of code that another program or person has created here, and call it your own" :-) And the compiler has hundreds of thousands of lines of C++ code and thousands of functions in it that I have created from nothing, over time.

But procedures add the ability to have true libraries. So microchip can, and will, change what peripheral they like and the compilers still have the "ability" to support them. But it will take some knowledge and study and time from users to do so. And share what they have learned and created.



SeanG_65

#23
I have re-hashed your code Les to this;

'
'  /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\    \/\\\                                                /\\\          /\\\
'  \/\\\\\\\\\\\/        /\\\\\    /\\\\\\\\\\    /\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'    \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\    \/\\\        \/\\\        /\\\\\\\\\\
'      \/\\\    \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\    \/\\\ /\\  /\\\/////\\\
'      \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\  \//\\\\\\\\/\\
'        \///        \///    \/////    \//////////    \//////////      \/////        \/////    \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Experimental procedures for 16-bit PWM1 on a PIC12F1571 device
' Written by Les Johnson for the Positron8 BASIC compiler. Modified by someone who has no idea of what he is doing.
'
    Device = 12F1571                                ' Tell the compiler what device to compile for
    Declare Xtal = 16                              ' Tell the compiler what frequency the device will be operating at (in MHz)
'
' Create some defines for the PWM1 peripheral
'
$define PWM1_Start() PWM1CONbits_EN = 1            ' Start the PWM1 peripheral
$define PWM1_Stop() PWM1CONbits_EN = 0              ' Stop the PWM1 peripheral
$define PWM1_LoadBuffer() PWM1LDCONbits_LDA = 1    ' Load the phase of the PWM1 peripheral
'
' Create a variable to act as a parameter alias, to save RAM
'
    Dim PWM_wParam As Word

' Create more variables

    dim BlSet as word
    dim BlVal as word
    dim BlOldVal as word

    'trisa = %00001111

    symbol P1SEL APFCON.0
    set p1sel                          ' Use PORTA.5 as PWM1



'----------------------------------------------------------------------------
' The main program starts here
'
Main:
    IntOsc_16MHz()                      ' Set the device to use the internal oscillator at 16MHz

'----------------------------------------------------------------------------
' Setp up PWM1 Parameters
'
    PWM_wParam = $0000                  ' \
    pwm1_phase (pwm_wparam)            ' / Phase SFRs

    PWM_wParam = $3C00                  ' \
    pwm1_duty (pwm_wparam)              ' / Duty SFRs

    PWM_wParam = $3D08                  ' \
    pwm1_period (pwm_wparam)            ' / Period SFRs

    PWM_wParam = $6D6D                  ' \
    pwm1_offset (pwm_wparam)            ' / Offset SFRs

    PWM_wParam = $0000                  ' \
    pwm1_timer (pwm_wparam)            ' / Offset SFRs

    PWM1_Init()







    END                                ' STOP Execution
'----------------------------------------------------------


' Initialise the PWM1 peripheral
' Input    : None
' Output    : None
' Notes    : None
'
Proc PWM1_Init()
    PWM1INTE = $00
    PWM1INTF = $00
    PWM1CLKCON = $00            ' No_Prescalar
    PWM1LDCON = $00
    PWM1OFCON = $00
    PWM1CON = 0b01000000        ' Setup for an active high, standard PWM
    PWMENbits_PWM1EN_A = 1
    PWMLDbits_PWM1LDA_A = 1
    PWMOUTbits_PWM1OUT_A = 0
    PinOutput PORTA.5          ' Make the PWM1 pin an output
EndProc

'----------------------------------------------------------
' Alter the phase of the PWM1 peripheral
' Input    : pPhase holds the 16-bit phase value
' Output    : None
' Notes    : None
'
Proc PWM1_Phase(pPhase As PWM_wParam)
    PWM1PHH = pPhase.Byte1
    PWM1PHL = pPhase.Byte0
EndProc

'----------------------------------------------------------
' Alter the duty of the PWM1 peripheral
' Input    : pDuty holds the 16-bit duty value
' Output    : None
' Notes    : None
'
Proc PWM1_Duty(pDuty As PWM_wParam)
    PWM1DCH = pDuty.Byte1
    PWM1DCL = pDuty.Byte0
EndProc

'----------------------------------------------------------
' Alter the period of the PWM1 peripheral
' Input    : pPeriod holds the 16-bit period value
' Output    : None
' Notes    : None
'
Proc PWM1_Period(pPeriod As PWM_wParam)
    PWM1PRH = pPeriod.Byte1
    PWM1PRL = pPeriod.Byte0
EndProc

'----------------------------------------------------------
' Alter the offset of the PWM1 peripheral
' Input    : pPeriod holds the 16-bit offset value
' Output    : None
' Notes    : None
'
Proc PWM1_Offset(pOffset As PWM_wParam)
    PWM1OFH = pOffset.Byte1
    PWM1OFL = pOffset.Byte0
EndProc
'----------------------------------------------------------
' Alter the timer of the PWM1 peripheral
' Input    : pPTimer holds the 16-bit timer value
' Output    : None
' Notes    : None

Proc PWM1_Timer(pTimer As PWM_wParam)
    PWM1TMRH = pTimer.Byte1
    PWM1TMRL = pTimer.Byte0
endproc
'----------------------------------------------------------
' Setup the internal oscillator to operate at 16MHz
' Input    : None
' Output    : None
' Notes    : None
'
Proc IntOsc_16MHz()
    OSCCON = 0b01111010  ' Setup for INTOSC, SPLLEN disabled, and 16MHz HF operation
    OSCTUNE = $00
    BORCON = $00
EndProc

'----------------------------------------------------------------------------
' Set the fuses for an internal oscillator on a PIC12F1571 device
'
    Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, BOREN_OFF, CLKOUTEN_OFF
    Config2 PLLEN_OFF, LVP_OFF, BORV_LO, WRT_OFF, STVREN_OFF, LPBOREN_OFF


This now ouputs on PortA.5 as I wanted to use contiguous bits (PLEASE can we have a "nibble" - 4 bits command) and is there a way to define a symbol as multiple bits like Symbol VariableX PortA.0:3?.

The reason I hacked your code was that I need to vary the duty cycle of the PWM according to the bit pattern on the lower 4 bits of A. I now have the "baseline" for my code, after playing around with this for 3 days (yeah, I'm a VERY slow learner) but IT WORKS, and it will now do EXACTLY what I want once the port A reads have been added (pity I can't get thge ElseIf to work with it for some odd reason.

I've also looked at the assembler this produces and there is NO WAY I could write better code. This compiler really does work miracles in it's efficiency. I'm in awe of it, especially at the price, indeed even at three times the price!

THANKS LES!!