Generating antiphase PWM signals with the dsPIC30F4011

I frequently receive queries from people who are using a dsPIC microcontroller to control power electronics of some kind, such as in an inverter, a voltage converter, or similar. Many of these queries relate to the generation of different combinations of pulse-width modulation (PWM) signals. In this article, I describe a simple example application in which the dsPIC30F4011 is used to generate two antiphase PWM signals with duty cycle varying between 10% and 45%. By antiphase, I mean that the two signals are 180 degrees (π radians) out of phase with each other. Apart from that, the two signals are identical.

I developed this example in response to a query from gunz which was posted as a comment on another article that I published on this blog a couple of years ago. In that article, I also generated antiphase PWM signals, but I used two of the dsPIC30F4011’s three PWM channels. Specifically, the signals were generated on pins PWM1H and PWM2L. Gunz asked how to generate the signals on the high and low pins of a single PWM channel (e.g. PWM1H and PWM1L), so that’s what I’m demonstrating in this example.

The signal specification is:

  • Two identical PWM signals 180 degrees out of phase.
  • The PWM frequency is 15kHz.
  • The duty cycle varies between 10% and 45% (hence the pulses on the two outputs never overlap).
  • The output pins are PWM1H and PWM1L.

The basic code is as follows:

//
// This dsPIC30F4011 example program generates PWM signals
// on PWM1H and PWM1L. The signals are identical except that
// they are 180 degrees out of phase. Duty cycle can vary
// between 0.1 and 0.45. PWM frequency is constant at 15kHz.
//
// The basic idea is to configure the PWM frequency to double
// what we want (30 kHz), but then only generate a pulse on
// each output every second period. We use the PWM interrupt
// (which is triggered every time the PWM timebase reaches
// the value PTPER and goes back to zero) to switch back and
// forth between PWM1H and PWM1L. i.e. The two pins take it
// in turns to output pulses.
//
// Written by Ted Burke - last updated 11-4-2015
//

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

//
// The PWM interrupt is triggered each time PTMR reaches the same
// value as PTPER and resets to zero. We use this ISR to switch back
// and forth between outputting a pulse on PWM1H and outputting a pulse
// on PWM1L. During each PTMR period, we override one or other pin to
// lock it at 0, suppressing its pulse. Because the OSYNC bit is set,
// PWM override changes do not take effect until the next time PTMR
// resets to zero. Updates therefore don't take effect until the start
// of the the next cycle.
//
void __attribute__((interrupt, auto_psv)) _PWMInterrupt(void)
{
    // Reset PWM interrupt flag
    _PWMIF = 0;
    
    // Alternate between overriding PWM1L and overriding PWM1H.
    // Whichever pin is overridden will be driven low during
    // the next PWM period, suppressing its output pulse.
    if (_POVD1H)
    {
        _POVD1H = 0;
        _POVD1L = 1;
    }
    else
    {
        _POVD1L = 0;
        _POVD1H = 1;
    }
}

int main(void)
{
    // Configure RD0 as a digital output for an indicator LED
    TRISD = 0b1110;
 
    // Configure PWM
    //
    // Note: To provide higher PWM duty cycle resolution, the dsPIC's
    // PDCx unit is only half as long as its PTPER unit. For example,
    // when PDC1 = PTPER, the PWM1 duty cycle is actually only 50%.
    // Furthermore, in this example, since we are alternating back and
    // forth between pulses on two outputs, each output only produces
    // every second pulse. Hence, in this case PTPER is really only
    // half of the full signal period. Yes, it's potentially confusing!
    //
    _PMOD1 = 1;   // Enable PWM channel 1 in independent mode
    _PEN1H = 1;   // Enable PWM1H pin
    _PEN1L = 1;   // Enable PWM1L pin
    _POUT1L = 0;  // When PWM1L is overriden, set it low
    _POUT1H = 0;  // When PWM1H is overriden, set it low
    _POVD1L = 1;  // Initially, enable override on PWM1L
    _POVD1H = 0;  // Initially, disable override on PWM1H
    _OSYNC = 1;   // Synchronise PWM override changes with PTMR reset
    _PWMIE = 1;   // Enable PWM interrupt
    _PTCKPS = 0;  // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
    PTPER = 999;  // Set PWM frequency to 30 kHz
    PDC1 = 400;   // Set duty cycle to 10%
    _PTEN = 1;    // Enable PWM time base
    
    // Now just blink an LED while the PWM ISR does the heavy lifting
    while(1)
    {
        _LATD0 = 1;          // Turn on LED on RD0
        __delay32(15000000); // 0.5 second delay
        _LATD0 = 0;          // Turn off LED on RD0
        __delay32(15000000); // 0.5 second delay
    }

    return 0;
}

