You are on page 1of 6

The new PIC16F62X series flash microcontroller from Microchip packs a lot of bang-forthe-buck, and the cost per

controller "even in small quantities" is considerably less than the older PIC16F84 controller. Cost Comparison: The PIC16F628-04/P normally runs around $3.63 each in single quantity. The older PIC16F84-04/P runs around $6.88 each in single quantity. The 16F628 has 2K program code space, whereas the 16F84 has only 1K. The 16F628 has additional hardware peripherals not available on the 16F84 series as well. Note: Prices will change like the wind depending on where you buy microcontrollers, so these prices are only estimates based on several sources at the time of putting together this article. The PIC16F628 is kind of like a baby version of the popular PIC16F877, but in an 18-pin DIP package instead of the larger 40-pin DIP package. It doesn't have all the features of the larger F877, but it's a 100% improvement over the 16F84. Just imagine a PIC16F84 with: 2K program memory (instead of 1K) 224 x 8 Data RAM 128 x 8 EEPROM data memory 15 I/O-pins (instead of 13) Internal or external oscillator (instead of external only) Capture, Compare, PWM (CCP) Module Hardware USART Analog comparator module Now. Imagine paying $3.25 less for this microcontroller than the PIC16F84, and you have the PIC16F628. Not a bad deal, and they're simple to use. For the hobbyist or embedded systems engineer - a difference of $3.25 per controller adds-up pretty quickly and the additional program memory, data RAM/EEPROM, and hardware features, make the 16F628 pretty much a no-brainer when selecting an 18-pin flash PIC microcontroller for that next project.

Two primary things to consider when replacing the 16F84 with the 16F628 are the port A comparators, and the internal 4MHz oscillator available on the 16F628. Port A comparator module:

Unlike the 16F84, the 16F628 Port A pins (A0, A1, A2, A3) default to comparator inputs on power up. The comparator mode control register CMCON controls the comparator input and output multiplexers, and will also turn OFF the onboard comparator module to allow Port A pins to be used as normal I/O-pins. An external or internal reference can be used depending on the comparator operating mode. The analog signal present on VIN- is compared to the signal at VIN+, and the digital output of the comparator is adjusted as shown below.

For a detailed explanation of using the onboard comparator module download the PIC16F628 datasheet from the Microchip web site. There are several options for configuring the comparators that require the datasheet as a reference guide, and detailed info on using the comparator module goes well beyond the scope of this article.

Using the Port A comparator pins for normal I/O. The lower 3-bits (CM2, CM1 and CM0) of the CMCON register control the comparator MODE. Out of eight possible modes, a value of "7" loaded into CMCON turns the comparators OFF, and allows all of Port A to function as normal digital I/O. Writing a value of 7 (binary 00000111) to CMCON turns OFF the comparators. Using PicBasic Pro, you simply add the following line of code to the beginning of your program to turn OFF the comparators, and use these pins as normal digital I/O-pins: CMCON = 7 Using assembly language, turning OFF the comparator module can be accomplished by: MOVLW MOVWF 0x07 ; Load 7 into W register CMCON ; CMCON = contents of W register 7h

Port A.0, A.1, A.2, and A.3 will now operate as normal I/O-pins with comparators turned OFF.

Using the internal 4MHz Oscillator

Depending on the PIC device programmer you're using, there should be an option for setting the configuration-bits. The example shown below is what you'll see on screen when using the PicStart Plus programmer from Microchip. Note: If you have an older version of the PicStart Plus you may need to upgrade the PicStart Plus firmware for support of the newer PIC16F62x series. The firmware upgrade is worth the investment, and you'll save considerably more $$ than the cost of the firmware upgrade when using the 16F62x series microcontroller as opposed to the older "more expensive", and less capable 16F84.

If you're using the EPIC programmer, click the drop-down menu for configuration, then oscillator, and select the following oscillator configuration shown below:

Note: Notice the oscillator selection INTRC (IN) with the EPIC, and Int RC with the PicStart Plus..? The oscillator pins on the 16F628 are RA.6, and RA.7.

