Wednesday, February 26, 2014

Chapter 2 : Digital Input and Output


Schematic Diagram Circuit
 
 
 
 

Realization of Circuit on Breadboard




Exercise 1

Source Code :

//Exercise 1 : Lighting LEDs using HEX and BINARY format
 
#include <18f4550.h> //Header file
#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration
#use delay (clock = 20M) 
 
//Main Function
void main()
{
  //Set PORTD as output
  set_tris_d(0x00); //set_tris_d(0b00000000);
 
  //Initialize PORTD
  output_d(0x00); //output_d(0b00000000);
 
  while(true)
  {
     output_d(0xFF);//output_d(0b11111111);
     delay_ms(100);
     output_d(0x00); //output_d(0b00000000);
     Delay_ms(100);
  }
}
 
 
 

Exercise 2:

Source Code:


//*********************************************************

 

//Exercise 2 : Lighting LED2 and LED4

#include <18f4550.h> //Header file

#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration

#use delay (clock = 20M)

 

//Define LEDs

#define   LED1 PIN_D0

#define   LED2 PIN_D1

#define   LED3 PIN_D2

#define   LED4 PIN_D3

#define   LED5 PIN_D4

#define   LED6 PIN_D5

#define   LED7 PIN_D6

#define   LED8 PIN_D7

 

//Main Function

Void main()

{

     //Set PORTD as output

     Set_tris_d(0x00); //set_tris_d(0b00000000);

 

     //Initialize PORTD

     Output_d(0x00); //output_d(0b00000000);

 

     While(true)

     {

          //Turn ON even number LEDs

          Output_high(LED2);

          Output_high(LED4);

          Output_low(LED6);

          Output_low(LED8);

          Delay_ms(100);

     }

}

 

 

Exercise 3:

 

Source Code:

 

//*********************************************************
 
//Exercise 3 : Creating Running Light
 
#include <18f4550.h> //Header file
#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration
#use delay (clock = 20M)
 
//Define LEDs
#define   LED1 PIN_D0
#define   LED2 PIN_D1
#define   LED3 PIN_D2
#define   LED4 PIN_D3
#define   LED5 PIN_D4
#define   LED6 PIN_D5
#define   LED7 PIN_D6
#define   LED8 PIN_D7
 
 
//Main Function
Void main()
{
     //Set PORTD as output
     Set_tris_d(0x00); //set_tris_d(0b00000000);
 
     //Initialize PORTD
     Output_d(0x00); //output_d(0b00000000);
 
     While(true)
     {
          //Running Light
          Output_d(0x01); //output_d(0b00000001);
          Delay_ms(100);
          Output_d(0x02); //output_d(0b00000010);
          Delay_ms(100);
          Output_d(0x04); //output_d(0b00000100);
          Delay_ms(100);
          Output_d(0x08); //output_d(0b00001000);
          Delay_ms(100);
          Output_d(0x10); //output_d(0b00010000);
          Delay_ms(100);
          Output_d(0x20); //output_d(0b00100000);
          Delay_ms(100);
          Output_d(0x40); //output_d(0b01000000);
          Delay_ms(100);
          Output_d(0x80); //output_d(0b10000000);
          Delay_ms(100);
    
     }
}
 
 
 


Exercise 3a: (Simplified)

Source Code:

//*********************************************************
 
//Exercise 3A : Creating Running Light (Simplified)
 
#include <18f4550.h> //Header file
#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration
#use delay (clock = 20M)
 
//Define LEDs
#define   LED1 PIN_D0
#define   LED2 PIN_D1
#define   LED3 PIN_D2
#define   LED4 PIN_D3
#define   LED5 PIN_D4
#define   LED6 PIN_D5
#define   LED7 PIN_D6
#define   LED8 PIN_D7
 
 
//Main Function
Void main()
{
     int i,j;
     char PORTD;
 
     //Set PORTD as output
     Set_tris_d(0x00); //set_tris_d(0b00000000);
 
     //Initialize PORTD
     Output_d(0x00); //output_d(0b00000000);
 
     While(true)
     {
          PORTD=0x01;
          For (int i=0;i<=7;i++)
          {
              PORTD=PORTD<<1 o:p="">


              Output_d(PORTD);

              Delay_ms(100);

          }

         

          PORTD=0x80;

          For(int j=0;j<=7;j++)

          {

              PORTD=PORTD>>1;

              Output_d(PORTD);

              Delay_ms(100);

          }

    

     }

}
 
 

Exercise 4:

Source Code:
 
//*********************************************************
 

 
 
//********************************************************* 
//Exercise 4 : Count Up
 
#include <18f4550.h> //Header file
#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration
#use delay (clock = 20M)
//Define LEDs
#define   LED1 PIN_D0
#define   LED2 PIN_D1
#define   LED3 PIN_D2
#define   LED4 PIN_D3
#define   LED5 PIN_D4
#define   LED6 PIN_D5
#define   LED7 PIN_D6
#define   LED8 PIN_D7
//Main Function
Void main()
{
     //Set PORTD as output
     Set_tris_d(0x00); //set_tris_d(0b00000000);
     //Initialize PORTD
     Output_d(0x00); //output_d(0b00000000);
     While(true)
     {
          //Count Up
          output_d(0b00111111);//0
          Delay_ms(100);
          output_d(0b00000110);//1
          Delay_ms(100);
          output_d(0b01011011);//2
          Delay_ms(100);
          output_d(0b01001111);//3
          Delay_ms(100);
          output_d(0b01100110);//4
          Delay_ms(100);
          output_d(0b01101101);//5
          Delay_ms(100);
          output_d(0b01111101);//6
          Delay_ms(100);
          output_d(0b00000111);//7
          Delay_ms(100);
          output_d(0b01111111);//8
          Delay_ms(100);
          output_d(0b01101111);//9
          Delay_ms(100);
 
     }
}
 
 
 

 

Exercise 5:

 
Source Code:
 
 

//*********************************************************
//Exercise 5 : Button
#include <18f4550.h> //Header file
#fuses HS, NOLVP, NOWDT, NOPROTECT  // Hardware configuration
#use delay (clock = 20M)
//Define LEDs
#define   LED1 PIN_D0
#define   LED2 PIN_D1
#define   LED3 PIN_D2
#define   LED4 PIN_D3
#define   LED5 PIN_D4
#define   LED6 PIN_D5
#define   LED7 PIN_D6
#define   LED8 PIN_D7
 
//Define Button
#define   BUTTON1   PIN_B0
#define   BUTTON2   PIN_B1   
//Main Function
Void main()
{
     //Set PORTD as output
     Set_tris_d(0x00); //set_tris_d(0b00000000);
     //Set PORTB as input
     Set_tris_b(0xff); //set_tris_b(0b11111111);
     //Initialize PORTD
     Output_d(0x00); //output_d(0b00000000);
     While(true)
     {
          If(!input(BUTTON1)) //Press Button1
 
          {
               //7Segment ON
               output_d(0b00111111);//0
               Delay_ms(100);
          }
          Else
          {
              //7Segment OFF
              output_d(0b00000000);//0
              Delay_ms(100);
          }
     }
}
 
 
 

 
 

No comments: