Friday, October 21, 2011

flashing led by microcontroller

Discription :
today is not first session in PIC microcontroller project.but i want to show you first lab for everyone want be programming .We will begain with an experiment that flashes an LED on and off.while this looks very simple it is 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 pic 16f877 and flash it continuously with 1 sec duration
Required Theory :
you must be know that :

  • Digital I/Oports of pic 16f877
  • direction control registers;(TRISA , TRISB,TRISC.TRISD.TRISE)
  • special fonction registers ADCON1 
Circuit Diagram :
for this lab we sill add a light-emitting-diode (led) to port pin RC2(17) with a current limiting resistor (330 ohm)in series .
Software :
Open a new project window in mikroC and select Device Name as pic16f877.nect assing 4MHz to device clock.go to next and provide the project name and the path of folder.it isalways a good practice to have a separate floder for each project. Creat a folder named lab1 and save the project inside it with a name (say, first project).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 include libraries , select include all option . next ,click finish button. You will see a program window with void main()function already included. now goto project >Edit project you will see the following windows
this window allows you to program the configuration bits for the 14-bit CONFIG register inside the pic 16f877.the device configuration bit allow each iser to customize certain aspects of he device(like reset and oscillatore configurations)to the needs of the application .when the device powers up,the state of thses bits determines the modes that  the device uses.
therefore,we also need to program the configuration bits as per experimental setup.select,
oscillator
watchdog
power up timer
master clear enable
code protect
data EEread protect
brown out detect
internal external switch over mode
monitor clock fail-safe
not that we have turned on power-up timer, it provides an additional delay of 72ms 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.
  • Here is the complete program that must be compiled and loaded into pic16f877 for flashing the LED. Copy and past this code to program in your main program window. you have to delete the voide main() function first that was already included . to compile, hit the builed button, and if there were no errors,the output HEX file will be generated in the same floder where the project file is then load the HEX  file into the pic16f877 microcontroller using your programmer 

for more information about hex file just contact me charaf_zohair@hotmail.com

Lab 1: Flashing LED with PIC16F877
Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF
Copyright @ Rajendra Bhatt
Oct 21,      2011 by charaf zouhair
*/
// Define LED @ RC0
sbit LED at RC0_bit;
void main() {
adcon1=0x07;
TRISC = 0b00000000; // PORTC All Outputs

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

No comments: