Posts Tagged ‘ Phase Correct PWM ’

Arduino Iambic Keyer – 2017

Last spring I started doing what I call “Morse Walking”. Going out with headphones on, beeping morse code. I’m doing this hoping I will get good enough at morse to actually use it on the air without making a fool of myself. I previously made two battery operated Arduino based keyers here and here that can generate random morse characters. But they are awkward, the 2015 version requires an external battery and the 2016 model won’t fit in my pocket. Obviously I needed another project.

Arduino Feather keyer

Arduino Feather keyer

 

I decided to use an Adafruit Feather, it has an Atmel 32u4 processer, built in USB interface, a charger/converter for a 3 volt lithium battery, and plenty of pins brought out. The idea was to cut down the previous keyer software to fit the 32u4 equipped with only the hardware I needed for battery powered practice. Like many of my projects, putting it all into a small box proved to be difficult. Not many parts in the schematic but that Altoids tin is only 2 1/4″ x 1 1/2″ and about 1/2″ deep.

Arduino Feather Iambic Keyer Schematic

Arduino Feather Iambic Keyer

Note the unusual wiring at the headphone jack. Ring is grounded while tip carries the signal. Sleeve is not connected. This puts the two headphone elements in series and presents a higher impedance to the output circuit. The ring normal contact can then signal power enable to the Feather when the phones are plugged in.

I sawed about a quarter inch off the breadboard end of the Feather to make it fit, along with two phone jacks, in an Altoids Small tin. I wired up the Feather along with a quadrature rotary encoder which I thought would be cool to use as a volume control. Porting the previous keyer sketch to the 32u4 proved more difficult than I expected. The encoder required major changes to the keyer code as volume control is now in software and requires manipulation of the sine wave synthesizer tables. I got that part mostly working but set the project aside in May.

So now it’s November and I’ve picked up the little keyer again. The software has been revamped and now has these features:

  1. Characters to be sent are buffered in an asynchronous circular queue so memory buttons or keyboard characters can be “typed ahead”.
  2. USB serial terminal is supported as a keyboard for sending or function programming.
  3. Paddle generated morse is interpreted and printed as ASCII letters on the serial terminal.
  4. Four programmable memories available with 251 character capacity each.
  5. Memories programmable from serial terminal or from the paddles.
  6. Random code practice modes, letters, letters and numbers, letters numbers and punctuation.
  7. Adjustable character spacing in code practice mode (Farnsworthiness).
  8. Morse character speed settable 10 to 45 WPM.
  9. Sidetone frequency settable 100 to 1200 Hz.
  10. Synthesized sine wave sidetone with leading and trailing edge envelope shaping.
  11. Memories and operating parameters stored in EEPROM and easily reset to defaults.
  12. Stand alone operation from LiPo battery.

I went for an hour walk today with the keyer in my shirt pocket and it performed flawlessly so I consider it done.

This is a photo of the unit with battery removed.

Arduino Feather keyer battery removed

Arduino Feather keyer battery removed

 

I had to move the JST connector to the side to get clearance, the battery would not fit under the rotary encoder. There are four memory buttons, plus the back switch on the encoder used for Function. I’m using my usual analog read routine to debounce the button switches. Metal cap switches from Adafruit, work much better than the 6mm plastic buttons I used in previous versions.

You can see here the battery just fits.

Arduino Feather keyer with battery

Arduino Feather keyer with battery

 

I spent quite a bit of time working on the waveform generation setup. The encoder routine generates a number 0-64 which is used to scale the synthesizer waveform. Four 16 byte amplitude tables must be regenerated in RAM each time the volume is changed. Initially the sine waves were horribly distorted so I made a spread sheet helped to see what was happening. I was able to work out code that produced a good stepped morse element.  There is very little audible clicking now at beginning or end of the morse elements.

This photo is a dit at about 25 WPM.

Arduino Feather keyer waveform

Arduino Feather keyer waveform

The software, detailed operating instructions, schematic, and spreadsheets can be downloaded from Dropbox.
Any future revisions will appear here

Version 1.0 12/3/2017
http://www.dropbox.com/s/o8cgh485wcqzgj3/MemoryKeyerFeather_V1.0.zip