I compiled the above code using Microchip’s free XC16 C compiler. This is my build script, which produces an binary file called “a.hex”:

xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out

I downloaded the file “a.hex” onto the dsPIC30F4011 using a PICkit 2 USB programmer. I don’t use MPLAB at all, so I just used the PICkit 2 software application (V 2.61 can be downloaded here).

The breadboard circuit I used for testing is shown below. In addition to the voltage supply connections I included in my circuit, I highly recommend connecting pin 20 (VSS) and pin 21 (VDD) to the voltage supply rails. In the past, I have tended not to bother connecting all voltage supply pins, but I have recently run into some very strange problems that it took me a long time to realise could be completely solved by connecting more voltage supply pins. Specifically, I had a lot of problems with mysterious resetting of the dsPIC when using PWM interrupts in parallel with mainline code (such as the LED flashing code I put in the main function in this example). I therefore highly recommend connecting all of the dsPIC’s voltage supply pins, even if it seems redundant.

An indicator LED (with 220\Omega; current-limiting resistor in series) is connected to RD0 (pin 23). The three wires which extend beyond the lower edge of the image are the oscilloscope connections – green is ground and the two blue wires connect PWM1H and PWM1L to different channels of the scope.

20150410_181128

This is how the circuit appeared once the code shown above was running. The LED on RD0 simply blinks on and off once a second.

I displayed the signals from PWM1H and PWM1L on the two channels of an oscilloscope. As shown below, the two signals are identical but 180 degrees out of phase.

Antiphase PWM signals displayed on oscilloscope.

The following modified version of the previous example shows how the PWM ISR can be used to update the duty cycle as well as alternating the generated pulses between the two outputs. Apart from comments, the only change from the previous example is the addition of lines 62-68 to the PWM ISR (the _PWMInterrupt function) which modulate the PWM duty cycle.

//
// This dsPIC30F4011 example program generates PWM signals
// on PWM1H and PWM1L. The signals are identical except that
// they are 180 degrees out of phase. This is a slightly
// modified version of the example in which duty cycle is
// modulated in a sawtooth pattern, increasing in small steps
// from 0.1 all the wat to 0.45, then resetting to 0.1 over
// and over again. PWM frequency is constant at 15kHz.
//
// The basic idea is to configure the PWM frequency to double
// what we want (30 kHz), but then only generate a pulse on
// each output every second period. We use the PWM interrupt
// (which is triggered every time the PWM timebase reaches
// the value PTPER and goes back to zero) to switch back and
// forth between PWM1H and PWM1L. i.e. The two pins take it
// in turns to output pulses.
//
// Written by Ted Burke - last updated 11-4-2015
//

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

//
// The PWM interrupt is triggered each time PTMR reaches the same
// value as PTPER and resets to zero. We use this ISR to switch back
// and forth between outputting a pulse on PWM1H and outputting a pulse
// on PWM1L. During each PTMR period, we override one or other pin to
// lock it at 0, suppressing its pulse. Because the OSYNC bit is set,
// PWM override changes do not take effect until the next time PTMR
// resets to zero. Updates therefore don't take effect until the start
// of the the next cycle.
//
void __attribute__((interrupt, auto_psv)) _PWMInterrupt(void)
{
    // Reset PWM interrupt flag
    _PWMIF = 0;
    
    // Alternate between overriding PWM1L and overriding PWM1H.
    // Whichever pin is overridden will be driven low during
    // the next PWM period, suppressing its output pulse.
    if (_POVD1H)
    {
        _POVD1H = 0;
        _POVD1L = 1;
    }
    else
    {
        _POVD1L = 0;
        _POVD1H = 1;
    }

    // This addition to the PWM ISR modulates the duty cycle in
    // a sawtooth pattern. Every 100th time the ISR runs, the
    // duty cycle is increased by incrementing PDC1. When the
    // duty cycle reaches 45%, it is reset to 10%.
    static int n=0;
    n = (n + 1) % 100;
    if (n==0)
    {
        PDC1 = PDC1 + 1;
        if (PDC1 > (0.45 * 4 * PTPER)) PDC1 = 0.1 * 4 * PTPER;
    }
}

