Programming a 16F627 PIC Microcontroller Chip with PICBASIC Pro Software and USB Programmer.

Patents, Prototypes, Manufacturing, and Marketing New Inventions

Programming the PIC microcontroller chips with Picbasic Pro.







Programming the Microchip PIC microcontroller chips for my machine controller project.

A couple of years ago I picked up a PIC microchip programmer (see photo below) and a copy of PICBASIC PRO software for programming the chips in BASIC language instead of Assembly code, which is a bit more difficult in my opinion.

The two chip types I will be using in my circuit and thus needing to program are a 12F675, and a 16F627.

I picked the 16F627 for a couple of reasons.  One reason is I needed more input/output pins than my fall-back chip the 12F675.  Another reason is the 16F627 has a "capture" module that works with one of the timers built into the chip making it easy to measure the speed of the shaft encoder. 

One of the biggest reasons is that I had one in my junk box. I am kind of impatient at times and if there is a part in my junk box that I can use right away that is usually what I do.

The other chip I am using is the 12F675.  The optical sensor and delay system on the harvester uses 40 of them.  They are a nice little 8 pin package and have timers, comparators, analog-to-digital converters, interrupts and more all built in. 

I'm using the 12F675 for the air regulation and will be using the built-in A/D converter for reading the pressure sensor, and for reading the pot used for adjusting the set point pressure. As for outputs I will be using two, one for the air valve and one for the alarm horn.

Picture of my PIC programmer
PIC microcontroller chip programer


Here are the PDF manuals for the two chips and the schematic for the circuit.

16F627-PDF manual
12F675-PDF manual
Project schematic
The programmer has software of course, and using the programming I set the configuration files in the configuration settings of the programming interface. 

Programming the 16F627...

These are the settings I am using to configure the internal clock, the watchdog timer, master clear brown out detector data protection etc.  They are fully explained in the Manual. The the programer interface has an area where you set the configurations. It's quite easy consisting primarily of pull down choices.
' INTRC_OSC_NOCLKOUT    Internal clock with no clock output pin
' WDT_OFF                          Turn the watchdog timer off
' PWRT_ON                          Enable Power-up reset
' MCLR_OFF                         Disable the master clear pin function
' BOD_ON                            Enable brown out reset
' LVP_OFF                           Disable the low voltage programming
' PROTECT_OFF                   Disable data protection
Then load the hex file the PicBasicPro program created assembled compiled or whatever it did into the programer, pop in the chip and hit the program button.  A few seconds later the chip is programmed.
Here is the Pic Basic Pro program I wrote for this project.
'* Name : Master Controller SC48 - 2010.BAS               *
'* Author : William Lund                                                       *
'* Notice : Copyright (c) 2011 Geiger Lund Harvesters   *
'* : All Rights Reserved                                                       *
'* Date : 2/26/2011                                                              *
'* Version : 1.0                                                                     *
'* Notes : up/down portion works good                             *
'* B+ on speed portion works                                             *
'* Alarm works                                                                     *
'*                                                                                            *
'* : 16F627 chip                                                                  *
'* : Schematic:  Master Control SC48-Rev2.gif              *

CMCON=7                                                          'Turn off comparators
TRISA = %01111111                                            'inputs: 0,1,2,3,4,5,6 output: 7
TRISB = %01001001                                            'inputs: 0,3,6 outs: 1,2,4,5,7

'variable definitions

ProxDnSw Var porta.2                                            ' pin 1    input
ProxUpSw var porta.3                                             ' pin 2    input
Reset Var porta.5                                                   ' pin 4    input
CylSw Var porta.6                                                  ' pin 15   output
SlowUpValve Var porta.7                                         ' pin 16  output
FastDnValve var portb.1                                           ' pin 7    output
SlowDnValve var portb.2                                          ' pin 8    output
HomePulse Var portb.3                                           ' pin 9    input
FastUpValve var portb.4                                          ' pin 10   output
Alarm Var portb.5                                                   ' pin 11   output
Bplus var portb.7                                                    ' pin 13   output
N var word
i var byte
' Set up peripheral devices

