Thursday, June 6, 2013

Playing with LEDs- Game1 & 2

Playing with LEDs - Game1

in this game, you are invited to play with a LED. In more obvious words, let's make a simpler flasher for one second.

Components:
PIC16F877A
LED
Resistor :33Ohm

Steps:
- Draw your schematic on ISIS proteus

- Write this C code on mikroC:
void main() {
            TRISB.F0 = 0 ; PORTB.F0 = 0 ;
           
            while(1)
            {
                    PORTB.F0 = 1 ; Delay_ms(1000) ;
                    PORTB.F0 = 0 ; Delay_ms(1000) ;
            }
}
- Build it to get hex file
- Upload hex file to PIC16F877A
- Start simulation

Code explanation 

void main()
==> "main" function is a necessary function and it takes no parameters and does not return any parameters

TRISB.F0 = 0 ;
==> Configure pin0 in PORTB as output 

PORTB.F0 = 0 ;
==> Initialize pin0 in PORTB

while(1)
==> an endless loop to ensure that microcontroller will work automatically

PORTB.F0 = 1 ; Delay_ms(1000) ;
==> Turn LED ON and wait for one second

PORTB.F0 = 0 ; Delay_ms(1000) ;
==> Turn LED OFF and wait for one second

==============================================

Playing with LEDs - Game2

In this game, you are invited to make a shifter using LEDs

Let's start...

Components:
PIC16F877A
8 LEDs
8 Resistors :33Ohm

Steps:
- Draw your schematic on ISIS proteus
 

- Write this C code on mikroC:
void main() {
             TRISB = 0 ; PORTB = 1 ;

             while(1)
             {
                     Delay_ms(100) ;
                     if(PORTB == 128) PORTB = 1 ;
                     else PORTB = PORTB << 1 ;

             }
}
- Build it to get hex file
- Upload hex file to PIC16F877A
- Start simulation


Code explanation 

void main()
==> "main" function is a necessary function and it takes no parameters and does not return any parameters

TRISB.F0 = 0 ;


==> Configure pin0 in PORTB as output 

PORTB.F0 = 0 ;
==> Initialize pin0 in PORTB

while(1)
==> an endless loop to ensure that microcontroller will work automatically

Delay_ms(1000) ;
==> Wait for one second

if(PORTB == 128) PORTB = 1 ;
==> Check if you reach the max limit to show on port
        (Remember: PORTB has 8 pins)

else PORTB = PORTB << 1 ;
==> If max limit is not reached, shift PORTB one bit and show data on PORTB

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...