Version 1.2 12/6/2017 Was running out of memory, rearranged startup and moved stuff to PROGMEM
https://www.dropbox.com/s/hqx54u6ha8uu6xn/MemoryKeyerFeather_V1.2.zip

Jim Harvey – wb8nbs@gmail.com

 

Advertisements

More Fun With Direct Digital Synthesis: Adafruit Feather 32u4

Feather Port with Volume Control

Long walks are a good thing and I’ve taken along one of my morse code keyers running in practice mode to pass the time. Neither of the keyers I made fit pockets very well so I decided to build yet another one, battery operated, with headphone only output. The target container is an Altoids Small tin easily carried in a shirt pocket.

An Adafruit Feather 32u4 Basic has built in USB and an on board battery charger so it is a good start for this project. I ported my previous 32u4 code easily. Reference my previous page on the 32u4 experiments here. Driving headphones directly from a Pulse Width Modulated output is different from my previous projects where I used a small audio amplifier to drive a speaker. Headphones have a much lower impedance than an amp so RC filter components change radically. Implementing a volume control would be a challenge. I tried a 500 ohm trim pot and that worked fairly well with some but not all of the headphones I tried. But I couldn’t find a pot with a shaft that would fit in the Altoids tin.

With PWM Direct Digital Synthesizer generating a sine wave I might be able to throttle the PWM output with software. I have an Adafruit Rotary Encoder that fits the Altoids tin if I’m careful so I worked that into the demo program.  Rotary Encoders are basically mechanical, therefore subject to contact bounce like any other switch. I Googled up a half dozen different Encoder sketches and all of them would glitch badly. I finally found code by Oleg Mazurov that uses a Grey coding technique to ignore invalid inputs. It works well. I was able to get that code running on the Feather and contributed the sketch to the Adafruit Feather forum.

This photo shows the slightly truncated Feather in the Altoids Small tin. The encoder is the green object in the center of the lid. There are a couple of Oscilloscope umbilicals attached and the battery is not yet installed.

Changing the apparent volume of the DDS output is a process of multiplying the Sine table values by a volume parameter between zero and one. I copy the Sine table into RAM and apply this transform. But since I use only integer math in the sketch, the method is, read the value from flash then multiply by 0-63 volume, then divide the result by 63. Close enough.

One more improvement: in the original sketch, even when the volume is set to zero the PWM signals are 50% high and 50% low, averaging zero as far as the Sine wave coupled through the output capacitor is concerned. That means the output pin is still driving full voltage at the PWM frequency. With the low load impedance it’s a significant drain on the DC power and I’m using a very small battery. The remedy was to lower the zero crossing base line in step with lowering the amplitude of the Sine table.  A new volatile variable maxSine passes the necessary correction to the DDS Interrupt Service Routine. The ISR doesn’t like having it’s variables changed on the fly. Rotating the Encoder makes a scratchy sound like a dirty potentiometer but it’s fine when you stop adjusting.

I measured 18 milliamps constant draw from the USB supply before implementing the zero crossing shift. With the shift, current varies from 10 ma at zero volume to 17.6 ma at maximum volume. The 150 mah battery should last 6-10 hours, way more than my morse code attention span.

My example sketch with all the experimental options in the previous version is downloadable from Dropbox. A short video showing the waveforms produced is on YouTube.

References:
https://www.circuitsathome.com/mcu/rotary-encoder-interrupt-service-routine-for-avr-micros/
https://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino/

Files:
https://www.dropbox.com/s/t1fud02jcfdhxjl/Feather32u4V2.zip?dl=0
https://www.youtube.com/watch?v=jbv-z4wvXI8&feature=youtu.be

More Fun With Direct Digital Synthesis: 32u4 and Fast PWM

32u4 Port

My 2015 winter project will be an expanded version of the Morse Code keyer I built last year. I now have a PJRC Teensy 2.0 controller board (from Sparkfun) to use. The Atmel 32u4 based Teensy 2.0 is even smaller than the Pro Minis I have been using. It is attractive because it has USB built in for program loading and serial transmission. And there’s lots of I/O pins available which will make an LCD easy to connect. I note that everybody making a 32u4 “Arduino” is defining a different pinout from the chip. The Adafruit Feather is quite ambitious, has it’s own IDE addon and can be bought with Bluetooth or WiFi  included.