int main(void)
{
    // Configure RD0 as a digital output for an indicator LED
    TRISD = 0b1110;
 
    // Configure PWM
    //
    // Note: To provide higher PWM duty cycle resolution, the dsPIC's
    // PDCx unit is only half as long as its PTPER unit. For example,
    // when PDC1 = PTPER, the PWM1 duty cycle is actually only 50%.
    // Furthermore, in this example, since we are alternating back and
    // forth between pulses on two outputs, each output only produces
    // every second pulse. Hence, in this case PTPER is really only
    // half of the full signal period. Yes, it's potentially confusing!
    //
    _PMOD1 = 1;   // Enable PWM channel 1 in independent mode
    _PEN1H = 1;   // Enable PWM1H pin
    _PEN1L = 1;   // Enable PWM1L pin
    _POUT1L = 0;  // When PWM1L is overriden, set it low
    _POUT1H = 0;  // When PWM1H is overriden, set it low
    _POVD1L = 1;  // Initially, enable override on PWM1L
    _POVD1H = 0;  // Initially, disable override on PWM1H
    _OSYNC = 1;   // Synchronise PWM override changes with PTMR reset
    _PWMIE = 1;   // Enable PWM interrupt
    _PTCKPS = 0;  // prescale=1:64 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
    PTPER = 999;  // Set PWM frequency to 30 kHz
    PDC1 = 400;   // Set duty cycle to 10%
    _PTEN = 1;    // Enable PWM time base
    
    // Now just blink an LED while the PWM ISR does the heavy lifting
    while(1)
    {
        _LATD0 = 1;          // Turn on LED on RD0
        __delay32(15000000); // 0.5 second delay
        _LATD0 = 0;          // Turn off LED on RD0
        __delay32(15000000); // 0.5 second delay
    }

    return 0;
}

Here’s a short video showing the sawtooth modulation of the PWM duty cycle displayed on the oscilloscope.

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | 2 Comments

Very simple Python / Tkinter GUI to send selected keystrokes via serial port

Following a query from Naomi Dickerson, I was playing around with my SerialSend program, which I often use for sending arbitrary bytes or characters to a connected microcontroller circuit. Sometimes, it’s convenient to send keystrokes in real time i.e. each keystroke is sent as soon as the user strikes the key. This could be done in a Windows console using something like getch(), but in this case I’ve chosen to use Python and Tkinter instead, supported by the PySerial module. Python is absolutely great for automating serial port chit chat. I’ve never found anything better for opening, reading, writing and closing a serial port in just a few lines of code.

Here’s what my ultra simple GUI (if you can even call it that) looks like:

Screenshot - 03102015 - 11:17:46 AM

Here’s the full Python code, which I saved as the file “keysender.py”:

# import libraries for serial port and Tkinter GUI
import serial
import Tkinter

# Open serial port
ser = serial.Serial('/dev/ttyUSB0', baudrate=9600, timeout=1)

# Create the root window
root = Tkinter.Tk()
root.geometry('400x200+100+100')
root.title('Serial Keystoke Sender')

# Create a keystroke handler
def key(event):
    if (event.char == 'q'):
        root.quit()
    elif event.char >= '0' and event.char <= '9':
        print 'keystroke:', repr(event.char)
        ser.write(event.char)

# Create a label with instructions
label = Tkinter.Label(root, width=400, height=300, text='Press 0-9 to send a number, or "q" to quit')
label.pack(fill=Tkinter.BOTH, expand=1)
label.bind('<Key>', key)
label.focus_set()

# Hand over to the Tkinter event loop
root.mainloop()