Tip #1: One nifty little un-documented trick when using the PicBasic Pro compiler is to place the following line in the head of your program code file: @ device INTRC_OSC_NOCLKOUT This tells the PicBasic Pro compiler to compile your code using the internal oscillator automatically, and you never need worry about it again.

Port-pin RA.6 can be programmed to output a clock signal 1/4 the oscillator frequency, or it can function normally as an additional bi-directional digital I/O-pin. Running on the internal 4MHz oscillator, RA.6 can output a 1MHz clock signal by selecting (INTRC Clockout), or RA.6 may be used for normal digital I/O by selecting INTRC (IN). You cut down the required number of external components (no crystal/capacitors or ceramic resonator), and gain two additional I/O-pins in the process...... Tip #2: Notice on the PicStart Plus configuration bits picture above that the Master Clear (MCLR) selection has been disabled. This eliminates the need for the external 4.7K pull-up resistor on RA.5 you'll see in most other PIC circuits. Not really a big-deal, but it does reduce the parts-count even further by eliminating this resistor from MCLR (RA.5) to Vcc.

Multitasking One H-U-G-E benefit of using the onboard hardware features of the PIC16F628 is the ability to have [TRUE] multitasking. Once you have configured the onboard hardware registers, and turned on the hardware peripheral feature, it runs in the background without further program code requirements or outside intervention. A line of code or sub-routine that's required to make something happen, written in C, BASIC, assembly, or whatever; simply is not multitasking. Background hardware operation "on its own" is.

Below is a code sample showing how to use hardware PWM with the PIC16F628 & PicBasic Pro. To use this code, connect three LEDs (as shown below) to each port-pin B.0, B.1, and B.2 through 470-ohm series resistors to ground.

Connect an oscilloscope to PortB.3 to view the PWM signal of approximately 38.5KHz output on B.3 using internal hardware PWM. Here's a peek at what the hardware PWM signal looks like using the internal oscillator option:

If you really want to get fancy, and see how multitasking works when using internal hardware features, try sending tons of serial data in a tight loop, and watch the PWM signal. It's totally un-affected by anything until you physically modify the control registers. CMCON VRCON TRISB PR2 CCPR1L CCP1CON T2CON = = = = = = = 7 0 %11110000 25 13 %00001100 %00000100 ' ' ' ' ' ' ' PortA = digital I/O A/D Voltage reference disabled B.3=PWM,B.0,B.1,B.2 blink LEDs Set PWM for approximately 38KHz Set PWM Duty-Cycle to 50% Mode select = PWM Timer2 ON + 1:1 prescale

begin: high PortB.0 pause 50 low PortB.0 pause 50 high PortB.1 pause 50 low PortB.1 pause 50 high PortB.2 pause 50 low PortB.2 pause 50 goto begin If you own the PicBasic Pro compiler, try programming this code into the PIC16F628, and setting the oscillator as internal. Next try setting it as external, and use a 4MHz ceramic resonator or external crystal. This will give you an idea of how the internal oscillator timing compares to an external oscillator. This will give you a good look at why you may want to use an external oscillator for timing critical applications as well. If you don't own the PicBasic Pro compiler or just prefer to do it the easy way, I have pre-compiled a couple files here for you.

Click HERE for INT-PWM.TXT (Save as INT-PWM.HEX) Click HERE for EXT-PWM.TXT (Save as EXT-PWM.HEX) INT-PWM.HEX uses the internal oscillator. The other uses an external oscillator @ 4MHz. Both files will output a 38.5KHz PWM signal on PortB.3 - while port-pins B.0, B.1, and B.2 blink three LEDs. Note: INT-PWM.HEX will be slightly OFF-frequency due to the internal oscillator being used. A slight fudge-factor normally will be necessary to get a precise PWM signal using the internal oscillator, and it will vary from PIC-to-PIC due to process variables. Your mileage may vary .....! Click Click Click Click HERE HERE HERE HERE to to to to stock-up on PIC16F628 or F627 micros. purchase PicBasic Compilers - and hardware. learn more about the PicBasic compiler. return to the PicBasic projects page.

Until the next project - have fun, don't blow anything up, and thanks for stopin by...;o]

You might also like