MCU Series, 2: Sometimes MCU choice doesn’t matter

This article was originally posted on Microcontroller Central, which is no longer on the air. Much of the content is as it was when I originally posted it, with updates when necessary to correct errors in the original articles or reflect more current information. The following first appeared February 22, 2012.

In an earlier post, I discussed taking a 555 timer-based oscillator out of a high-voltage supply and replacing it with a PWM signal from a microcontroller. The design, an open-source Geiger counter, already has an Atmel ATTINY2313 MCU, but I had read someplace that it couldn’t handle both PWM and the other processing required for the circuit. That didn’t and doesn’t make any sense to me, but because it came from the Internet, it has to be true. Right? Relying on my Internet information source, I was going to use a PIC MCU to provide the PWM signal in my design. However, rather than leaving the Atmel stone unturned, I decided to try out its PWM first and see for myself if the chip could handle the whole task. The Geiger counter firmware already uses two timers: Timer0 for PWM chirp tone generation and Timer1 for actual timing duties. I’ll need a third PWM to feed the LC part of the HV supply.

The Atmel chip has four PWM channels, but the clock setup is a little different from the PIC chips I normally use. My first interpretation of the ATTINY data sheet revealed only two timers on the chip: an eight-bit and a 16-bit. That didn’t seem to be enough to drive the four PWMs that the chip offers. Fortunately, a little more research uncovered that the chip has two eight bit timers, and that each half of the 16-bit timer drives a PWM independently. All four channels are covered, after all.

That question being answered, the next step is to generate a PWM signal and mod-wire it in to replace the 555-based oscillator. Pin 9 of the ATTINY2313, OC0B/T1/PD5, is unused in the open-source design, so I’ll use that pin for the feed to the HV supply. I need 4-5KHz for the power supply’s LC circuit.

Deciding to try the Atmel part instead of a PIC MCU gave me an opportunity to explore the differences between the two parts by comparing the code needed to configure the PWMs. First, here’s the PWM setup for my PIC16F1825:

  • // Set HV power supply CCP2 to 4.9KHz, ~20% duty cycle
  • CCPTMRS = 0b00010000; // Select timers for PWM. CCP2 = Timer 2, CCP3 = Timer 4
  • PR2 = 0x65; // PWM period 4.9KHz (With 8MHz system clock)
  • T2CON = 0b00000101; // Timer 2 on, Prescaler = 4
  • CCP2CON = 0b00001100; // Turn CCP2 (RA5) on
  • CCPR2L = 0x33; // Set to 20% duty cycle
  • TRISA5 = 0; // Clear pin direction to enable

Now the comparable code in the ATTINY2313:

  • // Set HV power supply OC0B to 4KHz, ~20% duty cycle
  • DDRD = _BV(PD6) | _BV(PD5); // Set PD5 as output for PWM
  • TCCR0B = (0<(0<OCR0B = 0x33; // Set to 20% duty cycle

The ATTINY version seems simpler to program, though it’s entirely possible that I’m missing something, or that other parts of the initialization are done someplace else in the open-source code I’m starting with. As far as function is concerned, though, the two devices are effectively the same.

The more familiar I get with Atmel chips, the fewer functional differences I see between them and their Microchip counterparts. Microchip seems to have more varieties, but each Atmel variety covers a broader feature set.

In the end, they’re pretty much the same in my book. Before I started using Atmel chips, I really didn’t see what the big argument between the two MCU camps was all about. Now I see it even less. The differences just don’t matter.

NOTE: Since this was originally published, Microchip purchased Atmel, so it may soon matter even less.