# Close serial port
ser.close()

The above code should be cross-platform compatible apart from the serial port name ‘/dev/ttyUSB0′, which is what my USB-to-serial converter appears as here in Crunchbang Linux. In Windows, I think you can either replace ‘/dev/ttyUSB0′ with your COM port name in inverted commas (e.g. ‘COM23′) , or even just a device number (0, 1, 2 or whatever).

Python itself is installed by default in Crunchbang, but I needed to install PySerial and Tkinter:

sudo apt-get install python-tk
sudo apt-get install python-serial

I ran my code as root so that I would have access to the serial port device:

sudo python keysender.py

There are more elegant ways to handle this by configuring file permissions or whatever, but for this kind of hardware hacking I’m happy enough to just sudo it.

Posted in Uncategorized | Leave a comment

Using a dsPIC30F4011 to generating 4 PWM signals with equal duty cycles but at 90 degree phase increments

In a recent comment on one of my blog posts, Saptarshi De posed an interesting problem: How can the dsPIC30F4011 be used to generate four PWM signals of equal (but variable) duty cycle at 90 degree phase increments? Saptarshi wants to control a 4-phase interleaved boost converter and he supplied an illustration similar to the following to show what he requires:

four_phase_pwm

The required PWM frequency is 50kHz and the four PWM signals must have equal duty cycle, but that duty cycle is variable. Saptarshi didn’t specify maximum and minimum values for the duty cycle, so I worked on the assumption that it can vary all the way from 0-100%.

At first, I was stumped. The most obvious approaches all have seemed to have show-stopping snags:

  • The dsPIC30F PWM module does most of what’s needed, but sadly only provides three channels. Each channel has two outputs, so it may somehow be possible to coax two of the channels to produce complementary output with appropriate deadtime to yield the required four PWM signals, but I couldn’t figure out how.
  • The Output Compare module provides four channels which can be used to generate PWM signals, but each of those channels must use either Timer 2 or Timer 3 as its timebase, which imposes some major limitations.

Ultimately, I opted for a variation of the second approach above using the Output Compare channels. Unfortunately, my solution requires that two of the channels be inverted externally, for example using a CMOS logic IC. If the duty cycle range is more constrained, this can be avoided, but for arbitrary duty cycle anywhere between 0% and 100%, I couldn’t work out how to do it without externally inverting two of the signals.

My solution is very similar to what I did previously to generate two out of phase PWM signals having identical duty cycle.

My approach is as follows:

  • The dsPIC uses the internal fast RC oscillator with 16x PLL multiplier so that it runs at 30 MIPS. Fcy = 30e6 and Tcy = 33.33ns.
  • Timer 2 and Timer 3 are configured with the same period of 600 * Tcy = 20us.
  • TMR2 is initialised to 150 and TMR3 is initialised to 0. The result is that when the timers are enabled, TMR2 leads TMR3 by a quarter cycle.
  • PWM1 is obtained from OC1 which uses Timer 2 as its timebase.
  • PWM2 is obtained from OC2 which uses Timer 3 as its timebase (lagging OC1 by 25% of a cycle).
  • PWM3 is obtained by inverting OC3 which uses Timer 2 as its timebase. The pulses on OC3 become the gaps between the pulses in PWM3.
  • PWM4 is obtained by inverting OC4 which uses Timer 3 as its timebase. The pulses on OC4 become the gaps between the pulses in PWM4.
//
// This dsPIC30F4011 example program generates four
// interleaved PWM signals to control a 4-phase
// interleaved boost circuit.
//
// Written by Ted Burke - last updated 5-3-2015
//

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

