SCREENSHOT 1. The CCS C compiler PIC
Wizard utility allows a programmer to twiddle
the bits on all of the selected PICs peripherals
and I/O. Once you have things configured the
way you want them, the Wizard writes the code
for you.
design before we can begin our C
coding. Since our requirements are small,
let’s keep the microcontroller design
small. I’ve chosen the venerable 28-pin
PIC18F2620 to manipulate the H-bridge.
You can see how I attached the
PIC18F2620’s I/O to it in Schematic 1.
CCS C Compiler Primer
I used the CCS C compiler’s Project
Wizard to generate the skeleton code for
our project here. Screenshot 1 is a
capture of the PIC Wizard window I used
to configure the PIC18F2620’s clock and
fuses. In reality, we can write all of the
code that the PIC Wizard generates manually. However, it is easier and faster to use the Wizard.
Here’s the code that the PIC Wizard produced from my clock and fuse inputs:
#include <18F2620.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz
//for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18)
//sed for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode
//disabled (Legacy mode)
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES MCLR //Master Clear pin enabled
#use delay(clock=20000000)
#define ACT_DIR PIN_B0
#define ACT_PWM PIN_B1
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
Note the pair of #define commands that associate logical names with the PIC18F2620’s PORTB
I/O pins. The use of this Wizard to create these associations is the reason that the #define
commands are in this code segment. In that the fuse and clock code is automatically encapsulated
into a #include file and is not displayed by default in the CCS PIC C IDE, I prefer to have the #define
commands lie in the main C source file where they are easily referenced during the coding process.
In our case, we can simply cut and paste them into the main C source file. We could have manually
coded the #define commands into our main C source file. Screenshot 2 shows how I used the PIC
Wizard to generate the ACT_DIR and ACT_PWM #define commands.
70 SERVO 08.2010