INTCON=%00000000                              ' Disable all interupts
PIE1.0=0                                                    'DISABLE TMR1 OVERFLOW INT.
PIE1.2=0                                                    'DISABLE CCP1 INT.
PIR1.2=0                                                    'CLEAR CCP1 INT. FLAG
CCP1CON=%00000101                         'capture mode every rising edge
T1CON=%00000000                                'TMR1 SETUP "1:8 prescale"
TMR1H=0                                                   'CLEAR TIMER 1 HIGH BYTE
TMR1L=0                                                   'CLEAR TIMER 1 LOW BYTE
CCPR1H=0                                              'CLEAR CAPTURE MODULE HIGH BYTE
CCPR1L=0                                               'CLEAR CAPTURE MODULE LOW BYTE
CCP1CON=%00000101                        'CAPTURE ON RISING EDGE
INTCON.7=1                                              'ENABLE GLOBAL INTERRUPTS
INTCON.6=1                                             'ENABLE PERIPH INTERRUPTS
PIE1.2=1                                                   'Enables the CCP1 interrupt
T1CON.0=1                                               'TURN ON TIMER

on interrupt goto intserv                  'go to subroutine intserv when an interrupt occurs

'initialize outputs
low SlowUpValve                                       ' make the outputs low
low FastDnValve
low SlowDnValve
low FastUpValve
low Alarm
low bplus

'begin program

if proxdnSw = 0 then                                     'if down sw is active, lower the header
high fastdnvalve
if proxdnsw = 0 then CheckSw                     'When down sw goes inactive stop header
low fastdnvalve

high bplus                                                       ' Turn on air valves
pause 2000                                                    ' pause 2 seconds for header bounce

StrtPgm:                                                       'label - begin main program

if proxdnsw = 0 then                                   ' if down swith active lower header slowly
high slowdnvalve
low slowdnvalve

If proxupsw = 0 then                                  ' if up switch active raise header slowly
high slowupvalve
low slowupvalve

if CylSw = 0 then                                       'if alarm switch or bad cyl. is low go to alarm subroutine
gosub alarmsub

goto strtpgm                                              ' do it again

Alarmsub:                                                    ' subroutine for cylinder fault
low slowupvalve                                          'turn off valves
low fastdnvalve
low slowdnvalve
low bplus
high alarm                                                    ' sound alarm horn
high fastupvalve                                          ' raise header quickly
pause 2000                                                  ' wait two seconds
low alarm                                                      ' turn off alarm horn
EndlessLoop:                                              ' pulse alarm horn every 5 sec. forever
pause 5000
high alarm
pause 500
low alarm
goto endlessloop                                        ' Wait and do nothing until master reset

DISABLE                                                      ' instruction for PBC compiler
IntServ:                                                         ' Interrupt service subroutine

PIR1.2=0                                          'CLEAR CCP1 INTERRUPT FLAG
N.lowbyte=CCPR1L                       'OUTPUT THE COMPARE MODULE LOW BYTE
N.highbyte=CCPR1H                             ' output the compare module high byte

If n > 20 then                                            'If n number of pulses have occurred in the
high bplus                                                 'sampled time period then turn on B+
low bplus

PIE1.0=0                                                    'DISABLE TMR1 INT.
PIE1.2=0                                                    'DISABLE CCP1 INT.
PIR1.2=0                                                    'CLEAR CCP1 INT. FLAG
CCP1CON=%00000101                         'capture mode every rising edge
T1CON=%00000000                               'TMR1 SETUP 1:8 PRESCALE
CCP1CON=%00000101                         'set to capture mode - every rising edge
INTCON.7=1                                               'ENABLE GLOBAL INTERRUPTS
INTCON.6=1                                               'ENABLE PERIPH INTERRUPTS
PIE1.2=1                                                     'Enables the CCP1 interrupt
T1CON.0=1                                                'TURN ON TIMER
RESUME                                                   ' send program back to where it came from



If you are familiar with BASIC language for computers then you should be able to understand the above program.  Well, you may need to read up on the microcontrollers as well.
As you can see, there isn't really much programming to do.  I would say the most diffcult thing to do is setting up the control registers correctly to get the capture compare and timer modules working correctly.
Hopefully this little article has furnished you with at leas some idea about how to go about programming a PIC microcontroller chip for your new invention.
I'll soon do another on the programming of the 12F675 and I will try to put more detail into the programming. 
If you are a novice and would like to get into programming PIC chips I would suggest visiting
micro'enigneering Labs Inc. for everything you need.  They are very helpful and provide great customer service.  Their website is:
  Happy programming.  Be careful it can be addictive.


© copyright 2015