int main()
{
    // Configure RD0, RD1, RD2, RD3 as digital outputs.
    // (Not sure if this is required when Output Compare is used)
    TRISD = 0b1111111111110000;

    // Set period and duty cycle
    float dutycycle = 0.2;      // 20% duty cycle for this example
    int pulse, space, period;   // pulse width, space width, period
    period = 600;               // f_m = 30e6 / 600 = 50 kHz
    pulse = dutycycle * period; // width of PWM pulses
    space = period - pulse;     // gap between PWM pulses

    // Configure Timers 2 & 3
    PR2 = period - 1;      // Set Timer 2 period for 50kHz
    PR3 = period - 1;      // Set Timer 3 period for 50kHz
    T2CONbits.TCKPS = 0;   // 1:1 prescale
    T3CONbits.TCKPS = 0;   // 1:1 prescale
    TMR2 = period / 4;     // Timer 2 leads Timer 3 by 25% of period.
    TMR3 = 1;              // Timer 3 lags Timer 2 by 25% of period. Give it a "head start" of 1 because Timer3 is enabled just after Timer2

    // Select timer for each channel
    OC1CONbits.OCTSEL = 0; // OC1 driven by Timer 2
    OC2CONbits.OCTSEL = 1; // OC2 driven by Timer 3
    OC3CONbits.OCTSEL = 0; // OC3 driven by Timer 2
    OC4CONbits.OCTSEL = 1; // OC4 driven by Timer 3

    // Output continuous pulses on all OC channels
    OC1CONbits.OCM = 0b101;
    OC2CONbits.OCM = 0b101;
    OC3CONbits.OCM = 0b101;
    OC4CONbits.OCM = 0b101;

    // Set OC1 to output continuous pulses of the desired width.
    // The pulses are positioned midway through the TMR2 up-count.
    OC1R = space / 2;       // pulse start time
    OC1RS = OC1R + pulse;   // pulse end time

    // Set OC2 to output continuous pulses of the desired width.
    // The pulses are positioned midway through the TMR3 up-count.
    OC2R = space / 2;       // pulse start time
    OC2RS = OC2R + pulse;   // pulse end time

    // Set OC3 to output continuous pulses. These will be inverted
    // to become the gaps between the pulses and vice versa. The
    // width of these pulses is therefore set to the width of the
    // gap between the final pulses in PWM3.
    OC3R = pulse / 2;       // pulse start time (start of gap in PWM3)
    OC3RS = OC3R + space;   // pulse end time (end of gap in PWM3)

    // Set OC4 to output continuous pulses. These will be inverted
    // to become the gaps between the pulses and vice versa. The
    // width of these pulses is therefore set to the width of the
    // gap between the final pulses in PWM4.
    OC4R = pulse / 2;       // pulse start time (start of gap in PWM4)
    OC4RS = OC4R + space;   // pulse end time (end of gap in PWM4)

    // Enable Timers 2 & 3
    //
    // It might be better to use some inline assembly language here
    // to ensure that the delay between the two timers being enabled
    // is very short and that we know exactly how many instruction
    // cycles "head start" Timer 3 should get to make the two timers
    // exactly 90 degrees out of phase.
    //
    T2CONbits.TON = 1; // Enable Timer 2
    T3CONbits.TON = 1; // Enable Timer 3

    // Now just let the Output Compare module to the work
    while(1);

    return 0;
}

I saved the program as “main.c” and compiled it using Microchip’s free XC16 C compiler, with the following simple build script:

xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out

My test circuit is shown below:

4-phase_PWM_breadboard

4-phase_PWM_circuit_diagram

[ Download editable Inkscape SVG version (same SVG file as earlier illustration) ]

I didn’t have an inverter IC available when I was doing this test, so I just cobbled together a couple of crappy NPN transistor inverters (I used BC237 transistors). These don’t respond fast enough for the system to work at 50 kHz, so I temporarily reduced the dsPIC clock speed by a factor of 4 for this experiment.

I tested the circuit using a two channel digital oscilloscope, so I wasn’t able to view all four signals simultaneously. Instead, I displayed PWM1 on scope channel 2 (the blue signal in each of the photos below) and then used scope channel 1 to view PWM2, PWM3 and PWM4 one at a time so that I could verify the phase shift of each signal relative to PWM1.

The photo below shows the signals PWM1 (blue) and PWM2 (yellow).
20150305_194939

The photo below shows the signals PWM1 (blue) and PWM3 (yellow). Note that PWM3 is obtained by inverting the signal from OC3.
20150305_194954

The photo below shows the signals PWM1 (blue) and PWM4 (yellow). Note that PWM4 is obtained by inverting the signal from OC4.
20150305_195008

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , , | 4 Comments

