Wednesday, November 18, 2009

Experiment No. 8: Use of PWM to control the brightness of a LED.

A PIC16F628A has an in-built Capture/Compare/PWM (CCP) module for which the I/O pin is served by RB.3 (Pin No. 9). In this experiment we are going to use the CCP as a PWM to control the power to a LED. PWM stands for the Pulse Width Modulation where the width of a digital waveform is varied to control the power delivered to a load. The underlying principle in the whole process is that the average power delivered is directly proportional to the modulation duty cycle. The term duty cycle describes the proportion of on time to the regular interval or period of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.
Image Source:
The mikroC has an in-built library functions for PWM hardware module. Click here for details.

Monday, November 2, 2009

Experiment No. 7: Timers and Interrupts

Many microcontroller applications like generating periodic signals, measuring time interval, keeping date and time, use time as their variable. Therefore, microcontrollers need to have some internal resources to accurately measure time. The PIC16F628A has 3 timer modules which are known as Timer0, Timer1, and Timer2. The basic unit of a timer is a free-run 8-bit or 16-bit incrementing synchronous counter which can be programmed to count internal or external pulses. The count number stored by each counter can be read or modified by accessing the special function register associated with that timer. Some of the bits in these registers are also the indicators of timer overflow, which, therefore, can generate interrupt request to the microcontroller. The use of timer modules to keep record of time elapsed allows the microcontroller to carry on with its other operations like controlling outputs, reading inputs, etc.

Timers can also have another asynchronour counter, known as prescaler, which can be configured to divide the  number of pulses received by the timer register to be divided by a factor of 2, 4, 8, 16, 32, 64, 128 or 256.

1. Timer0 Module

The Timer0 module is an  8-bit timer/counter which can be configured to count machine cycles or external pulses. When counting machine cycles, it is known to operate as a timer, and when counting the external pulses, it is called to operate as a counter. The external pulses are given to RA4/T0CKI pin. The Timer0 also consists of an 8-bit prescaler which is a programmable division factor.

The operation of the timer is set up by moving a suitable control code into the OPTION register. Timer mode is selected by clearing the T0CS bit (OPTION<5>). In Timer mode, the TMR0 register value will increment every instruction cycle (without prescaler). Counter mode is selected by setting the T0CS bit. In this mode, the TMR0 register value will increment either on every rising or falling edge of pin RA4/T0CKI pin. The incrementing edge is determined by the source edge (T0SE) control bit (OPTION<4>). Clearing the
T0SE bit selects the rising edge.

Timer0 interrupt is generated when the TMR0 register timer/counter overflows from FFh to 00h. This overflow sets the T0IF bit of INTCON register. The interrupt can be masked by clearing the T0IE bit (INTCON<5>). The T0IF bit (INTCON<2>) must be cleared in software by the Timer0 module interrupt service routine before reenabling this interrupt.

The prescalar value can be set using the PSA and PS<2:0> bits of OPTION register.

Tuesday, October 20, 2009

Experiment No. 6: Read/Write Internal EEPROM Memory

An EEPROM (Electrically-Erasable Programmable ROM) data memory is one of the important features of flash-based PIC microcontrollers. It is called non-volatile to indicate that it retains the data even when the power is down. Practically speaking, if you want to design a digital lock system, then the password to unlock the system can be saved into the EEPROM, so that when the power is down, the password will still be saved. And other good thing is that the data can be easily modified or overwritten with software control. In this experiment, I am going to show you how to read and write in to the internal EEPROM memory of PIC16F628A using mikroC EEPROM library functions. Here is what we are going to do:

We will write 0s to 10 EEPROM locations. We will read them first, then write 0-9 to these locations, and turn the power off. We will turn the power on, and read the data in those locations and see. I have created a simple menu on LCD with Read, Write and Delete functions.

Experimental Setup:
Connect the three push buttons on the board to RB.0, RB.1, and RB.2, and plug-in the LCD module.

Saturday, October 10, 2009

Experiment No. 5: Multiplexed Seven Segment Displays

In this experiment, we are going to learn how to interface more than one 7-segment LED display to a PIC Port using multiplexing technique. We are going to interface a 4-digit common cathode seven segment display to our PIC board. The multiplexing circuit is already built up in the board using 4 transistors and few resistors (Read Make Your Own PIC Development Board). The basic idea of multiplexing is that all seven segment displays are connected to the microcontroller in parallel and the microcontroller alternately prints ones, tens, hundreds, and thousands digits, selecting one at a time. The switching among the digits is so fast that it gives an impression of simultaneous light emission.

Experimental Setup:

1. Connect RA0 through RA3 to 7-Segment Digit Select headers DG1, DG2, DG3, and DG4 using jumper wires.
2. Insert 7FR5641AS 4-Digit Seven Segment module in to its place on the board.

Wednesday, September 30, 2009

Experiment No. 4 : Reading Temperature Values from DS1820 using 1-Wire Protocol

In this experiment, we are going to build a digital temperature meter using DS1820 connected to our PIC16F628A development board. The temperature value will be displayed on the LCD display. I have modified the sample program that comes with the compiler according to our PIC board requirements. Also I have elaborated comments in the program so that every step will be more clear to the readers.

Experimental Setup:
The experimental setup is very straight-forward. Place DS1820 device on the three-pin female header that we recently added to our board. And also connect the data pin of DS1820 to RB.0 pin of PIC16F628A using a jumper wire.

Tuesday, September 29, 2009

DS18S20 : 1-Wire Digital Thermometer and mikroC OneWire Library

