Monday, September 19, 2016

LED Rotating using Pic16f877a

Circuit Diagram















Source Code

#include <xc.h>  // this file contains the SFR information of the chip used
#include <delays.h> // header file for using delay functionss
//----------------------------------------------------------------------------------
#define _XTAL_FREQ     4000000L
//----------------------------------------------------------------------------------
//Function prototypes
void port_initialise(void);
void DelayMs(unsigned int msCount);
//--------------------------------------------------------------------------
//Configuaration bit setting

    #pragma config WDTE = OFF  
    #pragma config FOSC = HS
    #pragma config PWRTE = ON
    #pragma config BOREN = ON
    #pragma config LVP = OFF
    #pragma config CPD = OFF
    #pragma config WRT = OFF
    #pragma config DEBUG = ON
    #pragma config CP = OFF
//----------------------------------------------------------------------------

void main (void)
    { // main loop starts-------------
         unsigned char LEDpattern,n,count;
  port_initialise(); // setting up I/O lines as input and output
while(1) // Normally, every MCU programe is a never ending loop. here starts it
{ //while 1 loop starts----------
LEDpattern=0b00000001; PORTD= LEDpattern; //lights up a extreme end LED for(count=0;count<7;count++) { LEDpattern=(LEDpattern<<1); PORTD=LEDpattern; //left rotate 7 times so that it reaches the extrem DelayMs(200); //wait } LEDpattern=0b10000000; //load the left most bit PORTD= LEDpattern; for(count=0;count<7;count++) { LEDpattern=(LEDpattern>>1);//rpeat the same proess PORTD=LEDpattern; DelayMs(200); }
} //while 1 loop ends------------
} // main loop ends--------------
///*************************************************************************
void port_initialise (void)
{
TRISD = 0X00; // LED port is made as output
PORTD = 0X00; // and cleared
ADCON1 = 0X0F; // A/D module not ued.All pins digital
TRISC = 0X00;
PORTC = 0X00;
TRISB = 0B00000000;
PORTB = 0X00 ;
}
//**************************************************************************
void DelayMs(unsigned int msCount)
{
unsigned int locCnt;
for(locCnt=0;locCnt<=msCount;locCnt++)
{
__delay_ms(1); // This C18 function creates a delay of 1000 instructions= 1000uSec= 1 millisecond
// looping this msCount times will give that much milliseconds delay
}
//****************************************************************************************************************

Explanations
This module demonstrates some sort of a chaser effect in the LED array. This is not implemented as a practical application but to get a idea about the manipulations that can be done using C syntax. Out of the 8 LEDs a single led in the bottom will glow initially and then the next one above it, and then next..like that. So the LED will light up one by one towards the top and then it will move down and the process repeats.
There is a new concept introduced in this module which was not in the previous one.
// Variable declarations(Global)
unsigned char LEDpattern;                // this variable is rotated and moved to port
we are hereby assigning a location in PIC’s data memory to hold a specific variable, which is the one we are using to create the rotate effect to LEDS in port. unsigned char LEDpattern;// unsigned char denotes that this variable is of 1 byte width –means it an hold value up to 255 only. LEDpattern – is the name with which we will be referencing this location in this program. We will do the operations on this variable and output to port when needed.

Actually what we are doing here is loading number 1 in a variable and will out to port which will glow the lower most led, wait for sometime and then left rotate the variable and out again to port. Now the next led will lit up. We will repeat this 7 times. Then we will sound the buzzer for a short duration and load 80h (0b10000000) into that variable- move to port, and then right rotate 7 times – then the led moves down. After this we will repeat the process again. You can see the forloop working here.All the other preprocessor directives and program structure remains the same as in the first program.

No comments:

Post a Comment