Using the PIC16F62X Series Flash Microcontroller
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:
; Load 7 into W register
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.
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 = 7 ' PortA = digital I/O VRCON = 0 ' A/D Voltage reference disabled TRISB = %11110000 ' B.3=PWM,B.0,B.1,B.2 blink LEDs PR2 = 25 ' Set PWM for approximately 38KHz CCPR1L = 13 ' Set PWM Duty-Cycle to 50% CCP1CON = %00001100 ' Mode select = PWM T2CON = %00000100 ' 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.
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 .....!
Copyright © 1999-2008