The DS18S20 is a 1-Wire digital thermometer device from MAXIM that provides 9-bit Celsius temperature measurements and communicates over a 1-Wire bus with a central microprocessor. It also has in-built alarm function with nonvolatile user-programmable upper and lower trigger points. The operating temperature range of the device is –55°C to +125°C with an accuracy of ±0.5°C over the range of –10°C to +85°C. Each DS18S20 has a unique 64-bit serial code, which allows multiple DS18S20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18S20s distributed over a large area.

Tuesday, September 22, 2009

The 1-Wire Communication Protocol

The 1-Wire is a registered trademark of Dallas Semiconductor Corp (now Maxim) for a serial communication protocol using a single data line and a ground reference. A 1-Wire Master (a microcontroller) initiates and controls the communication with one or more 1-Wire Slave devices (usually sensors). Each 1-Wire slave device has a unique, factory-programmed , 64-bit identifier, which serves as device address on the 1-Wire bus. This globally unique address is composed of eight bytes divided into three main sections. Starting with the LSB, the first byte stores the 8-bit family codes that identify the device type. The next six bytes store a customizable 48-bit individual address. The last byte, the most significant byte (MSB), contains a cyclic redundancy check (CRC) with a value based on the data contained in the first seven bytes. This allows the master to determine if an address was read without error. With a 248 serial number pool, conflicting or duplicate node addresses on the net are never a problem.

The 1-Wire protocol uses conventional CMOS/TTL logic levels (maximum 0.8V for logic “zero” and a minimum 2.2V for logic “one”) with operation specified over a supply voltage range of 2.8V to 6V.

A Typical 1-Wire Communication Flow

The first part of any communication involves the bus master issuing a reset, which synchronizes the entire bus. A slave device is then selected for subsequent communications. This can be done by selecting all slaves, selecting a specific slave (using the registration number of the device), or by discovering the next slave on the bus using a binary search algorithm. These commands are referred to collectively as network function or read-only-memory (ROM) commands. Once a specific device has been selected, all other devices drop out and ignore subsequent communications until the next reset is issued.
Once a device is isolated for bus communication, the master can issue device-specific commands to it, send data to it, or read data from it. Because each device type performs different functions and serves a different purpose, each type has a unique protocol once it has been selected. Even though each device type may have different protocols and features, they all have the same selection process and follow the command flow seen in the figure below.

Thursday, September 17, 2009

Experiment No. 3: LCD Interface in 4-bit Mode

The objective of this experiment is to interface a 16x2 LCD to PIC16F628A in 4-bit mode. This means the data transfer will use only four pins of the microcontroller. There is no additional hardware setup needed for this experiment, as we have a ready-made LCD interface female header. We only need to define the data transfer and control pins in the software. Remember, the LCD interface in our development board uses the following pins of PIC16F628A:
Data Transfer : D4 -> RB4, D5 -> RB5, D6 -> RB6, D7 -> RB7
RS -> RA0, and EN -> RA1

About PIC16F628A

PIC16F628A is a powerful (200 nanosecond instruction execution) yet easy-to-program (only 35 single word instructions) CMOS FLASH-based 8-bit microcontroller from Microchip. It comes into an 18-pin package and is upwards compatible with the PIC16F628, PIC16C62XA, PIC16C5X and PIC12CXXX devices.

Sunday, September 13, 2009

Experiment No. 2 : Push Button and Seven Segment Display Interface

In this experiment, we will program the PIC16F628A as an UP/DOWN Decade Counter. The count value will be displayed on a Seven-Segment Display and will be incremented/decremented by two push buttons on the board.

Experimental Setup:
The board has built in interface for a multiplexed 4-digit seven segment display (HS-5461AS2 from will select only one digit by connecting a Digit Select pin to Vcc, as shown in figure below. A black jumper wire is used for this purpose. The seven segments will be driven through PORTB (already wired on the board). Connect Push Buttons (PB3 and PB4) to RA1 and RA0 female headers using jumper wires.

Wednesday, September 9, 2009

PIC microcontrollers: Nebojsa Matic (Free Online Book)

If you are a beginner to PIC microcontroller, I would recommend you to read this free e-book on PIC16F84 microcontroller. PIC16F84 is one of the most popular PIC family microcontrollers that resembles very much with PIC16F628A.
In this book you will find:
  • Introduction to microcontrollers 
    • Learn what they are, how they work, and how they can be helpful in your work.
  • Practical connection samples for  Relays, Optocouplers, LCD's, Keys, Digits, A to D Converters, Serial communication etc.
  • How to write your first program, use of macros, addressing modes...
  • Instruction Set
    • Description, sample and purpose for using each instruction...
  • MPLAB program package
    • How to install it, how to start the first program, following the program step by step in the simulator...

      Ready to read now? Click Here

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

mikroC PRO for PIC 2009

mikroC PRO for PIC 2009 is a C compiler for PIC microcontrollers. We are going to use the demo version of this for our PIC board.

Download Here

mikroC PRO for PIC 2009 Manual

Install the Compiler and read the manual before doing experiments.

Wednesday, September 2, 2009

PIC16F628A Development Board

The development board we are going to make for our experimental microcontroller PIC16F628A will look like this. Here are the features it is going to have:
  • Access to all I/O pins through female header pins
  • 4 Push Buttons for Input
  • 4 LEDs for Output
  • An LCD Interface Port
  • A 4-digit Seven-Segment Display Interface
  • LCD Backlight Switch and Contrast Adjustment
  • ICSP Programming (Very Important)

Why use the PIC?

Source: PICmicro MCU C : An introduction to programming the Microchip PIC in CCS C by Nigel Gardner

Microcontroller-based Embedded Systems Design