This breadboard photo shows the Teensy 2.0 at top left, Sparkfun Pro Mini at center, Sparkfun RedBoard top right, and a MicroCenter Pro Mini clone at bottom right.

Arduinos

 

PJRC has ported many of the standard Arduino libraries to their 32u4 Teensy form factor and these are included in your Arduino IDE when you download and install the Teensyduino addon. But my keyer sketch uses Direct Digital Synthesis to form a nice sine wave side tone which requires direct setup of a Pulse Width Modulation timer inside the AVR chip. My implementation is based on code from Jeff Whitlatch, KO7M.  I used timer 2 in last winters UNO based keyer as that’s the timer that does tone() in the Arduino UNO and I completely superseded that function. Reading the data sheet on 32u4 shows that it doesn’t even *have* a timer 2. It has timer 0 (8 bit), timers 1 and 3 (16 bit), and timer 4 (10 bit). Digging into the Teensy Arduino libraries shows that PJRC is using timer 3 for tone() so I needed a port that just changed the timer used. Comparing the 32u4 data sheet with the Mega328 data sheet showed that the only difference besides the timer number would be in the Waveform Generation Mode bits. 32u4 has four WGM bits, Mega328 has 3. I used a mode setting that turns the 16 bit 32u4 timer 3 into an 8 bit timer and Success! The port was working in my test program.

Fast PWM

Phase Correct PWM in an AVR processor uses the timer in a manner that has it count from 0 to 255 then count back down to zero for each interrupt cycle. The PWM signal goes high on the way up and low on the way down. For an 8 bit counter, that’s 510 counts per cycle (read the data sheet to find out why it skips two) so with no clock scaling the sample rate is 16 MHz/510 or 31.4 KHz. In Fast PWM mode the timer counts from 0 to 255 then rolls over back to zero. A PWM wave on the output pin goes high on the way up and low on overflow. This results in some jitter on the PWM pulses but the sample rate is twice as fast, 16 MHz/256 or 62.5 KHz. I decided to try Fast PWM in the test program. It required only a change in the math that calculates the sample rate, and a slightly different set of WGM bits.

The following images show comparisons of the 31.4 KHz (Phase Correct) and the 62.5 KHz (Fast) rate tone waveforms from the test program. The PWM signal is filtered by a single 10k resistor and a 0.01 ufd capacitor to ground at the scope connection.

Comparison of Phase Correct PWM with Fast PWM

Comparison of Phase Correct PWM with Fast PWM at 100 Hz

 

Comparison of Phase Correct PWM with Fast PWM at 1000 Hz

Comparison of Phase Correct PWM with Fast PWM at 1000 Hz

 

Comparison of Phase Correct PWM with Fast PWM at 5000 Hz

Comparison of Phase Correct PWM with Fast PWM at 5000 Hz

 

Comparison of Phase Correct PWM with Fast PWM at 10000 Hz

Comparison of Phase Correct PWM with Fast PWM at 10000 Hz

 

There is a clear improvement at the higher sample rate in the amount of “fuzz” on the waveform. The fuzz is at the sample rate and is the result of the 0.01 ufd capacitor integrating the PWM square wave. At twice the rate, the capacitor has half the time to charge or discharge. You can see at 5 KHz, the Fast PWM signal is much closer to a sine wave than Phase Correct. At 10 KHz, both are falling apart. A better output filter that cuts off the sample frequency would help but for morse keyer side tone from a tiny speaker, the average amateur radio operators ear won’t know the difference. I may try a switched capacitor filter some time in the future just to see. Watch this space.

Revision History

2015/12/08   The photos above are all from the 32u4 port. The program uses quarter wave symmetry to reduce the table size by a factor of 4. Two sketches, one running on UNO or other Mega328 controllers, the other for the PJRC 32u4 Teensy are at:
https://dl.dropboxusercontent.com/u/40929640/ArduinoSineSynth/Quarter_Wave_Demos.zip

I also back ported the changes to an earlier sketch that did not take advantage of symmetry. Scope traces were identical to the 32u4 port. Since the complete 360 degree cycle is specified, the code may be modified to generate arbitrary non-sinusoidal waveforms by manipulating the tables. Download at:
https://dl.dropboxusercontent.com/u/40929640/ArduinoSineSynth/Full_Table_Demos.zip

Each of these zip archives has a copy of the spreadsheet I used to construct the sine tables.