Introduction: Led Series With Arduino and Pic16f877a Microcontroller

About: hi keep learning keep supporting.

hello friends for the past couple of weeks I am working as intern in "FLDEC" as intern period we were trained to do various task since most of my friends have very less basic knowledge so the my team leader planed to train us from the scratch.

Arduino - code, sketch, youtube video.

PIC - code, sketch, simulation file(proteus)

Today i will show how i finished task one

TASK 1:connect 8 led at PORT A of pic16f877a and a bush button to control the led at PORTC, when button is pressed the led should glow in series one by one when the button is released it should stop and restart again when the button is pressed hold and when the led sequence is complete it should stop and should not restart unless the button is released and repressed, and we are also asked to create delay with out utilizing inbuilt delay function.example-delay(1000) or __delay_ms(1000) the main intention of this special case is to understand how exactly delay function works.

this seems to be simple but some minute problems were faced while doing this. lets get started.

I did this project in proteus simulation i don't have pic programmer to implement practically but I do have arduino I will do the same on arduino uno,nano mini,mega I have all of them.

Supplies

for simulation - proteus, MPLABX.

for project (link will be available shortly)

arduino uno or (nano) * 1

led * 8

resistor * 1

breadboard * 1

USB cable to power arduino * 1

jumper wires(10 -15)

Step 1: Gathering Required Components

pick components in proteus - pic16f877a, button, led-red, resistor.

make the connections as shown.

for your arduino make the connections as shown for simulation you can run with out resistors but for practical implementations resistors are must for led otherwise the led will draw more current which can not be handled by arduino and this could burn your arduino

complete program is given at the final step for both arduino and pic

Step 2: Include Header Files and Initialize PORTS ,variables and Other Functionality of the Program

first we need to add the required header files "xc.h" is used to configure the configuration bits and supports various inbuilt functions "pic16f877a.h" this functions maps the commands to there respective addresses based on the IC which is used.

According to the problem statement we should not use inbuilt delay command so i used for loop to make the MCU busy in executing task since most of the low level MCU can not do multi tasking this trick will definitely work

we have to initialize all the ports we are going to use for the project and variables should be declared as local or global according to our requirement.

Step 3: Draw the Schematic

for now I am using a Proteus where i can draw schematic and simulate it.

The images clearly represent what are the things i'am using for the simulations which u can get it form the library window.just follow the images and everything will be clear.

This project is for Arduino as well as for the PIC 16f877a MCU I don't have PIC programmer so I cannot run my code on Proteus and I will repeat the same procedure with arduino i will show the schematic how i connected components to it just refer the images given above.

while wiring the pic controller care must be taken while connecting led with RA4 because its open collector pin. image is shown.

Step 4: Program Code

since I have commented everything along with the program code so I will not repeat the same procedure again.

PIC16f877 MCU CODE

#include<xc.h>
#include<pic16f877a.h> //required header files for the program
void xtalbased_delay(int xtal_frequency){ //delay function based on the crystal frequency
for(int xtal = 1;xtal<=xtal_frequency;xtal++) { //loop to generate delay by making the MCU busy
for(int i=0;i<12500;i++);
for(int i=0;i<12500;i++); 
}
}
void main(){
char clk = 1; //value of crystal in mega hz
char count = 1; // number of times the sequence should repeat when button is pressed and hold
TRISA = 0; // PORTA as output to connect 6 led(PORT offers only 6 IO pins)
TRISB = 0; //PORTB as output for remaining 2 leds
TRISC = 1; //PORTC as input for push button
PORTA = 0b010000; // initially all leds are in OFF state RA4 is open collector so negate logic works fine for him
PORTB = 0b00000000; //remaining led also cleared.
while(1){
for(int j=0;j<=5 && RC7 == 1 && count == 1;j++){ //increment operation for shifting the bits to make led glow serially
PORTA = 0b000001 << j ^ 0b010000; // bit shifting with EX-OR for common collector terminal
xtalbased_delay(clk); //fixed delay
}
PORTA = 0b010000; // force default state after PORT A sequence complete
for(int j=0;j<=1 && RC7 == 1 && count == 1;j++){ //increment operation for shifting the bits to make led glow serially
PORTB = 0b00000001 << j; //bit shifting at PORTB for the remaining leds
xtalbased_delay(clk); //fixed delay
}
<br>PORTB = 0b00000000; //force default state after all sequence complete
PORTA = 0b010000; //force default state after all sequence completecount--;//count down for number of times sequence completed
}
}

CODE FOR ARDUINO

void setup() {
pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); pinMode(6, OUTPUT); pinMode(5, INPUT); } void loop() { for(int i=13;digitalRead(5) == HIGH;i--){ //when button is pressed and hold led sequence begins. digitalWrite(i, HIGH); delay(500); //for(long j=0;j<16000000;j++); //not working well with arduino may be this is supposed to generate (4 sec delay but cpu process instruction much more faster or compiler is not good in working with huge numerical values. digitalWrite(i, LOW); } }

////////////////this code do not satisfies all the conditions of the given statement.

Step 5: Output

The simulation output is shown in the image. The same program was implemented with the help of arduino.

In pic we had one open collector pin which makes things complex but arduino it really easy to do projects.