Faster Mandelbrot image generation using numpy in Python

#
# fractal.py - Mandelbrot image generation using numpy
# Written by Ted Burke - last updated 2-11-2014
#

import numpy

# Define image size and region of the complex plane
w,h = 1280,1280
remin,remax = -2.0, 2.0
immin,immax = -2.0, 2.0

# Create numpy arrays for pixels and complex values
p = numpy.zeros((h, w), dtype=numpy.uint8)
z = numpy.zeros((h, w), dtype=complex)
c = numpy.linspace(remin,remax,w) * numpy.ones((h,w)) + \
    1j * numpy.linspace(immax,immin,h).reshape(h,1) * numpy.ones((h,w))

# Iterative pixel calculation
for n in range(255):
    z = z*z + c
    p = p + (abs(z) < 2.0)

# Output image to binary PGM file
f = open('fractal.pgm', 'wb')
f.write('P5\n{0} {1}\n255\n'.format(w, h))
f.write(p)
f.close()

I ran the program and converted the image to PNG format using ImageMagick as follows:

python fractal.py
convert fractal.pgm fractal.png

fractal

Posted in Uncategorized | Leave a comment

Fractal variations using Python

Every once in a while, I spend an hour or two playing with code to generate fractal images. I find these patterns intriguing and the process of working with them can make for a great workout in complex number arithmetic. When I’m playing with code like this, I find it particularly handy to use Python because it handles the complex numbers very neatly and you can produce a good looking fractal with just a few lines of code. For example, here’s a quick Mandelbrot example:

import numpy

print 'P2\n800 800\n255'

for y in numpy.linspace(-2.0,2.0,800):
    for x in numpy.linspace(-2.0,2.0,800):
        c = x + y*(0+1j)
        z = 0
        for p in range(256):
            z = z*z - c
            if numpy.abs(z) > 4:
                break
        print p,
    print

Please excuse the lack of explanatory comments, but I’m trying to make the point that you don’t need many lines of code!

I ran the program and piped the output into a plain text pgm file, then converted it to PNG format using ImageMagick’s convert command, as shown below:

python mandelbrot.py > mandelbrot.pgm
convert mandelbrot.pgm mandelbrot.png

Ok, here’s the image it produced:

mandelbrot

The process of generating each pixel value in an image like the one above involves starting with a complex number determined by the position within the image (the image spans a rectangular region of the complex plane) and iterating a function recursively to generate a sequence of other points on the complex plane. The faster this iterative process “blows up” (by which I mean that the complex numbers get very large) the darker the pixel.

Today, I was playing with this example and I decided to try to visualise something a little different – for each pixel, I repeated the iteration until the magnitude of the complex value passed a threshold (4 as it happens) then chose the pixel colour based on two things: the distance between the last two complex values and the difference in angle between them. I was thinking of this as “how fast the point is moving” and “how fast the point is revolving around the origin”. Neither of these descriptions are really completely appropriate for an iterative process like this which by definition moves in discrete steps, but that’s how I was thinking of it.

Anyway, the image I produced gave me much food for thought, so I figured I’d post it here in case I want to come back to it again. First, here’s the Python code:

import numpy

print 'P3\n1280 1280\n255'

for y in numpy.linspace(0.0,0.25,1280):
    for x in numpy.linspace(-2.5,-1.25,1280):
        c = x + y*(0+1j)
        z = 0
        for p in range(256):
            newz = z*z + c
            if numpy.abs(newz) > 4:
                z = newz - z
                break
            else:
                z = newz
        r = min(255, int(10 * numpy.abs(z)))
        g = min(255, 128 + int(10 * numpy.angle(z)))
        b = min(255, 128 - int(10 * numpy.angle(z)))
        print r, g, b,
    print

I ran the program and piped the output into a plain text ppm file, then converted it to PNG format using ImageMagick’s convert command, as shown below:

python colours.py > colours.ppm
convert colours.ppm colours.png

This image shows the region of the complex plane where the real part is between -3 and +3 and the imaginary part is between -3j and +3j.

colours1

This image shows the region of the complex plane where the real part is between -1.5 and -1.25 and the imaginary part is between 0j and 0.25j.

colours3

Posted in Uncategorized | Leave a comment

Using SendInput to type unicode characters

I received a query from reader Partha D about generating unicode keystrokes using the SendInput function in Windows. As I understand it, Partha wants to generate one or more unicode keystrokes when a particular keyboard shortcut is pressed. The following example program illustrates the use of the SendInput function to generate keyboard events for unicode characters. It just generates a single keystroke (a Bengali character) after a 3-second delay.

//
// unicodeinput.c - sends a unicode character to whichever
// window is in focus after a delay of 3 seconds (to allow
// time to switch to e.g. Notepad from the command window).
//
// Written by Ted Burke - last updated 2-10-2014
//
// To compile with MinGW:
//
//      gcc unicodeinput.c -o unicodeinput.exe
//
// To run the program:
//
//      unicodeinput.exe
//

// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
#include <windows.h>

int main()
{
    // Create a keyboard event structure
    INPUT ip;
    ip.type = INPUT_KEYBOARD;
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // 3 second delay to switch to another window
    Sleep(3000);

    // Press a unicode "key"
    ip.ki.dwFlags = KEYEVENTF_UNICODE;
    ip.ki.wVk = 0;
    ip.ki.wScan = 0x09A4; // This is a Bengali unicode character
    SendInput(1, &ip, sizeof(INPUT));

    // Release key
    ip.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));

    // The End
    return 0;
}

To test the program, I opened a command window and a Notepad window. I compiled and ran the program in the command window, as shown below:

Compiling and running my unicode input example program in a command window

Then I immediately switched the focus to a Notepad window. After three seconds, the following unicode character was typed (I’ve increased the font size to the maximum for clarity):

Screenshot of a Notepad window in which a unicode character has been "typed" by my unicodeinput example program

Posted in Uncategorized | Tagged , , , , , , , , , , , , , , , , | 2 Comments

Three PWM outputs with three different frequencies using the dsPIC30F4011 microcontroller

Arising out of an interesting online discussion with Jaime Mora in the Costa Rica Institute of Technology, I wrote this example program to show how three PWM outputs, each with a different frequency, can be generated using the dsPIC30F4011 microcontroller. Generating three or four PWM outputs with the same frequency is very straightforward, but mutliple PWM outputs with different frequencies is more complicated because each PWM signal must be generated using a different clock source.

Jaime is using the three PWM signals to control IGBT’s in a high-current buck converter. His chosen frequencies are 17 kHz, 19kHz and 23 kHz. My understanding is that these particular frequencies were chosen to avoid harmonic interference. Since 17, 19 and 23 are prime numbers, I think I’m right in saying that the first harmonic frequency that’s common to any two of these PWM signals will be at 323 kHz (because 17 x 19,000 = 19 x 17,000 = 323,000). The three PWM outputs used in this program are:

  • A: PWM1H on pin 37. This 17 kHz PWM signal is generated by the “Motor Control PWM” module. The clock source is the dedicated PWM clock in that module.
  • B: OC1 on pin 23. This 19 kHz PWM signal is generated using Output Compare channel 1 (in PWM mode), with Timer 2 as its clock source.
  • C: OC2 on pin 18. This 23 kHz PWM signal is generated using Output Compare channel 2 (in PWM mode), with Timer 3 as its clock source.

I included a handy function called set_duty_cycles, which allows all three channels’ duty cycles to be set at once. Each duty cycle should be specified as a floating point value between 0 and 1. This is the function prototype:

void set_duty_cycles(float a, float b, float c);
  • a is the duty cycle (between 0 and 1) for PWM1H on pin 37.
  • b is the duty cycle (between 0 and 1) for OC1 on pin 23.
  • c is the duty cycle (between 0 and 1) for OC2 on pin 18.

I’ve tested this example program with LEDs attached to the three output pins so that I could confirm that PWM was working and that the duty cycle really was varying on each channel (as shown in the video below). Jaime was kind enough to send me the following screen captures from his oscilloscope when he was testing his circuit. They seem to confirm that the PWM frequencies are 17 kHz, 19 kHz and 23 kHz, as expected.

Here’s a video of the three LEDs pulsating:

