Using Example and Tutorial Code

Examples and tutorials are a great way to learn how to use features of the hardware and software.  Often, actual code can illustrate how to accomplish something better than mere descriptions.

The examples post here on Digital-DIY and on other forums have probably been developed using different hardware than you have, so some adjustments will have to be made for your particular hardware.


Devices of the same family (PIC18F series for example) will usually work for examples, as long as the necessary features are included on the chip you'd like to use.  Any special hardware requirements are usually specified in the example description or program comments.  If tutorial is for using a chip's EEPROM to store data, the chip must have an example of special hardware features required.

The DEVICE statement in the example code must be changed to reflect the part actually in use.  This statement lets the compiler understand different parts.

' Device = 18F242 'original device used
Device = 18F2420 ' actual device used



The crystal used (or internal oscillator setup) control the speed of the chip.  The clock statement in no way controls the speed of the chip.  Instead, the clock statement tells the compiler what speed the chip is running at so that all time-based functions can be computed correctly.  This obviously includes things like delay statements but also includes the timing for all serial communications protocols.  If the clock statement is incorrect, the baud rate for UART transmissions will be incorrect and garbage will be received by the host computer.

The clock statement must agree with the crystal frequency used.  In some cases where the chip's PLL functions are used to boost the speed, the clock speed will be diffrent than the crystal frequency.  Check the data sheet for oscillator options.

'Clock = 4 'original clock speed = 4 MHz
Clock = 20 'actual clock speed = 20 MHz, as labeled on crystal

Port Pins

The port pins used for LEDs, switches and other I/O devices usually does not matter; different development boards will have different arrangements of installed components.  The example code must be changed to reflect the reality of your situation.

Some features of the microprocessor chip use specific pins which cannot be altered if the example code is to work.  Some of these include:

RxD & TxD for the hardware UART

SDA & SCL for hardware I2C

ICSP lines

LCD data lines (may be changed to a different port, but either a whole port (8 bits) is used, or specifically the upper or lower 4 bits)

The example text and illustrations will discuss the connection arrangements and connections can be verified in the code.   The code must be altered to suit the hardware being used.

Below is some code fo my servo clock.  It expects the servo to be connected to Port C, bit 2.  There is nothing magic about this connection, and Port B, bit 0 would work equally as well as long as the code is modified to reflect the change.

'High(PORTC.2) 'create servo pulse, repeatedly send approximately 
High(PORTB.0) 'changed servo pin from C2 to B0 
DelayUS(Posit) 'every 200 milliseconds
'Low (PORTC.2) 'original servo connection
Low (PORTB.0) 'changed to reflect actual servo connection


The code must be changed to the actual connections for all external devices.  Careful examination of the code is necessary to ensure that all locations are changed.



When using example and tutorial code, it is unlikely that its use will be as simple as cutting and pasting.  Your hardware will require changes to the code to accommodate its arrangement.  As long as the necessary code changes are made, the examples and tutorials should run without any problems.  Remember, the examples and tutorials probably won't fit your needs exactly - they serve as a starting point for your own code development.

Forum Activity

  • No posts to display.

Member Access