Thursday, June 6, 2013

Playing with LEDs- Game3 & 4

Playing with LEDs - Game3

In this game, you are invited to make a shifter using LEDs like the Game2 but more complicated

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() {

             unsigned char i ;
             TRISB = 0 ; PORTB = 0 ;

             while(1)
             {
                     PORTB = 1 ;
                     for(i = 1 ; i < 8 ; i++)
                     {
                           Delay_ms(100) ;
                           PORTB = PORTB << 1 ;
                     }
                    
                     Delay_ms(500) ;
                     for(i = 1 ; i < 8 ; i++)
                     {
                           Delay_ms(100) ;
                           PORTB = PORTB >> 1 ;
                     }
                     Delay_ms(500) ;

             }
}
- 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 = 1 ;
==> Put the first pattern to show on the port


for(i = 1 ; i < 8 ; i++)
==> For loop 


 Delay_ms(100) ;
 PORTB = PORTB << 1 ;
==> Wait for 100 ms and then shift PORTB one bit and show data on PORTB


Delay_ms(500) ;
==> Wait for half second 

here the first shifter is accomplished and now the second shifter is to be written 


for(i = 1 ; i < 8 ; i++)
                     {
                           Delay_ms(100) ;
                           PORTB = PORTB >> 1 ;
                     }
==> It is the same algorithm but the opposite shift operator 


Delay_ms(500) ;
==> Wait for half second 

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

Playing with LEDs - Game4

in this game, you will invited to make a simple counter from 1 to 9 using 4 LEDs.
Components:
PIC16F877A
4 LEDs
4 Resistors :33Ohm

Steps:
- Draw your schematic on ISIS proteus

- Write this C code on mikroC:
void main() {
              TRISB = 0 ; PORTB = 0 ;
           
              while(1)
              {
                      PORTB ++ ;
                      Delay_ms(1000) ;
                      if(PORTB == 9) PORTB = 0 ;
              }
}
- 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 ++ ;
==> Increment PORTB data 

Delay_ms(1000) ;
==>
 Wait for one second 
                    
if(PORTB == 9) PORTB = 0 ;
==> If you reache 9, reinitialize the port






No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...