Programming the PIC 16F628

I just had a chance to program the PIC 16F628 microcontroller as a part of a project and thought of scribbling down the things i learned and did till now. To start with PIC 16F628 is a general purpose 8 bit microcontroller with a very compact instruction set, 35 instruction, which make the programming much more interesting.

One of the simplest burning circuit for PIC 16F628, which i am using is the one by Jim Paris. Jim Paris had also designed a software called jimpic to burn the hex code of the PIC program onto the microcontroller. You could get the burning circuit fom here. I am using gpasm assembler for assembling the code. Now lets write the first pic program….

list p=16f628

                include “”


                movlw 0×0


The program stores the value 0 to the accumulator w. The first line of the program specifies which type of PIC is being used. The second line is responsible for including the file which contain a lot of symbol definitions which comes handy while writing programs. The third line specifies the configuration of the microcontroller ie., what all special features we require like switching off/on the watch dog timer, using internal/external clock etc… The ‘end’ directive denotes the end of the program code.

Using the timer to get a delay:

The PIC 16F628 microcontroller contains 3 Timer modules. Let us use the Timer0 module. The Timer0 is been initialized by clearing the T0CS bit of the OPTION register. Now the timer got intialized and thus the value of TMR0 register get incremented in each cycle, and when the value of TMR0 overflows (ie., when the value exceeds ffh) the T0IF bit of the INTCON register get set (this is an interrupt for the timer). We can also prescale the timer by clearing PSA bit of OPTION register by setting PS2:PS0 bits. When the bits PS2:PS0 is all set 1 we get a prescale of 1:256, so the total time delay for an overflow to occur is about 256*255 microsecond (which is about 65 ms). Now let us write a procedure which on executing creates a delay of 65 ms…

                bsf STATUS, RP0
                bcf OPTION_REG,T0CS
                bcf OPTION_REG,PSA
                bsf OPTION_REG,PS2
                bsf OPTION_REG,PS1
                bsf OPTION_REG,PS0
                bcf STATUS,RP0

                btfss INTCON,T0IF
                goto delay
                bcf INTCON,T0IF

The procedure init_timer is to be called only once which sets up the timer initializations like the prescaler and initial value of TMR0 (by default it is 0) etc.., and when a delay of 65 ms is required, the procedure ‘delay’ is been called.

“bsf” is an assembly instruction used to set a bit in the specified register (bsf stands for bit set ‘f’ implies that the operand following is a special purpose register or a memory address). Similarly to clear a bit the “bcf” instruction is been used. “btfss” is used for comparison, it vhecks if the specified bit is set and if set then it skips the following instruction.

Now it is time to blink an LED:-

Even though the blinking of an LED is not a great deal, sometimes it could make one the happiest man in the world. I was so much excited when I made my PIC blink an LED.

As we got a procedure for delay what we require right now is output port to provide the LED with voltage. PIC 16f68 has two I/O ports, PORTA and PORTB. Each port is associated with a TRIS register which controls the direction of flow (if to make a pin output or input) of the port. Writing 0 to TRISB makes all pins of PORTB output pins. Now the TRISB register is available only in Bank 1 and hence we need to switch the Bank to set PORTB pins as output/input pins. Here is the code for blinking the LED..

                movlw 0×0
                bsf STATUS,RP0
                movwf  TRISB
                bcf STATUS,RP0
                bsf PORTB,0
                call delay
                bcf PORTB,0
                call delay
                goto infinite


To see the LED blinking we need running circuit which can be made by a simple set of wiring… Connect the Vcc(pin 14) of the PIC to a 5V supply and ground(pin 5) of the PIC to ground, connect MCLR(pin 4) to ground through a 2k resistor and connect the led between RB0 and ground with a resistor(1k) in series.

NP: You should add a ‘goto main’ just before ur program(procedure) starts ie., just after the _CONFIG….. line, this is to transfer control to the main procedure.

For a much more cool project of monitoring the temperature you can refer ,the place where i referred throughout the project ..

Happy programming in PIC….

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>