News:

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

Main Menu

simple HPWM routine

Started by m.kaviani, Jan 06, 2022, 08:45 AM

Previous topic - Next topic

m.kaviani

hi all,
simple HPWM module operation.
the result on the scope was clear. use pb1 and pb2 to change the duty cycle. 0..200

Device = 18F66K80

Config_Start
  RETEN = OFF    ;Ultra low-power regulator is Disabled (Controlled by REGSLP bit)
  INTOSCSEL = Low    ;LF-INTOSC in Low-power mode during Sleep
  SOSCSEL = Dig    ;High Power SOSC circuit selected
  XINST = Off    ;Enabled
  FOSC = HS1    ;HIGH SPEED oscillator
  PLLCFG = On    ;Enabled
  FCMEN = OFF    ;Disabled
  IESO = OFF    ;Disabled
  PWRTEN = OFF    ;Disabled
  BOREN = SBORDIS    ;Enabled in hardware, SBOREN disabled
  BORV = 3    ;1.8V
  BORPWR = ZPBORMV    ;ZPBORMV instead of BORMV is selected
  WDTEN = OFF    ;WDT disabled
  WDTPS = 1048576    ;1:1048576
  CANMX = PORTB    ;ECAN TX and RX pins are located on RB2 and RB3, respectively
  T0CKMX = PORTB    ;Timer0 gets its clock input from the RB5/T0CKI pin on 64-pin packages
  T3CKMX = PORTG    ;Timer3 gets its clock input from the RG2/T3CKI pin on 64-pin packages
  MSSPMSK = MSK7    ;7 Bit address masking mode
  MCLRE = On    ;MCLR Enabled, RE3 Disabled
  STVREN = On    ;Enabled
  BBSIZ = BB1K    ;2K word Boot Block size
  Cp0 = OFF    ;Disabled
  CP1 = OFF    ;Disabled
  CP2 = OFF    ;Disabled
  CP3 = OFF    ;Disabled
  CPB = OFF    ;Disabled
  CPD = OFF    ;Disabled
  WRT0 = OFF    ;Disabled
  WRT1 = OFF    ;Disabled
  WRT2 = OFF    ;Disabled
  WRT3 = OFF    ;Disabled
  WRTC = OFF    ;Disabled
  WRTB = OFF    ;Disabled
  WRTD = OFF    ;Disabled
  EBTR0 = OFF    ;Disabled
  EBTR1 = OFF    ;Disabled
  EBTR2 = OFF    ;Disabled
  EBTR3 = OFF    ;Disabled
  EBTRB = OFF    ;Disabled
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
    Declare Xtal 32            ' 8 Mhz x 4xPLL
    Declare Optimiser_Level = 2
    Declare Dead_Code_Remove = 1

    Include "hpwm_2.bas"
    Include "lcd_dev_board.bas"
    Include "i2c_dev_board.bas"
   
    Clear ADCON0.0         ' TURN OF THE A/D CONVERTER
    ADCON1 = 0
    ADCON2 = 0
 
    Clear ANCON0.5
    Clear TRISC.1
    Clear TRISE.5      ' SET AS OUTPUT
    Clear TRISB.5      ' SET AS OUTPUT
    Clear TRISE.4      ' SET AS OUTPUT
    Clear TRISC.3

    Set   TRISC.0      ' SET AS INPUT
    Set   TRISF.5      ' SET AS INPUT
    Set   TRISF.4      ' SET AS INPUT

    Symbol LED1          =  PORTE.5
    Symbol LED2          =  PORTB.5
    Symbol LED3          =  PORTE.4

    Symbol PB1           =  PORTC.0  ' PUSH BOTTON 1
    Symbol PB2           =  PORTF.5  ' PUSH BOTTON 2
    Symbol PB3           =  PORTF.4  ' PUSH BOTTON 3
   
    Symbol DAC_WRITE        = %11000010 ' digital to analog write command
    Symbol DAC_COMMAND      = %01000000 ' 64 HEX

    Bcf LED1 : Bcf LED2 : Bcf LED3

    Dim B1         As Word
    Dim B2         As Word
    Dim temp       As Word
    Clrf B1 : Clrf B2
    Cls
MAIN:

    If PB1 = 1 And B1 < 200 Then
        Inc B1
        DelayMS 30
    EndIf
   
    If PB2 = 1 And B1 > 0 Then
        Dec B1
        DelayMS 30
    EndIf
   
    If PB3 = 1 Then
        Bsf TRISB.5   ' shut down pwm pin like smps feedback
    Else
        Bcf TRISB.5   ' allow output to run
    EndIf

    Nop
    If temp <> B1 Then      ' change the parameter of pwm if B1 value had been changed
    B2 = B1
    B2 = B2 >> 2            ' pwm module is 10 bit, low byte is B1 <9..2> bits
                            ' shift righ 2 bit
    CCPR2L = B2.LowByte
    CCPR5L = B2.LowByte
   
    ccp2con_bit5 = B1.1    ' 2 LSBs bit <1.0> of CCPRXL
    ccp2con_bit4 = B1.0
   
    ccp5con_bit5 = B1.1
    ccp5con_bit4 = B1.0
   
    Bsf timer2_on          ' run timer2 to start pwm module
    temp = B1
    EndIf
   
GoTo MAIN
Stop

CCP2CON = %00001100  ' make ccp2 for pwm (disable capture & compare)
    CCP5CON = %00001100  ' make ccp5 for pwm (disable capture & compare)
   
    CCPTMRS = 0          ' chosse timer1/2 insted of timer3/4 for PWM operation
   
    Clear TRISC.2      ' makse as out put , HPWM channel 2
    Clear TRISB.5      ' makse as out put , HPWM channel 5

    Symbol CCP2_lowbyte  = CCPR2L
    Symbol CCP2_highbyte = CCPR2H
    Symbol ccp2con_bit4  = CCP2CON.4
    Symbol ccp2con_bit5  = CCP2CON.5
   
    Symbol CCP5_lowbyte  = CCPR5L
    Symbol CCP5_highbyte = CCPR5H
    Symbol ccp5con_bit4  = CCP5CON.4
    Symbol ccp5con_bit5  = CCP5CON.5
   
    Symbol timer2_preiod = PR2      ' pwm frequncy
    Symbol timer2_on     = T2CON.2
    Symbol timer2_ps_0   = T2CON.0  ' timer 2 presclae bit 0
    Symbol timer2_ps_1   = T2CON.1  ' timer 2 prescale bit 1
   
    timer2_preiod = $3f   ' set PR2 frequency for 156KHz  with 40Mhz but we have 32Mhz so 125KHz
    Bcf timer2_ps_0       ' timer2 prescale is 1
    Bcf timer2_ps_1       ' timer2 prescale is 1

trastikata

Hi m.kaviani,

I notice the negative overshoot spike - is that measured on a FET gate?

top204

#2
A nice piece of coding. Many thanks for adding it to the forum.

One thing.... Be careful using the raw assembler mnemonics in the BASIC code. They do not manipulate RAM banks, and not all SFRs (Special Function Registers) are in access RAM anymore on most 18F devices. Instead of using the mnemonics Bcf or Bsf, use the commands Clear or Set or = 0 or = 1. The same with the Clrf mnemonic. The BASIC commands create the same underlying assembler mnemonics, but also manipulate RAM banks if required.

m.kaviani

Quote from: trastikata on Jan 06, 2022, 09:40 AMHi m.kaviani,

I notice the negative overshoot spike - is that measured on a FET gate?
Hi, trastikata,
yes, it was a problem because of the scope bad quality probe. I changed the probe with quality one and the negative overshoot is gone.