News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

PIC18F1330 PWM generator

Started by rick.curl, May 04, 2021, 01:08 AM

Previous topic - Next topic

rick.curl

Hi folks-
I've been struggling trying to get the PWM generator working in an 18F1330. The PWM setup seems to be a bit different than other chips I've used where the clock source comes from one of the timers.

This **should** be simple. All I need is a single free-running PWM output.  The frequency and duty cycle will be determined later.  At this point I'm just trying to get ANYTHING out of PWM1 (the only output I can use if I just need a single output).  When the basic settings didn't work I thought I might try the HPWM command, but the compiler tells me "Device does not contain any suitable hardware PWM channels...".

Would some kind soul please look over my code and tell me what I've messed up?

Device = 18F1330

Config_Start
  OSC = INTIO2 ;Internal oscillator, port function on RA6 and RA7
  FCMEN = OFF ;Fail-Safe Clock Monitor disabled
  IESO = OFF ;Oscillator Switchover mode disabled
  PWRT = On ;PWRT enabled
  BOR = BOHW ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 3 ;Minimum setting
  WDT = OFF ;WDT disabled (control is placed on the SWDTEN bit)
  WDTPS = 32768 ;1:32768
  PWMPIN = ON ;PWM outputs drive active states upon Reset
  LPOL = High ;PWM0, PWM2 and PWM4 are active-high (default)
  HPOL = High ;PWM1, PWM3 and PWM5 are active-high (default)
  FLTAMX = RA5 ;FLTA input is muxed onto RA5
  T1OSCMX = Low ;T1OSO/T1CKI pin resides on RB2
  MCLRE = OFF ;RA5 input pin enabled, MCLR pin disabled
  STVREN = On ;Reset on stack overflow/underflow enabled
  BBSIZ = BB256 ;256 Words (512 Bytes) Boot Block size
  XINST = OFF ;Instruction set extension and Indexed Addressing mode disabled
  Debug = OFF ;Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
  Cp0 = OFF ;Block 0 is not code-protected
  CP1 = OFF ;Block 1 is not code-protected
  CPB = OFF ;Boot Block is not code-protected
  CPD = OFF ;Data EEPROM is not code-protected
  WRT0 = OFF ;Block 0 is not write-protected
  WRT1 = OFF ;Block 1 is not write-protected
  WRTC = OFF ;Configuration registers are not write-protected
  WRTB = OFF ;Boot Block is not write-protected
  WRTD = OFF ;Data EEPROM is not write-protected
  EBTR0 = OFF ;Block 0 is not protected from table reads executed in other blocks
  EBTR1 = OFF ;Block 1 is not protected from table reads executed in other blocks
  EBTRB = OFF ;Boot Block is not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Xtal = 32
OSCCON =  %01110000  'Internal oscillator 8 Mhz
Nop
Nop
OSCTUNE = %01000000  'PLL enabled

'* Set port directions *
TRISA = %11000000
TRISB = %00000000 
LATA = 0
LATB = 0

'* Set up the PWM generator *
PTCON0 = %00000000  'no prescale, no postscale, free running
PTCON1 = %10000000  'Time base ON
PWMCON0 = %00010100  'PWM1 enabled
PDC0L = 0
PDC0H = 128
PTPERH = 128
PTPERL =  0
OVDCOND = %00000010    'PWM1 controlled by PWM time base
OVDCONS = %00000010  'PWM1 active


While 1=1
Wend

THANK YOU!
-Rick

Giuseppe

In reality the 18f1330 has HPWM .Could it be a compiler problem?

Stephen Moss

Quote from: rick.curl on May 04, 2021, 01:08 AMHi folks-
I've been struggling trying to get the PWM generator working in an 18F1330. The PWM setup seems to be a bit different than other chips I've used where the clock source comes from one of the timers.

This **should** be simple. All I need is a single free-running PWM output.  The frequency and duty cycle will be determined later.  At this point I'm just trying to get ANYTHING out of PWM1 (the only output I can use if I just need a single output).  When the basic settings didn't work I thought I might try the HPWM command, but the compiler tells me "Device does not contain any suitable hardware PWM channels...".

Would some kind soul please look over my code and tell me what I've messed up?
I may be wrong having only taken a quick scan of the datasheet but it looks to me like you have set the PWM period (PTPER Registers) and the Duty Cycle (PDC0 Registers) to the same value.
PTPERL = 0 & PDC0L = 0
PTPERH = 128 & PDC0H = 128
Therefore would not the modulation depth be 100% and so the output permanently high (assuming high is the active state)?


Quote from: Giuseppe on May 04, 2021, 08:44 AMIn reality the 18f1330 has HPWM .Could it be a compiler problem?
I may be wrong but I think if the way Microchip have setup the device is not compatible with the method the compiler uses that it issue a warning

rick.curl

Quote from: Stephen Moss on May 04, 2021, 09:25 AMI may be wrong having only taken a quick scan of the datasheet but it looks to me like you have set the PWM period (PTPER Registers) and the Duty Cycle (PDC0 Registers) to the same value.
PTPERL = 0 & PDC0L = 0
PTPERH = 128 & PDC0H = 128
Therefore would not the modulation depth be 100% and so the output permanently high (assuming high is the active state)?
I was thinking that if the PTPER register was all zeros the output would stay low, if it's all ones the output would stay high, and anywhere in between gives a proportionate duty cycle. Just in case, I changed PTPERH to 64 but the output pin is still staying low.   

QuoteI may be wrong but I think if the way Microchip have setup the device is not compatible with the method the compiler uses that it issue a warning
I agree.

I did try writing simple code to toggle the RB0 (PWM1) pin just to make sure I could control the pin, and that is working OK.

I've got to be overlooking something obvious.....

-Rick

rick.curl

Oops- I said I set PTPERH to 64.  Actually it was PDC0H.  I left PTPERH at 128.
Still no joy.

-Rick