Zibb

Design Idea

Unused port adds a PWM/analog channel to a microcontroller

Take advantage of unused pins to generate a PWM signal and then filter it into analog.

Vishwas Vaidya, Tata Motors Ltd, Pune, India; Edited by Martin Rowe and Fran Granville -- EDN, 10/8/2009

Low-cost, 8-bit, single-chip microcontrollers are stingy when it comes to on-chip PWM (pulse-width-modulation) resources. The use of a PWM resource often forces a designer to sacrifice a capture/compare or timer channel because the PWM channel shares the same on-chip hardware. This Design Idea describes how you can use an on-chip unused synchronous serial port to generate PWM signals and convert them to a slow-moving analog signal (Figure 1). Many microcontroller-based stand-alone electronic units don’t use the synchronous serial port. Thus, you can use the microcontroller’s baud-rate generator and parallel-to-serial-converter blocks to generate bit patterns to form a 256-bit PWM pattern. You can then filter the PWM output with an RC filter to extract an analog signal (Reference 1). The synchronous communication is devoid of the start and stop bits of asynchronous mode, so the bit pattern can generate long periods of high or low level.

See all of EDN's
Design Ideas

You can generate raw data with a decimal value of 165 using this concept (Figure 2). A PWM-conversion cycle consists of generating 256 bits—that is, 32 bytes. The number of “on” bits corresponds to the value of the raw data to convert into PWM. Hence, for 165 bits as the raw data, 165 bits are on and 91 bits are off. To generate a 165-bit on-period, the first 20 bytes—that is, 160 bits—transmit as 0×ff on-state bytes. The trick lies in judiciously composing the 21st, or transition, byte. This byte has some of its LSBs (least significant bits) as ones and the rest as zeros to form the required length of the on-period. In this case, the circuit needs five more on bits: 160+5=165. Hence, the transition byte should have a 00011111b pattern (byte=0×1f).

Figure 3 illustrates the process in flow-chart form. You can tailor the PWM frequency to your application by selecting a crystal, PLL (phase-locked loop), and baud rate. A simple RC filter can convert the PWM into a slow-moving analog value. Although this idea describes an 8-bit PWM, you can increase or decrease resolution by changing the total bits per PWM cycle. You correspondingly increase or decrease the conversion time.

Listing 1 provides a sample code for illustrating the concept. The code uses the Microchip PIC18F4525, which has a 4-MHz crystal and 10-kHz baud rate for the synchronous serial communication, yielding 10,000/256=39.31 Hz of PWM frequency. You can filter it with a 0.1-sec RC filter, which is sufficient for slow-moving analog signals, such as speed setpoints for motion-control applications. By using a 20-MHz crystal, you can achieve synchronous serial baud rates greater than 1.5 MHz and PWM frequencies of a few kilohertz.


Reference
  1. Mitchell, Mike, “Make a DAC with a microcontroller’s PWM timer,” EDN, Sept 5, 2002, pg 110.


Reed Business Information Resource Center

Featured Company


Related Resources

ADVERTISEMENT

ADVERTISEMENT

Feedback Loop


Post a CommentPost a Comment

There are no comments posted for this article.

Related Content

 

By This Author

There are no additional articles written by this author.


ADVERTISEMENT

Knowledge Center


Events

Oxford University Practical RF/Microwave Design Short Course
Dates: 1/18/2010 - 1/22/2010
Location: Oxford, United Kingdom

Phosphor Global Summit 2010
Dates: 3/23/2010 - 3/25/2010
Location: San Diego, CA

Submit an EventSubmit an Event




Technology Quick Links

EDN Marketplace


©1997-2009 Reed Business Information, a division of Reed Elsevier Inc. All rights reserved.
Use of this Web site is subject to its Terms of Use | Privacy Policy

Please visit these other Reed Business sites