Here’s the complete source code:

//
// This example shows how the dsPIC30F4011 can generate three PWM
// signals, each with a different frequency (17000, 19000, 23000 Hz)
//
// To generate three different frequencies, three separate clock sources
// must be used, which necessitates some mixing and matching:
//
//   PWM signal A (17 kHz) is generated using the PWM module with its
//   own timebase (PTMR) used as the clock source.
//
//   PWM signal B (19 kHz) is generated using Output Compare channel 1
//   with Timer 2 as the clock source.
//
//   PWM signal C (23 kHz) is generated using Output Compare channel 2
//   with Timer 2 as the clock source.
//
// I compiled this with Microchip's XC16 C compiler, using the
// following commands:
//
//   xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
//   xc16-bin2hex a.out
//
// Written by Ted Burke, Last updated 24-9-2014
//
 
#include <xc.h>
#include <libpic30.h>
#include <math.h>
 
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin
 
// Function prototype
void set_duty_cycles(float a, float b, float c);
 
int main(void)
{
    // Use PWM module for PWM output A with PTMR as clock source
    PWMCON1 = 0x0011;       // Enable PWM1 (high and low pins)
    PTCONbits.PTCKPS = 0;   // prescale=1:1 (0=1:1, 1=1:4, 2=1:16, 3=1:64)
    PTPER = 1764;           // 17 kHz PWM frequency (PTPER + 1 = 30000000 / 17000)
    PDC1 = 1200;            // 50% duty cycle on PWM channel 1
    PTMR = 0;               // Clear 15-bit PWM timer counter
    PTCONbits.PTEN = 1;     // Enable PWM time base
 
    // Use OC1 for PWM output B with Timer 2 as clock source
    PR2 = 1578;             // 19 kHz PWM frequency (PR2 + 1 = 30000000 / 19000)
    OC1R = PR2 / 2;         //
    OC1RS = PR2 / 2;        // Select 50% duty cycle initially
    OC1CONbits.OCTSEL = 0;  // Select Timer 2 as clock source for OC1
    OC1CONbits.OCM = 0b110; // PWM mode
    T2CONbits.TON = 1;      // Turn on Timer 2
 
    // Use OC2 for PWM output C with Timer 3 as clock source
    PR3 = 1303;             // 23 kHz PWM frequency (PR3 + 1 = 30000000 / 23000)
    OC2R = PR3 / 2;         //
    OC2RS = PR3 / 2;        // Select 50% duty cycle initially
    OC2CONbits.OCTSEL = 1;  // Select Timer 3 as clock source for OC2
    OC2CONbits.OCM = 0b110; // PWM mode
    T3CONbits.TON = 1;      // Turn on Timer 3
    
    // Make OC1 and OC2 outputs (same pins as RD0 and RD1)
    TRISD = 0b1111111111111100;
    
    //
    // Now, vary duty cycles on the three PWM outputs so that the
    // three LEDs pulsate.
    //
    // I'm using an inverted squared sine waveform to pulsate
    // each LED. Between each pair of LEDs, there's a phase shift
    // of 2*pi/3 radians.
    //
    // It doesn't really matter what's actually happening in this part
    // of the program - it's just something to show that the duty
    // cycle really is variable on each PWM output.
    //
    float t=0, pi=3.1428, f=0.25, a, b, c;
    while(1)
    {
        a = sin(2 * pi * f * t);
        a = 1 - a * a;
        b = sin(2 * pi * f * t + 2 * pi / 3);
        b = 1 - b * b;
        c = sin(2 * pi * f * t + 4 * pi / 3);
        c = 1 - c * c;
        
        set_duty_cycles(a, b, c);
        
        __delay32(300000); // 10 ms delay
        t += 0.01; // t is in seconds
    }
}
 
//
// This function provides a way to set the duty cycle on all three
// PWM output channels in one go. Arguments a, b and c are the three
// desired duty cycle values - each should be in the range from 0 to 1.
//
void set_duty_cycles(float a, float b, float c)
{
    PDC1 = a * 2 * PTPER;
    OC1RS = b * PR2;
    OC2RS = c * PR3;
}
Posted in Uncategorized | Leave a comment