Wednesday, September 9, 2009

Experiment No. 1 : 4-Bit Binary Counter

Experimental Setup:
The first experiment that we are going to do with our PIC16F628A board is a 4-bit binary counter that counts from 0(00h) to 15(0Fh) with 1sec delay between each count. The output will be at RB.0 through RB.3 and will be displayed on 4 LEDs. Use four jumper wires to connect RB.0 through RB.3 to LEDs. The picture below shows these connections.

Figure 1. Jumper Connections for a 4-bit Binary Counter

Here is the C program written for mikroC for PIC 2009 compiler.
 * Project name:
     4-bit Counter
 * Copyright:
     (c) Rajendra Bhatt, 2009.
 * Description:
     This is a simple 4-bit counter. The output will be displayed on 4 LEDs
     connected to PORT RB.0-RB.3
 * Test configuration:
     MCU:             PIC16F628A
     Dev.Board:       PIC16F628A Board
     Oscillator:      XT, 04.0000 MHz

void main() {

  TRISB = 0x00;          // set direction to be output
  PORTB = 0x00;        // Turn OFF LEDs on PORTB
  do {
    Delay_ms(1000);      // 1 second delay
    PORTB ++ ;
  } while(PORTB < 0x0F);            // Till PORTB < 15

Experiment Output Video:


  1. i have a question. i write the program in micro C, i compiled and i got .hex file. I make the board and i want to program de pic with jdm programator using IC-prog. How i select the internal osc of the pic? Or the external osc?

  2. You should be able to change the options with Edit Project window. I recommend to read this:

  3. Thanks, I read that and now i have a lot of questions.

    At the oscillator is write HS (HS- Crystl upto 20MHz) and i supose this is n external oscillator because in the data sheet is write 4MHZ internal. Another option is XT (XT- Crystal up to 4.096) and that what kind of oscilator is it?
    ExtClk - external clock from crystal oscilator, etc. Wwhat is this option?
    IntRC I/O -Internal 4MHz & the oscillator lines become I/O is the internal oscillator?

    The last question: what you selected when you make the program in C and write on the PIC?

  4. The HS (High Speed, 4-20 MHz), XT(up to 4 MHz), and LP (Low Power, 32.768KHz) modes all require external crystals. I am using an external 4MHz crystal, so I am using XT mode.
    INTOSC is for internal oscillator modes. Remember that PIC oscillator input pins are multiplexed with digital I/O pins. This means, if you want to use internal oscillator, then you can use pins 12 and 13 of PIC16F628A as digital I/O pins (RB6 and RB7). Read the datasheet for PIC16F628A further details.
    The option EC is for external clock which is sometimes used to synchronize the PIC with some other circuits that already have got clock.

  5. Hi Raj I want help to write code in ccs for something like this:

    1. PIC18F2220
    2. 4 x 74HC373 connected to PORT B(B0-B7) through a bus wire in Isis Professional 7 software.
    3. 4 x 7-Segment Common Anode connected to outputs of 74HC373.
    4. The LE pins of 74HC373 are connected to Port C(C1-C4).
    5. There is a Switch connected to RC0.
    6. When you close the Switch(RC1) the program must display a value from 0-9999 on the 7-Segs and when you open the switch at any moment it should stop counting when you close it again it should start fron 0 again. e.g. like a stopwatch.

    Thanks in advance.

  6. First of all, I don't use CCS but I hope that won't be very different. Your project doesn't seem that difficult. All you need to do is to connect the inputs of all 74HC373 in parallel so that the data from the PIC could be sent to all the latch ICs but will be read only the one whose LE is active. You don't even need time-division multiplexing among the 4-displays, as the digit to be displayed on individual displays will be latched separately.

  7. Hi, Raj am just developed interest in PIC MCU i have bought a PIC 16F628,an 5x7 Dot matrix display, and other electronic components,am hoping to buy more others. pliz guide on how to make a running message display, if possible, your advice on the the type of compiler and the actual program code will be of great help.


Microcontroller-based Embedded Systems Design