Code Example #1: Led Blinky

Time to get dirty. Copy/Paste the following code in the source code windows
Code:
        '-------------------------------< LED Blinky >------------------------------------
        '
        '    File name  : LedBlinky.bas
        '    Version    : 1.0
        '    Company    : Mister E 
        '    Programmer : Steve Monfette
        '    Date       : June 22, 2011
        '    Device     : PIC16F690
        '
        '---------------------------------------------------------------------------------                    
        
        '---------------------------< Project description >-------------------------------
        '
        '   Beginner code example to blink a Led on PORTc.0 using:
        '       1) PBPDEMO version
        '       2) MPLAB
        '       3) PICKIT 2 Programmer
        '
        '---------------------------------------------------------------------------------                

        '----------------------------< Hardware listing >---------------------------------
        '
        '   - Microchip Low Pin Count Demo Board
        '   - PIC16F690 installed
        '   - *** All jumper have to be installed ***
        '   - Target board will be powered by the PICKIT 2
        '
        '---------------------------------------------------------------------------------                                        
      
        '
        '       Pic Configuration
        '       =================
                '   All PIC configuration fuses (Configuration bits) are listed in the 
                '   C:\PBPDEMO\p16f690.inc file.  Also see datasheet section 14.1
                '
        @  __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON & _PWRTE_ON
                '   _INTRC_OSC_NOCLKOUT: Using Internal Oscillator
                '   _WDT_ON: Enable Watch Dog Timer
                '   _MCLRE_ON: Enable MCLR 
                '   _CP_OFF: Disable Code Protection
                '   _CPD_OFF: Disable Data Code Protection
                '   _FCMEN_OFF: Disable Fail Safe Clock Monitor
                '   _IESO_OFF: Disable Internal/External switchover
                '   _BOR_ON: Enable Brown out detect
                '   _PWRTE_ON: Enable Power up Timer
                '
        OSCCON  = %01110000  ' datasheet section 3
                ' -x-------- n/a
                ' --111----- IRCF<2:0> Internal Oscillator Frequency Select Bits (8MHz)
                ' -----xxxx- Read Only
                '
        DEFINE OSC 8    ' tells PBP we use a 8 MHZ clock        
                
        '                      
        '       Hardware assignment
        '       ===================
                '   Here we assign Aliases to PORT pin
                '   this makes the code easier to read
                '   I'll use the Aliases name as written on the schematic
                '
                '   LEDs
                '   ----
                DS1     VAR PORTC.0 
                DS2     VAR PORTC.1 
                DS3     VAR PORTC.2 
                DS4     VAR PORTC.3
                 
        '
        '       Hardware configuration
        '       ======================
                '   
                '       I/Os (Datasheet section 4.0 & above)
                '       ------------------------------------
                TRISA = 0   ' All PORTA I/O set as Output (When capable of)
                TRISB = 0   ' All PORTB I/O set as Output
                TRISC = 0   ' All PORTC I/O set as Output
                '
                '       ADCs (datasheet section 9)
                '       --------------------------
                ANSEL = 0   ' Disable all 
                ANSELH = 0  '     ADCs 
                '
                '       Comparator (datasheet section 8)
                '       --------------------------------
                        '   At Power On, the comparator are already disabled
                        '   but let's play safe.
                        '
                CM1CON0 = 0 ' Disable comparator 1
                CM2CON0 = 0 ' Disable comparator 2
                
        '
        '       Software/Hardware initialisation
        '       ================================
        PORTA = 0   ' clear PORTA output pins (0v)
        PORTB = 0   ' clear PORTB output pins (0v)
        PORTC = 0   ' clear PORTC output pins (0v)
        
        WHILE !OSCCON.2 : Wend  ' Wait untill the internal OSC get stable 
        
        '------------------------------< Main program >-----------------------------------
        '
Start:
        ds1 = 1     ' turn ON LED DS1
        PAUSE 500   ' wait 500 mSec
        ds1 = 0     ' turn OFF LED DS1
        PAUSE 500   ' wait 500 mSec
        GOTO Start  ' do it again
        '
        '---------------------------------------------------------------------------------
Let's compile it.
  1. From the top menu, click on Project
  2. Choose Compile (F10 also work but that's your job to find some shortcuts )

Oh noes...
ERROR: Unable to execute mpasmwin.Error[118] C:\PBP_PROG\LEDBLINKY.ASM 63 : Overwriting previous address contents (2007)

Welcome in real life! Compilation error happen! Deal with it now!

First of, have a good reading at this thread, at least POST #1 & POST #5. Post #5 explain how to fix it. Once you've fixed the 16F690.inc file, hit F10, and it will compile fine.

Loaded C:\PBP_Prog\LedBlinky.COF.
BUILD SUCCEEDED: ...


Aaaaahhhh, feel better now. Now let's program the PIC and do some light show.
  1. On the top menu, click on Programmer
  2. Click on Select Programmer and choose PICKIT 2
  3. The Output window should show you a message like
    Initializing PICkit 2 version 0.0.3.63
    Found PICkit 2 - Operating System Version 2.32.0
    Target power not detected - Powering from PICkit 2 ( 5.00V)
    PIC16F690 found (Rev 0x5)
    PICkit 2 Ready


    There's no real problem with the above message, in fact, it confirm we have the right PIC installed, and the PICKIT 2 WILL provide the 5Volt to our target board. Fabulous!
  4. On the top menu, click on Programmer
  5. Click on Program
    OutPut
    Programming Target (2011-06-23 01:35:20)
    PIC16F690 found (Rev 0x5)
    Erasing Target
    Programming Program Memory (0x0 - 0x4F)
    Verifying Program Memory (0x0 - 0x4F)
    Programming Configuration Memory
    Verifying Configuration Memory
    PICkit 2 Ready

AND we have a really fabulous... not working light show. Why? Use you multimeter and measure the voltage across Vdd & Vss... there's something there. How about MCLR pin? Duh... 0V.. That mean the PICKIT 2 is holding MCLR to GND... now we need to fix it.

How to?
  1. On the top menu, click on Programmer
  2. Click on Settings
  3. Check Run after a sucessful program option
  4. Click on Apply
  5. Click on OK
  6. Still nothing there, just program your PIC again and see what happen

Yeah! LedBlinky rocks!