Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB 1: FLASHING AN LED

Description
Today is our first session in PIC microcontroller lab, and we will begin with an experiment that
flashes an LED on and off. While this looks very simple it is the best project to start because this makes
sure that we successfully wrote the program, compiled it, loaded inside the PIC, and the circuit is
correctly built on the breadboard.
In this lab session we will connect an LED to one of the port pin of PIC16F688 and flash it
continuously with 1 sec duration.

Required Theory
You must be familiarized with,
1. digital I/O ports (PORTA and PORTC) of PIC16F688
2. direction control registers, TRISA and TRISC
3. special function registers CMCON0 and ANSEL
If you are not. then please read this first: Digital I/O Ports in PIC16F688.

Circuit Diagram
To our basic setup on the breadboard (read Getting Ready for the First Lab), we will add a light-
emitting-diode (LED) to port pin RC0 (10) with a current limiting resistor (470 Ohm) in series. The
complete circuit diagram is shown below.

Figure 1.1. Flashing LED circuit


Figure 1.2. Prototyped circuit on the breadboard

Software
Open a new project window in mikroC and select Device Name as PIC16F688. Next assign 4.0 MHz
to Device Clock. Go to next and provide the project name and the path of the folder. It is always a good
practice to have a separate folder for each project. Create a folder named Lab1 and save the project
inside it with a name (say, FlashLED). The mikroC project file has .mccpi extension. The next window
is for Add File to Project . Leave it blank (there are no files to add to this project) and click next. The
next step is to include libraries, select Include All option. Next, click Finish button. You will see a
program window with a void main() function already included. Now, go to Project -> Edit Project. You
will see the following window.

Figure 1.3. Project Settings


This window allows you to program the configuration bits for the 14-bit CONFIG register inside the
PIC16F688 microcontroller. The device configuration bits allow each user to customize certain aspects
of the device (like reset and oscillator configurations) to the needs of the application. When the device
powers up, the state of these bits determines the modes that the device uses. Therefore, we also need to
program the configuration bits as per our experimental setup. Select:
1. Oscillator -> Internal RC No Clock
2. Watchdog Timer -> Off
3. Power Up Timer -> On
4. Master Clear Enable -> Enabled
5. Code Protect -> Off
6. Data EE Read Protect -> Off
7. Brown Out Detect -> BOD Enabled, SBOREN Disabled
8. Internal External Switch Over Mode -> Enabled
9. Monitor Clock Fail-Safe -> Enabled
Note that we have turned ON the Power-Up Timer. It provides an additional delay of 72 ms to the
start of the program execution so that the external power supply will get enough time to be stable. It
avoids the need to reset the MCU manually at start up.
Read PIC16F688 datasheet for details on Configuration Word.

Figure 1.4. Appropriate configuration bit selection

Heres the complete program that must be compiled and loaded into PIC16F688 for flashing the
LED. Copy and paste this whole program in your main program window. You have to delete the void
main() function first that was already included. To compile, hit the build button, and if there were no
errors, the output HEX file will be generated in the same folder where the project file is. Then load the
HEX file into the PIC16F688 microcontroller using your programmer.

/*
Lab 1: Flashing LED with PIC16F688
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Copyright @ Rajendra Bhatt
Oct 7, 2010
*/
// Define LED @ RC0
sbit LED at RC0_bit;
void main() {
ANSEL = 0b00000000; //All I/O pins are configured as digital
CMCON0 = 0x07 ; // Disbale comparators
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00001000; // PORTA All Outputs, Except RA3

do {
LED = 1;
Delay_ms(1000);
LED = 0;
Delay_ms(1000);
} while(1); // Infinite Loop
}

We used the in-built library function Delay_ms() to create the 1 sec delay for flashing On and Off.
Delay_ms() returns a time delay in milliseconds.

Output
You will see the LED flashing On and Off with 1 sec duration.

Source:
http://embedded-lab.com/blog/embedded-lab-experiments/
Format:
1. Select All. Apply the following format.

2. Font style: Times New Roman. Main Header: Bold, Underline. Sub-header: Underline.
3. Font style of Code: Cambria
3. No space after each paragraph. Two spaces (two Enter) before next header.
4. Change Bulleted List to Numbered List.
5. Follow the Figure Numbering.
6. Group Assignment.
Group 1 Lab2, 7, 12, 21
Group 2 Lab3, 8, 13, 20
Group 3 Lab4, 9, 14, 19
Group 4 Lab5, 10, 15, 18
Group 5 Lab6, 11, 16, 17
7. Soft Copy to be submitted on 8/15/17. 10:30AM to 1:30PM

You might also like