PWM versus enhanced PWM, oposite polarity output?

15 views
Skip to first unread message

vsurducan

unread,
Aug 1, 2021, 4:14:12 AM8/1/21
to jal...@googlegroups.com
Hi all,
PIC18F2550, 28pin PIC device has only the PWM implemented, the enhanced PWM is available on 40/44pin devices only. However, If I want on a 28pin PIC  two PWMs running at the same frequency, with the same duty-cycle, how can I generate one active high and one active low PWM signals without using any external inverter? No deadband between the output signals is required.
Any experience is appreciated.
thank you!

Rob CJ

unread,
Aug 1, 2021, 7:17:11 AM8/1/21
to jal...@googlegroups.com
Hi Vasile,

Not sure if I understood you correctly. If you want the PWM signals to be exactly the same but only inverted you can use the bridge mode for one of the PWM's that is when one output goes high the other goes low. 




Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens vsurducan <vsur...@gmail.com>
Verzonden: zondag 1 augustus 2021 10:14
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: [jallib] PWM versus enhanced PWM, oposite polarity output?
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4qvHSZrsUtoF_1GcDrNS2_mup_hc5ZbYOuQu4uGw5TYuAQ%40mail.gmail.com.

vsurducan

unread,
Aug 1, 2021, 8:58:41 AM8/1/21
to jal...@googlegroups.com
Thx Rob, the bridge mode (enhanced CCP) works only for a 40 pin device, I have a 28 pin device on my board. Enhanced CCP is available only on large size microcontroller. :(   Almost all 28 pin microcontrollers suffer from this issue (PIC18F25k50, etc.)

Since I need two signals with opposite phases, 50% duty cycle, whose frequency should be programmable between 1KHz and 2KHz I think they can be generated in compare mode...or perhaps in software.  I also need to trigger an ADC read during the high level of those pulses from time to time.

Oliver Seitz

unread,
Aug 1, 2021, 10:48:56 AM8/1/21
to jal...@googlegroups.com
Hi!

Well... almost all 28pin? Chips with suffixes like k20, k22, k50, k80, j11, j13, j50 and j80 do have ECCP, chips with suffixes like q40, q41, q43, q83, q84, k42, k83 and numerous more have CWG and/or CLC modules, which both allow the generation of those signals using a simple PWM.


With the PIC18F2550, I only have one idea... Externally feed the pmw signal back to pins A0 and A1, set the CVRef module to somewhere in the middle, set comparator mode 0b110, and set one of the CxINV bits. Then you have the symmetric signals on C1out and C2out. It's not the most elegant solution, I admit...

Greets,
Kiste



Am Sonntag, 1. August 2021, 14:58:43 MESZ hat vsurducan <vsur...@gmail.com> Folgendes geschrieben:





Thx Rob, the bridge mode (enhanced CCP) works only for a 40 pin device, I have a 28 pin device on my board. Enhanced CCP is available only on large size microcontroller. :(   Almost all 28 pin microcontrollers suffer from this issue (PIC18F25k50, etc.)
Since I need two signals with opposite phases, 50% duty cycle, whose frequency should be programmable between 1KHz and 2KHz I think they can be generated in compare mode...or perhaps in software.  I also need to trigger an ADC read during the high level of those pulses from time to time.

On Sun, Aug 1, 2021 at 2:17 PM Rob CJ <rob...@hotmail.com> wrote:
>  
>  
>  Hi Vasile,
>
>  
>
>
>  Not sure if I understood you correctly. If you want the PWM signals to be exactly the same but only inverted you can use the bridge mode for one of the PWM's that is when one output goes high the other goes low. 
>
>  
>
>
>  
>
>
>  
>
>
>  
>
>
>  Kind regards,
>
>  
>
>
>  Rob
>
>  
>
>
>
> ________________________________
> Van: jal...@googlegroups.com <jal...@googlegroups.com> namens vsurducan <vsur...@gmail.com>
> Verzonden: zondag 1 augustus 2021 10:14
> Aan: jal...@googlegroups.com <jal...@googlegroups.com>
> Onderwerp: [jallib] PWM versus enhanced PWM, oposite polarity output?
>  
>
>
>  
>  
> Hi all,
>
> PIC18F2550, 28pin PIC device has only the PWM implemented, the enhanced PWM is available on 40/44pin devices only. However, If I want on a 28pin PIC  two PWMs running at the same frequency, with the same duty-cycle, how can I generate one active high and one active low PWM signals without using any external inverter? No deadband between the output signals is required.
>
> Any experience is appreciated.
>
> thank you!
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "jallib" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
> To view this discussion on the web visit  https://groups.google.com/d/msgid/jallib/CAM%2Bj4qvHSZrsUtoF_1GcDrNS2_mup_hc5ZbYOuQu4uGw5TYuAQ%40mail.gmail.com.
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "jallib" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/AM0PR07MB624142E76C9FF412EBE22FC5E6EE9%40AM0PR07MB6241.eurprd07.prod.outlook.com.
>


--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4qstLOMHMLtJcLLEfywQ_%2BTYOWzEUCkLtghwrskyk%3DO%3DAg%40mail.gmail.com.

vsurducan

unread,
Aug 2, 2021, 12:38:34 AM8/2/21
to jal...@googlegroups.com
Hi Kiste, as far as I read my version of k50 (PIC18F25k50) does not have ECCP it in 28 pin, I have to stick on my available stock of microcontrollers during the inexistence at this point (Farnell, TME) of too many options to buy immediately PIC microcontrollers.
Thanks for your solution, I think I will do it in interrupts using TMR0 for a short timebase, count it for the needed frequency and toggle the output pins.
Yes, indeed a CLC would be helpful with the standard CCP...
best wishes,

vsurducan

unread,
Aug 2, 2021, 1:18:07 AM8/2/21
to jal...@googlegroups.com
Kiste, thanks for opening my eyes, PIC18F25k50 will do my job, it has indeed the ECCP, I didn't read correctly the notes 4 and 5 at pin descriptions. Thank you again.

Oliver Seitz

unread,
Aug 2, 2021, 1:19:17 AM8/2/21
to jal...@googlegroups.com
Hi,

if I download the datasheet (PIC18F2X_45K50-30000684B.pdf), it says the chip has one ECCP and one CCP module. The output of CCP2, the simple one, is on pin 12 (pin 9 on QFN package) and can be relocated to pin 24 (pin 21 on QFN). The outputs A,B,C and D of CCP1 are on pins 13, 23, 22 and 25 (10, 20, 19 and 22 on QFN). The Errata doesn't mention any problems with ECCP. 

2kHz is not that fast, so you can use an interrupt of course. I try to avoid that, as interrupts can introduce some jitter (about +/-2Tcy) and if that causes a problem, it may be hard to trace.

Greets,
Kiste


Am Montag, 2. August 2021, 06:38:36 MESZ hat vsurducan <vsur...@gmail.com> Folgendes geschrieben:
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4quga9W%2B1gNP1UFBJDR8cJM04eh24ecHRR8%2BA5uOcnC4Yw%40mail.gmail.com.

Oliver Seitz

unread,
Aug 2, 2021, 1:38:51 AM8/2/21
to jal...@googlegroups.com
You're very welcome :-)

I want to correct myself about interrupts and jitter: I wrote about +/-2Tcy. That's the hardware part. But then there's JALs interrupt handling: If, say, serial_hw_int_cts is transmitting a string and has just finished one byte shortly before the timer overflows, servicing the timer interrupt will be delayed until serial_hw_int_cts has finished preparing the next byte, which can take quite some time. Then, depending on the order of the service routines which you don't have control over, the timer interrupt can be served immediately afterwards, or, some more service routiunes are called, control may even be given back to the main program to execute one machine instruction just to then go back to the list of interrupt service routines until finally servicing the timer interrupt. 

That means, if there's more than one interrupt in use, servicing can have a jitter of indefinite amount, even to the point when interrupt events are skipped.

Greets,
Kiste



Am Montag, 2. August 2021, 07:18:09 MESZ hat vsurducan <vsur...@gmail.com> Folgendes geschrieben:
https://groups.google.com/d/msgid/jallib/CAM%2Bj4qvV6WzGGmX3qBpyPbGszEk_n7Mc5onbMrtePRbxf2WJ0Q%40mail.gmail.com
.

vsurducan

unread,
Aug 3, 2021, 12:42:17 AM8/3/21
to jal...@googlegroups.com
Thx, I know the jitter issue caused by the compiler delays, in the past I was using my own ISR based on Bresenham code. It creates a negligible jitter down to 10uS, I think at 48MHz I can go below 1uS without huge issues. Fortunately data has to be sent rarely somewhere between 1s and 10s.For that I want to use the usb_serial library.
The major problem I have is that usb_serial does not work reliably for me with PIC18F25k50, with all good help Rob sustained.  In my tests the library works reliably on PIC18F2550 or PIC18F26j50 but not with PIC18F25k50. I've so far tested SO and SS packages and two manufacturing lots thinking it is a microcontroller issue. It seems it is not.

Oliver Seitz

unread,
Aug 3, 2021, 2:23:38 AM8/3/21
to jal...@googlegroups.com
I've given up on USB a long time ago... If I have to connect to a computer which has no serial port, I'm using a raspberry pi as uart to network interface.

Greets,
Kiste

Am Dienstag, 3. August 2021, 06:42:19 MESZ hat vsurducan <vsur...@gmail.com> Folgendes geschrieben:





Thx, I know the jitter issue caused by the compiler delays, in the past I was using my own ISR based on Bresenham code. It creates a negligible jitter down to 10uS, I think at 48MHz I can go below 1uS without huge issues. Fortunately data has to be sent rarely somewhere between 1s and 10s.For that I want to use the usb_serial library.The major problem I have is that usb_serial does not work reliably for me with PIC18F25k50, with all good help Rob sustained.  In my tests the library works reliably on PIC18F2550 or PIC18F26j50 but not with PIC18F25k50. I've so far tested SO and SS packages and two manufacturing lots thinking it is a microcontroller issue. It seems it is not.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4qu%2BMue5z%3DB8nrvbdYmV_59N6RPMQVi85WafLs%2BZUNPR3A%40mail.gmail.com.

vasi vasi

unread,
Aug 5, 2021, 2:24:52 AM8/5/21
to jal...@googlegroups.com
How PIC16F1454 fares on USB? I would use that as interface between PC and any microcontroller.




--
Vasi

vasi vasi

unread,
Aug 5, 2021, 2:31:08 AM8/5/21
to jal...@googlegroups.com
As development board, I would step up to PIC24H/dsPIC33F with Firewing Basic that generates C code that passes it to an internal (read it, an XC16 compiled from sources) compiler to produce a highly optimized code.
--
Vasi

vsurducan

unread,
Aug 6, 2021, 2:04:26 AM8/6/21
to jal...@googlegroups.com
Hi Vasi, for me it would be a bit weird to use a separate PIC for RS232/USB interface, Rob did it too on his Instructables. I'm using lower cost interfaces for that which does need speed configuration.
The algorithm I found to use the usbserial library without huge struggle ( mostly when using complex interrupts) is to make the software fully functionally using USART and only after that migrating to the usbserial. So far so good.
Still, the jallib usb serial does not allow full speed, it has to be rewritten for that and I'm not quite sure it will be able to handle at least 1Mbps (not 12Mbps as the PIC interface is capable).

About migrating to PIC24 or dsPIC,  I'm not a fan of already made boards. My boards are designed for specific projects containing everything it needs...

However I would like to look at that firewing basic if you can provide a specific link. Which would be the benefit against MPLAB C for example?

best wishes,


vasi vasi

unread,
Aug 6, 2021, 3:39:32 AM8/6/21
to jal...@googlegroups.com
Hi Mr. Surducan,

The link to Firewing Basic is this: http://firewing.info/

As for advantages... it uses BASIC language, very close to VB.Net syntax, the resulting .hex file is as  optimized as in a fully professional XC16, no limits. The author, David Barker (that makes also Swordfish Basic for PIC18, a commercial compiler this time) compiled the sources of XC16 (an old version 1.11) and removed the limitations. If you install a certain package, you can instruct the Firewing IDE to use an external XC16 toolchain (if you need a more recent version for a newer PIC). The default installation comes with only a couple of PIC24 micros, but you can manually install all the micros supported by the XC16 1.11 version.

It supports also some of the PIC32 micros if uses the internal XC32 compiler (with full optimization) and the full PIC32 family if you set it to use the external XC32 compiler (if there is one installed).

And some PIC18 (for these, it generates assembler directly, and if you want full PIC18 support, you have to buy Swordfish Basic)

The normal user does not need to know C, he works in Basic and gets a hex file. In background, the basic is converted to C and from the IDE option, you can choose to keep also the C source. By default the C source is deleted after compilation.

It comes with some nice libraries and in the forums there is also an ECAN library for PiC24

Recently, the site was down and some of the zip files in the forums are corrupted, but if asked, users will upload them again.

This language is Windows only but it can be run under Linux using Wine.

NOTE: Mainly, this language was made to support the Arduino like Firewing boards, that have the Arduino bootloader installed, but you can use it also for applications that do not need a bootloader - there are some tips in the forum. The following program can be used as a template for an application without bootloader if support for the PICs mentioned inside is installed correctly:

' ======  NO BOOTLOADER APPLICATION SKELETON  =======================
' ___________________________________________________________________
' As firewing16 device, you can use any of the following:
' 24HJ32GP302, 24HJ32GP304
' 24HJ64GP202, 24HJ64GP204, 24HJ64GP502, 24HJ64GP504
' 24HJ128GP202, 24HJ128GP204, 24HJ128GP502, 24HJ128GP504
' 33FJ32GP302, 33FJ32GP304
' 33FJ64GP202, 33FJ64GP204, 33FJ64GP802, 33FJ64GP804
' 33FJ128GP202, 33FJ128GP204, 33FJ128GP802, 33FJ128GP804
device = 33FJ128GP802
' clock speed in MHz, you can use any of the following:
' 80, 64, 32, 16, 8
clock = 80            

' config statements for pic24hj micro without bootloader,
' pic that can be programmed with pickit 2 programmer (pk2cmd)
config  FBS = {BWRP_WRPROTECT_OFF}
#if _device in (24HJ32GP302, 24HJ32GP304, 33FJ32GP302, 33FJ32GP304)
#else
config  FSS = {SWRP_WRPROTECT_OFF}
#endif
config  FGS = {GWRP_OFF}
config  FOSCSEL = {FNOSC_FRCPLL, IESO_OFF}
config  FOSC = {POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSECMD}
config  FWDT = {WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF}
config  FPOR = {FPWRT_PWR128, ALTI2C_OFF}
config  FICD = {ICS_PGD1, JTAGEN_OFF}

macro SetSysClock()
   #if _device in (24HJ32GP302,24HJ32GP304,24HJ64GP202,24HJ64GP204,24HJ64GP502,24HJ64GP504,24HJ128GP202,24HJ128GP204,24HJ128GP502,24HJ128GP504,33FJ32GP302,33FJ32GP304,33FJ64GP202,33FJ64GP204,33FJ64GP802,33FJ64GP804,33FJ128GP202,33FJ128GP204,33FJ128GP802,33FJ128GP804)
   #else
      checkparam(etError, "unsupported firewing16 micro")
   #endif

   ' Fosc = Fin * (M/N1*N2))
   ' CLKDIVbits.FRCDIV = 0    (CLKDIV[10:8]) FRC/1= 7.3728MHz
   ' CLKDIVbits.PLLPOST = N2  (CLKDIV[7:6])  N2: 00=2, 01=4, 11=8
   ' CLKDIVbits.PLLPRE = 2-N1 (CLKDIV[4:0])  N1 = 2-33
   ' PLLFBDbits.PLLDIV = M-2
   #if (_clock = 80)      ' 40 MIPS
      const FRCDIV = 0
      const PLLPOST = 0
      const PLLPRE = 1
      const PLLDIV = 63   ' 39.94
      WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
      CLKDIV = WREG4
      PLLFBD = PLLDIV              
   #elseif (_clock = 64)  ' 32 MIPS                  
      const FRCDIV = 0
      const PLLPOST = 0
      const PLLPRE = 1
      const PLLDIV = 50   ' 31.95
      WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
      CLKDIV = WREG4
      PLLFBD = PLLDIV            
   #elseif (_clock = 32)  ' 16 MIPS                  
      const FRCDIV = 0
      const PLLPOST = 1
      const PLLPRE = 1
      const PLLDIV = 50   ' 15.97
      WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
      CLKDIV = WREG4
      PLLFBD = PLLDIV                
   #elseif (_clock = 16)  ' 8 MIPS
      const FRCDIV = 0
      const PLLPOST = 3
      const PLLPRE = 1
      const PLLDIV = 50   ' 7.99
      WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
      CLKDIV = WREG4
      PLLFBD = PLLDIV              
   #elseif (_clock = 8)   ' 4 MIPS
      const FRCDIV = 1    ' note: this is outside the VCO range... should use FNOSC_FRC mode
      const PLLPOST = 3
      const PLLPRE = 1
      const PLLDIV = 50
      WREG4 = (CLKDIV and &HF800) or (FRCDIV<<8) or (PLLPOST<<6) or PLLPRE
      CLKDIV = WREG4
      PLLFBD = PLLDIV              
   #else
      checkparam(etError, "unsupported system clock freq")
   #endif

   ' trim osc for accurate baud (optional)
   'OSCTUN = &H03A

   ' wait until the PLL is locked
   while (OSCCON.bits(5) = 0)    ' check LOCK bit
   end while
end macro

' this code executes when the PIC first starts up
' for a pic24hj micro without bootloader
sub OnStartup() handles PIC.OnStartup
   RPINR18 = RPINR18 and &HFFE0 or &B00100  ' map Uart1 RX to D0 (RB.4)
   RPOR2 = RPOR2 and &HE0FF or &B00011 << 8 ' map Uart1 TX to D1 (RB.5)
   'RPINR18 = RPINR18 or &H001F  ' disable Uart1 RX
   'RPOR2 = RPOR2 and &HE0FF     ' disable Uart1 TX
end sub

'program entry point...
sub Main()
   ' the following line is mandatory! don't erase it!
   SetSysClock()
   ' code your app bellow...
   
end sub
 



--
Vasi
Reply all
Reply to author
Forward
0 new messages